﻿var ec = ec || {};       // E-commerce project root namespace
ec.sc = ec.sc || {};     // Shopping cart namespace
ec.sc.qv = ec.sc.qv || {};   // Quick view cart - summary
ec.sc.Cart = ec.sc.Cart || {};

/**
* show the summary quick view cart
*/
ec.sc.showQuickCart = function (id) {
    var win = $find(id);
    ec.sc.quickViewWin = win;
    win.show();
    return false;
}

ec.sc.setShoppingCartAnchor = function () {
    if (typeof (ec.sc.cart) === 'undefined')
        return;

    var items = ec.sc.cart.item_list;

    var count = 0;
    var cost = 0;
    var price = 0, qty;

    for (var i in items) {
        price = parseFloat(items[i].price);
        qty = parseFloat(items[i].quantity);

        cost = cost + (price * qty);
        count++;
    }

    var element = document.getElementById('quickViewCartAnchor');

    if (element != null)
        element.innerHTML = "(" + count + ") items - $" + Util.CurrencyFormatted(cost);
}

/**
* This function would configure the Quick View Cart Summary pop up window.
* This function is triggered by the cardData change Event.
**/
ec.sc.configureQVCSummary = function () {
    var items = ec.sc.cart.item_list;
    var ary = new Array();

    var container = jQuery('#quickViewSummaryContainer');
    var optionContainer;

    for (var key in items)
        ary.push(items[key]);

    container.html('');
    jQuery("#quickViewCartTpl").render(ary).appendTo(container);

    for (var i = 0; i < ary.length; i++) {
        jQuery('#span_QVS_' + ary[i].id).bind('click', ary[i], removeQVSummary);

        optionContainer = jQuery('#qv_' + ary[i].id);
        optionContainer.append('<br/>' + ary[i].options);
    }

    if (ary.length == 0) {
        jQuery('#QV_summarTbl').hide();
        jQuery('#QV_checkoutBtn').hide();
        jQuery('#QV_noItem').show();
    } else {
        jQuery('#QV_noItem').hide();
        jQuery('#QV_summarTbl').show();
        jQuery('#QV_checkoutBtn').show();
    }

    /** Total Cost **/
    var total = ec.sc.calculatePrice();
    jQuery('#QV_total').html('$' + total);

}

ec.sc.calculatePrice = function () {
    var total = parseFloat(0);

    for (var key in ec.sc.cart.item_list) {
        var row = ec.sc.cart.item_list[key];
        total = total + parseFloat(row.price) * parseFloat(row.quantity);
    }
    return Util.CurrencyFormatted(total);
}
 /**
    * This function will update the cart locally and will fire CART_CHANGE Event
    * This will be called by server after an ajax call to update cart data in the client.
    */
ec.sc.updateCartData = function (json) {
    if (json != null)
        ec.sc.cart = json;
    jQuery(document).trigger('CART_CHANGE');
}

ec.sc.Cart = {

    updateCartServer: function (itemID, qty) {
        ec.sc.cart.item_list[itemID].quantity = qty;
        var ary = new Array();
        ary.push('update');
        var json = { qty: qty, item_id: parseInt(itemID) };
        ary.push(JSON.stringify(json));
        ec.sc.ajaxManager.ajaxRequest(ary.join('|'));
     },

    deleteCartItem: function (itemID) {
        // delete the item in client cache
        delete ec.sc.cart.item_list[itemID];

        // delete in the server
        var ary = new Array();
        ary.push('delete');
        var json = {};
        json.item_id = parseInt(itemID);
        ary.push(JSON.stringify(json));
        ec.sc.ajaxManager.ajaxRequest(ary.join('|'));
    },

    addCartItem: function (json) {
        var ary = new Array();
        ary.push('add');
        ary.push(JSON.stringify(json));
        ec.sc.ajaxManager.ajaxRequest(ary.join('|'));
    }
}

removeQVSummary = function (e) {
        ec.sc.Cart.deleteCartItem(e.data.id);
    }

    /**
    Do quantity validation before passing it to the server.
    */
quantityChangeHandler = function (qty, itemID) {
    qty = parseInt(qty);
    ec.sc.Cart.updateCartServer(itemID, qty);
}

