﻿//masterpage scripts
$(document).ready(function () {

    // Navigation animations
    $(".navbox").mouseenter(function () {
        $(this).children(".subnav").show();
    });
    $(".navbox").mouseleave(function () {
        $(this).children(".subnav").hide();
    });
    $(".navbox a").click(function () {
        $(".navbox").children(".subnav").hide();
    });

    // Site search 
    $("#topsearch").autocomplete("/ajax/autocomplete.aspx", { max: 8, selectFirst: false, selectCurrent: false });
    var doonce = 0;
    $("#topsearch").focus(function () {
        if (doonce == 0) {
            $(this).val("");
            $(this).css("color", "#000000");
            doonce = 1;
        }
    });
    $("#search_button").click(function () {
        if ($("#topsearch").val() != "" && $("#topsearch").val() != "Search") {
            var url = "/Search/?searcher=" + $("#topsearch").val();
            document.location.href = url;
        }
    });
    $("#topsearch").keyup(function (e) {
        if (!e) {
            e = window.event;
        }
        if (e.which == 13) {
            if ($("#topsearch").val() != "" && $("#topsearch").val() != "ENTER KEYWORD OR STYLE #") {
                var url = "/Search/?searcher=" + $("#topsearch").val();
                document.location.href = url;
            }
        }
    });

    // Google Analytics Custom Event Tracking
    $(".ac_over").live("click", function () {
        _gaq.push(['_trackEvent', 'Autocomplete', '', '']);
    });
    $(".storelink").click(function () {
        _gaq.push(['_trackEvent', 'Find a Retailer', 'Icon', '']);
    });


    /****************************/
    /* Newsletter signup        */
    /****************************/

    // Click handler for email signup in ad bucket.
    $("#popsubtemp").live("click", function () {
        showSignUp(-400, -400);
    });

    // Click handler for email signup in footer
    $("#popsub").live("click", function () {
        showSignUp(-250, -100);
    });

    // FadeIn the email signup form
    function showSignUp(marginTop, marginLeft) {
        var popup = $("#signuppop");
        popup.css("margin-top", marginTop + "px");
        popup.css("margin-left", marginLeft + "px");
        popup.stop();
        popup.fadeIn();
    }

    // Close the email signup popup.
    $(".eclose").live("click", function () {
        var popup = $("#signuppop");
        popup.stop();
        popup.fadeOut();
    });

    // Occupation dropdown "Other" field
    $("#eoccupation").change(function () {
        var list = $("#eoccupation");
        var text = $("#eother");
        if (list.val() == "Other") {
            text.fadeIn();
            text.focus();
        }
        else {
            text.fadeOut();
            list.focus();
        }
    });

    // Email newsletter submission
    $("#epopsub").click(function () {
        var email = $("#eemail").val();
        var name = $("#ename").val();
        var city = $("#ecity").val();
        var state = $("#estates").val();
        var country = $("#ecountry").val();
        var occupation = $("#eoccupation").val();
        var other = $(".eother").val();
        var gender = $(".egender:checked").val();
        var age = $("#eage").val();

        var alertText = "";

        if (!checkEmail(email)) {
            alertText += "Please provide a valid email address.\n";
        }
        if (name == "") {
            alertText += "Please provide your name.\n";
        }
        if ((city == "" || state == "") && country == "") {
            alertText += "Please provide either City and State or Country.\n";
        }
        if (occupation == "") {
            alertText += "Please provide your Occupation.\n";
        }
        if ($(".egender:checked").length == 0) {
            alertText += "Please provide your Gender.\n";
        }
        if (age == "") {
            alertText += "Please provide your Date of Birth.\n";
        }
        if (alertText != "") {
            alert(alertText);
            return;
        }

        $("#eresponse").load("/ajax/newsletter.aspx", {
            email: email,
            name: name,
            city: city,
            state: state,
            country: country,
            occupation: occupation,
            other: other,
            gender: gender,
            age: age,
            source: "Dot Com Form"
        },
        function (response, status, xhr) {
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                $("#eresponse").html(msg + xhr.status + " " + xhr.statusText);
            } else {
                $("#eresponse").html(response);
                $("#signuppop").delay(5000).fadeOut();
            }
        });
    });


    /****************************************/
    /* Misc.                                */
    /****************************************/
    var timer;
    $("#email").focus(function () {
        $(this).val("");
        $(this).css("color", "#0000ff");
    });
    function hollaBack() {
        clearInterval(timer);
        $("#enterme").load("/ajax/newsletter.aspx #answer");
    }
    function otherWise(obj) {
        var oth = document.getElementById("other");
        if (obj.options[obj.options.selectedIndex].value == "Other") {
            oth.style.display = "block";
            oth.focus();
        } else {
            oth.style.display = "none";
        }
    }
});

