﻿var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

$(document).ready(function () {

    $("#loginReturnUrl").val(window.location);

    $("#txtHeaderSearch").focus(function () {
        if ($(this).val() == "search products") {
            $(this).val("");
        }
    });

    $("#txtHeaderSearch").blur(function () {
        if ($(this).val() == "") {
            $(this).val("search products");
        }
    });

    $("#txtFindRetailer").focus(function () {
        if ($(this).val() == "enter zip code") {
            $(this).val("");
        }
    });

    $("#txtJoinFamily").focus(function () {
        if ($(this).val() == "enter email") {
            $(this).val("");
        }
    });

    $("#txtTopJoinFamilyEmail").focus(function () {
        if ($(this).val() == "enter email") {
            $(this).val("");
        }
    });

    $("#txtSignInEmail").focus(function () {
        if ($(this).val() == "enter email") {
            $(this).val("");
        }
    });

    //Auto search functionality
    var loadUrl = "searchService.ashx";
    var dropDownObjectString = new Array();
    var searchDropDownText = "";
    var keyPressedForUpButton = false;
    var keyPressedForDownButton = false;
    var divNum;
    var currentTabIndex;

    document.onkeydown = function (e) {
        var k = e.keyCode;
        if (k >= 37 && k <= 40) {
            return false;
        }
        return true;
    };

    $('#txtHeaderSearch').keydown(function (event) {
        if (event.keyCode == '40' && !keyPressedForDownButton) {
            $('#item' + 1).focus();
            keyPressedForDownButton = true;
        }
        if (event.keyCode == '38' && !keyPressedForUpButton) {
            divNum = $("#searchList > div").size();
            currentTabIndex = divNum;
            $('#item' + currentTabIndex).focus();
            keyPressedForUpButton = true;
        }
    });
    $('#txtHeaderSearch').keyup(function (event) {
        if (event.keyCode == '40' && keyPressedForDownButton) {
            keyPressedForDownButton = false;
        }
        if (event.keyCode == '38' && keyPressedForUpButton) {
            keyPressedForUpButton = false;
        }
    });

    $('#keyWorkingArea').keydown(function (event) {
        currentTabIndex = document.activeElement.tabIndex;
        divNum = $("#searchList > div").size();

        if (event.keyCode == '40' && !keyPressedForDownButton) {
            if (currentTabIndex >= divNum || currentTabIndex < 0) {
                currentTabIndex = 0;
            }

            currentTabIndex = currentTabIndex + 1;
            $('#item' + currentTabIndex).focus();
            keyPressedForDownButton = true;
        }
        if (event.keyCode == '38' && !keyPressedForUpButton) {
            if (currentTabIndex == 1) {
                $("#txtHeaderSearch").focus();
            }
            currentTabIndex = currentTabIndex - 1;
            //                        if (currentTabIndex == 0) {
            //                            currentTabIndex = 4;
            //                            $('#item' + currentTabIndex).focus();
            //                        }
            if (currentTabIndex != 0 && currentTabIndex != divNum) {
                $(this).find('#item' + currentTabIndex).focus();
            }

            keyPressedForUpButton = true;

        }
        if (event.keyCode == '13') {

            var mylink = $(this).find('#item' + currentTabIndex).find("a");

            var url = mylink.attr("href");
            window.open(url, "_self");
        }
    });

    $('#keyWorkingArea').keyup(function (event) {
        if (event.keyCode == '40' && keyPressedForDownButton) {
            keyPressedForDownButton = false;
        }
        if (event.keyCode == '38' && keyPressedForUpButton) {
            keyPressedForUpButton = false;
        }
    });

    $("#txtHeaderSearch").keyup(function (event) {
        if (event.keyCode == '40' || event.keyCode == '38') {
            return;
        }
        if ($("#txtHeaderSearch").val() != "search products" && $("#txtHeaderSearch").val() != "") {
            var loadUrl = "searchService.ashx?SearchTerm=" + $("#txtHeaderSearch").val();
            $.get(loadUrl, function (data) {
                $("#searchList").html("");
                $("#searchList").append(data);
                $("#searchList").fadeIn(300);
                $("#searchDropDownText").fadeIn(300);


                $(".searchItem").hover(function () {
                    $(this).focus();
                    $(".viewAllButton").hover(function () {
                        $(this).focus();
                    });
                });


                $(".productSwatchImage").click(
                        function () {
                            $(this).parent().find(".productSwatchImage").each(function () {
                                $(this).find(".selectedSwatchBorder").removeClass().addClass("swatchBorder");
                            });

                            $imageLink = $(this).parent().parent().parent().parent().children(".productImageContainer").children("a");

                            $(this).find(".swatchBorder").removeClass().addClass("selectedSwatchBorder");
                            $(this).attr("data-originalUrl", $imageLink.children("img").attr("src"));

                            $imageLink.children("img").attr("", newImageUrl);
                            var newImageUrl = $(this).attr("data-imageUrl");
                            $imageLink.children("img").attr("src", newImageUrl);

                            var oldProductLink = $imageLink.attr("href");
                            var urledColor = $(this).attr("data-urledColor");
                            var newProductLink = oldProductLink.replace(/([&?])color[=].*/i, "$1color=" + urledColor);
                            if (newProductLink.indexOf("&color=", 0) < 0 && newProductLink.indexOf("?color=", 0) < 0) {
                                if (newProductLink.indexOf("?") < 0) {
                                    newProductLink = newProductLink + "?color=" + urledColor;
                                } else {
                                    newProductLink = newProductLink + "&color=" + urledColor;
                                }
                            }
                            $imageLink.attr("href", newProductLink);
                            $(this).parent().parent().children(".productName").children("a").attr("href", newProductLink);
                        });
            });
        } else {
            $("#searchList").html("");
            $("#searchList").css("display", "none");
            $("#searchDropDownText").css("display", "none");
        }
    });

    $("body").click(function (e) {

        //We use these methods because blur() doesn't allow you to click buttons without changing states
        //not click on bottom join Kelty control
        var joinKeltyBottomOffset = $("#joinTheFamily").offset();
        if (e.pageX < joinKeltyBottomOffset.left || e.pageY < joinKeltyBottomOffset.top ||
                e.pageX > joinKeltyBottomOffset.left + $("#joinTheFamily").width() || e.pageY > joinKeltyBottomOffset.top + $("#joinTheFamily").height()) {
            if ((e.srcElement != undefined && e.srcElement.id != "btnJoinFamily") || (e.target != undefined && e.target.id != "btnJoinFamily")) {
                $("#txtJoinFamily").val("enter email")
            }
        }

        //not click on quick search dropdown
        var searchListOffset = $("#searchList").offset();
        if (e.pageX < searchListOffset.left || e.pageY < searchListOffset.top ||
                e.pageX > searchListOffset.left + $("#searchList").width() || e.pageY > searchListOffset.top + $("#searchList").height()) {
            $("#searchList").fadeOut(10);
            $("#searchDropDownText").fadeOut(10);
        }

        //not click on top join family control or it's tab
        var joinFamilyTopOffset = $("#joinFamilyTop").offset();
        var joinFamilyBgOffset = $("#joinFamilyBackground").offset();
        if (!(e.pageX > joinFamilyTopOffset.left && e.pageY > joinFamilyTopOffset.top && e.pageX < joinFamilyTopOffset.left + $("#joinFamilyBackground").width() && e.pageY < joinFamilyTopOffset.top + $("#joinFamilyBackground").height()) &&
            !(e.pageX > joinFamilyBgOffset.left && e.pageY > joinFamilyBgOffset.top && e.pageX < joinFamilyBgOffset.left + $("#joinFamilyBackground").width() && e.pageY < joinFamilyBgOffset.top + $("#joinFamilyBackground").height())) {
            if ((e.srcElement != undefined && e.srcElement.id != "btnJoinFamilyTop") || (e.target != undefined && e.target.id != "btnJoinFamilyTop")) {
                $("#txtTopJoinFamilyEmail").val("enter email")
                $("#joinFamilyBackground").fadeOut(10);
                $("#joinFamilyTop").removeClass("open");
            }
        }

        //not click on find a retailer control
        var searchDealersOffset = $("#findARetailer").offset();
        if (e.pageX < searchDealersOffset.left || e.pageY < searchDealersOffset.top ||
                e.pageX > searchDealersOffset.left + $("#findARetailer").width() || e.pageY > searchDealersOffset.top + $("#findARetailer").height()) {
            if ((e.srcElement != undefined && e.srcElement.id != "btnFindRetailer") || (e.target != undefined && e.target.id != "btnFindRetailer")) {
                $("#txtFindRetailer").val("enter zip code");
            }
        }

        //not click on sign In/Out control or it's tab
        var signInOutOffset = $("#signInOut").offset();
        var signInOutBgOffset = $("#signInOutBackground").offset();
        if (!(e.pageX > signInOutOffset.left && e.pageY > signInOutOffset.top && e.pageX < signInOutOffset.left + $("#signInOutBackground").width() && e.pageY < signInOutOffset.top + $("#signInOutBackground").height()) &&
            !(e.pageX > signInOutBgOffset.left && e.pageY > signInOutBgOffset.top && e.pageX < signInOutBgOffset.left + $("#signInOutBackground").width() && e.pageY < signInOutBgOffset.top + $("#signInOutBackground").height())) {
            if ((e.srcElement != undefined && e.srcElement.id != "btnSignIn") || (e.target != undefined && e.target.id != "btnSignIn")) {
                $("#txtSignInEmail").val("enter email")
                $("#signInOutBackground").fadeOut(10);
                $("#signInOut").removeClass("open");
                $("#txtSignInPassword").val("");
            }
        }
    });

    //open top join family control
    $("#joinFamilyTop").click(function (e) {
        if ((e.srcElement && (e.srcElement.id == "joinFamilyTop" || e.srcElement.id == "joinFamilyTab")) ||
        (e.target && (e.target.id == "joinFamilyTop" || e.target.id == "joinFamilyTab"))) {
            if ($("#joinFamilyTop").is(".open")) {
                $("#txtTopJoinFamilyEmail").val("enter email");
                $("#joinFamilyBackground").fadeOut(10);
                $("#joinFamilyTop").removeClass("open");
            } else {
                $("#joinFamilyBackground").css("display", "block");
                $("#joinFamilyTop").addClass("open");
            }
        }
    });

    //open login control
    $("#signInOut").click(function (e) {
        if ((e.srcElement && (e.srcElement.id == "signInOut" || e.srcElement.id == "signInOutTab")) ||
        (e.target && (e.target.id == "signInOut" || e.target.id == "signInOutTab"))) {
            if ($.trim($("#signInOutTab").text()) == "Login") {
                if ($("#signInOut").is(".open")) {
                    $("#txtSignInEmail").val("enter email");
                    $("#signInOutBackground").fadeOut(10);
                    $("#signInOut").removeClass("open");
                    $("#txtSignInPassword").val("");
                } else {
                    $("#signInOutBackground").css("display", "block");
                    $("#signInOut").addClass("open");
                }
            } else {
                window.location = "signout.aspx";
            }
        }
    });
});
