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, type, pays, numero, nbNumeros){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_panier.php',
			method: 'GET',
			async: true,
			encoding:'utf-8',
			noCache: true,
			data: 'ajout=1&ajax=true&qte=' + ( (quantity && quantity != null) ? quantity : '1') + '&id=' + idProduct + '&type='+type+'&pays='+encodeURI(pays)+'&numero='+numero+'&nbNumeros='+nbNumeros,
			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;
					});*/
					elementToTransfert = callerElement ;

				}
				else
					elementToTransfert = $$(addedFromProductPage ? 'div#imageProduit' : ('div#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);

						Sexy.confirm('<p>Votre article a été ajouté à votre commande.</p>', {
							textBoxBtnOk: 'Continuer mes achats',
							textBoxBtnCancel:'Terminer ma commande',
							onComplete:	function(returnvalue) {
								if (!returnvalue)
								{
									window.location = './commande.php';
								}
							}
						});

						/**
						 * Pas de quantité affiché dans le pod_panier sur journaux
						 *
						new Fx.Tween($('qteProd'+idProduct), {
							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, type, numero){


		//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+'&type='+type+'&numero='+numero,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
				new Fx.Tween($('Produit'+idProduct+'-'+type+'-'+numero), {
					duration: 800,
					onComplete: function() {
						new Fx.Tween($('Produit'+idProduct+'-'+type+'-'+numero), {
							duration: 800,
							onComplete: function() {
								new Request.HTML({
									method: 'GET',
									url: 'commande_ajax.php',
									async: true,
									noCache: true,
									update: 'panier',
									onSuccess: function(responseTree2, responseElements2, responseHTML2, responseJavaScript2)
									{
										ajaxPanier.updateCart(responseHTML);
									}
									}).get();
							}
						}).start('height', 0);
					}
				}).start('opacity', 0);
			}
		}).get();
	},
	// Modifier la quantité d'un article
	modify : function(idProduct, quantity, type, numero){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_panier.php',
			method: 'GET',
			async: true,
			noCache: true,
			data: 'modifier=1&ajax=true&qte=' + ( (quantity && quantity != null) ? quantity : '1') + '&id=' + idProduct + '&type='+type + '&numero='+numero,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
				new Request.HTML({
					method: 'GET',
					url: 'commande_ajax.php',
					async: true,
					noCache: true,
					update: 'panier',
					onSuccess: function(responseTree2, responseElements2, responseHTML2, responseJavaScript2)
					{
						ajaxPanier.updateCart(responseHTML);
					}
					}).get();
			}
		}).get();
	},

  /* STDI
     Début Requêtes AJAX pour les promotions
  */
	//Supprimer la promo
	removepromo : function(){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_promo.php',
			method: 'GET',
			async: true,
			noCache: true,
			data: 'supprimerpromo=1&ajax=true',
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
				new Request.HTML({
					method: 'GET',
					url: 'commande_ajax.php',
					async: true,
					noCache: true,
					update: 'panier',
					onSuccess: function(responseTree2, responseElements2, responseHTML2, responseJavaScript2)
					{
						//ajaxPanier.updateCart(responseHTML);
					}
					}).get();
			}
		}).get();
	},
	// Modifier la promotion
	modifypromo : function(idpromo){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_promo.php',
			method: 'GET',
			async: true,
			noCache: true,
			data: 'modifierpromo=1&ajax=true&idpromo=' + idpromo,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
			{
			  Sexy.alert('<p>'+responseHTML+'</p>');
				new Request.HTML({
					method: 'GET',
					url: 'commande_ajax.php',
					async: true,
					noCache: true,
					update: 'panier',
					onSuccess: function(responseTree2, responseElements2, responseHTML2, responseJavaScript2)
					{
						//ajaxPanier.updateCart(responseHTML);
					}
					}).get();
			}
		}).get();
	},
  /* STDI
     Fin Requêtes AJAX pour les promotions
  */
  /* STDI
     Début Requêtes AJAX pour les commandes fantomes
  */
	// Envoyer le mail
	commandefantome : function(){
		//send the ajax request to the server
		new Request.HTML({
			url: 'ajax_commande_fantome.php',
			method: 'GET',
			async: true,
			noCache: true
		}).get();
	},
  /* STDI
     Fin Requêtes AJAX pour les commandes fantomes
  */

	//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();
//});
