var Cart = {

	proccessorUri: cart_proc_url,
	showOverlay: true,
	successMessage: 'Добавление в корзину прошло успешно...',
	oldValue: new Array(),
	
	add: function(id, type, inCart) {
		
		this.showOverlay = true;
		var self = this;
		this.successMessage = 'Добавление в корзину прошло успешно...';
		var quantity = parseInt($('inp' + id).value);
		
		if (!quantity) {
			quantity = 0;
		}
		if(type == '1') {
			this.oldValue[id] = quantity;
			quantity = quantity + 1;
			$('inp' + id).value = quantity;
		} 
		
		if(type == '2') {
			/*this.successMessage = 'Удаление из корзины прошло успешно...';*/
			this.oldValue[id] = quantity;
			if (quantity > 0) {
				quantity = quantity - 1;
			}	
			$('inp' + id).value = quantity;
		}
		
		if(type == '3') {
			/*this.successMessage = 'Удаление из корзины прошло успешно...';*/
			this.oldValue[id] = quantity;
			quantity = 0;
			$('inp' + id).value = '0';
		}
		//alert(this.oldValue[id]);
		if (this.oldValue[id] < quantity) {
			this.successMessage = 'Добавление в корзину прошло успешно...';
		} else {
			this.successMessage = 'Удаление из корзины прошло успешно...';
		}
		
		
		/*
		if(!quantity) {
			alert('Пожалуйста, введите желаемое количество...');
			
			new Effect.Highlight('how_many' + id);
			
			return false;
		}
		/**/
		if(this.oldValue[id] != quantity) {
			//$("dv"+id).style.display = "block";
			var options = {id: id, quantity: quantity, action: 'add', incart: inCart};
			this.proccessSending(options, function(transport) {
				//$("dv"+id).style.display = "none";
				/**/	
				//self.updateCartBlock(inCart);
				
				var response = transport.responseText.replace("ok\n", '');
				response = response.split("\n");

				$('cart_itemsCount').update(response[1]);
				$('cart_sum').update(response[0]);
			
/*				new Effect.Highlight($('cart_itemsCount'));
				new Effect.Highlight($('cart_sum'));
*/
			
				if (id) {
					$('sumCt'+id).update(response[2]);
//					new Effect.Highlight($('sumCt'+id));
				}
				
			});
		}	
	},
	
	proccessSending: function(parameters, callback) {
		var self = this;
		
		var options = {
			method: 'POST',
			parameters: parameters,
			
			onComplete: function(transport) {
				/*
				if(self.showOverlay) {
					Messages.overlayHide();
				}
				/**/
				if(transport.responseText.substring(0, 2) == 'ok') {
					
					if(self.showOverlay) {
						//$tempvar.growlUI(self.successMessage);	
						/*Messages.addMessage(self.successMessage);*/
					}
					
					if(typeof callback == 'function') {
						callback(transport);
					}
				} else {
					$tempvar.growlUI('Во время обращения к корзине произошла ошибка, попробуй вновь позже...');
				}
			}
		};
		/*
		if(this.showOverlay) {
			Messages.overlayShow_waitBox();
		}
		*/
		new Ajax.Request(Cart.proccessorUri + '?nocache=' + (new Date()).getTime(), options);		
	},

	updateItemCartLabel: function(newQuantity, id) {
		var cartLabel = $('in_cart' + id);
		if(!cartLabel.innerText) {
			cartLabel.hide();
			cartLabel.update('В&nbsp;корзине:&nbsp;' + newQuantity +'&nbsp;шт.');
			new Effect.Appear(cartLabel, {duration: 1});
		} else {
			cartLabel.update('В&nbsp;корзине:&nbsp;' + newQuantity +'&nbsp;шт.');
		}
		
		$('how_many' + id).value = '';
	},
	
	multipleAdd: function() {
		var checked = $H();
		$A($$('input.quantity')).each(function(input) {
			if(parseInt(input.value) > 0) {
				checked[parseInt(input.id.replace('how_many', ''))] = parseInt(input.value);
			}
		});
		
		if(checked.keys().length == 0) {
			alert('Введите желаемое количество специальных полях у товаров...');
			return false;
		}
		
		var options = {
			id: checked.keys().toString(),
			value: checked.values().toString(),
			action: 'addMultiple'
		}
		
		var self = this;
		
		this.proccessSending(options, function(transport) {
			var response = transport.responseText.replace("ok\n", '');
			response = response.split(',');
			
			$A(response).each(function(value) {
				var value = value.split(':');
				
				self.updateItemCartLabel(value[1], value[0]);
				
				new Effect.Highlight($('in_cart' + value[0]));
			});
			
			self.updateCartBlock();
		});
	},
	
	updateCartBlock: function(id) {
		if(id) {
			var options = {
				action: 'getInfo',
				id: id
			};
		} else {
			var options = {
				action: 'getInfo'
			};	
		}	
		var self = this;
		
		this.showOverlay = false;
		this.proccessSending(options, function(transport) {
			var response = transport.responseText.replace("ok\n", '');
			response = response.split("\n");
/*
			if (response[0] == '0') {
				$('inCart').style.display = "none";
				$('noneCart').style.display = "block";
			} else {
				$('inCart').style.display = "block";
				$('noneCart').style.display = "none";
			}
	/**/		
			$('cart_itemsCount').update(response[0] + ' шт.');
			$('cart_sum').update(response[1]);
			
			new Effect.Highlight($('cart_itemsCount'));
			new Effect.Highlight($('cart_sum'));
			
			if (id) {
				$('sumCt'+id).update(response[2]);
				new Effect.Highlight($('sumCt'+id));
			}
			
			self.showOverlay = true;
		});
	},
	
	save: function(id) {
		this.oldValue[id] = parseInt($('inp'+id).value);
	},
	
	deleteItem: function(id) {
		if (!confirm('Вы точно желаете удалить эту позицию из корзины?')) {
			return false;
		}
		
		var options = {
			action: 'deleteItem',
			id:	id
		};
		var self = this;
		
		this.successMessage = 'Удаление из корзины прошло успешно...';
		this.proccessSending(options, function(transport) {
			$('tritem' + id).remove();
			/*
			if(Prototype.Browser.Gecko) {
				new Effect.Fade($('tritem' + id));
			} else {
				$('tritem' + id).remove();
			}
			/**/
		});		
	},
sendAllowed: function() {
		if(cart_sum < 3000) {
			return confirm("Внимание!\nВаш заказ меньше минимальной суммы заказа (мин. 3000 рублей).\n\nВаш заказ не будет обработан (только если это не дозаказ).\n\nПродолжить?");
		} else {
			return true;
		}
	}
}