﻿//Setup quick contains method for array shredding.
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}
//Variables
ie6 = $.browser.msie && ($.browser.version == "6.0");
var offset = 5;
//Numbers 0-9 on keyboard + keypad + backspace
var keyCodes = [8,9, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104];
function pageLoad() {
    // Event handler that causes the minibasket scroll up button to work
    $(".miniCart A[id$=lnkMiniCartUp]").click(function() {
        var topVal = $(this).parents(".miniCart").find(".cartContents").css("top");
        topVal = parseInt(topVal) + 30;
        if (topVal <= 0) {
            $(this).parents(".miniCart").find(".cartContents").animate({
                top: topVal
            }, 100);
        }
        else {
            $(this).parents(".miniCart").find(".cartContents").animate({
                top: 0
            }, 100);
        }
        return false;
    });
    // Event handler that causes the minibasket scroll down button to work
    $(".miniCart A[id$=lnkMiniCartDown]").click(function() {
        var topVal = $(this).parents(".miniCart").find(".cartContents").css("top");
        topVal = parseInt(topVal) - 30;

        if (Math.abs(topVal) <= $(this).parents(".miniCart").find(".cartContents").height()) {
            $(this).parents(".miniCart").find(".cartContents").animate({
                top: topVal
            }, 100);
        }
        return false;
    });
    initCategoryMenu();
    // contains 'override for btn click event handler'
    if (typeof AttachFavoritesEventHandlers == 'function') {
        AttachFavoritesEventHandlers();
    }
    if (typeof AttachCardTypeEventHandlers == 'function') {
        AttachCardTypeEventHandlers();
    }
    // attaches click event handlers to chooseWeight buttons in the basket listings control.
    if (typeof AddBasketListingEventHandlers == 'function') {
        AddBasketListingEventHandlers();
    }  
    if (typeof (IE6Fixes) == 'function')
        IE6Fixes();
    //turn the cursor to a wait symbol when an update is proccessing
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
    //One time bind of mouse move, keyup,keydown and mouseout events for productlistings
    $('#productListingContainer, #referentialListingContainer,#divProductDetails').delegate('.tooltip_image', 'mousemove', function(ev) {
        var $note = $(this);
        var ieOffset = ie6 ? $('#siteContent').offset() : null;
        var x = ev.pageX + offset - (ie6 ? ieOffset.left : 0);
        var y = ev.pageY + offset - (ie6 ? ieOffset.top : 0);
        $note.siblings('.tooltipInfo').show().css({ left: x, top: y });
    }).delegate('.tooltip_image', 'mouseout', function(e) {
        $(this).siblings('.tooltipInfo').hide();
    }).delegate('input.inputQty', 'keydown', function(e) {
        return keyCodes.contains(e.keyCode);
    }).delegate('input.inputQty', 'keyup', function(e) {
        //Prevent quantity of 0
        if (this.value < 1) {
            this.value = 1;
        }
    });
    if (ie6) {
        $('.AspNet-Menu').delegate('.AspNet-Menu-WithChildren', 'mouseover', function() {
            var $i = $('<iframe frameborder="0" scrolling="no" align="bottom" marginheight="0" marginwidth="0"></iframe>');
            $i.height($(this).children('ul').height()).width($(this).children('ul').width()).css("position", "absolute").css("top", "24px");
            if ($(this).children('iframe').size() == 0) {
                $(this).prepend($i);
            }
            $(this).children('iframe').show();
            $(this).children('ul').show();
        }).delegate('li', 'mouseout', function() {
            $(this).children('ul').hide();
            $(this).children('iframe').hide();
        });
    }
}
//This is stupid. 
function InitializeRequest(sender, args) {
    document.body.style.cursor = 'wait';
}
function EndRequest(sender, args) {
    document.body.style.cursor = 'auto';
}
function getProductIDFromListingRow(controlID) {
    var container = $('#' + controlID).parents(".product_info_container");
    var pid = parseInt(container.attr("id"));
    return pid;
}
function chooseWeightFromProductListing(btnChooseWeightId, inBasket) {
    var pid = getProductIDFromListingRow(btnChooseWeightId);
    chooseWeightSetupAndShow(btnChooseWeightId, inBasket, pid, "LISTING");
}
function CheckBoxValidator_MustBeChecked(source, args) {
    args.IsValid = ($("[id$=btnTermsAndConditions]").is(':checked'));
}
function initCategoryMenu() {
    var leafID = $("[id$=selectedLeafCategory]").val();
    if (leafID != "0" && leafID != "undefined") {
        $('#' + leafID).addClass('selected').parentsUntil('.lvl1').addClass('selected').slideDown('fast');    
    }
    $('.departmentCategories').undelegate('li', 'click').delegate('li', 'click', function(event) {
        event.stopPropagation();
        var $li = $(this);
        $li.toggleClass('selected');        
        if ($li.hasClass('lf')) {
            productNavigate($li[0].id);
        }
        $li.find('> ul').slideToggle('fast').toggleClass('selected');
        $li.siblings().removeClass('selected').find('ul').slideUp('fast');
    });
}
function productNavigate(id) {
    $("[id$=hfSelectedCategory]").val(id);
    $("[id$=_upProcessing]").show();
    $("[id$=btnCategorySelected]").click();
}

