﻿/**********************************************
Master Load
**********************************************/
$(document).ready(function() {
    setHint();
    setScrollFollow();
});

/**********************************************
Force Postback
**********************************************/
forcePostBack = function(sender, e) {
    __doPostBack(sender, e);
}

umapPostBack = function (postbackCall, textBoxId) {
    __doPostBack(postbackCall, document.getElementById(textBoxId).value);
}

/**********************************************
Set Hint
***********************************************/
setHint = function() {
    $("[INPUT:text[title!='']").unbind("hint").hint();
}

/**********************************************
Set Scroll Follow
***********************************************/
setScrollFollow = function() {
    $(".ScrollFollow").scrollFollow({speed: 1000,offset: 0,killSwitch: '',onText: '',offText: ''});
}

/**********************************************
Open Modal Windows
***********************************************/
openModal = function(modalId) {
    $("#" + modalId).modal({
        overlay: 100
        , opacity: 80
        , overlayCss: { backgroundColor: '#c4daf0', 'padding-bottom': '20px'}
        , zIndex: 3001
        , position: ['10%', '']
        , closeClass: 'ModalClose'
        , appendTo: 'FORM'
        , persist: true
    });
}

/**********************************************
Refresh QuickView Shopping Cart
***********************************************/
function refreshQuickView() {
    $("[id $='QuickViewCart']").each(function() {
        this.contentWindow.location.reload(true);
    });
    //$("[id $='QuickViewCart']").attr("src", $("[id $='QuickViewCart']").attr("src"));
}

/**********************************************
Image Mouseover Events
***********************************************/
$(document).ready(function() {
    setImageHover();
});

function setImageHover() {
    $(".Hover, .hover").each(function() {
        if ($(this).attr("src") != null) {
            $(this).hover(function() {
                $(this).attr("src", $(this).attr("src").replace("_off", "_on"));
                $(this).attr("src", $(this).attr("src").replace("_down", "_off"));
            }, function() {
                $(this).attr("src", $(this).attr("src").replace("_on", "_off"));
                $(this).attr("src", $(this).attr("src").replace("_down", "_off"));
            });
        } else { //HyperLink 
            $(this).hover(function() {
                $(this).addClass("HoverText");
                if ($(this).find("IMG").length > 0
                    && $(this).find("IMG").attr("src") != null
                    && $(this).find("IMG").attr("src") != 'undefined') {
                    $(this).find("IMG").attr("src", $(this).find("IMG").attr("src").replace("_off", "_on"));
                    $(this).find("IMG").attr("src", $(this).find("IMG").attr("src").replace("_down", "_off"));
                }
            }, function() {
                $(this).removeClass("HoverText");
                if ($(this).find("IMG").length > 0
                    && $(this).find("IMG").attr("src") != null
                    && $(this).find("IMG").attr("src") != 'undefined') {
                    $(this).find("IMG").attr("src", $(this).find("IMG").attr("src").replace("_on", "_off"));
                    $(this).find("IMG").attr("src", $(this).find("IMG").attr("src").replace("_down", "_off"));
                }
            });
        }
    });
}

/**********************************************
Sliding Account Information 
***********************************************/
$(document).ready(function() {
    $(".btnOpenCloseAccountInfo").hover(function() {
        $(this).find("IMG:last").show();
    }, function() {
        $(this).find("IMG:last").hide(); 
    });
    $(".btnOpenCloseAccountInfo").click(function() {
        $("DIV[id*='AccountInfoWrapper']").slideToggle("slow", function() {
            if ($.cookie('LJCOOKIE_AccountInfoOpen') == '1') {
                $.cookie('LJCOOKIE_AccountInfoOpen', '0');
                $(".btnOpenCloseAccountInfo").find("IMG:first").attr("src", $(".btnOpenCloseAccountInfo").find("IMG:first").attr("src").replace("close", "open"));
            } else {
                $.cookie('LJCOOKIE_AccountInfoOpen', '1');
                $(".btnOpenCloseAccountInfo").find("IMG:first").attr("src", $(".btnOpenCloseAccountInfo").find("IMG:first").attr("src").replace("open", "close"));
            };
        });
    });
});

/**********************************************
Tab Events (including tab mouseover)
***********************************************/
$(document).ready(function() {
    //Handle Mouse Overs First
    $('div.Tabs ul.TabNavigation A IMG').click(function() {
        //Reset Tabs
        $('div.Tabs ul.TabNavigation A IMG').each(function() {
            $(this).attr("src", $(this).attr("src").replace("_down", "_off"))
        });
        //Cleanup Set Selected
        $(this).attr("src", $(this).attr("src").replace("_off", "_down"));
        $(this).attr("src", $(this).attr("src").replace("_on", "_down"));
    });

    var tabContainers = $('div.Tabs > div');
    tabContainers.hide().filter(':first').show();
    $('div.Tabs ul.TabNavigation A IMG').filter(':first').each(function() {
        $(this).attr("src", $(this).attr("src").replace("_off", "_down"));
    });
    $('div.Tabs ul.TabNavigation a').click(function() {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.Tabs ul.TabNavigation a').removeClass('Selected');
        if ($(this).find('IMG').attr("src") != null) {
            $(this).find('IMG').attr("src", $(this).find('IMG').attr("src").replace("_on", "_down"));
        }
        return false;
    }).filter(':first').click();
});


/**********************************************
TabsTwo Events 
***********************************************/
$(document).ready(function() {
    $('DIV.TabsTwo UL.TabNavigation A').click(function() {
        var _button = $(this);
        var _hash = _button.attr("href");

        if (_hash == null || _hash == "")
            _hash = _button.attr("tabid");

        var _parent = _button.closest("DIV.TabsTwo");
        var _offset = _button.context.offsetLeft + (_button.width() / 2);

        _parent.find("DIV.TabContent:visible").hide();
        _parent.find("A.Selected").removeClass("Selected");
        _button.addClass("Selected");
        _parent.find("IMG.SelectedArrow").animate({ "left": _offset + "px" }, "slow");

        $("[id $= '" + _hash.replace("#", "") + "']").show();

        //Save the selected tab on the Default Page only. 
        if (document.location.pathname == "/"
            || document.location.pathname == "/Default.aspx") {
            $.cookie('LJCOOKIE_HomePage_Tab', _hash, { path: '/', expires: 1 });
        }
        if (typeof setUpSellSlider == 'function') {
            setUpSellSlider();
        }
        return false;
    });

    //If a cookie has been saved with a default Tab on the home page, 
    //then select that tab automatically on load. 
    var _cookieTabHash = $.cookie('LJCOOKIE_HomePage_Tab');
    if ((document.location.pathname == "/"
        || document.location.pathname == "/Default.aspx")
        && _cookieTabHash != null && _cookieTabHash != '') {
        $("[href $='" + _cookieTabHash + "'], [tabid $='" + _cookieTabHash + "']").each(function() {
            $(this).click();
        });
    } else {
        //Open the first tabs
        $("DIV.TabsTwo").each(function() {
            $(this).find("UL.TabNavigation A").filter(':first').click();
        });
    }
});

/**********************************************
Flyout Sub Menu
***********************************************/
$(document).ready(function() {
    $(".ListSquareHeaderHover").hover(function() {
        $(this).find(".ListSquareFlyoutWrapper").show();
    }, function() {
        $(this).find(".ListSquareFlyoutWrapper").hide();
    });
});


/**********************************************
Sub Page - Left Navigation
***********************************************/
$(document).ready(function() {
    $(".LeftNavigationHeader").click(function() { //Reset
        if ($(this).next().is(":hidden")) {
            $(".LeftNavigationHeader").removeClass("Selected");
            $(".LeftNavigationMenuList").slideUp("slow");
            
            $(this).next().slideDown("slow");
            $(this).addClass("Selected"); 
            return false;
        }
    });
    $(".LeftNavigationHeader").each(function() {
        if ($(this).attr("Selected") == 'true') {
            $(this).addClass("Selected");
            $(this).next().show();
        }
    });
});

/**********************************************
Sub Page - Right Anchor Navigation
***********************************************/
$(document).ready(function() {
    $(".GroupNavigationWrapper").find("A").click(function() {
        $(".GroupNavigationWrapper").find("A").removeClass("Selected");
        $(this).addClass("Selected");
    });
});

//Updates the QuickBasket on the top of the page.
function updateQuickBasket() {
    $(".QB_QuickViewGrid").find(".ProgressWindow").show();
    try {
        BasketWebService.GetBasket(function(basket) {
            var bWrap = $(".QB_QuickViewGrid");
            var iList = bWrap.find("DIV.QB_QuickViewGridItem");
            var iNumber = 0;
            var iListCount = iList.length;

            if (basket.BasketItemList.length > 0) {
                $(".QB_EnabledCart").show();
                $(".QB_DisabledCart").hide();
                $(".QB_EmptyCart").hide();
            } else {
                $(".QB_EnabledCart").hide();
                $(".QB_DisabledCart").show();
                $(".QB_EmptyCart").show();
            }

            for (var i = 0; i < basket.BasketItemList.length; i++) {

                //Clone the Template or the last record if there are
                //no more available DOM objects. 
                if (i >= iListCount) {
                    var clone = bWrap.find("DIV.QB_QuickViewGridItem:last").clone(false);
                    iList.push(clone);
                    bWrap.append(clone);
                }

                var cItem = $(iList[iNumber]);
                var pName = basket.BasketItemList[i].ProductFullName + " (" + basket.BasketItemList[i].SKU + " )";
                cItem.find(".QB_Quantity").html(basket.BasketItemList[i].Quantity);
                cItem.find(".QB_Item").html("<a href='" + basket.BasketItemList[i].SKUSEOURL + ".aspx' title='" + pName + "' >" + pName + "</a>");
                cItem.find(".QB_CustomerPrice").html(formatCurrency(parseFloat(basket.BasketItemList[i].CustomerPriceExtended)));
                cItem.show();

                iNumber++;
            }

            //Hide the rest of the records until we are ready to show them again
            if (iNumber < iListCount) {
                for (var i = iNumber; i < iListCount; i++) {
                    var cItem = $(iList[i]);
                    cItem.hide();
                    iNumber++;
                }
            }

            $(".BasketSubTotal").html(basket.BasketSubTotalFormatted);
            $(".BasketItemCount").html(basket.BasketItemCount);

        }, function(error) {
            //alert(GENERICERROR); //TODO: figure out a better way to handle this. 
        });
    } catch (e) {
        //alert(GENERICERROR); //TODO: figure out a better way to handle this.
    }

    $(".QB_QuickViewGrid").find(".ProgressWindow").hide();
}

/**********************************************
HELPER FUNCTIONS
**********************************************/
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}
