﻿

function PixelCart_Ajax_ClearItems(cartID, objectID, objectType) {
    var args = "CartID=" + cartID;
    args += "&Command=Clear";

    PixelCart_Ajax_Callback(args);
}

function PixelCart_Ajax_PlusItem(cartID, objectID, objectType) {

    var args = "CartID=" + cartID;
    args += "&Command=PlusItem";
    args += "&OrderItemObjectId=" + objectID;
    args += "&OrderItemObjectType=" + objectType;

    PixelCart_Ajax_Callback(args);
}

function PixelCart_Ajax_MinusItem(cartID, objectID, objectType) {

    var args = "CartID=" + cartID;
    args += "&Command=MinusItem";
    args += "&OrderItemObjectId=" + objectID;
    args += "&OrderItemObjectType=" + objectType;

    PixelCart_Ajax_Callback(args);
}

function PixelCart_Ajax_RemoveItem(cartID, objectID, objectType) {

    var args = "CartID=" + cartID;
    args += "&Command=Remove";
    args += "&OrderItemObjectId=" + objectID;
    args += "&OrderItemObjectType=" + objectType;

    PixelCart_Ajax_Callback(args);
}

function PixelCart_Ajax_Recieve(cartID, itemsText, headerText, footerText, baseText) {

    var itemCell = document.getElementById(cartID + "ItemsCell");
    var listCell = document.getElementById(cartID + "_ItemsCell_ItemsCell_i_Items_List");
    var headerCell = document.getElementById(cartID + "_ItemsCell_ItemsCell_i_Items_Header");
    var footerCell = document.getElementById(cartID + "_ItemsCell_ItemsCell_i_Items_Footer");
    var baseCell = document.getElementById(cartID + "_Base_Cell");

    listCell.innerHTML = itemsText;
    headerCell.innerHTML = headerText;
    footerCell.innerHTML = footerText;
    baseCell.innerHTML = baseText;

}

function PixelCart_Add_ItemToCart(cartID, objectID, objectType, objectQuantity, objectDescription, objectQuantityLimit, objectUM, objectPrice) {
    var itemCell = document.getElementById(cartID + "_ItemsCell");

    var args = "CartID=" + cartID;
    args += "&OrderItemObjectId=" + objectID;
    args += "&OrderItemObjectType=" + objectType;

    if (objectQuantity == null) {
        objectQuantity = 1;
    }

    

    if (objectDescription == null) {
        // Express addition using Server Side Event Handlers
        args += "&Command=AddExpress";
        args += "&OrderItemQuantity=" + objectQuantity;
        
        PixelCart_Ajax_Callback(args);
    }
    else {
        args += "&Command=Add";
        args += "&OrderItemDescription=" + objectDescription;
        args += "&OrderItemQuantity=" + objectQuantity;
        args += "&OrderItemUM=" + objectUM;
        args += "&OrderItemUnitGross=" + objectPrice;
        args += "&OrderItemQuantityLimit=" + objectQuantityLimit;


        PixelCart_Ajax_Callback(args);


    }


    if (true) {
        PixelCart_DisplayItems(cartID, false);

        itemCell.slidingHideTimeout = setTimeout(function() {
            if (itemCell.Visible) {
                clearTimeout(itemCell.slidingHideTimeout);
            }

            PixelCart_DelayedHideItems(cartID, 1000);
        }, 1000);
    }
}

function PixelCart_ClearIntervals(cartID) {
    var itemCell = document.getElementById(cartID + "_ItemsCell");

    clearInterval(itemCell.slidingShowInterval);
    clearInterval(itemCell.slidingHideTimeout);
    clearInterval(itemCell.slidingAutoHideInterval);
    clearInterval(itemCell.slidingHideInterval);
}

function PixelCart_DisplayItems(cartID, autoHide) {
    PixelCart_ClearIntervals(cartID);

    var itemCell = document.getElementById(cartID + "_ItemsCell");


    // we dont want to shoot it down immediately so we gradually slide it down starting fast then getting slow

    var windowTopPosition = PixelPress_ScrollPosition.y;


    var currentPosition = GetTop(itemCell) + windowTopPosition;

    var itemsHeight = GetHeight(itemCell);
    var itemsIteration = (currentPosition * -1) / 1.5;
    var iterationDivider = 10;


    if (!itemCell.Visible) {
        // Before we start we want the margin top to be just above the page not 1000px
        itemCell.style.marginTop = "-" + (itemsHeight + windowTopPosition) + "px";

        itemCell.slidingShowInterval = setInterval(function() {
            if (currentPosition >= windowTopPosition) {
                // we are set
                itemCell.style.marginTop = windowTopPosition + "px";
                clearInterval(itemCell.slidingShowInterval);
                itemCell.slideShowing = false;
                itemCell.Visible = true;

                PixelCart_ItemsVisibleTasks(cartID);
            }
            else {

                itemCell.slideShowing = true;
                // we keep going
                currentPosition += itemsIteration;

                itemsIteration = Math.round((windowTopPosition - currentPosition) / iterationDivider);

                if (itemsIteration == 0) {
                    itemsIteration = 1;

                }

                itemCell.style.marginTop = currentPosition + "px";
            }

        }, 1);
    }

    if (autoHide) {
        PixelCart_DelayedHideItems(cartID, 1000);
    }

}

function PixelCart_DelayedHideItems(cartID, delay) {
    var itemCell = document.getElementById(cartID + "_ItemsCell");



    itemCell.slidingAutoHideInterval = setInterval(
    function() {
        if (!IsMouseOverElementCoordinates(itemCell) && !itemCell.slideShowing) {
            PixelCart_HideItems(cartID);
            itemCell.Visible = false;
            clearInterval(itemCell.slidingAutoHideInterval);
        }
    }, delay);
}

function PixelCart_ItemsVisibleTasks(cartID) {
    var itemCell = document.getElementById(cartID + "_ItemsCell");

    itemCell.ItemsVisibleTasksInterval = setInterval(function() {
        if (itemCell.Visible) {
            var windowTopPosition = PixelPress_ScrollPosition.y;
            itemCell.style.marginTop = windowTopPosition + "px";
        }
        else {
            clearInterval(itemCell.ItemsVisibleTasksInterval);
        }

    }, 1);
}

function PixelCart_HideItems(cartID) {
    var itemCell = document.getElementById(cartID + "_ItemsCell");


    var currentPosition = GetTop(itemCell);

    var itemsIteration = 30;
    var iterationDivider = 10;


    itemCell.slidingHideInterval = setInterval(function() {
        if (currentPosition <= -1000) {
            // we are set
            itemCell.style.marginTop = "-1000px";
            clearInterval(itemCell.slidingHideInterval);
            itemCell.slideHiding = false;

            itemCell.Visible = false;
        }
        else {
            // we keep going
            currentPosition -= itemsIteration;

            itemCell.style.marginTop = currentPosition + "px";
            itemCell.slideHiding = true;
        }

    }, 1);

}