﻿var mybasket = null;
var mybasketScrollThreshold = 41;

function updateTopBasketCount(count) {
    $('#spanTopBasketCount').html('(' + count + ')');
}

$(document).ready(function() {
    /* Configure the basket object */
    mybasket = new basket($('#divBasketTop'), $('#aBasketTopLink'));
    mybasket.onAddToBasketSuccess = function(result) {
        mybasket.refresh(true);
        updateTopBasketCount(result);

        mybasket.basketTopContainer.slideUp();
    };
    mybasket.onRefreshSuccess = function(result) {
        mybasket.basketTopContainer.html(result);
        mybasket.highlightBasket();

    };

    /* Setup basket highlight animation */
    mybasket.highlightBasket = function() {
        positionBasket();

        mybasket.basketTopContainer.slideDown(function() {
            clearTimeout(mybasket.basketHighlightTimerId);
            mybasket.basketHighlightTimerId = setTimeout(function() { if (!mybasket.basketHover) mybasket.basketTopContainer.slideUp(); }, 2000);
        });
    };

    /* Bind basket link with highlight animation */
    mybasket.basketTopLink.bind('click', mybasket.highlightBasket);
    mybasket.basketTopContainer.hover(
    function() { mybasket.basketHover = true; }, function() {
        mybasket.basketHover = false;
        clearTimeout(mybasket.basketHighlightTimerId);
        mybasket.basketHighlightTimerId = setTimeout(function() { if (!mybasket.basketHover) mybasket.basketTopContainer.slideUp(); }, 2000);
    });

    $(window).scroll(function() {
        positionBasket();
    });

});

function positionBasket()
{
    if ($(window).scrollTop() >= 41) {
        $("#divBasketTopWrapper").css("top", $(window).scrollTop() + "px").css("margin-top", "0px");
    }
    else {
        $("#divBasketTopWrapper").css("top", "").css("margin-top", "8px");
    }
}

function basket(basketTopContainer, basketTopLink) {
    this.basketHighlightTimerId = -1;
    this.basketHover = false;
    this.basketTopContainer = basketTopContainer;
    this.basketTopLink = basketTopLink;
    
    this.refresh = function(showLatestAddition) {
    StoreWebService.GetTopBasket(showLatestAddition, this.onRefreshSuccess, this.onRefreshFailure); 
    };
    this.onRefreshSuccess = function(result) { alert(result); };
    this.onRefreshFailure = function(result) { alert(exception.get_error()); };
    this.onAddToBasketSuccess = function(result) { alert(result + " items added to your basket - "); };
    this.onAddToBasketFailure = function(error) { alert("There was a problem adding to your basket: " + error.get_message()); };
    this.addToBasket = function(productId, variationId, quantity) {
        mybasket.basketTopContainer.html("<div class=\"padding_tb10\" style=\"width:275px; text-align:center;\">Basket updating ..</div>");
        mybasket.basketTopContainer.slideDown();
        StoreWebService.AddItemToBasket(productId, variationId, quantity, this.onAddToBasketSuccess, this.onAddToBasketFailure);
    };

    this.highlightBasket = function() { };
}

