

var nbTotalProducts = 0;


$(document).ready(function(){
	init_cart();
});
function regex(pattern,value) { 
  return (value.match(pattern)) ? value.match(pattern) : null; 
} 

function init_cart() {


	$('.addtocart').each(function(){
		$(this).click(function(event){
			url=$(this).attr('href');
			id = cart_getProductId( $(this).attr('href'));
			id_attribute = $('#idCombination').val();
			addtoCart(id,null,url + '&ajax=true'+'&id_product_attribute='+id_attribute);
						return false;
		});
	});


	// patch du formulaire

	if ($('buyForm') && $('panier-btn')) {
		$('panier-btn').onclick =  function(){

			addtoCart(
						$('product_page_product_id').value,
						'panier-btn',
						'cart.php?ajax=true'
					);
			return false;
		};
	}	
}

function cart_getProductId(url) {
	name = 'id_product';
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var name = "[\\?&]"+name+"=([^&#]*)";
	var results = regex(name,url);

	if( results == null )
		return "";
	else
		return results[1];

}

function addtoCart(prodId,butId,url) {


	if (butId)
	{
		$('#'+butId).css('background-position','top');
	}
	var a = $.ajax({
  type: 'POST',

  url: url,
  data: $('#buyForm').serialize(),
  dataType: 'text',
  success:  function(request)
	  {

	    if(request)
		{
			var results = request;
		}
		if(results.hasError == true)
		{
				var msg = "";
				for(i=0;i<results.errors.length;i++)
				{
					msg += results.errors[i] + '<br />';
					
				}
				$.gritter.add({
					// (string | mandatory) the heading of the notification
					title: 'Erreur',
					// (string | mandatory) the text inside the notification
					text: msg,
					// (string | optional) the image to display on the left
					
					// (bool | optional) if you want it to fade out on its own or just sit there
					sticky: false, 
					// (int | optional) the time you want it to be alive for before fading out
					time: ''
				});
				
		}
		else
		  {
			$.gritter.add({
					// (string | mandatory) the heading of the notification
					title: 'Merci !',
					// (string | mandatory) the text inside the notification
					text: 'Cet article a bien été ajouté à votre panier',
					// (string | optional) the image to display on the left
					
					// (bool | optional) if you want it to fade out on its own or just sit there
					sticky: false, 
					// (int | optional) the time you want it to be alive for before fading out
					time: '1300'
				});
				buildCartContent(results);

		  }
	 
	  }
});


}

function buildCartContent(results) {

	//la sortie
	var container = $('#panier-nb2');
	container.empty();

	if (jQuery.browser.msie)
	{
		results = eval('('+results+')');
	}
	else
	{
			results =JSON.parse(results);


	}

	nbTotalProducts = results.nbTotalProducts;

	var nbStr = nbTotalProducts + ' article' + (nbTotalProducts > 1 ? 's' : '');
	$('#panier-details').html(nbStr);

}




/* ---------------------------------------------------------------------------*/
//
// Les variantes
//
/* ---------------------------------------------------------------------------*/

function nat_findCombination() {

}
