	/**
	 * 
	 * Javascript Cart class
	 * 
	 * @name ModuleCart
	 * @author Vincent Cantin Bellemare
	 * @since 2009-09-19
	 * @version 1.0.0
	 * @package reptileframework
	 * 
	 */
	
	var Cart = 
	{
		timeout 				: null,
		CLASSNAME_DISPLAY_TOP 	: 'cart_top_display',
		CLASSNAME_MESSAGE_TOP 	: 'cart_top_message',
		PREFIX_TOP 				: 'header_cart_',
		API_TOP_CART 			: 'cart.apiCartAttributes',
		
		/*
		 * Ajax call to adjust top cart
		 */
		getTotalCartAjax : function(callbacks)
		{
			API.call
			(
				Cart.API_TOP_CART,
				{},
				function()
				{
					var cartAttributes = this.responseJSON.top_cart_attributes;
			
					if(!Object.isUndefined(cartAttributes))
					{
						$A(callbacks).each
						(
							function (func)
							{
								func(cartAttributes);
							}
						);
					}
					else
					{
						Site.log('Error ' + Cart.API_TOP_CART);
					}
				}
			);	
		},
		
		popCartMessage : function(topCartAttributes)
		{
			topCartAttributes = $H(topCartAttributes);
			var itemsMessage 	= topCartAttributes.get(Cart.PREFIX_TOP + 'nb_items_text_add');
			
			clearTimeout(Cart.timeout);
			Site.popAlert(itemsMessage,{width : 250});
			Cart.timeout = Site.closeAlert.bind(Site).delay(20);			
		},
		
		/*
		 * Adjust cart attributes
		 * @param objct topCartAttributes
		*/
		displayTopCart : function(topCartAttributes)
		{
			topCartAttributes = $H(topCartAttributes);
			
			/*
			 * Setting display
			 */
			var displayTop 		= topCartAttributes.get(Cart.PREFIX_TOP + 'display');		
			var itemsMessage 	= topCartAttributes.get(Cart.PREFIX_TOP + 'nb_items_text');
			
			$$('.' + Cart.CLASSNAME_DISPLAY_TOP).each
			(
				function(item)
				{
					item.setStyle
					({
						display: displayTop
					});				
					
					if(item.hasClassName(Cart.CLASSNAME_MESSAGE_TOP))
					{
						item.update(itemsMessage)
					}
				}
			);
		},
		
		/*
		 * Display errors when user adds a product
		 */
		displayErrors : function(errors)
		{
			var strMessage = '';
			
			if(Object.isArray(errors))
			{
				$A(errors).each
				(
					function(error)
					{
						strMessage += error.parameters.message + '<br/>';	
					}
				);	
			}
		
			Site.popAlert(strMessage);
		},
		
		List : 
		{
			PREFIX_FIELDS 			: 'cart_fields_',		
			PREFIX_PRODUCTS		 	: 'cart_products_',
			API_TOP_CART 			: 'cart.updateTotalFields',
			deleteRows 				: Array('_row_1','_row_2'),	
			
			updateTotalFields 		: function()
			{
				Cart.List._displayUpdateProductsDiv(true);
				
				API.call
				(
					Cart.List.API_TOP_CART,
					{},
					function()
					{
						Cart.List.transport = this;
						Cart.List._updateTotalFields();	
					}
				);			
			},
			
			_displayUpdateProductsDiv : function(showDiv)
			{
				var productDiv = $('updating_products');
				
				if(showDiv)
				{
					//Cart.List._center(productDiv);
					productDiv.show();				
				}
				else
				{
					productDiv.hide();				
				}
			},
			
			_updateTotal : function()
			{
				$H(Cart.List.transport.responseJSON).each
				(
					function(pair)
					{
						var fieldId = Cart.List.PREFIX_FIELDS + pair.key;
						
						if($(fieldId))
						{
							if (Object.isString(pair.value)) 
							{
								$(fieldId).update(pair.value);
								Site.log(fieldId + ' updated');
							}
						}
					}
				);		
			
				Cart.List._displayUpdateProductsDiv(false);
			},
			
			deleteRow : function(productId)
			{
				Cart.List.deleteRows.each
				(
					function(row)
					{
						var rowId = Cart.List.PREFIX_PRODUCTS + productId + row;
						Site.log(rowId);
						
						if(!Object.isUndefined(currentRow = $(rowId)))
						{
							currentRow.hide();	
						}
						else
						{
							alert('Row ' + rowId + ' not found');
						}
					}
				);
			},
			
			_updateTotalFields : function()
			{
				Cart.List._updateTotal();
				Cart.List._updateProducts();	
				Plugins.triggerEvent('onUpdateFields',Cart.List.transport);
			},
			
			_updateProducts : function()
			{
				var updatedProducts = false;
				
				var productsList = Cart.List.transport.responseJSON.products;
				
				if(!Object.isUndefined(Cart.List.transport.responseJSON.products))
				{
					productsList.each
					(
						function(product)
						{				
							if(product.quantity > 0)
							{
								updatedProducts = true;
								
								$H(product).each
								(
									function(pair)
									{						
										var fieldId = Cart.List.PREFIX_PRODUCTS + product.product_id + '_' + pair.key;
										
										if($(fieldId))
										{
											if (Object.isString(pair.value) || Object.isNumber(pair.value)) 
											{
												if ($(fieldId).match('input')) 
												{
													$(fieldId).value = pair.value;
												}
												else
												{
													try
													{
														$(fieldId).update(pair.value);
													}
													catch(e){}
												}
											}
										}
									}
								);	
							}
							else
							{
								Cart.List.deleteRow(product.product_id);
							}	
						}
					);	
				}
			
				if(!updatedProducts)
				{
					location.reload();
				}
			},
			
			updateProducts : function()
			{
				var updatedProducts = false;
				
				Cart.List.transport.responseJSON.products.each(function(product)
				{
					if (product.quantity > 0) 
					{
						updatedProducts = true;
						
						$H(product).each(function(pair)
						{
							var fieldId = Cart.List.PREFIX_PRODUCTS + product.product_id + '_' + pair.key;
							
							if ($(fieldId)) 
							{
								if (Object.isString(pair.value) || Object.isNumber(pair.value)) 
								{
									try 
									{
										$(fieldId).update(pair.value);
										$(fieldId).value = pair.value;
										Site.log(fieldId + ' updated to ' + pair.value);
									} 
									catch (e){}
								}
							}
						});
					}
					else 
					{
						Cart.List.deleteRow(product.product_id);
					}
				});
				
				if (!updatedProducts) 
				{
					location.reload();
				}
			}
		}
	};
	
	
