var ATBProduct;
var ATBVisible = 0; 

function jsProduct()
{
    this.WebProductId = 0;
    this.WebDescription = 0;    
    this.ImagePath = 0;
    this.Price = 0;
}


function QuickAddProductItemToBasket(WebProductId, WebDescription, ImagePath, Price)
{
    ATBProduct = new jsProduct();
    ATBProduct.WebProductId = WebProductId;
    ATBProduct.WebDescription = WebDescription;
    ATBProduct.ImagePath = ImagePath;
    ATBProduct.Price = Price;

    QuickBasket.QuickAddProductItemToBasket(WebProductId, ProductAdded);
}
function Checkout(Url)
{
    ATBChangeQty("Checkout");
    HideATB();
    setTimeout("window.location = '" + Url + "';", 500)
}
function ProductAdded()
{
    RefreshBasket_Start();
} 

function QuickUpdateProductBasketQty(productID)
{
    QuickBasket.QuickUpdateProductBasketQty(productID, newQty, "Update", SucceededCallback);
}

function RefreshBasket_Start()
{
    QuickBasket.GetBasketContents(RefreshBasket_Complete);
}
function RefreshBasket_Complete(result)
{
    var TotalQty = 0;
    for(Index = 0; Index < result.BasketLines.length; Index++)
    {
        TotalQty += result.BasketLines[Index].Quantity;
    }
    $("span.QuickBasketText").html("Cart: " + TotalQty + " Item" + (TotalQty != 1 ? "s":"") + " | Total: &pound;" + result.TotalGross.toFixed(2));
    if(ATBProduct)
    {
        $("#ATBQuickImage").attr("src", ATBProduct.ImagePath);
        $("#ATBQuickImage").attr("alt", "Thumbnail image of " + ATBProduct.WebDescription);

        $("#ATB-Qty").val(0);
        for(Index = 0; Index < result.BasketLines.length; Index++)
        {
            if(result.BasketLines[Index].ProductID == ATBProduct.WebProductId)
            {
                $("#ATB-Qty").val(result.BasketLines[Index].Quantity);
            }
        }

        $("#ATB-WebDescription").text(ATBProduct.WebDescription);
        $("#ATB-PriceIncVat").text(ATBProduct.Price);

        CentreATB();
        ShowATB();
        CentreATB(); 
    }
}

function CentreATB()
{
    var YOffset = 0;
    var windowWidth = 0;
    var windowHeight = 0;

    if(typeof(window.pageYOffset) == 'number')
    {
        YOffset = window.pageYOffset;
        windowWidth = $(document).width();
        windowHeight = document.documentElement["clientHeight"];
    }
    else if(document.body && document.body.scrollTop)
    {
        YOffset = document.body.scrollTop;
        windowWidth = $(document).width();
        windowHeight = $(window).height();
    }
    else if(document.documentElement && document.documentElement.scrollTop)
    {
        YOffset = document.documentElement.scrollTop;
        windowWidth = $(document).width();
        windowHeight = $(window).height();
    }
    else
    {
        YOffset = 0;
        windowWidth = $(document).width();
        windowHeight = $(window).height();
    }

    var popupHeight = $("#AddToBasket-Container").height();
    var popupWidth = $("#AddToBasket-Container").width();

    $("#AddToBasket-Container").css("position", "absolute");
    $("#AddToBasket-Container").css("top", YOffset + (windowHeight / 2 - popupHeight / 2));
    $("#AddToBasket-Container").css("left", windowWidth / 2 - popupWidth / 2); 
    $("#backgroundPopup").css("height", $(document).height());

    if(ATBVisible == 1) setTimeout("CentreATB()", 500)
}

function HideATB()
{
    if(ATBVisible == 1)
    {
        $("#backgroundPopup").fadeOut("slow");
        $("#AddToBasket-Container").fadeOut("slow");
        ATBVisible = 0;
        ATBProduct = null;
    }
}

function ShowATB()
{
    if(ATBVisible == 0)
    {
        $("#ATB-Qty").css("background-color", "");
        $("#backgroundPopup").css("opacity", "0.3");
        $("#backgroundPopup").fadeIn("slow");
        $("#AddToBasket-Container").fadeIn("slow");
        ATBVisible = 1;
    }
}

function ContinueShopping()
{
    ATBChangeQty("Continue");
    HideATB();
}

function ATBChangeQty(updateType)
{
    if(ATBProduct)
    {
        if(!isNaN($("#ATB-Qty").val()) && $("#ATB-Qty").val() > 0)
        {
            $("#ATB-Qty").css("background-color", "");
            QuickBasket.QuickUpdateProductBasketQty(ATBProduct.WebProductId, $("#ATB-Qty").val(), updateType, RefreshBasket_Start);
        }
        else
        {
            $("#ATB-Qty").css("background-color", "red");
        }
    }
}

function RemoveItem()
{
    if(ATBProduct)
    {
        QuickBasket.QuickUpdateProductBasketQty(ATBProduct.WebProductId, 0, "Remove", RefreshBasket_Start);
        HideATB();
    }
}
$(document).ready(function(){

    $("#backgroundPopup").click(function(){
        HideATB();
    });

    $(document).keypress(function(e){
        if(e.keyCode == 27 && ATBVisible == 1)
        {
            HideATB();
        }
    });

    $("#AddToBasket-Qty").keypress(function(e){
        if(e.keyCode == 13)
        {
            ContinueShopping();
            return false;
        }
    });

}); 