var $j = jQuery.noConflict(), h = window.location.href;

$j(document).ready(function () {

    try {
        if (window.location.href.toLowerCase().indexOf('returnitem.aspx') > -1) {
            //cannot apply decrementing z-index styles to selectMenus in dynamic grid on shared page to prevent bottom menus from appearing on top of above menu's drop-down items. removed jquery/selectbox and use simple styles
            $j('select').css('background-color', 'black').css('color', 'white').css('border', 'none').css('height', '20px').css('padding', '2px').css('cursor', 'pointer');
            //cover up the gray down-arrow
            $j('select').wrap('<div class="jquery-menuwrapper" style="position:relative;overflow:hidden;">');
            $j('.jquery-menuwrapper').css('width', $j('select').css('width')).append('<img id="btn4" src="/images/menuarrow.gif" style="z-index:5;position:absolute;top:2px;right:0px;" />');
        }
        else {
            $j('select').selectbox();
        }
    } catch (err) { }

    try {
        $('.sub-menu3').hover(function () {
            try {
                zoom_hide();
            } catch (Error) { }
        }, function () {
            try {
                zoom_show('700', '870');
            } catch (Error) { }
        });
    } catch (Error) { }

    try {
        $('.sub-menu4').hover(function () {
            try {
                zoom_hide();
            } catch (Error) { }
        }, function () {
            try {
                zoom_show('700', '870');
            } catch (Error) { }
        });
    } catch (Error) { }

    try {
        $('.sub-menu5').hover(function () {
            try {
                zoom_hide();
            } catch (Error) { }
        }, function () {
            try {
                zoom_show('700', '870');
            } catch (Error) { }
        });
    } catch (Error) { }

    try {
        $('.sub-menu9').hover(function () {
            try {
                zoom_hide();
            } catch (Error) { }
        }, function () {
            try {
                zoom_show('700', '870');
            } catch (Error) { }
        });
    } catch (Error) { }

    $j('#tabs A').click(function (e) {
        curr = $j(this).attr('href');
        $j('#tabs LI').removeClass('active');
        $j(this).parent('li').addClass('active');
        $j('.tab').removeClass('open');
        $j(curr).addClass('open');

        if ($j(this).hasClass('.fit')) {
            $j('.fit').click(function () {
                $j('#last-tab').addClass('active');
            });
        } else {
            return false;
        }
    });

    $j('.fit').click(function () {

        $j('#last-tab').addClass('active');
        $j('#tab2').removeClass('open');
        $j('#tab1').removeClass('open');
        $j('#tab3').addClass('open');
    });

    $j("div.enterleave").mouseenter(function () {
        navigatingShoppingTote = true;
        try {
            zoom_hide();
        } catch (Error) { }
    }).mouseleave(function () {
        navigatingShoppingTote = false;
        closeTote();
        try {
            zoom_show('1000', '1182')
        } catch (Error) { }
    });

    $j(".unselected").click(function () {
        $j('.jquery-selectbox').selectbox().removeClass(".jquery-selectbox");
        var $jfoo = $j('.jquery-custom-selectboxes-replaced-list');
        for (var i = 0, n = $jfoo.length; i < n; ++i) {
            var element = $jfoo[i];
            if (i != 1)
                $j('.jquery-custom-selectboxes-replaced-list').remove();
        }
        $j('.jquery-selectbox').selectbox().addClass(".jquery-selectbox");
    });

    $j(".selected").click(function () {
        $j('.jquery-selectbox').selectbox().removeClass(".jquery-selectbox");
        var $jfoo = $j('.jquery-custom-selectboxes-replaced-list');
        for (var i = 0, n = $jfoo.length; i < n; ++i) {
            var element = $jfoo[i];
            if (i != 1)
                $j('.jquery-custom-selectboxes-replaced-list').remove();
        }
        $j('.jquery-selectbox').selectbox().addClass(".jquery-selectbox");
    });

    $j('.photo-prod-block A.view-details').click(function () {
        $j('.ajax-window').show();
        return false;
    });
    $j('.ajax-window A.close').click(function () {
        $j('.ajax-window').hide();
        return false;
    });

    var s = $j('.sub'), twos = $j('.the-world-of-sub'), cs = $j('.collection-sub');

    if ($j('a.shop', '.main-menu li.active').length) {
        s.setActiveTierVisibility();
    }

    else if (h.indexOf('the-world-of') != -1 || h.indexOf('the-story') != -1) {
        twos.setActiveTierVisibility();
        setActiveClasses('two');
    }

    else if (h.indexOf('collections') != -1) {
        cs.setActiveTierVisibility();
        setActiveClasses('c');
    }

    $j('.main-menu li a.shop').hover(function () {
        s.setTierVisibility();
    },
    function () {
        restoreActiveTierVisibility();
    });

    $j('.main-menu li a.the-world-of').hover(function () {
        twos.setTierVisibility();
    },
    function () {
        restoreActiveTierVisibility();
    });

    $j('.main-menu li a.collections').hover(function () {
        cs.setTierVisibility();
    },
    function () {
        restoreActiveTierVisibility();
    });

    $j('.main-menu li a.jv-funhouse').hover(function () {
        stopActiveTierAnimation();
        $j('.tier2').css('visibility', 'hidden');
    },
    function () {
        restoreActiveTierVisibility();
    });

});

restoreActiveTierVisibility = function (t) {
    $j('.active-tier').stop(true).animate({ left: 0 }, 500, function () {
        $j(this).css('visibility', 'visible').siblings('.tier2').css('visibility', 'hidden');
    });
}

stopActiveTierAnimation = function () {
    $j('.active-tier').stop(true);
}

setActiveClasses = function (a) {
    switch (a) {
        case 'two':
            {
                $j('.the-world-of').parent().addClass('active');
                if (h.indexOf('in-the-press') != -1) {
                    $j('.nav-press').addClass('active');
                }
                else if (h.indexOf('celebrity') != -1) {
                    $j('.nav-celebrity').addClass('active');
                }
                else if (h.indexOf('events') != -1) {
                    $j('.nav-events').addClass('active');
                }
                else if (h.indexOf('tv') != -1) {
                    $j('.nav-tv').addClass('active');
                }
                else if (h.indexOf('campaigns') != -1) {
                    $j('.nav-campaigns').addClass('active');
                }
                else if (h.indexOf('the-story') != -1) {
                    $j('.nav-story').addClass('active');
                }
            }
            break;
        case 'c':
            {
                $j('.collections').parent().addClass('active');
                if (h.indexOf('jv-collection') != -1) {
                    $j('.nav-jv-collection').addClass('active');
                }
                else if (h.indexOf('jv-star-usa') != -1) {
                    $j('.nav-jv-star-usa').addClass('active');
                }
                else if (h.indexOf('converse') != -1) {
                    $j('.nav-converse').addClass('active');
                }
                else if (h.indexOf('time') != -1) {
                    $j('.nav-time').addClass('active');
                }
                else if (h.indexOf('eyewear') != -1) {
                    $j('.nav-eyewear').addClass('active');
                }
                else if (h.indexOf('fragrance') != -1) {
                    $j('.nav-fragrance').addClass('active');
                }
                else if (h.indexOf('music') != -1) {
                    $j('.nav-music').addClass('active');
                }
            }
            break;
    }
}

$j.fn.setTierVisibility = function () {
    stopActiveTierAnimation();
    $(this).css('visibility', 'visible').hover(function () {
        stopActiveTierAnimation();
    }, function () {
        restoreActiveTierVisibility();
    }).siblings('.tier2').css('visibility', 'hidden');
}

$j.fn.setActiveTierVisibility = function () {
    $(this).css('visibility', 'visible').addClass('active-tier').siblings('tier2').removeClass('active-tier');
}


function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}
function setFooter() {
    if (document.getElementById) {
        var windowHeight = getWindowHeight();
        if (windowHeight > 0) {
            var contentHeight = document.getElementById('content').offsetHeight;
            var footerElement = document.getElementById('footer');
            var footerHeight = footerElement.offsetHeight;

            if (windowHeight - (contentHeight + footerHeight) >= 0) {
                footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
            }
            else {
                footerElement.style.top = '0px';
            }
        }
    }
}
window.onload = function () {
}
window.onresize = function () {
}
var cartopen = false;
var t = null;
var cartopen = false;
var shoppingToteOpen = false;
var navigatingShoppingTote = false;
var clickEventSet = false;

$j(document).click(function () {
    if (cartopen == true) {
        closeTote();
        cartopen = false;
        return false;
        $j("#slidingTopContent").hide();
    }
});

$j(document).ready(function () {

    showMiniCart();
    $j('#shopping-cart-content').click(function () {
        if (cartopen == true)
            return false;
        $j("#slidingTopContent").slideToggle("slow", function () {
            cartopen = true;
            setTimeout(closeTote, 5000);
            return false;
        });
    });
});

function openTote() {
    shoppingToteOpen = true;
    $j("#slidingTopTrigger").fadeTo(1000, 1, function () {
        $j("#slidingTopContent").slideToggle("slow", function () {
        });
    });
}

function closeTote() {
    if (navigatingShoppingTote == false) {
        $j("#slidingTopContent").slideUp("slow", function () {
            $j("#slidingTopContent").hide();
        });
        shoppingToteOpen = false;
    }
    navigatingShoppingTote = false;
    cartopen = false;
}

function setClickEvent() {

    if (!navigatingShoppingTote) {
        if (shoppingToteOpen == true)
            closeTote();
        else
            openTote();
    }
    else {
        clearTimeout(t);
    }
}

function SearchVarvSetQuery(searchfield, storeUrl, btnId, searchfieldid) {
    if (false == ValidateSearch(searchfieldid)) {
        return false;
    }
    searchfield = trim(searchfield.toLowerCase());
    if (false == ValidateSearch(searchfieldid)) {
        return false;
    }
    else if (searchfield == "y378k4-zx4") {
        document.MM_returnValue = false;
        window.location.href = "/_Superfine_Merino_Wool_Long_Sleeve_Striped_Henley_/pd/c/21/np/21/p/1297.html";
    }
    else if (searchfield == "j245k4b-aajc") {
        document.MM_returnValue = false;
        window.location.href = "/_Lightweight_Cotton_Beale_Jean_/pd/c/24/np/24/p/1319.html";
    }
    else if (searchfield == "careers" || searchfield == "job" || searchfield == "jobs" || searchfield == "employment" || searchfield == "internships" || searchfield == "internship") {
        document.MM_returnValue = false;
        window.location.href = "/contact_us.html";
    }
    else if (searchfield == "store locator" || searchfield == "stores" || searchfield == "store" || searchfield == "store locations") {
        document.MM_returnValue = false;
        window.location.href = "/store-locator";
    }
    else if (searchfield == "gift card" || searchfield == "gift cards" || searchfield == "gift certificate") {
        document.MM_returnValue = false;
        window.location.href = "/store/giftcardproduct.aspx";
    }
    else if (searchfield == "women") {
        document.MM_returnValue = false;
        window.location.href = "/store/productslist.aspx?categoryid=6&s=womens";
    }
    else if (searchfield == "shipping") {
        document.MM_returnValue = false;
        window.location.href = "/help.html";
    }
    //          else if (searchfield == "shopping spree" || searchfield == "sweepstakes") {
    //              document.MM_returnValue = false;
    //              window.location.href = "/store/sweepstakes.aspx";
    //          }
    else if (searchfield == "alice cooper" || searchfield == "chris cornell" || searchfield == "franz ferdinand" || searchfield == "velvet revolver" || searchfield == "iggy pop" || searchfield == "iggy pop" || searchfield == "joe perry" || searchfield == "ryan adams" || searchfield == "perry farrell" || searchfield == "zz top" || searchfield == "cheap trick") {
        document.MM_returnValue = false;
        window.location.href = "/the-world-of/campaigns";
    }
    else if (searchfield == "p267l1-aebh") {
        document.MM_returnValue = false;
        window.location.href = "/Slim_Fit_Arrow_Top_Pant/pd/c/23/np/23/p/1457.html";
    }
    else {
        document.MM_returnValue = false;
        window.location.href = storeUrl + CallEncode(searchfield);
    }
}

function trim(str) {
  
}

//override func in /store/os/misc.js in order to reset size ddl to 'Select Color' whenever a color swatch is clicked
function reload_sizes() {
    if (size_count == 0)
        return;

    var ps = getElementByID_Master("ProductSizes");
    if (ps == null)
        return;

    //updated to enable quicklook on pdp for huds
    if (ps.options != null) {
        var curr_value = ps.options[ps.selectedIndex].value;
        var curr_index = 0;

        ps.length = 0;
        ps.options[0] = new Option("Select " + g_sizealias, "-1");
        curr_index++;


        for (var i = 1; i <= size_count; i++) {
            if (g_color == 0 || aSCSA[g_color][i] == true || g_showwhenoos == 1) {
                ps.options[curr_index] = new Option(size_names[i], size_ids[i]);
                if (curr_value == size_ids[i])
                    ps.selectedIndex = curr_index;

                curr_index++;
            }
        }
    }
    if (ps.length == 2 && (getElementByID_Master("DisableOneSizeSelection") == null || (getElementByID_Master("DisableOneSizeSelection") != null && getElementByID_Master("DisableOneSizeSelection").value != 1))) {
        ps.selectedIndex = 1;
    }
    $j('#ContentPlaceHolder1_ProductSizes').val('-1');
}
