﻿var basketVisible = false;
var basketScrollInterval = null;
var basketScrollDistance = 5;
var basketScrollTime = 10;

function initialiseBasketScroll() {

    setBasketScrollButtons();

    $("#basketScrollUp a").unbind();
    $("#basketScrollDown a").unbind();

    $("#basketScrollUp a").mousedown(function () {
        basketScrollInterval = setInterval("scrollBasketUp()", basketScrollTime);
    });
    $("#basketScrollUp a").mouseup(function () {
        clearInterval(basketScrollInterval);
    });

    $("#basketScrollDown a").mousedown(function () {
        basketScrollInterval = setInterval("scrollBasketDown()", basketScrollTime);
    });
    $("#basketScrollDown a").mouseup(function () {
        clearInterval(basketScrollInterval);
    });
}

function scrollBasketToTop() {
    $("#basketScrollContent").css("top", "1px");
}

function scrollBasketUp() {
    var currentPosition = $("#basketScrollContent").position();
    if (currentPosition.top < 0)
        $("#basketScrollContent").css("top", (currentPosition.top + basketScrollDistance) + "px");
    else
        clearInterval(basketScrollInterval);
}

function scrollBasketDown() {
    var currentPosition = $("#basketScrollContent").position();

    var scrollerHeight = $("#basketScrollContent").height();
    var containerHeight = $("#basketScrollContainer").height();

    var remainingScroll = scrollerHeight - containerHeight;

    if (currentPosition.top > -(remainingScroll))
        $("#basketScrollContent").css("top", (currentPosition.top - basketScrollDistance) + "px");
    else
        clearInterval(basketScrollInterval);
}

function setBasketScrollButtons() {

    var scrollerHeight = $("#basketScrollContent").height();
    var containerHeight = $("#basketScrollContainer").height();

    if (scrollerHeight <= containerHeight) {
        $("#basketScrollUp a").hide();
        $("#basketScrollDown a").hide();
    }
    else {
        $("#basketScrollUp a").show();
        $("#basketScrollDown a").show();
    }
}


function showBasket() {
    // Open the basket if needed
    $('#basketShow').slideDown(400, function () {
        // Reinitialise scroll so it matches the height of the left hand column including the basket.
        if (typeof (initScrollingPanel) == "function")
            initScrollingPanel();
        initialiseBasketScroll();
    });
}

function reloadBasket() {
    var basketSelector = '#basketShow';

    // Current time is added to the URL to prevent caching of the page.
    basketUrl = $.addToQueryString(basketUrl, 'time', new Date().getTime());

    $(basketSelector).load(basketUrl, null, function () {
        $(basketSelector).stop();

//        // Scroll the window to show the basket if it is not already on screen.
//        var basketPostition = $(basketSelector).offset().top;
//        var windowPosition = $(window).scrollTop();
//        if (windowPosition > basketPostition)
//            $(window).scrollTop(basketPostition);

        showBasket();
    });
}


function updateDone(button) {
    var productContainer = $.locateContainer(button, 'Aspidistra.Ecommerce.Catalogue.Product');
    productContainer.find(".UpdatedNeeded").fadeOut(250);
}

function updateNeeded(button) {
    var productContainer = $.locateContainer(button, 'Aspidistra.Ecommerce.Catalogue.Product');
    if (productContainer) {
        productContainer.find(".QuantityMessage").fadeOut(250);
        productContainer.find('.UpdatedNeeded').fadeIn(250);
    }
}

function removeQuantities() {
    $(".QuantityMessage").hide();
}

function displayQuantity(prodId, quantity) {
    $('.ItemContainer').each(function () {
        if ($(this).data('ItemId') == prodId) {
            $(this).find(".QuantityMessage").html("<a href=\"#\" onmousedown=\"$('#basketShow').slideDown(400); return false;\">" + quantity + " in basket</a>");
            $(this).find(".QuantityMessage").show();
        }
    });
}

$(document).ready(function () {
    $(window).bind('addToOrderSucceeded', function () {
        reloadBasket();
    });
    $(window).bind('changeOrderItemQuantitySucceeded', function () {
        reloadBasket();
    });
    $(window).bind('deleteOrderItemSucceeded', function () {
        reloadBasket();
    });

//    // This gives each quantity control a unique ID so the spinners work independently.  Before only the first 
//    // spinner on the page would update no matter which buttons were pressed.
//    var quantityCounter = 0;
//    $('.AddToOrderQuantity').each(function () {
//        var id = $(this).attr('id') + (quantityCounter++);
//        $(this).attr('id', id);
//    });

    // Toggle display of the basket.
    $('#slick-toggle').click(function () {
        if (basketVisible) {
            $('#basketShow').slideUp(400, function () {
                // Reinitialise scroll so it matches the height of the left hand column including the basket.
                if (typeof (initScrollingPanel) == "function")
                    initScrollingPanel();
            });
        }
        else {
            showBasket();
        }
        basketVisible = !basketVisible;
        return false;
    });

    initialiseBasketScroll();



});

