CART = {
	items : {},
	addToCart : function(obj) {
		AJAX.bindLoader();
		
		jQuery(obj).children('input:submit').attr('disabled', true);
		data = jQuery(obj).serialize();
		jQuery.ajax(
			{
				type : 'post',
				url: '/cart-action/?action=add_item', 
				dataType: 'json',
				data: data, 
				success: function(data) {
					if(data) {
						jQuery(obj).siblings('.json-message').text(data.message);
						CART.reviewCart();
						//jQuery(obj).prev().fadeOut(2000);
					}
				},
				complete: function() {
					AJAX.removeLoader();
					jQuery(obj).children('input:submit').attr('disabled', false);
					
				},
				error : function() {
					AJAX.removeLoader();
				}
			}
		);
		
	},
	reviewCart : function() {
	    jQuery.ajax(
		{
		    type : 'post',
		    url: '/reviewcart/',
		    dataType: 'json',
		    success: function(data) {
			if(data) {
			    jQuery('#cart-count', top.document).text(data.value);
				//setCookie(data.cookie.name, data.cookie.value, { expires: 3600, path : '/'} );
			}
		    },
		    complete: function() {

		    }
		}
	    );
	},
	removeItem : function(id) {
		AJAX.bindLoader();
		var contract_id = jQuery("#cartItem_"+id+" .item_contract_id").val();
		var items_in_contract = jQuery("#cart_items_table .item_contract_id[value="+contract_id+"]").length;
		jQuery('#cartItem_'+id).remove();
		if(items_in_contract == 1) {
			jQuery('#contract_item_row_'+contract_id).remove();
		}
		jQuery('#priceItem_'+id+' .already-in-cart', top.document).remove();
		if(jQuery('#cart_items_table tr').length < 3) {
			jQuery('#cart_items').empty();
			jQuery('#cart_items').append('<h5>Корзина пуста</h5>');
		}
		jQuery.ajax(
			{
				type : 'post',
				url: '/cart-action/', 
				dataType: 'json',
				data: "action=remove_item&id="+id, 
				success: function(data) {
					CART.reviewCart();
					AJAX.removeLoader();
					CART.recount();
				},
				complete: function() {
					CART.reviewCart();
					AJAX.removeLoader();
					CART.recount();
				},
				error : function() {
					CART.reviewCart();
					AJAX.removeLoader();
					CART.recount();
				}
			}
		);		
	},
	recount : function() {
		var sum = 0;
		items_price = jQuery('#cart_items_table .item_price');
		items_count = jQuery('#cart_items_table .item_count');
		items_length = items_price.length;
		for(i = 0; i < items_length; i++) {
			var price = 0, count = 0;
			try {
				price = parseFloat(items_price.eq(i).text());
				count = parseInt(items_count.eq(i).text());
			} catch(e) {return false;}
			if(price && count) {
				sum += count*price;
			}		
		}

		if(sum) {
			jQuery('#cart_total_price').text(CART.formatPriceDigit(sum));
			
		}
	},
	recountCartRow : function(id) {
		CART.recountTotal();
	},
	recountTotal : function() {
		var sum = 0;
		$('#cart_items_table .row_item').each(function(i, e) {
			var id = $(e).attr('id');
			try {
				price = parseFloat($('#'+id+' .item_price').text());
				count = parseInt($('#'+id+' .item_count').val());
			} catch(e) {return false;};
			if(price && count) {
				sum += CART.formatPrice(count*price, 2);
			}
			
		});
		$('#cart_total_price').text(CART.formatPriceDigit(sum));
	},
	formatPrice : function(price, precision) {
		if(!isNaN(price)) {
			return parseFloat(price.toFixed(precision));
		}
		return 0;
	},
	formatPriceDigit : function(price) {
		return price.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1\u2009');
	},
	changeCountTimeout : function(id, count) {
		if(id && count) {
			jQuery.ajax(
				{
					type : 'post',
					url: '/cart-action/?action=change_item_count', 
					dataType: 'json',
					data: 'item_id='+id+'&count='+count, 
					success: function(data) {
						GLOBAL.setInputTimeout[id] = undefined;
					},
					complete: function() {
						GLOBAL.setInputTimeout[id] = undefined;
					},
					error : function() {
						GLOBAL.setInputTimeout[id] = undefined;
					}
				}
			);
		}
	},
	removeCheckedItems : function() {
	    AJAX.bindLoader();

	    ids = $('.remove_items:checked');
	    if(ids.length > 0) {
		var id = [];
		ids.each(function(i, e){
		    if($(e).val() > 0) {
			jQuery('#cartItem_'+$(e).val()).remove();
			jQuery('#priceItem_'+$(e).val()+' .already-in-cart', top.document).remove();
			id.push($(e).val());
		    }
		});
		ids = id.join(',');
		if(jQuery('#cart_items_table tr').length < 3) {
			jQuery('#cart_items').empty();
			jQuery('#cart_items').append('<h3>Корзина пуста</h3>');
		}
		this.recount();
		jQuery.ajax(
			{
				type : 'post',
				url: '/cart-action/',
				dataType: 'json',
				data: "action=remove_items&ids="+ids,
				success: function(data) {

				},
				complete: function() {
				    AJAX.removeLoader();
				    CART.reviewCart();
				},
				error : function() {
				    AJAX.removeLoader();
				    CART.reviewCart();
				}
			}
		);
	    }
	}
};
