var wdk_basket = {
    totalItems: 0,
    data: {"total":"0.00","currency":"£","cart":null,"items":0},
    refresh: function() {
        $.getJSON("/shop/ajax/basket.php?json=true", function(data) {
            wdk_basket.data = data;
            wdk_basket.updateHTML();
        });
    },
    updateHTML: function() {
        // Hide any baskets if total=0
        if(wdk_basket.data.items == 0 && typeof livepreview != 'boolean') {
            if( $(".basket--side-column[data-basket-hidden='1']").length > 0 ) {
                $(".wdk_sidecolumn_container--basket").css({display: 'none', 'visibility': 'hidden'});
            }
            // Display logic for the nav basket - N.B. This doesn’t work in live Preview
            // @TODO: This could be named better
            $(".wdk_basket_empty").show();
            $(".wdk_basket_not_empty").hide();
        }
        else {
            if( $(".basket--side-column[data-basket-hidden='1']").length > 0 ) {
                $(".wdk_sidecolumn_container--basket").css({display: 'block', 'visibility': 'visible'});
            }
            // Display logic for the nav basket - N.B. This doesn’t work in live Preview
            // @TODO: This could be named better
            $(".wdk_basket_empty").hide();
            $(".wdk_basket_not_empty").show();
        }
        // Update the container which holds a list of the items in the basket
        $("[data-basket-id]").each(function() {
            wdk_basket.totalItems = 0;
            var basketId = $(this).attr("data-basket-id");
            $(this).html('');
            if(wdk_basket.data.cart) {
                for(var i=0; i <= (wdk_basket.data.cart.length-1); ++i) {
                    $("[data-basket-id=" + basketId + "]").append(wdk_basket.buildCartRow(i, wdk_basket_row_html[basketId-1]));
                }
            } 
        });
        // Update the total amount
        $(".wdk_basket_total").html(this.data.currency + this.data.total);
        $(".wdk_basket_total_items").html(this.totalItems);
        // Old Quick basket compatibility
        $(".wdk_widget-shopbasket-newtotal").html("Total: " + this.data.currency + this.data.total).show();
        // Sidecolumns
        if( typeof quickBasketHideSidecols != 'undefined' ) {
            quickBasketHideSidecols();
        }
    },
    buildCartRow: function(i, html) {
        var data = this.data.cart[i];
        this.totalItems += parseFloat(data.qty);
        html = html.replace(/\<\!\-\-WDK\:basket\:item\:url\-\-\>/g, data.link);
        html = html.replace(/\<\!\-\-WDK\:basket\:item\:name\-\-\>/g, data.product);
        html = html.replace(/\<\!\-\-WDK\:basket\:item\:quantity\-\-\>/g, data.qty);
                html = html.replace(/\<\!\-\-WDK\:basket\:item\:price\-\-\>/g, this.data.currency + data.unit_price);
        html = html.replace(/\<\!\-\-WDK\:basket\:item\:subtotal\-\-\>/g, this.data.currency + data.subtotal);
        html = html.replace(/\<\!\-\-WDK\:basket\:item\:currency\-\-\>/g, this.data.currency);
        return html;
    }
};
if(typeof wdk_basket_row_html == 'undefined') {
    var wdk_basket_row_html = new Array();
}
$(function() {
    wdk_basket.updateHTML();
});