var ajaxPanier = {
	
	//remplace tous les boutons en relation avec le panier dans la page
	remplaceBoutons : function(){
		//Pour tous les boutons ajout au panier
		var btns = $$('.bouton_ajax_ajout_panier');
		btns.each(function(btn){
			btn.removeEvents('click');
			btn.addEvent('click', function(event){
				event.stop();
				var idProduct = this.get('rel').replace('ajax_idproduit_','');
				ajaxPanier.add(idProduct, false, this);
			});
		});
		
		//Page produit, bouton ajouter
		$$('#ajax_prod_ajout_panier input').each(function(item, array, index) {
			item.removeEvents('click');
			item.addEvent('click', function(event){
				event.stop();
				ajaxPanier.add( $('idproduit').get('value'), true, null, $('quantite').get('value'));
			});
		});
	
		//Bouton supprimer du panier
		$$('.ajax_panier_supprimer').each(function(item, array, index) {
			item.removeEvents('click');
			item.addEvent('click', function(event){
				event.stop();
				// on recupere l'id produit du panier
				var firstCut =  this.get('id').replace('panier_produit_', '');
				var ids = firstCut.split('_');
				//if product has attributes
				if(firstCut[1])
					ajaxPanier.remove(ids[0], ids[1]);
				else
					ajaxPanier.remove(ids[0]);
				return false;
			});
		});
	},
	
	//add a product in the cart via ajax
	add : function(idProduct, addedFromProductPage, callerElement, quantity, idmultiCondi){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_panier.php',
			method: 'GET',
			async: true,
			noCache: true,
			data: 'ajout=1&ajax=true&qte=' + ( (quantity && quantity != null) ? quantity : '1') + '&id=' + idProduct + '&idmulticondi='+idmultiCondi,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
				//apply 'transfert' effect
				var elementToTransfert = null;
				if (callerElement && callerElement != null)
				{
					callerElement.getParents().each(function(item, array, index) {
						if (item.hasClass('ajax_produit')) elementToTransfert = item;
					});
				}
				else
					elementToTransfert = $$(addedFromProductPage ? 'div#imageProduit' : ('.ajax_id_produit_' + idProduct) )[0];
				
				var debut = elementToTransfert.getCoordinates();
				
				var myDiv = new Element('div',{
					'class': 'transfertProduit',
					'styles': {
						'position': 'absolute',
						'width': debut.width,
						'height': debut.height,
						'top': debut.top,
						'left': debut.left
					}
				});
				elementToTransfert.adopt(myDiv);
				
				var arrivee = $('pod_panier').getCoordinates();
				
				/* On déplace le div et en même temps on le met aux bonnes dimensions */
				new Fx.Morph(myDiv, {
					link: 'ignore',
					duration: 800,
					onComplete: function(){
						myDiv.destroy();
						ajaxPanier.updateCart(responseHTML);
						new Fx.Tween($('qteProd'+idProduct+"-"+idmultiCondi), {
							duration: '100',
							link: 'chain'
						}).start('opacity', 0).start('opacity', 1).start('opacity', 0).start('opacity', 1).start('opacity', 0).start('opacity', 1);
					}
				}).start({
					'height': arrivee.height,
					'width': arrivee.width,
					'left': arrivee.left,
					'top': arrivee.top,
					'margin-top': 0
				});
			}
		}).get();
	},
	
	//remove a product from the cart via ajax
	remove : function(idProduct, idCombination, idMultiCondi){
		//send the ajax request to the server
		new Request.HTML({
			method: 'GET',
			url: 'ajax_panier.php',
			async: true,
			noCache: true,
			data: 'suppression=1&ajax=true&qte=1&id=' + idProduct+'&idmulticondi='+idMultiCondi,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
				new Fx.Tween($('Produit'+idProduct+'-'+idMultiCondi), {
					duration: 800,
					onComplete: function() {
						new Fx.Tween($('Produit'+idProduct+'-'+idMultiCondi), {
							duration: 800,
							onComplete: function() {
								ajaxPanier.updateCart(responseHTML);
							}
						}).start('height', 0);
					}
				}).start('opacity', 0);
			}
		}).get();
	},
	
	//Met à jour le panier
	updateCart : function(jsonData) {
		$('pod_panier').set('html', jsonData);
		// Remplace les actions sur les boutons ajoutés
		ajaxPanier.remplaceBoutons();
	}
}

//quand la page est chargée
window.addEvent('domready', function(){
	/*$('pod_panier_replier').addEvent('click', function(){
		ajaxPanier.collapse();
	});
	
	$('pod_panier_deplier').addEvent('click', function(){
		ajaxPanier.expand();
	});*/
	ajaxPanier.remplaceBoutons();
});