﻿/******************************************************************************************************/
/*      jquery.header.js                                                            */
/*                                                                                                    */
/*      DefaultHeaderUserControl.ascx presentation encapsulation                                      */
/*                                                                                                    */
/******************************************************************************************************/
jQuery.header = function () {
    /****************************************** iFrames ******************************************/
    // Global variables to kill window timeouts
    var windowTimeout = 2000;
    var headerLoginWindowTimeout;
    var orderStatusWindowTimeout;
    var persistentCartWindowTimeout;
    var isHeaderLoginIframeLoaded = false;
    var isOrderStatusIframeLoaded = false;
    var isPersistentCartIframeLoaded = false;
    var headerLoginIframeWidth = 181;
    var orderStatusIframeWidth = 162;
    var persistentCartIframeWidth = 282;
    var headerLoginIframeHeight = 119;
    var orderStatusIframeHeight = 98;
    var persistentCartIframeHeight = 50;
    var defaultDomain = 'http://';
    var aDdmain = document.domain.split(".");
    var ddmain = "";
    for (var x = 1; x < aDdmain.length; x++) ddmain += ((x > 1) ? "." : "") + aDdmain[x];
    defaultDomain += (document.location.hostname.indexOf('search.') != 0) ? document.location.hostname : ddmain;
    //(document.location.hostname.indexOf('www.') != 0) ? 'www.' + document.domain : document.domain;
    //defaultDomain += (document.location.hostname.indexOf('search.') != 0) ? document.location.hostname : (document.location.hostname.indexOf('www.') != 0) ? 'www.' + document.domain : document.domain;
    var selectedIframe;
    var activeElement;

    //validate the login
    callLogIn();
    validateLogin();

    // Get the subcat from XML
    createSubCats();

    //Display contactContainer
    if ($.browser.msie)//.version == "6.0" || $.browser.version == "7.0")
        $('.contactContainer ul').css('visibility', 'visible');

    $('.contactContainer ul').fadeIn('slow');

    //Function to show/hide the SignIn iframe
    //$('.linkToShowHeaderLoginBox').mouseover(function (e) { signInMouseOver(e, true); });
    $('.linkToShowHeaderLoginBox').click(function (e) { signInMouseOver(e, true); });
    $('.headerLoginBox, .headerLoginSelectedTab').bind('mouseover click', function (e) { signInMouseOver(e, false); });
    $('.linkToShowHeaderLoginBox, .headerLoginSelectedTab').mouseout(function (e) { headerIframeMouseOut(e, $('.headerLoginBox'), $('.headerLoginSelectedTab'), 'signIn'); });
    $('.headerLoginBox').contents().mouseout(function (e) { headerIframeMouseOut(e, $('.headerLoginBox'), $('.headerLoginSelectedTab'), 'signIn'); });

    //Function to show/hide the Order Status iframe
    //$('.linkToShowOrderStatusBox').mouseover(function (e) { orderStatusMouseOver(e, true); });
    $('.linkToShowOrderStatusBox').click(function (e) { orderStatusMouseOver(e, true); });
    $('.orderStatusBox, .orderStatusSelectedTab').bind('mouseover click', function (e) { orderStatusMouseOver(e, false); });
    $('.linkToShowOrderStatusBox, .orderStatusSelectedTab').mouseout(function (e) { headerIframeMouseOut(e, $('.orderStatusBox'), $('.orderStatusSelectedTab'), 'orderStatus'); });
    $('.orderStatusBox').contents().mouseout(function (e) { headerIframeMouseOut(e, $('.orderStatusBox'), $('.orderStatusSelectedTab'), 'orderStatus'); });

    //Function to show/hide the Persistent Cart iframe
    //$('.linkToShowPersistentCartBox').mouseover(function (e) { persistentCartMouseOver(e, true); });
    $('.linkToShowPersistentCartBox, .cartName').click(function (e) { persistentCartMouseOver(e, true); });
    $('.persistentCartBox, .persistentCartSelectedTab').bind('mouseover click', function (e) { persistentCartMouseOver(e, false); });
    $('.linkToShowPersistentCartBox, .persistentCartSelectedTab').mouseout(function (e) { headerIframeMouseOut(e, $('.persistentCartBox'), $('.persistentCartSelectedTab'), 'persistentCart'); });
    $('.persistentCartBox').contents().mouseout(function (e) { headerIframeMouseOut(e, $('.persistentCartBox'), $('.persistentCartSelectedTab'), 'persistentCart'); });

    //Function to show the Forgot Password screen
    $('.forgotPasswordLink').click(function (e) {
        try {
            e.preventDefault();
            $('.sslLoginFrame').hide();
            $('.retrievePassword').show();
            $('.headerLoginSelectedTab').show();
            $('#custEmailRP').focus();
        } catch (e) {
            alert('An error occured');
        }
    });

    // Function for Forgot Password
    $('#btnSendPassword').click(function (e) {
        e.preventDefault();
        var cEmail = $('#custEmailRP').val();
        $.ajax({
            type: "POST",
            url: "/ssl/p_sendpassword.asp",
            data: "custEmail=" + cEmail + "&ajr=true",
            success: function (result) {
                if (result == "EMAILNOTFOUND" || result == "EMAILFAILED") {
                    displayFPErrorMessage();
                    return false;
                } else {
                    $('.sslLoginFrame').before('<div class="passwordSent">Your password has been sent.</div>');
                    $(".signinErrorMessageDiv").hide();
                    $('.retrieveEmailInstructions').hide();
                    $('.sslLoginFrame').hide();
                    //$('.headerLoginDiv').css({ "height": "86px" });
                }
            },
            error: function (result) {
                displayFPErrorMessage();
            }
        });
    });

    //Function to SignOut
    $('.linkToSignOut').click(function (e) {
        try {
            e.preventDefault();
            headerIframe_close();

            $.ajax({
                type: 'POST',
                timeout: 10000,
                url: window.location.protocol + "//" + window.location.host + '/WebService.asmx/SignOut',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    $.cookie('User', null);
                    $.cookie('sfCustomer', null);

                    //if returnUrl is not empty, reload the page
                    if (typeof returnUrl == 'undefined') {
                        validateLogin();
                    }
                    else {
                        if (returnUrl != null) {
                            location.reload();
                        }
                        else {
                            validateLogin();
                        }
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert("An error occurred processing request\nReady State: " + xhr.readyState + "\nStatus: " + xhr.status);
                    alert("Response Text: " + xhr.responseText);
                }
            });
        }
        catch (err) {
            document.location = defaultDomain + "/ssl/MyAccount/MyAccountLogout.asp";
        }
    });

    $(window).bind("scroll", function (e) {
        selectedIframe = null;
    });

    //SignIn MouseOver
    function signInMouseOver(e, closeIframe) {
        try {
            e.preventDefault();

            isOrderStatusIframeLoaded = false;
            clearTimeout(headerLoginWindowTimeout);
            if (!isHeaderLoginIframeLoaded) {
                isHeaderLoginIframeLoaded = true;
                setTimeout(function () { $('#custEmail').focus(); }, 300);
            }
            else {
                if (closeIframe) {
                    activeElement = ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) ? 'custEmail' : $('.headerLoginIframe').contents().find('#activeElement').val();
                    activeElement = activeElement != null ? activeElement : 'custEmail';
                    setTimeout(function () { $('.headerLoginIframe').contents().find('#' + activeElement).focus(); }, 300);
                }
            }

            setTimeout(function () {
                if (closeIframe) {
                    headerIframe_close();
                    $('.retrievePassword').hide();
                    $('#custEmailRP').val('');
                    $('#custEmailLabelRP').show();
                    $(".signinErrorMessageDiv").hide();
                    $('.retrieveEmailInstructions').hide();
                    $('.passwordSent').remove();
                    $('.sslLoginFrame').show();
                    if ($('#custEmail').val() != '' || $('#custEmail').val().length > 0) $('#custEmailLabel').hide();
                    else $('#custEmailLabel').show();
                    if ($('#custPassword').val() != '' || $('#custPassword').val().length > 0) $('#custPasswordLabel').hide();
                    else $('#custPasswordLabel').show();
                }
                $('.headerLoginBox').removeClass('displayNone');
                $('.headerLoginBox').show();
                $('.headerLoginSelectedTab').show();
            }, 1);
            selectedIframe = $('.headerLoginIframe');
        }
        catch (e) {
            document.location = defaultDomain + "/ssl/myaccount/myaccount.asp";
        }
    }

    //OrderStatus MouseOver
    function orderStatusMouseOver(e, closeIframe) {
        try {
            e.preventDefault();

            isHeaderLoginIframeLoaded = false;
            clearTimeout(orderStatusWindowTimeout);
            if (!isOrderStatusIframeLoaded) {
                isOrderStatusIframeLoaded = true;
                setTimeout(function () { $('.orderStatusIframe').contents().find('#orderNumber').focus(); }, 300);
            }
            else {
                if (closeIframe) {
                    activeElement = ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) ? 'orderNumber' : $('.orderStatusIframe').contents().find('#activeElement').val();
                    activeElement = activeElement != null ? activeElement : 'orderNumber';
                    setTimeout(function () { $('.orderStatusIframe').contents().find('#' + activeElement).focus(); }, 300);
                }
            }

            setTimeout(function () {
                if (closeIframe) {
                    headerIframe_close();
                }
                $('.orderStatusBox').removeClass('displayNone');
                $('.orderStatusBox').show();
                if (closeIframe) {
                    $('.orderStatusSelectedTab').show();
                    $('#orderNumber').focus();
                }
            }, 1);
            selectedIframe = $('.orderStatusIframe');
        }
        catch (e) {
            document.location = defaultDomain + "/order-status.asp";
        }
    }

    //PersistentCart MouseOver
    function persistentCartMouseOver(e, closeIframe) {
        try {
            // PreventDefault was causing the links not to work!
            //e.preventDefault();

            isHeaderLoginIframeLoaded = false;
            isOrderStatusIframeLoaded = false;
            clearTimeout(persistentCartWindowTimeout);

            setTimeout(function () {
                if (closeIframe) {
                    headerIframe_close();
                }
                $('.persistentCartBox').removeClass('displayNone');
                $('.persistentCartBox').show();
                $('.persistentCartSelectedTab').show();
                $('.persistentCartBox').focus();
            }, 1);
            selectedIframe = $('.persistentCartIframe');
        }
        catch (e) {
            document.location = defaultDomain + "/order.asp";
        }
    }

    $('map[name="adMap"]').html('<area shape="rect" coords="770,44,1000,94" href="http://www.instawares.com/shipping-policy.asp?&utm_source=mktg&utm_medium=site&utm_term=1cent&utm_content=mastheadad&utm_campaign=1centmhad" alt="1 Cent Shipping" title="1 Cent Shipping">');

    //SignIn/OrderStatus/PersistentCart MouseOut
    function headerIframeMouseOut(e, iframeBox, selectedTab, frameName) {
        e.preventDefault();
        switch (frameName) {
            case 'signIn': headerLoginWindowTimeout = setTimeout(function () { closeIframeBox(iframeBox, selectedTab); isHeaderLoginIframeLoaded = false; }, windowTimeout);
                break;
            case 'orderStatus': orderStatusWindowTimeout = setTimeout(function () { closeIframeBox(iframeBox, selectedTab); isOrderStatusIframeLoaded = false; }, windowTimeout);
                break;
            case 'persistentCart': persistentCartWindowTimeout = setTimeout(function () { closeIframeBox(iframeBox, selectedTab); isPersistentCartIframeLoaded = false; }, windowTimeout);
                break;
        }
    }

    //Close Iframe
    function closeIframeBox(iframeBox, selectedTab) {
        if (selectedIframe != null) iframeBox.focus();
        iframeBox.addClass('displayNone');
        iframeBox.hide();
        selectedTab.hide();
        activeElement = null;
    }

    $('input#searchTerm').val('');

    function headerIframe_close() {
        $('.headerLoginBox').addClass('displayNone');
        $('.orderStatusBox').addClass('displayNone');
        $('.persistentCartBox').addClass('displayNone');
        $('.headerLoginSelectedTab').hide();
        $('.orderStatusSelectedTab').hide();
        $('.persistentCartSelectedTab').hide();
    };

    //Function to show the login iframe
    $('.loginButton').click(function (e) {
        e.preventDefault();
        var custEmail = $("#custEmail").val();
        var custPassword = $("#custPassword").val();

        if ($.trim(custEmail) == "" || $.trim(custPassword) == "") {
            displayErrorMessage();
        }
        else {
            $(this).attr("src", "/ssl/images/checkout/member_checkout_animated.gif");
            // This was the URL:
            // url: window.location.protocol + "//" + window.location.host + '/WebService.asmx/Login',
            try {
                $.ajax({
                    type: 'POST',
                    timeout: 10000,
                    url: defaultDomain + '/WebService.asmx/Login',
                    data: "{'Email':'" + custEmail + "', 'Pass':'" + custPassword + "'}",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (result) {
                        if (result.d.indexOf("FAILED") == -1) {
                            //clear textboxes
                            $("#custEmail").val('');
                            $("#custPassword").val('');
                            $('.headerLoginBox').addClass('displayNone');
                            validateLogin();
                        } else {
                            //alert("unsuccessful");
                            displayErrorMessage();
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        //alert("An error occurred processing request\nReady State: " + xhr.readyState + "\nStatus: " + xhr.status);
                        //alert("Response Text: " + xhr.responseText);
                    }
                });
            }
            catch (err) {
                alert("An error occurred:\n" + err.description);
                displayErrorMessage();
            }
            $(this).attr("src", "/ssl/images/checkout/member_checkout_button.png");
        }
    });

    $('.viewStatusButton').click(function (e) {
        e.preventDefault();
        var orderID = $("#orderNumber").val();
        var shipZip = $("#shippingZipCode").val();

        if ($.trim(orderID) == "" || $.trim(shipZip) == "") {
            displayOrderErrorMessage();
        }
        else {
            try {
                $.ajax({
                    type: 'POST',
                    timeout: 10000,
                    url: window.location.protocol + "//" + window.location.host + '/WebService.asmx/CheckOrderStatus',
                    data: "{'OrderID':" + orderID + ", 'ZipCode':'" + shipZip + "'}",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (result) {
                        var rtn = eval('(' + result.d + ')');
                        if (rtn.val > 0) document.location = "/Order-Status.asp?postBack=1&orderID=" + orderID + "&shipZip=" + shipZip;
                        else displayOrderErrorMessage();
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        //alert("An error occurred processing request\nReady State: " + xhr.readyState + "\nStatus: " + xhr.status);
                        //alert("Response Text: " + xhr.responseText);
                        displayOrderErrorMessage();
                    }
                });
            }
            catch (err) {
                //alert("There was a problem processing your request.");
                displayOrderErrorMessage();
            }
        }
    });

    //Function to display the forgot password page content
    $('#retrievePasswordAnchor').click(function (e) {
        e.preventDefault();

        // Clear the textboxes
        $("#custEmail").val('');
        $("#custPassword").val('');

        $('.sslLoginFrame').hide();
        $(".signinErrorMessageDiv").hide();
        $('.retrieveEmailInstructions').html("Enter your email to have your password sent to you.");
        $('.retrieveEmailInstructions').show();
        $('.retrievePassword').show();
    });

    //Function to display the forgot password page content
    $('#joinAnchor').click(function (e) {
        document.location = "/ssl/myaccount/myaccountregister.asp";
    });

    var currentEmail = "";
    //Trap key on email field.
    $("#custEmail").bind("keyup", function (e) {
        e.preventDefault();
        var code = e.charCode || e.keyCode;
        if (code == 13 && currentEmail != '') {
            e.stopPropagation();
            $('.loginButton').click();
            return false;
        }
        else {
            var email = $(this).val();
            if (email == '') {
                $('#custEmailLabel').show();
                //$(this).addClass("textboxError");
                currentEmail = "";
            }
            else {
                $('#custEmailLabel').hide();
                //$(this).removeClass("textboxError");
                currentEmail = email;
            }
        }
    });

    //Trap copy/paste on password field.
    $("#custEmail").bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var email = $(el).val();
            if (currentEmail != email) {
                currentEmail = "";
                $("#custEmail").keyup();
            }
        }, 100);
    });

    var currentPassword = "";
    //Trap key on Password field.
    $("#custPassword").bind("keyup", function (e) {
        e.preventDefault();
        var code = e.charCode || e.keyCode;
        if (code == 13 && currentPassword != '') {
            e.stopPropagation();
            $('.loginButton').click();
            return false;
        }
        else {
            var password = $(this).val();
            if (password == '') {
                $('#custPasswordLabel').show();
                //$(this).addClass("textboxError");
                currentPassword = "";
            }
            else {
                $('#custPasswordLabel').hide();
                //$(this).removeClass("textboxError");
                currentPassword = password;
            }
        }
    });

    //Trap copy/paste on password field.
    $("#custPassword").bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var password = $(el).val();
            if (currentPassword != password) {
                currentPassword = "";
                $("#custPassword").keyup();
            }
        }, 100);
    });

    function displayErrorMessage() {
        $('.signinErrorMessageDiv').removeClass('displayNone');
        $('.signinErrorMessageDiv').show();
        $("#custEmail").addClass('textboxError');
        $("#custPassword").addClass('textboxError');
        $('.headerLoginDiv').css({ "height": "180px" });
    }

    function displayOrderErrorMessage() {
        $('.orderStatusErrorMessageDiv').removeClass('displayNone');
        $('.orderStatusErrorMessageDiv').show();
        $("#orderNumber").addClass('textboxError');
        $("#shippingZipCode").addClass('textboxError');
        $('.orderStatusDiv').css({ "height": "150px" });
    }

    function displayFPErrorMessage() {
        $(".signinErrorMessageDiv").show();
        $('.retrieveEmailInstructions').hide();
        //$('.headerLoginDiv').css({ "height": "127px" });
    }

    // Hide label if user presses key in textbox
    $('#custEmail').keypress(function () {
        if ($('#custEmail').val() != '' || $('#custEmail').val().length > 0) $('#custEmailLabel').hide();
        else $('#custEmailLabel').show();
    });

    $('#custPassword').keypress(function () {
        if ($('#custPassword').val() != '' || $('#custPassword').val().length > 0) $('#custPasswordLabel').hide();
        else $('#custPasswordLabel').show();
    });

    $('#custEmailRP').keypress(function () {
        if ($('#custEmailRP').val() != '' || $('#custEmailRP').val().length > 0) $('#custEmailLabelRP').hide();
        else $('#custEmailLabelRP').show();
    });

    $('#orderNumber').keypress(function () {
        if ($('#orderNumber').val() != '' || $('#orderNumber').val().length > 0) $('#orderNumberLabel').hide();
        else $('#orderNumberLabel').show();
    });

    $('#shippingZipCode').keypress(function () {
        if ($('#shippingZipCode').val() != '' || $('#shippingZipCode').val().length > 0) $('#shippingZipCodeLabel').hide();
        else $('#shippingZipCodeLabel').show();
    });

    //Trap copy/paste on order number field.
    $("#orderNumber").bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var orderNum = $(el).val();
            if ($.trim(orderNum) != '') {
                $("#orderNumber").keypress();
            }
        }, 100);
    });

    //Trap copy/paste on shipping zipcode field.
    $("#shippingZipCode").bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var shipZip = $(el).val();
            if ($.trim(shipZip) != '') {
                $("#shippingZipCode").keypress();
            }
        }, 100);
    });

    //Trap copy/paste on customer email field for retrieve password.
    $("#custEmailRP").bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var custEmRP = $(el).val();
            if ($.trim(custEmRP) != '') {
                $("#custEmailRP").keypress();
            }
        }, 100);
    });

    // Move focus to the textbox if user clicks on label
    $('#custEmailLabel').click(function () {
        $('#custEmail').focus();
    });

    $('#custPasswordLabel').click(function () {
        $('#custPassword').focus();
    });

    $('#orderNumberLabel').click(function () {
        $('#orderNumber').focus();
    });

    $('#shippingZipCodeLabel').click(function () {
        $('#shippingZipCode').focus();
    });

    //    $("input").bind("focus", function () {
    //      $('#activeElement').val($(this).attr('id'));
    //    });
    //    $('#activeElement').val(document.activeElement.id);

    /****************************************** Search ******************************************/
    //Set default cursor on homepage.
    if (document.location.pathname.indexOf('default.aspx') > -1 || document.location.pathname == "/") {
        $('input#searchTerm').focus();
    }

    //Trap key on Search field.
    var currentSearchTerm = "";
    $('input#searchTerm').bind("keyup", function (e) {
        e.preventDefault();
        var code = e.charCode || e.keyCode;
        if (code == 13 && $.trim(currentSearchTerm) != '') {
            e.stopPropagation();
            $('.searchButtonInput').click();
            return false;
        }
        else {
            var searchTerm = $.trim($(this).val());
            if (searchTerm == '') {
                currentSearchTerm = "";
            }
            else {
                //$('iframe#sli_search_1_iframe').css({ display: 'none', visibility: 'hidden' });
                currentSearchTerm = searchTerm;
            }
        }
    });

    //Trap copy/paste on Search field.
    $('input#searchTerm').bind("input paste", function (e) {
        var el = $(this);
        setTimeout(function () {
            var searchTerm = $.trim($(el).val());
            if (currentSearchTerm != searchTerm) {
                currentSearchTerm = "";
                $('input#searchTerm').keyup();
            }
        }, 100);
    });

    //Click event
    $('.searchButtonInput').click(function (e) {
        e.preventDefault();
        var mySearchTerm = $.trim($('input#searchTerm').val());
        if (mySearchTerm.length > 0) {
            var url = 'http://' + document.domain + '/' + mySearchTerm.replace(/\s+/g, '-') + '.0.3.0.htm';
            //var url = 'http://' + document.domain + '/' + mySearchTerm.replace(/\s+/g, '-') + '.0.3.1.0.htm'; Use this when the parts tab is added
            $(location).attr('href', url);
        }
        else {
            return false;
        }
    });


    /****************************************** Super Category Menu ******************************************/
    //Hover event
    $('li.firstNav').hover(function () { navMouseOver(this, 'firstNav'); }, function () { navMouseOut(this, 'firstNav'); });
    $('li.navLink').hover(function () { navMouseOver(this, 'navLink'); }, function () { navMouseOut(this, 'navLink'); });
    $('li.lastNav').hover(function () { navMouseOver(this, 'lastNav'); }, function () { navMouseOut(this, 'lastNav'); });

    // Array to hold the subcat strings
    var sCatsArray = new Array();

    //Drop Shadow
    function applyDropShadow() {
        if (!($.browser.msie && $.browser.version == "6.0")) {
            var navLinks = 1;
            var navLinksHeight = 6;
            var topOffSet = 0;
            var leftOffSet = ($.browser.msie && $.browser.version == "7.0") ? 5 : 4;
            $('div.navLinks').each(function () {
                if (navLinks == 10) {
                    navLinksHeight = 34;
                    topOffSet = -28;
                    $("<div name='lastNavLinkDiv'>").appendTo($(this)).css({
                        position: 'absolute',
                        zIndex: -99998,
                        width: 99 + 'px',
                        height: 28 + 'px',
                        border: 0 + 'px',
                        top: (topOffSet) + 'px',
                        filter: 'alpha(opacity=0)',
                        opacity: 0.0,
                        cursor: 'pointer',
                        left: 0 + 'px'
                    });
                    $("div[name='lastNavLinkDiv']").bind('click', function () { document.location = $('li.lastNav a').attr('href'); });
                }
                $("<div name='dropshadow'>").appendTo($(this)).css({
                    position: 'absolute',
                    zIndex: -99999,
                    width: $(this).width() + 2 + 'px',
                    height: $(this).height() + navLinksHeight + 'px',
                    background: 'url(/images/drop_shadow.png) right bottom no-repeat',
                    border: 0 + 'px',
                    top: (topOffSet) + 'px',
                    left: ($(this).offset().left + leftOffSet) + 'px'
                });
                $(this).css('z-index', 10);
                navLinks++;
            });
        }
    }

    //IE7 z-index workaround
    if ($.browser.msie) {
        /*
        ** For each div with class menu (i.e.,
        ** the thing we want to be on top),
        */
        $("div.navLinks").parents().each(function () {
            var p = $(this);
            var pos = p.css("position");

            // If it's positioned,
            if (pos == "relative" ||
			   pos == "absolute" ||
			   pos == "fixed") {
                /*
                ** Add the "on-top" class name
                */
                p.addClass("on-top");
            }
        });
        /*
        ** Set all the dropshadows to be on the bottom
        */
        if (!($.browser.msie && $.browser.version == "6.0")) {
            $("div[name='dropshadow']").each(function () {
                var ds = $(this);
                ds.removeClass("on-top");
                ds.addClass("on-bottom");
            });
        }
    }

    // ie6 png transparency fix for cart image
    if ($.browser.msie && $.browser.version == "6.0") {
        var img = $('.cartImage img');
        img.css({ "width": img.width(), "height": img.height(), "filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr("src") + "', sizingMethod='scale')" });
        img.attr("src", "/images/blank.gif");
    }

    var currentFocus = null;
    function navMouseOver(obj, className) {
        $(obj).removeClass(className + 'NoHover');
        $(obj).addClass(className + 'Hover');
        // Get the supercat's index so we can pull the correct subcat
        var index = $(obj).attr('index');
        $(obj).children('div').html(sCatsArray[index]);
        applyDropShadow();
        $('div', obj).show();
        //Remove focus from form and reassign to drop down menu
        currentFocus = (document.location.pathname == "/ShoppingCart.aspx") ? null : (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'SELECT') ? document.activeElement : null;

        if (currentFocus != null) {
            $(currentFocus).blur();
        }

        if ($.browser.version == "6.0") {
            $('div', obj).bgiframe();
        }
    }

    function navMouseOut(obj, className) {
        //Set focus back to active element
        if (currentFocus != null) {
            $(currentFocus).focus();
        };
        $('div', obj).hide();
        if (!$(obj).is('.navSelected')) {
            $(obj).removeClass(className + 'Hover');
            $(obj).addClass(className + 'NoHover');
        }
    }

    function createSubCats() {
        $.ajax({
            type: "GET",
            url: window.location.protocol + "//" + window.location.host + "/defaultXMLData/CategoryMenu.xml",
            dataType: "xml",
            success: function (xml) {
                // Iterate through all of the supercats
                $('.scatName').each(function (idx) {
                    var el = $(this).text();
                    var menuText = "<ol class='leftOL'>";
                    $(xml).find(el).each(function () {
                        var supCatID = $(this).attr('superCategoryID');
                        // Figure out how many subcats there are so we can split them into Left and Right halves
                        var numSubcats = 0;
                        $(this).find('subCategory').each(function () {
                            numSubcats++;
                        });
                        var leftColumnCount = numSubcats > 20 ? numSubcats % 2 == 0 ? numSubcats / 2 : Math.floor((numSubcats / 2) + 1) : 10;
                        if (supCatID == 8) leftColumnCount = numSubcats;
                        // Now we reset the numSubcats variable and compare it to the leftColumnCount so we can switch to the right in our loop
                        numSubcats = 0;
                        $(this).find('subCategory').each(function (idx) {
                            var subCatName = $(this).find('name').text();
                            var subCatUrl = $(this).find('url').text();
                            menuText += "<li><a href='" + subCatUrl + "'>" + subCatName + "</a></li>";
                            if ((idx + 1) == leftColumnCount) menuText += "</ol><ol class='rightOL'>";
                        });
                        menuText += "</ol>";
                    });
                    // Put the subcat into our array
                    sCatsArray[idx] = menuText;
                });
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("An error occurred processing request\nReady State: " + xhr.readyState + "\nStatus: " + xhr.status);
                alert("Response Text: " + xhr.responseText);
            }
        });
    }
}

