﻿$.namespace("NicoleFarhi.Core.Header.Basket.Constructor.Summary");
$.namespace("NicoleFarhi.Core.Header.Basket.Summary");

NicoleFarhi.Core.Header.Basket.Constructor.Summary = $.Class.extend({
    TotalPayableControl: null,
    TotalItemsControl: null,
    init: function () {

        this.TotalItemsControl = arguments[0];
        this.ReBindBasketSummary();

    },
    ReBindBasketSummary: function () {
        dotCommerce.Clients.NicoleFarhi.Shop.Resources.Services.Basket.Summary(this.SetBasketValues.bind(this));
    },
    ReBindAndShowBasketSummary: function () {
        dotCommerce.Clients.NicoleFarhi.Shop.Resources.Services.Basket.Summary(this.ShowBasketCallback.bind(this));
    },
    ShowBasketCallback: function (e) {
        this.SetBasketValues(e);
        $('#basketFlyout').hover(
                  function () {
                      $(this).stop(true, true);
                  },
                  function () {
                      $(this).delay(3000).slideUp('slow');
                  }
                ).slideDown('slow').delay(3000).slideUp('slow');
    },
    SetBasketValues: function (e) {
        //this.TotalPayableControl.html(e.BasketTotal);
        this.TotalItemsControl.html(e.TotalItems);
        var subTotal = $('#basketFlyout .sub-total');
        subTotal.text(e.FormattedSubTotal);

        var htmlItems = $('#basketFlyout table > tbody');
        htmlItems.html('');

        for (var i = 0; i < e.Items.length; i++) {
            htmlItems.append(
                    $('<tr>')
						.append($('<td>').html(e.Items[i].Quantity))
						.append($('<td>').addClass("image")
                                .append($('<img>').attr('src', e.Items[i].Thumbnail).attr('alt', e.Items[i].Name).addClass("float-left"))
                                .append($('<a>').attr('href', e.Items[i].Url).html(e.Items[i].Name))
                                )
						.append($('<td>').addClass("colour").html(e.Items[i].Detail))
                        .append($('<td>').text(e.Items[i].FormattedTotalPrice))
            );
        }
    }
});
