﻿Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

$(document).ready(function() {
    $('ul.AspNet-Menu li:last a').css('background','none');
    $(".department_menu [id$=btnDepartment]").click(function() {
        DepartmentSelected(this.id);
    });

    $("[id$=btnTermsAndConditions]").click(function() {
        CheckBoxMustBeChecked_SetHiddenField(this.id);
    });
});


function pageLoad() {

    var siteUrlWithTheme = $("*[ID$='hfSiteUrlWithTheme']").val();

    MM_preloadImages(siteUrlWithTheme + "/images/tab_home_on.gif",
        siteUrlWithTheme + "/images/tab_shoponline_on.gif",
        siteUrlWithTheme + "/images/tab_rewardcard_on.gif",
        siteUrlWithTheme + "/images/icons/icon_go_on.png");
    
    // attach event handler for product listings, Choose Weight button.
    $("#productListingContainer A[id$=btnChooseWeight]").click(function() {

        var inBasket = false;

        if ($("#" + this.id).attr("class") == "updateweight") {
            inBasket = true;
        }

        chooseWeightFromProductListing(this.id, inBasket);
    });

    $("#productListingContainer A[id$=btnChooseWeight]").css("cursor", "pointer");
    
    
    
    // attach event handler for product listings, Add button.
    $("#productListingContainer A[id$=btnAddToBasket]").click(function() {

        // ----- Referential event handlers -----

        if ($("#" + this.id).attr("class") == "update") {
            inBasket = true;
        }

        var pid = getProductIDFromListingRow(this.id);
        addItemToBasket(pid);
    });

    $("#productListingContainer A[id$=btnAddToBasket]").css("cursor", "pointer");
    $("#productListingContainer A[id$=btnFavourites]").css("cursor", "pointer");
    




    // ----- Referential event handlers -----

    $(".product_referential A[class$=add], .product_referential_wrap A[class$=add]").click(function(event) {
        addEachItemFromReferential(this.id);
    });

    $(".product_referential A[class$=add], .product_referential_wrap A[class$=add]").css("cursor", "pointer");

    
    $(".product_referential A[class$=updateweight], .product_referential A[class$=selectweight], .product_referential_wrap A[class$=updateweight]," +
    " .product_referential_wrap A[class$=selectweight]").click(function(event) {
        var inBasket = false;

        if ($("#" + this.id).attr("class") == "updateweight") {
            inBasket = true;
        }

        chooseWeightFromReferential(this.id, inBasket);
    });

    $(".product_referential A[class$=updateweight], .product_referential A[class$=selectweight], .product_referential_wrap A[class$=updateweight]," +
    " .product_referential_wrap A[class$=selectweight]").css("cursor", "pointer");
    
    

    
    // contains 'override for btn click event handler'
    if (typeof AttachFavouritesEventHandlers == 'function') {
        AttachFavouritesEventHandlers();
    }

    if (typeof AttachCardTypeEventHandlers == 'function') {
        AttachCardTypeEventHandlers();
    }

    // attaches click event handlers to chooseWeight buttons in the basket listings control.
    if (typeof AddBasketListingEventHandlers == 'function') {
        AddBasketListingEventHandlers();
    }

    categoryHover();
    
    if (typeof(pngfix) == 'function')
        pngfix();
    if (typeof(IE6Fixes) == 'function')
        IE6Fixes();    
    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];
    $('#productListingContainer, #referentialListingContainer,.productDetails').delegate('.tooltip_image', 'mousemove', function(ev) {
        var $note = $(this);       
        var x = ev.pageX + offset;
        var y = ev.pageY + offset;
        $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;
        }
    });
    
    
    //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);

        
// end page load
}




function InitializeRequest(sender, args) {
    document.body.style.cursor = 'wait';
}

function EndRequest(sender, args) {
    document.body.style.cursor = 'auto';
}


$("select").change(function() {
    var str = "";
    $("select option:selected").each(function() {
        str += $(this).text() + " ";
    });
    $("div").text(str);
})
        .change();



