﻿Global.load(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 Your ZIP Code") {
            $(this).val("");
        }
    });

    $("#txtFindRetailer").blur(function () {
        if ($(this).val() == "") {
            $(this).val("Enter Your ZIP Code");
        }
    });

    var searchText = '',
        $searchInput = $('#searchPageInput');

    $searchInput.bind('focus', function () {
        searchText = $searchInput.val();
        searchText == 'Search Products' ? $searchInput.val('') : $searchInput.val(searchText);
    });

    $searchInput.bind('blur', function () {
        searchText = $searchInput.val();
        searchText == '' ? $searchInput.val('Search Products') : $searchInput.val(searchText);
    });

    //Auto search functionality
    var keyPressedForDownButton = false,
        keyPressedForUpButton = false;

    window.top.document.onkeydown = function (evt) {
        evt = evt || window.event;
        if (evt.keyCode >= 38 && evt.keyCode <= 40 && evt.keyCode != 39) {
            return false;
        }
        return true;
    };

    $(window).keyup(function (event) {
        if (event.which == 27) {
            closeLoginBox();

            if (window.closeAddressBox != undefined) {
                closeAddressBox();
                closeEditAddressBox();
            }
        }
    });

    $('.search-dropdown').keydown(function (event) {
        var currentTabIndex = document.activeElement.tabIndex,
            divNum = $("#searchList > li").size() + 1;

        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 != divNum) {
                $(this).find('#item' + currentTabIndex).focus();
            }

            keyPressedForUpButton = true;
        }
        if (event.keyCode == '13') {
            var mylink = $(this).find('#item' + currentTabIndex).find("a"),
                url = mylink.attr("href");
            window.open(url, "_self");
        }
    });



    $('.search-dropdown').keyup(function (event) {
        if (event.keyCode == '40' && keyPressedForDownButton) {
            keyPressedForDownButton = false;
        }
        if (event.keyCode == '38' && keyPressedForUpButton) {
            keyPressedForUpButton = false;
        }
    });

    $('#txtHeaderSearch').keydown(function (event) {
        if (event.keyCode == '40' && !keyPressedForDownButton) {
            $('#item' + 1).focus();
            keyPressedForDownButton = true;
        }
        if (event.keyCode == '38' && !keyPressedForUpButton) {
            var divNum = $("#searchList > li").size(),
                currentTabIndex = divNum + 1;
            $('#item' + currentTabIndex).focus();
            keyPressedForUpButton = true;
        }
    });

    $("#txtHeaderSearch").keyup(function (event) {
        if (event.keyCode == '40' && keyPressedForDownButton) {
            keyPressedForDownButton = false;
        }
        if (event.keyCode == '38' && keyPressedForUpButton) {
            keyPressedForUpButton = false;
        }
        if (event.keyCode == '40' || event.keyCode == '38') {
            return;
        }

        var $txtHeaderSearch = $("#txtHeaderSearch");
        if ($txtHeaderSearch.val() != "search products" && $txtHeaderSearch.val() != "") {
            var loadUrl = "searchService.ashx?SearchTerm=" + $txtHeaderSearch.val();
            $.get(loadUrl, function (data) {
                $("#search-results .results .search-dropdown").html("").append(data).show();

                // Automatically selecting the first color swatch on each product and applying a class of active to it.
                $('#searchList .productSwatches').each(function () {
                    var $this = $(this),
                    $swatch = $this.children('li:eq(0)').attr('data-color') != null ? $this.children('li:eq(0)') : $this.children('li:eq(1)');

                    // Creates divs on all the swatches to place the checkmarks in if they don't already exist.
                    if ($this.find('div.checkmarkOverSwatch').length < 1) {
                        $('.swatch-link').each(function () {
                            $('<div class="checkmarkOverSwatch small" />').insertBefore($(this).children('div:eq(0)'));
                        });
                    }

                    findAndSetParentImageColorAndLink($swatch.children('a'), $swatch.children('a').attr("data-urledcolor"));
                    $swatch.addClass('active');
                });

                $('#searchList li, #item4').hover(function () {
                    $(this).focus();
                });

                $("#searchList .productSwatchImage").click(
                        function () {
                            var $this = $(this);
                            $this.parent().find(".productSwatchImage").each(function () {
                                $(this).find(".selectedSwatchBorder").removeClass().addClass("swatchBorder");
                            });

                            var $imageLink = $this.parent().parent().parent().parent().children(".productImageContainer").children("a"),
                                newImageUrl = $this.attr("data-imageUrl"),
                                oldProductLink = $imageLink.attr("href"),
                                urledColor = $this.attr("data-urledColor"),
                                newProductLink = oldProductLink.replace(/([&?])color[=].*/i, "$1color=" + urledColor);

                            $this.find(".swatchBorder").removeClass().addClass("selectedSwatchBorder");
                            $this.attr("data-originalUrl", $imageLink.children("img").attr("src"));
                            $imageLink.children("img").attr("", newImageUrl);
                            $imageLink.children("img").attr("src", newImageUrl);


                            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 {
            $("#search-results .results .search-dropdown").html("").hide();
        }
    });

    // Temporary fix for very strange behavior when clicking the search box or search results while results are showing.
    $('#txtHeaderSearch, #searchList').live('click', function () {
        setTimeout(function () {
            if ($('#txtHeaderSearch').val().length > 1) {
                $('.search-dropdown').show();
            }
        }, 13);
    });
});
