﻿
function setupSizeListing() {
    $('#' + AddToBasketLinkButtonID).hide()
    if (availableSizes.length == 1) {
        $('#' + SizesDropDownListID + ' > option[value="Please Choose your size."]').remove();
        var selectedVal = $('#' + SizesDropDownListID).val();
        var inStock = false;
        for (var i = 0; i < availableSizes.length; i++) {
            if (availableSizes[i] == selectedVal) {
                inStock = true
            }
        }
        if (inStock) {
            $('#' + AddToBasketLinkButtonID).show()
        } else {
            $('#' + AddToBasketLinkButtonID).hide()
        }
        //$('#' + SizesDropDownListID).hide();
    }
    $('#' + SizesDropDownListID).change(function() {

        $('#' + SizesDropDownListID + ' > option[value="Please Choose your size."]').remove();
        var selectedVal = $('#' + SizesDropDownListID).val();
        var inStock = false;
        for (var i = 0; i < availableSizes.length; i++) {
            if (availableSizes[i] == selectedVal) {
                inStock = true
            }
        }
        if (inStock) {
            $('#' + AddToBasketLinkButtonID).show()
        } else {
            $('#' + AddToBasketLinkButtonID).hide()

        }
        refreshAll();
    });
}
function swap(str, str2) {
    $('#productImagery > a > img').load(function() {
    $('#productImagery > a > img').css("opacity", "1");
    });
    $('#productImagery > a > img').css("opacity", ".5");
    $('#productImagery > a > img').attr("src", str)
    $('#productImagery > a ').attr("href", str2)
    return false;
    
}
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
String.prototype.startsWith = function(str)
{ return (this.match("^" + str) == str) }

String.prototype.endsWith = function(str) 
{ return (this.match(str+"$")==str) }

$(document).ready(function() {


    if (nav != null) {
        var navParts = nav.split("\n");
        $(".navLeft ul li a").hover(function() {
            var dep = $(this).parent().parent().parent().parent().prev().html();
            for (var i = 0; i < navParts.length; i++) {
                var np = navParts[i].replace(/-/g, " ").replace(/_/g, "/"); ;
                np = unescape(np);

                if (np.startsWith(dep + ":" + $(this).text())) {

                    $(this).parent().parent().parent().parent().find("a").filter(function() {
                        return $(this).html()== np.split(':')[2]
                    }).css({ color: "#E9B600" })

                }

            }

        }, function() {
            $(this).parent().parent().parent().parent().find("a").css({ color: "" });
        });
        $(".navRight ul li a").hover(function() {
            var dep = $(this).parent().parent().parent().parent().prev().html();
            for (var i = 0; i < navParts.length; i++) {
                var np = navParts[i].replace(/-/g, " ").replace(/_/g, "/");
                np = unescape(np);

                if (np.startsWith(dep + ":") && np.endsWith($(this).text())) {

                    $(this).parent().parent().parent().parent().find("a").filter(function() {
                        return $(this).html()== np.split(':')[1]
                    }).css({ color: "#E9B600" })

                }

            }

        }, function() {
            $(this).parent().parent().parent().parent().find("a").css({ color: "" });
        });
    }
    /*
    if (readCookie("rightColSel") == "recentlyviewed") {
    $('#hotpicks').hide();
    } else {
    $('#recentlyviewed').hide();
    }
    $('#recentlyviewedbutton').click(function() {
    createCookie("rightColSel", "recentlyviewed");
    $('#hotpicks').slideUp('fast', function() {
    $('#recentlyviewed').slideDown('normal');
    });
    });
    $('#hotpicksbutton').click(function() {
    createCookie("rightColSel", "hotpicks");
    $('#recentlyviewed').slideUp('fast', function() {
    $('#hotpicks').slideDown('normal');
    });
    });
    if (readCookie("leftColSel") == "brands") {
    $('#categories').hide();
    } else {
    $('#brands').hide();
    }*/
    $('#brandsbutton').click(function() {
        createCookie("leftColSel", "brands");
        $('#categories').slideUp('fast', function() {
            $('#brands').slideDown('normal');
        });
    });
    $('#categoriesbutton').click(function() {
        createCookie("leftColSel", "categories");
        $('#brands').slideUp('fast', function() {
            $('#categories').slideDown('normal');
        });
    });
    $('.searchTextBox').focus(function() {
        if ($(this).val() == "Search...") {
            $(this).val("");
        }
        this.select();
        $(this).css("font-style", "normal");
        $(this).css("color", "Black");
    });
    $('.weloveBox').mouseover(function() {
        $(this).children('.weloveInfo').show()
    });
    $('.weloveBox').mouseout(function() {
        $(this).children('.weloveInfo').hide()
    });


});


function showQuickSearch() {
    $('#productSearch').hide();
    $('#quickSearch').show();
}
function showProductSearch() {
    $('#productSearch').show();
    $('#quickSearch').hide();
}
function departmentChange() {
    $("#" + LabelCheckBoxListID + " label").hide();
    $("#" + LabelCheckBoxListID + " input").hide();
    //$("#" + LabelCheckBoxListID + " br").hide();
    for (var i = 0; i < paramData.length; i++) {
        if (paramData[i][0] == $('#productSearch select').val()) {
            $("#" + LabelCheckBoxListID + " label:contains('" + paramData[i][1] + "')").show();
            $("#" + LabelCheckBoxListID + " label:contains('" + paramData[i][1] + "')").prev().show();
            //$("#" + LabelCheckBoxListID + " label:contains('" + paramData[i][1] + "')").next().show();
        }
    }
    $("#" + LabelCheckBoxListID + " input:hidden").checked = false;
    $("#" + LabelCheckBoxListID + " input:first").triggerHandler('click');

}
function setupProductSearchParas() {


    $('#productSearch select').change(departmentChange);
    
    $("#" + LabelCheckBoxListID + " input").click(function() {
        $("#" + GroupCheckBoxListID + " input").hide();
        //$("#" + GroupCheckBoxListID + " br").hide();
        $("#" + GroupCheckBoxListID + " label").hide();
        $("#searchGroupsEmpty").show();
        $("#" + LabelCheckBoxListID + " input:checked").each(function() {
            for (var i = 0; i < paramData.length; i++) {
                //alert(paramData[i][0]+" == "+$('#productSearch > select').val());
                if (paramData[i][0] == $('#productSearch select').val()
                    &&
                    paramData[i][1] == $(this).next().text()) {

                    //$("#" + GroupCheckBoxListID + " label:contains('" + paramData[i][2] + "')").next().show(); //show the br
                    $("#" + GroupCheckBoxListID + " label:contains('" + paramData[i][2] + "')").show(); //show the label
                    $("#" + GroupCheckBoxListID + " label:contains('" + paramData[i][2] + "')").prev().show(); //show the checkbox
                    $("#searchGroupsEmpty").hide();
                }
            }
        })
        $("#" + GroupCheckBoxListID + " input:hidden").each(function(i) {
            this.checked = false;
        });
        $("#" + GroupCheckBoxListID + " input:first").triggerHandler('click');

    });
    $("#" + GroupCheckBoxListID + "  input").click(function() {
        $('#sizeLibrariesDiv h3').hide();
        $('#sizeLibrariesDiv h3').each(function(i) {
            $(this).next().hide(); //hide the span
            //$(this).next().next().hide();//hide the space
        });
        $("#searchSizesEmpty").show();
        var dept=$('#productSearch select').val()
        $("#" + GroupCheckBoxListID + " input:checked").each(function() {
            var group = $(this).next().text();
            $("#" + LabelCheckBoxListID + " input:checked").each(function() {
                for (var i = 0; i < paramData.length; i++) {
                    if (paramData[i][0] == dept
                        &&
                        paramData[i][1] == $(this).next().text()
                        &&
                        paramData[i][2] == group) {
                        $("#sizeLibrariesDiv h3:contains('" + paramData[i][3] + "')").show();
                        $("#sizeLibrariesDiv h3:contains('" + paramData[i][3] + "')").next().show();
                        //$("#sizeLibrariesDiv > h3:contains('"+paramData[i][3]+"')").next().next().show();
                        $("#searchSizesEmpty").hide();
                    }

                }
            });
        });
        $('#sizeLibrariesDiv > span:hidden > input').each(function(i) {
            this.checked = false;
        });
    });
    departmentChange();
}


var curModal;
function openModal(m) {
    if (curModal != null) {
        closeModal(function() {
            doOpenModal(m)
        }, true);
    } else {

        doOpenModal(m)
    }

}

function doOpenModal(m) {
    curModal = m;
    $("#modalBackground").css("display", "block");
    $(document).scrollTop(0);
    $("#modalBackground").animate({ opacity: 0.7 }, 200, "linear", function() {
        curModal.css("display", "block");


        var wW = $("#main").width()
        var mW = curModal.outerWidth()
        var wH = $(window).height()

        var mH = curModal.outerHeight()
        curModal.css("left", Math.max((wW - mW) / 2, 0));
        curModal.css("top", Math.max((wH - mH) / 2, 0));
        curModal.animate({ opacity: 1 }, 200, "linear", function() {
            curModal.css("opacity", "");
        });
    });
}
function closeModal(f, leaveBack) {
    curModal.animate({ opacity: 0 }, 300, "linear", function() {
        curModal.css("display", "none");
        curModal = null;
        if (leaveBack) {
            if (f != null)
                f();
        } else {
            $("#modalBackground").animate({ opacity: 0.0 }, 300, "linear", function() {
                $("#modalBackground").css("display", "none");

                if (f != null)
                    f();
            });
        }
    });
}
$(document).ready(function() {

    $("a[href*='#Modal']").click(function() {
        openModal($($(this).attr("href").substr($(this).attr("href").indexOf("#"))));
        return false;
    });
    $(".modalBox").css("opacity", 0);
    $("#modalBackground").css("opacity", 0);
    $(".modalBox a.closeModal").click(function() { closeModal() });
    $(".modalBox input.closeModal").click(function() { closeModal() });
    $(window).resize(function() {

        if (curModal == null)
            return;
        var wW = $("#main").width()
        var mW = curModal.outerWidth()
        var wH = $(window).height()
        var mH = curModal.outerHeight()
        curModal.css("left", Math.max((wW - mW) / 2, 0));
        curModal.css("top", Math.max((wH - mH) / 2, 0));
    });
    $(".modalBox").each(function() {
    });
    $(".modalBox").scroll(function() {
    });
});