
	/* --------------------------
		Add to cart
	-------------------------- */

He.Bootstrap('He.Components.AddToCart');

He.Components.AddToCart.Interval = null;
He.Components.AddToCart.UnitPriceObj = {
		ref		: null,
		price	: null,
		quantity: null,
		total	: null
}

He.Components.AddToCart.AjaxCall = function()
{
	callAjaxAddToCart(He.Components.AddToCart.UnitPriceObj.skuKey, He.Components.AddToCart.UnitPriceObj.quantity);
	He.Components.AddToCart.Interval = window.setInterval('He.Components.AddToCart.CheckResponse()',500);
}

He.Components.AddToCart.CheckResponse = function()
{
	if(He.Application.User.Cart != null)
	{
		window.clearInterval(He.Components.AddToCart.Interval);
		He.Components.AddToCart.Response();
	}
}

He.Components.AddToCart.Response = function()
{
	if(He.Application.User.Cart.status == 'ok')
	{
		He.Components.AddToCart.ResponseOK();
	}
	else
	{
		He.Components.AddToCart.ResponseFail();
	}
}

He.Components.AddToCart.ResponseOK = function(response)
{
		
	var uidpk_ref	= He.Application.User.Cart.log.code;
	var num_item	= He.Application.User.Cart.content.items.length;
	
	for(i = 0; i < num_item; i++)
	{
		if(He.Application.User.Cart.content.items[i].uidpk == uidpk_ref)
		{
			$('#add_to_cart p.item_desc')
			.html(
				$('#He_Template_AddToCart')
				.html()
				.parseTemplate(He.Application.User.Cart.content.items[i])
			);
			
			if(He.Application.Config.BadBrowser)
			{
				// get all pngs on page
				$('#add_to_cart img[src$=.png]').each(function()
				{
					if (!this.complete)
					{
						this.onload = function() { He.Application.FixPng(this) };
					}
					else
					{
						He.Application.FixPng(this);
					}
				});
			}
			
			He.Components.AddToCart.DisplayResponse();
			
			break;
		}
	}
	
	$('#user_cart_href').remove();
		
	$('#user_cart')
	.prepend(
		$('#He_Template_Basket_link').html().parseTemplate(He.Application.User.Cart.content)
	);
}

He.Components.AddToCart.DisplayResponse = function()
{	
	$('#add_to_cart')
	.removeClass('popup_small')
	.addClass('popup_large');

	$('#add_to_cart_loading').hide();
	$('#add_to_cart_content_fail').hide();

	$('#add_to_cart_content').show();
}

He.Components.AddToCart.ResponseFail = function(msg)
{
	$('#add_to_cart_content_fail p.grey_dark').html('<p class="grey_dark">' + He.Application.User.Cart.message + '');
	
	$('#add_to_cart_loading').hide();
	$('#add_to_cart_content').hide();

	$('#add_to_cart_content_fail').show();
}

He.Components.AddToCart.ResetPopup = function()
{
	$('#add_to_cart')
	.removeClass('popup_large')
	.addClass('popup_small');
	
	$('#add_to_cart_content').hide();
	$('#add_to_cart_content_fail').hide();
	
	$('#add_to_cart_loading').show();
}

He.Components.ToggleSelection = function()
{
	$('#check_all').click(function()				
	{
		var checked_status = this.checked;
		
		$("input[type='checkbox']").each(function()
		{
			this.checked = checked_status;
		});
	});
}

He.Components.AddToCart.GetRef = function(obj, ref)
{
	ref = (He.IsUndefined(ref)) ? $(this).val() : ref;
	var num_item = He.Components.AddToCart.ItemUnitPriceRef.productSkus.length;
	for(i = 0; i < num_item; i++)
	{
		//si ipad (this).val renvoie le skuKey au lieu du skuLabel
		if(He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuLabel == ref || He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuKey == ref)
		{
			He.Components.AddToCart.UnitPriceObj.skuKey		= He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuKey;
			He.Components.AddToCart.UnitPriceObj.skuLabel	= He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuLabel;
			He.Components.AddToCart.UnitPriceObj.price		= He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].price;
			He.Components.AddToCart.UpdateUnitPrice();
		}
	}
}

He.Components.AddToCart.UpdateProductPageBySkuLabel = function(skuLabel)
{
	var num_item	= He.Components.AddToCart.ItemUnitPriceRef.productSkus.length;
	for(i = 0; i < num_item; i++)
	{
		if(He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuLabel == skuLabel)
		{
			He.Components.AddToCart.UpdateProductPage(i);
		}
	}
}

He.Components.AddToCart.UpdateProductPageBySkuCode = function(skuCode)
{
	var num_item	= He.Components.AddToCart.ItemUnitPriceRef.productSkus.length;
	for(i = 0; i < num_item; i++)
	{
		if(He.Components.AddToCart.ItemUnitPriceRef.productSkus[i].skuLabel == skuCode)
		{
			He.Components.AddToCart.UpdateProductPage(i);
		}
	}
}

He.Components.AddToCart.UpdateProductPage = function(position)
{
	He.Components.AddToCart.UnitPriceObj.skuKey		= He.Components.AddToCart.ItemUnitPriceRef.productSkus[position].skuKey;
	He.Components.AddToCart.UnitPriceObj.skuLabel	= He.Components.AddToCart.ItemUnitPriceRef.productSkus[position].skuLabel;
	He.Components.AddToCart.UnitPriceObj.price		= He.Components.AddToCart.ItemUnitPriceRef.productSkus[position].price;
	He.Components.AddToCart.UpdateUnitPrice();
}

He.Components.AddToCart.GetQuantity = function(obj, quantity)
{
	quantity = (He.IsUndefined(quantity)) ? $(this).val() : quantity;
	if( quantity > 0 ) {
		He.Components.AddToCart.UnitPriceObj.quantity = quantity;
		He.Components.AddToCart.UpdateUnitPrice();
	}
}

He.Components.AddToCart.UpdateUnitPrice = function()
{

	// This part is here to prevent the absence of price. price are number, and without price we have a string.
	var objToPrintPrice;
	var objToPrintTotal;
	if(He.Components.AddToCart.UnitPriceObj.price != '--'){
		objToPrintPrice = new String(He.Components.AddToCart.UnitPriceObj.price.toFixed(2).replace('.',','));
	} else {
		objToPrintPrice = '--';
	}
	if(He.Components.AddToCart.UnitPriceObj.price != '--'){
		objToPrintTotal = He.Components.AddToCart.UnitPriceObj.price * He.Components.AddToCart.UnitPriceObj.quantity;
		objToPrintTotal = objToPrintTotal.toFixed(2).replace('.',',');
	} else {
		objToPrintTotal = '--';
	}
	
	var objToPrint = {
		skuKey		: He.Components.AddToCart.UnitPriceObj.skuKey,
		skuLabel	: He.Components.AddToCart.UnitPriceObj.skuLabel,
		price		: objToPrintPrice,
		quantity	: He.Components.AddToCart.UnitPriceObj.quantity,
		total		: objToPrintTotal
	}

	$('#item_unit_price')
	.html(
			$('#He_Template_Item_Unit_Price')
			.html()
			.parseTemplate(objToPrint)
		);
	
	$('#item_total_price')
	.html(
			$('#He_Template_Item_Total_Price')
			.html()
			.parseTemplate(objToPrint)
		);
}

He.Components.AddToCart.Init = function()
{
	/* Popup Trigger */
	$('#add_to_cart').jqm(
	{
		trigger: '#buyButton',
		onShow: function(hash)
		{
			hash.w.css('display', 'block');
			He.Components.AddToCart.ResetPopup();
			window.setTimeout(He.Components.AddToCart.AjaxCall, 500);
		}
	});
	
	$('#add_to_cart a.jqmClose').click(function()
	{
		He.Components.AddToCart.ResetPopup();
	});
}

