// JavaScript Document
function ajout_panier(bouton,lg){
	if(!lg) lg = 'fr';
	if($('container') != null){
			$('container').remove();
	}
	
	//recuperation des valeurs
	var form = bouton.form;
	/*Pas géré par IE ...
	var itm = form.getElement('input[name=itm]').value;
	var qty = form.getElement('input[name=qty]').value;
	var nom_produit = form.getElement('input[name=]').value;*/
	var itm = document.getElementById('itm').value;
	var qty = document.getElementById('qty').value;
	var nom_produit = document.getElementById('nom_produit').value;
	var pointure = document.getElementById('pointure').value;
	// var inputs = form.getElementsByTagName('input');
	// for(var i = 0; i < inputs.length; i++){
		// if (inputs[i].name == 'itm'){ itm = inputs[i].value; }
		// if (inputs[i].name == 'qty'){ qty = inputs[i].value; }
		// if (inputs[i].name == 'nom_produit'){ nom_produit = inputs[i].value; }
	// }
	
	
	
	//creation des elements
	container = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '100%',
											'height': '100%',
											'padding': '0px',
											'left': '0',
											'margin': '0px',
											'position': 'fixed',
											'top': '0',
											'text-align': 'center',
											'background-image': 'url(/images/overlay.png)',
											'z-index': '5'
										}
								  }
							);
	
	if (window.ie && !window.ie7){
		container.style.position = 'absolute';
		container.style.height = document.documentElement.clientHeight;
		container.style.background = 'none';
		container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='/images/overlay.png')";
		container.style.zIndex = '0';
		
		container2 = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '100%',
											'height': '100%',
											'padding': '0px',
											'left': '0',
											'margin': '0px',
											'position': 'absolute',
											'top': '0',
											'text-align': 'center',
											'z-index': '10'
										}
								  }
							);
		//pour ie6 (pour avoir les liens cliquables
		container2.setAttribute('id','container2');
		container2.injectInside(document.body);
	}
	
	container.setAttribute('id','container');
	container.injectInside(document.body);
	
	//afficher barre de chargement
	loading = new Element('div', {
										'styles': 
										{
											'display': 'block',
											'width': '220px',
											'height': '19px',
											'padding': '0px',
											'text-align': 'left',
											'margin':'0 auto',
											'background-image': 'url(/images/smoothgallery/loading-bar-black.gif)',
											'top' : '50%'
										}
								  }
						 );
	loading.setAttribute('id','loading');
	loading.injectInside(container);
	//centrer verticalement par rapport à la page
	valigncenter(loading);
	
	var url='/pl/cart_add_sup.pl';
	var get_html_panier = new Ajax(url, {
		method: 'get',
		data: 'itm='+ itm +'&qty='+ qty +'&pointure='+ pointure +'&ac=add',
		onSuccess: function(){
			//document.write(get_html_panier.response.text);
			if (get_html_panier.response.text=='ok'){
				ajout_ok(itm, qty, nom_produit, lg);
			}else{
				ajout_ko(itm, qty, nom_produit, lg);
			}
		},
		onFailure: ajout_ko
	}).request();
}
function ajout_ok(itm, qty, nom_produit, lg){
	var info_ajout_panier = new Element('div', {
													'styles': 
													{
														'display': 'block',
														'width': '350px',
														'height': '105px',
														'padding': '0px',
														'text-align': 'left',
														'margin':'0 auto',
														'z-index':'1000',
														'position':'absolute'
													}
											  }
										);
		 
	info_ajout_panier.setAttribute('id','info_ajout_panier');
	
	if (window.ie && !window.ie7){
		info_ajout_panier.injectInside($('container2'));
	}else{
		info_ajout_panier.injectInside($('container'));
	}
	
	valigncenter(info_ajout_panier);
	var myFx = new Fx.Style(info_ajout_panier, 'opacity').set(0);	
	
	//on recupere le html à afficher
	var url='/tmpl_dev_shopping/'+lg+'/commande/popup_panier_ajout.html';
	var get_html_panier = new Ajax(url, {
		method: 'get',
		update: info_ajout_panier,
		onSuccess: function(){
				$('loading').remove();
				//on realigne car l'element ne fait plus la meme taille
				valigncenter(info_ajout_panier);
				//inserer le nom du produit et la quantitée
				$$('.panier_popup_nom_produit')[0].innerHTML = nom_produit;
				$('quantite_ajoute_panier').innerHTML = qty;
				
				//TODO: inserer event clic sur le bouton "rester sur cette page" -> remove container
				$('rester_page').addEvent('click', function(){
						var container = $('container');
						//effet de fondu avant la fermeture
						new Fx.Style(container, 'opacity', {
							onComplete : function(){
								//on detruit la galeries
								container.remove();
								if (window.ie && !window.ie7){
									$('container2').remove();
								}
								window.location.reload();
							}
						}).start(1,0);
				});
				
				//TODO: inserer event clic sur le bouton "aller sur le panier"  -> ??
				$('aller_panier').addEvent('click', function(){
															 	window.location.href='/pl/cart.pl'; 
															  });
				
				var myFx = new Fx.Style(info_ajout_panier, 'opacity').start(0,1);
		},
		onFailure: function(){//on a pu ajouté au panier mais pas chargé le contenu du layer
			//feinte pour le problème des accents
			var txt = new Element('span');
			txt.innerHTML = 'Le(s) produit(s) ont été ajoutés au panier';
			alert(txt.innerHTML);
		}
	}).request();
}
function ajout_ko(itm, qty, nom_produit, lg){	
	//feinte pour le problème des accents
	// var txt = new Element('span');
	// txt.innerHTML = 'Le produit n\'a pas pu être ajouté au panier\n'+toto;
	// txt.injectTop(document.body);
	//alert(txt.innerHTML);
	// $('container').remove();
	// if (window.ie && !window.ie7){
		// $('container2').remove();
	// }
		var info_ajout_panier = new Element('div', {
													'styles': 
													{
														'display': 'block',
														'width': '350px',
														'height': '105px',
														'padding': '0px',
														'text-align': 'left',
														'margin':'0 auto',
														'z-index':'1000',
														'position':'absolute'
													}
											  }
										);
		 
	info_ajout_panier.setAttribute('id','info_ajout_panier');
	
	if (window.ie && !window.ie7){
		info_ajout_panier.injectInside($('container2'));
	}else{
		info_ajout_panier.injectInside($('container'));
	}
	
	valigncenter(info_ajout_panier);
	var myFx = new Fx.Style(info_ajout_panier, 'opacity').set(0);	
	
	//on recupere le html à afficher
	var url='/tmpl_dev_shopping/'+lg+'/commande/popup_panier_echec.html';
	var get_html_panier = new Ajax(url, {
		method: 'get',
		update: info_ajout_panier,
		onSuccess: function(){
				$('loading').remove();
				//on realigne car l'element ne fait plus la meme taille
				valigncenter(info_ajout_panier);
				//inserer le nom du produit et la quantitée
				$$('.panier_popup_nom_produit_blanc')[0].innerHTML = nom_produit;
				//$('quantite_ajoute_panier').innerHTML = qty;
				
				//TODO: inserer event clic sur le bouton "rester sur cette page" -> remove container
				$('rester_page').addEvent('click', function(){
						var container = $('container');
						//effet de fondu avant la fermeture
						new Fx.Style(container, 'opacity', {
							onComplete : function(){
								//on detruit la galeries
								container.remove();
								if (window.ie && !window.ie7){
									$('container2').remove();
								}
								window.location.reload();
							}
						}).start(1,0);
				});
				
				//TODO: inserer event clic sur le bouton "aller sur le panier"  -> ??
				//$('aller_panier').addEvent('click', function(){
				//											 	window.location.href='/pl/cart.pl'; 
				//											  });
				
				var myFx = new Fx.Style(info_ajout_panier, 'opacity').start(0,1);
		},
		onFailure: function(){//on a pu ajouté au panier mais pas chargé le contenu du layer
			//feinte pour le problème des accents
			var txt = new Element('span');
			txt.innerHTML = 'Le(s) produit(s) ont été ajoutés au panier';
			alert(txt.innerHTML);
		}
	}).request();
}
function valigncenter(lmt)
{
	var page = document.documentElement;
	if(lmt && page)
	{
	    var containerHeight;
	    if (page.innerWidth)
	    {
            containerHeight = page.innerHeight;
		}
		else
		{
            containerHeight = page.clientHeight;
		}
	    var lmtHeight;
	    if (lmt.innerWidth)
	    {
            lmtHeight = lmt.innerHeight;
		}
		else
		{
            lmtHeight = lmt.offsetHeight;
		}
		var y = Math.ceil((containerHeight - lmtHeight) / 2);
		if(y < 0)
		{
			y = 0;
		}
		lmt.style.position = "relative";
		lmt.style.top = y + "px";
	}
	if (document.getElementById)
	{
		document.body.style.visibility = 'visible';
	}
}
