var Cart = {};

Cart.Block = {};
Cart.Module = {};

Cart.dataStore = {}

Cart.loadData = function(){
    new Ajax.Request('/cart/list', {
        method: 'get',
        asynchronous: false,
        onSuccess: function(transport){
            Cart.dataStore = transport.responseText.evalJSON(true);
        }
    });
}

Cart.add = function(itemId, itemQuantity, callback){
    if (itemId && itemQuantity) {
        new Ajax.Request('/cart/add', {
            method: 'post',
            parameters: {
                id: itemId,
                quantity: itemQuantity
            },
            onSuccess: callback
        });
    }
}

Cart.changeQuantity = function (itemId, itemQuantity, callback){
    new Ajax.Request('/cart/changequantity', {
        method: 'post',
        parameters: {
            id: itemId,
            quantity: itemQuantity
        },
        onSuccess: callback
    });
}

Cart.remove = function(itemId, callback){
    new Ajax.Request('/cart/remove', {
        method: 'post',
        parameters: {
            id: itemId
        },
        onSuccess: callback
    });
}

Cart.countTotalSum = function(){
    var total = 0;
    for (i in Cart.dataStore) {
        Cart.dataStore[i].sum = parseFloat(Cart.dataStore[i].price * Cart.dataStore[i].quantity);
        total += Cart.dataStore[i].sum;
    }
    return total;
}

Cart.clear = function(callback){
	new Ajax.Request('/cart/clear', {
        method: 'get',
        onSuccess: callback
    });
}

Cart.Module.showEmptyMessage = function(){
	$(Cart.Module.options.holderId).hide();
	$(Cart.Module.options.cartEmptyMessageId).show();
}

Cart.Block.show = function(){
	$(Cart.Block.options.holderId).show();
    var holder = $(Cart.Block.options.itemsHolderId);
	holder.update('');
    
    var total = 0;
    for (i in Cart.dataStore) {
        var sum = parseFloat(Cart.dataStore[i].price * Cart.dataStore[i].quantity)
		Cart.dataStore[i].sum = number_format(sum, 2, ',', ' ');
		Cart.dataStore[i].price = number_format(Cart.dataStore[i].price, 2, ',', ' ');
        total += sum;
        var text = Cart.Block.options.itemTemplate.evaluate(Cart.dataStore[i]);
		holder.insert(text, holder);
    }
    
    $(Cart.Block.options.totalId).innerHTML = Cart.Block.options.totalTemplate.evaluate({
        total: number_format(total, 2, ',', ' ')
    });
}

Cart.Block.add = function(itemId, itemQuantity){
	itemId = parseInt(itemId);
    itemQuantity = parseInt(itemQuantity);
	
	var callback = function(transport){
        Cart.dataStore = transport.responseText.evalJSON(true);
        Cart.Block.show();
		new Effect.Highlight($(Cart.Block.options.holderId), {
            startcolor: Cart.Block.options.highlight.startColor,
            endcolor: Cart.Block.options.highlight.endColor,
			restorecolor: Cart.Block.options.highlight.restoreColor
        });
    }
	
	Cart.add(itemId, itemQuantity, callback);
}

Cart.Module.changeQuantity = function (itemId, itemPrice, itemQuantity){
    itemId = parseInt(itemId);
    itemPrice = parseFloat(itemPrice);
    itemQuantity = parseInt(itemQuantity);
    
    var callback = function(transport){
        Cart.dataStore = transport.responseText.evalJSON(true);
        var sum = itemPrice * itemQuantity;
        var cells = $(Cart.Module.options.rowIdBegin + itemId).getElementsByTagName('td');
        cells[Cart.Module.options.itemSumColumnNumber].innerHTML = number_format(sum, 2, ',' ,' ') + ' руб.';
        Cart.Module.countTotalSum();
    }
    Cart.changeQuantity(itemId, itemQuantity, callback);
}

Cart.Module.remove = function(itemId){
    itemId = parseInt(itemId);
	var callback = function(transport){
		Cart.dataStore = transport.responseText.evalJSON(true);
		if (Cart.dataStore.length || Cart.dataStore.length < 1){
			Cart.Module.showEmptyMessage();
			return;
		}
        if ($('cartRow_' + itemId)){
            $('cartRow_' + itemId).remove();
        }
        Cart.Module.countTotalSum();
    }
    Cart.remove(itemId, callback);
}

Cart.Module.countTotalSum = function(){
	var sum = number_format(Cart.countTotalSum(), 2, ',' ,' ');
	$(Cart.Module.options.totalId).innerHTML = Cart.Module.options.totalTemplate.evaluate({total: sum})
}

Cart.Module.clear = function(){
    var callback = function(){
		Cart.Module.showEmptyMessage();
    }
    Cart.clear(callback);
}




