﻿$(document).ready(function() {
    basketFlyoutRefresh();
});

function basketFlyoutRefresh(blink) {
    var shop_pane_expanded = $('#shop_pane_expanded');
    var shop_pane = $('#shop_pane');
    var shop_pane_list = $('#shop_pane_list');
    var show_basket = $('#show_basket');

    shop_pane_expanded.hide();
    shop_pane_list.html('');
    show_basket.unbind('click.toggle');

    if (OrderWebService) {
        OrderWebService.GetBasketJSON(function(res) {
            eval("var basket = " + res);
            if (basket.TotalItems == "0") {
                show_basket.children('span').text("Shopping Bag is empty.");
                show_basket.children('img').hide();
            } else {
                show_basket.children('span').text("Shopping Bag " + basket.TotalItems + " Total " + basket.Total);
                show_basket.children('img').show();
                show_basket.bind('click.toggle', function() {
                    shop_pane_expanded.slideToggle();
                });
                // draw basket items
                $(basket.Items).each(function() {
                    shop_pane_list.append(basketFlyout_GetListItem(this));
                });

                if (blink) {
                    basketFlyout_Blink();
                }
            }
        }, function(error) {
            show_basket.text("Could not load basket...");

        });
    } else {
        show_basket.text("Could not load basket...");
    }
}
function basketFlyout_GetListItem(item) {
    return $('<li></li>').append($('<img></img>').attr({ src: item.Image, width: 46, height: 46, alt: item.Description }).addClass("float_left"))
                         .append(item.Name + ((item.IsGift) ? " (gift)" : ""))
                         .append('<br/>')
                         .append(basketFlyout_GetValuesString(item.VariationValues))
                         .append('<br/>')
                         .append('Price: ' + item.TotalPrice)
                         .append(' x ' + item.Quantity);
}
function basketFlyout_GetValuesString(attributes) {
    var str = "";
    $(attributes).each(function() {
        if (str.length > 0)
            str += " ";
        str += this.Label + ": " + this.Value;
    });
    return str;
}
function basketFlyout_Blink() {
    var shop_pane_expanded = $('#shop_pane_expanded');
    if (!shop_pane_expanded.is(':visible')) {
        shop_pane_expanded.slideDown(function() {
            setTimeout(function() { shop_pane_expanded.slideUp(); }, 2000);
        });
    } else {
        setTimeout(function() { shop_pane_expanded.slideUp(); }, 2000);
    }
}
