﻿$(function() {

    // create slideshow for most popular solutions
    // fx is the transition effect (see http://malsup.com/jquery/cycle/browser.html for a list of effects)
    // speed is the duration in ms of the transition effect
    // timeout is the interval betwen automatic trasitions in ms
    // next and prev are the IDs of next and previous link elements
    // pause indicates that the slideshow should pause when mouse is over slide
    $("#solutionsCarousel").cycle({
        fx: "scrollHorz",
        speed: 500,
        timeout: 8000,
        next: "#nextSolution",
        prev: "#prevSolution",
        pause: 1,
        cleartype: 0
    });

    // side vertical hover menus
    $("#peripheralNav").superfish({
        delay: 1000,                            // delay on mouseout 
        animation: { opacity: 'show', height: 'show' },  // fade-in and slide-down animation
        speed: 500,
        onShow: function() {
            $(this).css("z-index", "99");
            $(this).find("li").css("z-index", "999");
        }
    });

    // client login page
    // show login form
    $("#btnLogin").click(function(event) {
        // prevent default link behavior
        event.preventDefault();
        // if the login form is already shown, slide it up
        if ($("#btnLogin ~ div").length > 0) {
            $("#btnLogin ~ div").slideUp(500);
            return;
        }
        // create a div to hold the login form
        $("#btnLogin").after("<div></div>");
        // load login form into div
        $("#btnLogin ~ div").hide().load("/ClientLogin", function(data) {
            // slide down login form to show it
            $("#btnLogin ~ div").slideDown(500);
            // validate login form
            $("#client_login").validate({
                rules: {
                    username: "required",
                    password: "required"
                },
                messages: {
                    username: "Username is required",
                    password: "Password is required"
                },
                submitHandler: function(form) {
                    // submit the login form via ajax
                    $(form).ajaxSubmit({
                        beforeSubmit: function() {
                            // disable submit button to prevent multiple submissions
                            $("#btnLogin").attr("disabled", "disabled");
                        },
                        success: function(data) {
                            if (data != "False") {
                                // login was successul, redirect to specified url
                                window.location = "/" + data;
                            }
                            else {
                                // login failed, re-enable login button and show failure message
                                $("#btnLogin").attr("disabled", "");
                                $("#login_error").text("Login failed").fadeIn(500);
                            }
                        }
                    });
                    //return false;
                }
            });
        });
    });


    //*******************
    // popup contact form 
    //*******************
    $("#contactFormDialog").jqm({
        trigger: false,
        modal: true, /* FORCE FOCUS */
        toTop: true,
        onHide: function(h) {
            h.o.remove(); // remove overlay
            h.w.fadeOut(500); // hide window

        }
    });
    $("#practiceNav a:contains('Contact'),#footer a:contains('Contact')").click(function(event) {
        event.preventDefault();
        $("#contactFormDialog").jqmShow();
    });
    $("#closeContactForm").click(function(event) {
        event.preventDefault();
        $("#contactFormDialog").jqmHide();
    });
    // popup contact form validation
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, "");
        return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");

    $("#popupForm").validate({
        rules: {
            lead_name: "required",
            lead_phone_number: {
                required: true,
                phoneUS: true
            },
            lead_email: "email",
            pinellasCountyCase: "required"
        },
        messages: {
            lead_name: "Please fill in your name",
            lead_phone_number: {
                required: "Please fill in your phone number",
                phoneUS: "Please enter a valid US phone number"
            },
            lead_email: "Please enter a valid email address",
            pinellasCountyCase: "Check the box below to verify that your case is a Pinellas County matter in order to proceed."
        },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "pinellasCountyCase")
                error.insertBefore(element);
            else error.insertAfter(element);
        },
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                beforeSubmit: function() {
                    // disable submit button to prevent multiple submissions and add ajax loading class
                    $("#btnSubmitPopup").attr("disabled", "disabled");
                    $("#popupForm .loading").show();
                },
                success: function(data) {
                    $("#popupForm .loading").hide();
                    if (data == "True") {
                        $("#popupSubmitResult").slideDown(400);
                    }
                    else {
                        // there was an error sending the email
                        // notify visitor?
                        alert("There was an error sending your message. Please try again or call us 727-578-0303");
                        // re-enable submit button
                        $("#btnSubmitPopup").attr("disabled", "");
                    }
                }
            });
            //return false;
        }
    });

    // add or remove required validation for email based on dropdown for preferred contact method
    $("#popup_lead_contact_method").change(function() {
        var selected = $("option:selected", this).text();
        if (selected == 'Telephone') {
            $("#popup_lead_phone_number").rules("add", {
                required: true,
                phoneUS: true,
                messages: {
                    required: "Please enter your phone number",
                    phoneUS: "Please enter a valid US phone number"
                }
            });
            $("#popup_lead_email").rules("remove", "required");
        }
        else {
            $("#popup_lead_email").rules("add", {
                required: true,
                messages: {
                    required: "Please enter your email address"
                }
            });
            $("#popup_lead_phone_number").rules("remove", "required");
            $("#popup_lead_phone_number").rules("remove", "phoneUS");
        }
    });

    // cancel and close popup buttons
    $("#btnCancelPopup,#btnClosePopup").click(function(event) {
        event.preventDefault();
        $("#contactFormDialog").jqmHide();
    });

    //**********************
    // on page contact form
    //**********************
    $("#contactForm form").validate({
        rules: {
            lead_name: "required",
            lead_phone_number: {
                required: true,
                phoneUS: true
            },
            lead_email: "email",
            pinellasCountyCase: "required"
        },
        messages: {
            lead_name: "Please fill in your name",
            lead_phone_number: {
                required: "Please fill in your phone number",
                phoneUS: "Please enter a valid US phone number"
            },
            lead_email: "Please enter a valid email address",
            pinellasCountyCase: "Check the box below to verify that your case is a Pinellas County matter in order to proceed."
        },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "pinellasCountyCase")
                error.insertBefore(element);
            else error.insertAfter(element);
        },
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                beforeSubmit: function() {
                    // disable submit button to prevent multiple submissions and add ajax loading class
                    $("#btnSubmitContactForm").attr("disabled", "disabled");
                    $("#contactForm .loading").show();
                },
                success: function(data) {
                    $("#contactForm .loading").hide();
                    if (data == "True") {
                        $("#contactResult").slideDown(400);
                    }
                    else {
                        // there was an error sending the email
                        // notify visitor?
                        alert("There was an error sending your message. Please try again or call us 727-578-0303");
                        // re-enable submit button
                        $("#btnSubmitContactForm").attr("disabled", "");
                    }
                }
            });
        }
    });

    // add or remove required validation for email based on dropdown for preferred contact method
    $("#lead_contact_method").change(function() {
        var selected = $("option:selected", this).text();
        if (selected == 'Telephone') {
            $("#lead_phone_number").rules("add", {
                required: true,
                phoneUS: true,
                messages: {
                    required: "Please enter your phone number",
                    phoneUS: "Please enter a valid US phone number"
                }
            });
            $("#lead_email").rules("remove", "required");
        }
        else {
            $("#lead_email").rules("add", {
                required: true,
                messages: {
                    required: "Please enter your email address"
                }
            });
            $("#lead_phone_number").rules("remove", "required");
            $("#lead_phone_number").rules("remove", "phoneUS");
        }
    });
    
    // video vault
    // initialize the player
    var initialVideoUrl = $("#videoList a:first").attr("href");
    $("#videoVaultDialog a.player").attr("href", initialVideoUrl);
    var player = $f("#videoVaultDialog a.player", "/Content/flowplayer-3.0.3.swf", {
        clip: {
            autoPlay: false,
            onStart: function(clip) {
                _gaq.push(["_trackEvent","Videos","play",clip.url]);
            },
            onPause: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "pause", clip.url]);
            },
            onStop: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "stop", clip.url]);
            },
            onFinish: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "finish", clip.url]);
            }
        }
    });
    // initialize popup dialog
    $("#videoVaultDialog").jqm({
        trigger: false,
        modal: true,
        toTop: true,
        onHide: function(h) {
            h.o.remove(); // remove overlay
            h.w.fadeOut(500); // hide window
        }
    });
    // show the dialog
    $("#videoLink").click(function(event) {
        event.preventDefault();
        $("#videoVaultDialog").jqmShow();
        player.load();
    });
    // hide the dialog when close link clicked
    $("#closeVideoVault").click(function(event) {
        event.preventDefault();
        $("#videoVaultDialog").jqmHide();
        player.unload();
    });
    // when a link in the video list is clicked play corresponding video
    $("#videoList a").click(function(event) {
        event.preventDefault();
        var videoUrl = $(this).attr("href");
        $f("a.player", "/Content/flowplayer-3.0.3.swf", {
            clip: {
                url: videoUrl,
                autoPlay: true,
                onStart: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "play", clip.url]);
                },
                onPause: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "pause", clip.url]);
                },
                onStop: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "stop", clip.url]);
                },
                onFinish: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "finish", clip.url]);
                }
            }
        });
    });

    // dui video popup
    $("#duiVideoDialog").jqm({
        trigger: false,
        modal: true,
        toTop: true,
        onHide: function(h) {
            h.o.remove(); // remove overlay
            h.w.fadeOut(500); // hide window
        }
    });
    $("#duiVideoLink").click(function(event) {
        event.preventDefault();
        $("#duiVideoDialog").jqmShow();
        $f("duiPlayer", "/Content/flowplayer-3.0.3.swf", {
            clip: {
                autoPlay: true,
                onStart: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "play", clip.url]);
                },
                onPause: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "pause", clip.url]);
                },
                onStop: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "stop", clip.url]);
                },
                onFinish: function(clip) {
                    _gaq.push(["_trackEvent", "Videos", "finish", clip.url]);
                }
            }
        });
    });
    $("#closeDuiVideo").click(function(event) {
        event.preventDefault();
        $("#duiVideoDialog").jqmHide();
    });


    // embedded video player
    $f("a.embeddedVideo", { src: "/Content/flowplayer-3.0.3.swf", wmode: 'opaque' }, {
        clip: {
            autoPlay: true,
            onStart: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "play", clip.url]);
            },
            onPause: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "pause", clip.url]);
            },
            onStop: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "stop", clip.url]);
            },
            onFinish: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "finish", clip.url]);
            } 
        } 
    });

    // single video popup player
    $("#popupPlayer").attr("href", $("a.popupVideo").attr("href"));
    var popupPlayer = $f("popupPlayer", "/Content/flowplayer-3.0.3.swf", {
        clip: {
            autoPlay: true,
            onStart: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "play", clip.url]);
            },
            onPause: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "pause", clip.url]);
            },
            onStop: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "stop", clip.url]);
            },
            onFinish: function(clip) {
                _gaq.push(["_trackEvent", "Videos", "finish", clip.url]);
            }
        }
    });
    
    // setup link action. it will fire our overlay  
    $("a.popupVideo").overlay({
        // use the Apple effect for overlay 
        effect: 'apple',
        // when overlay is opened, load our player
        onLoad: function() {
            popupPlayer.load();
        },
        // when overlay is closed, unload our player 
        onClose: function() {
            popupPlayer.unload();
        }
    });
});
