﻿/****************************************************************
** Setup shopping stuff.
** Change all elements with class buy-button into a buy link.
****************************************************************/
function SetupShoppingStuff() {
    /* Change all elements with class buy-button into a buy link. */
    $(".buy-button").each(function (index) {
        artCode = $(this).attr("articlecode");
        //                $(this).replaceWith('<input type="image" id="' + artCode + '" src="images/buytbtn.gif" onclick="AddToShoppingCart(\'' + artCode + '\');"/>');
        //$(this).replaceWith('<a input id="' + artCode + '" src="images/buytbtn.gif" onclick="AddToShoppingCart(\'' + artCode + '\');">Köp nu!</a>');
        //$(this).replaceWith('<a href="" onclick="AddToShoppingCart(\'' + artCode + '\');return false;"><img style="padding-top:0px;" class="noBorder" src="images/buy.gif"/></a>');
        $(this).replaceWith('<a title="Klicka här för att lägga till leksaken i din varukorg!" style="vertical-align: bottom;" href="" src="images/buytbtn.gif" onclick="AddToShoppingCart(\'' + artCode + '\');return false;"><span class="buy-now" id=' + artCode + '>Köp nu!</span></a>');
    });
}

/****************************************************************
** Adds an item into the shopping cart and fetches the updated
** shopping cart and shows on the screen.
****************************************************************/
function AddToShoppingCart(articleCode) {
    FetchWithCallback("shoppingcartservice.asmx/AddToShoppingCart",
                "{articleCode: '" + articleCode + "'}", GetShoppingCartTotal);

    $("#" + articleCode).text("Inlagd i varukorgen.");
    $("#" + articleCode).ClearTypeFadeIn('fast');

    currArticleCode = articleCode;
    setTimeout(FadeBack, 1000);

    //        FetchAndFill("shoppingcartservice.asmx/AddToShoppingCart",
    //                "{articleCode: '" + articleCode + "'}",
    //                $("#theList"));
    //ShowTotal();
} //AddToShoppingCart

var currArticleCode;

function FadeBack(articleCode) {
    $("#" + currArticleCode).text("Köp nu!");
    $("#" + currArticleCode).ClearTypeFadeIn('fast');
}

/****************************************************************
** Fetches the shopping cart from the server and outputs
** it in the appropriate element.
****************************************************************/
function GetShoppingCartTotal() {
    FetchAndFill("shoppingcartservice.asmx/ShoppingCartTotal",
                    "",
                    $("#shopping-cart"));
} //GetShoppingCartTotal
