﻿function setUpScreenDisplay()
{
    // setup the page tabs
    $("#watchdetails > ul").tabs();
    // ensure the add to cart button show the hand.....
    $('#butAddtoCart').hover(function() { $(this).addClass('button-hover'); }, function() { $(this).removeClass('button-hover'); });
}


function ShowPopUp() {
    //centering with css
    centerPopup();
    loadPopup();

    // set up the mouse pointer
    $('#popupCartKeepShopping').hover(function() { $(this).addClass('button-hover'); },  function() { $(this).removeClass('button-hover'); });         
    $('#popupCartCheckout').hover(function() { $(this).addClass('button-hover'); }, function() { $(this).removeClass('button-hover'); });
    $("#popupCartKeepShopping").click(function() { disablePopup(); });
    $("#backgroundPopup").click(function() { disablePopup(); });
    //Press Escape event and close the popup!
    $(document).keypress(function(e) { if (e.keyCode == 27 && popupStatus == 1) { disablePopup(); } });
}

function AddToBasketSucceeded(result) {   
    UpdateBasketInformationOnScreen();
    ShowPopUp();
}

function AddToBasketFailed(results) {
    alert('We were unable to add your selected item to your basket due to the following error: ' + results.statustext);
}

// update basket information on the page
function UpdateBasketInformationOnScreen() {
    CallPageMethodNoParams(pagePath, "GetBasketTotal", UpdateBasketTotalSuccess, UpdateBasketTotalFailed);
}

function UpdateBasketTotalSuccess(results) {
    if (results.d.itemcount > 1) {
        $("#orderPopupTotalItems").html("Your Order has " + (results.d.itemcount) + " items");
    } 
    else {
        $("#orderPopupTotalItems").html("Your Order has " + (results.d.itemcount) + " item");
    }

    $("#orderPopupSubTotal").html('Sub Total : ' + results.d.baskettotal);

    updateHeaderShoppingBasket(results.d);
}

function UpdateBasketTotalFailed(results) {

    alert('We were unable to update the status of your basket due to the following error : ' + results.statustext);
}


// page load event :)
$(document).ready(function() {

    setUpScreenDisplay();

    // set up ajax call to add to cart.
    $("#butAddtoCart").click(AddToCart);

});

function AddToCart() {
    var itemid = $("#cartid").val();
    var modelid = $("#cartmodelid").val();

    if (itemid != null) {
        CallPageMethod(pagePath, "AddStockToBasket", ["id", itemid], AddToBasketSucceeded, AddToBasketFailed);
    }

    if (modelid != null) {
        CallPageMethod(pagePath, "AddModelToBasket", ["id", modelid], AddToBasketSucceeded, AddToBasketFailed);
    }
}

function selectTab(index) {
    $(document).ready(function() {
        $("#watchdetails > ul").tabs("select",index);
    });
}
