var popUpJDialog = true;

function AjouterProduitPanier(reference, libelle, quantite, e) 
{
	if(libelle != "pdt_lies")	
	{
		($('#id_sous_produit').val() != null) ? reference = $('#id_sous_produit').val() : reference = reference;
	}
	
	($('#quantiteProduit').val() != null) ? quantite = $('#quantiteProduit').val() : quantite = quantite;
		
	/* === Site correspond au type de site, Grand public ou Professionnel ===*/
	PanierActions('ajouter', reference, quantite, e);
}

function SupprimerProduitPanier(reference, callback)
{
	PanierActions('supprimer', reference, 0, null, callback);
}

function ModifierProduitPanier(reference, quantite, callback)
{
	PanierActions('modifier', reference, quantite, null, callback);
}

function confirmeCookie()
{
	PanierActions('cookie','0',0);
}

function PanierActions(fonction, reference, quantite, e, callback)
{
	if(fonction == 'ajouter')
		sourisxy(e);
	
	/*
	getXhr();

	//on définit l'appel de la fonction au retour serveur
	xhr.onreadystatechange = function() { handleHttpResponse(fonction); };

	xhr.open('POST','/panier',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
	xhr.send('do=' + escape(fonction) + '&reference=' + escape(reference) + '&quantite=' + quantite);
	*/
	
	$.post(
		"/panier", { "do" : escape(fonction), "reference" : escape(reference), "quantite" : quantite },
		function(data) { 	
			//alert(data);
			handleResponse(fonction, data, null, reference);
			
			if(callback != null)
				callback();
		},
		"xml"
	)
		
}

function handleResponse(fonction, data, e, ref) 
{
	
	var docXML = clean(data)
	
	var nbTotal= docXML.getElementsByTagName("nbTotal")[0].firstChild.nodeValue;
	var prixTotal= docXML.getElementsByTagName("TOTAL")[0].firstChild.nodeValue;
	
	if($("#nbArticlesPanierTop").size() == 1)
		RemoveChildNodes(document.getElementById('nbArticlesPanierTop')).appendChild(document.createTextNode(nbTotal));
		
	if($("#totalPanierTop").size() == 1)
		RemoveChildNodes(document.getElementById('totalPanierTop')).appendChild(document.createTextNode(prixTotal));
		
	if(fonction == 'ajouter')
	{
		panierPopUp("panierPopUp", x, y, "show", ref);
	}
	/*
	else
	{
		UpdateProduitsRecap(docXML.getElementsByTagName("produit"));
	}
	*/
	
	/*
	if($(".total .row2 .col2 h5 span").size() == 1)
	{
		$(".total .row2 .col2 h5 span").html(prixTotal);
	}
	
	if(docXML.getElementsByTagName("messCP")[0] != null && $('#CPMessage').size() == 1 ) 
	{
		$('#CPMessage').html(docXML.getElementsByTagName("messCP")[0].firstChild.nodeValue);
	}
	*/
	
	/*
	else if(fonction == 'modifier') {
		//alert('Ce produit a été modifié !');
		UpdateProduitsRecap(docXML.getElementsByTagName("produit"))
	}
	else if(fonction == 'supprimer') {
		//alert('Ce produit a été supprimé !');
		UpdateProduitsRecap(docXML.getElementsByTagName("produit"))
	}
	*/
	
	if(document.getElementById('totalPanierRecap') != null)
		RemoveChildNodes(document.getElementById('totalPanierRecap')).appendChild(document.createTextNode(prixTotal));
		
	//var taille= docXML.getElementsByTagName("taille")[0].firstChild.nodeValue;
	//var produits= docXML.getElementsByTagName("produits");
	//var produit= produits.item(0).getElementsByTagName("produit");
	
}


////// DEBUT des fonctions de gestion de la confirmation d'ajout au panier d'un article //////
var x;
var y;

/**
function panierPopUp(element, x, y, action) {
	var name = navigator.appName
	if(action == "show"){
		var height=document.getElementById(element).style.height;
		var aHeight = parseInt(height.substr(0, height.length-2));
		var width=document.getElementById(element).style.width;
		var aWidth = parseInt(width.substr(0, width.length-2));
		var sTop=document.documentElement.scrollTop;
		var sLeft=document.documentElement.scrollLeft;
		if (name != "Microsoft Internet Explorer") {
			var posY=(y)-(aHeight + (aHeight/7));
			var posX=(x)-(aWidth - (aWidth/3));
		}
		else {
			var posY=((y)-(aHeight + (aHeight/7))) + sTop;
			var posX=(x)-(aWidth - (aWidth/3)) + sLeft;
		}
		document.getElementById(element).style.top=posY+"px";
		document.getElementById(element).style.left=posX+"px";
		
		$('#panierPopUp').style.display = '';
		//Effect.Appear('panierPopUp');
	}
	else if(action =="close"){
		$('#panierPopUp').style.display = 'none';
		//Effect.Fade('panierPopUp');
	}
}
**/

function panierPopUp(element, x, y, action, ref) {
	
	$.post(
		"/panier-popup/&ic=0", { reference : ref },
		function(data) { 
			
			if(popUpJDialog){ /** Si on utilise la popup JDialog **/
				var name 	= navigator.appName
			
				/** Params affichage **/
				var height 	= 'auto';
				var width	= 228;
				
				
				var sTop	= document.documentElement.scrollTop;
				var sLeft	= document.documentElement.scrollLeft;
				
				if (name != "Microsoft Internet Explorer") 
				{
					var posY	= (y)-(140);
					var posX	= (x)+(width - 350 );
				}
				else 
				{
					var posY	= ((y)- 160) + sTop;
					var posX	= (x)-(width - (width/2)) + sLeft;
				}
				
				
				//$("#" + element).css	= ("left", posX + "px");
				
				/* Desactivation de la popUp */
				
				$(data).dialog({
					autoOpen : true,
					height : height,
					width : width,
					modal : false,
					resizable : false,
					zIndex : 9999
					/*buttons : {						
						'Continuer vos achats': function() { $("#" + element).dialog("close"); 
												$("#" + element).dialog("destroy"); 
												$("#" + element).remove();
								   },
						'Valider ma commande': function() { $("#" + element).dialog("close"); 
												$("#" + element).dialog("destroy");
												$("#" + element).remove();
												window.location.href='/commande-panier';
								   }
					}*/
				});
				
				$(".ui-dialog-titlebar-close").click(function() {
					$("#" + element).dialog("close"); 
					$("#" + element).dialog("destroy"); 
					$("#" + element).remove();
				});
				
				$(".ui-dialog").css("top", posY + "px");
				$(".ui-dialog").css("left", posX + "px");
				
			}
			else if(!popUpJDialog){
				$("#panierPopUp").html("");
				$("#panierPopUp").html(data);
				$("#panierPopUp").css("display", '');
				$("#articlesPanier").css("display", 'none');
			}
			
		},
		"html"
	)
	
}

function sourisxy(e)
{
  x = (navigator.appName=="Netscape") ? e.pageX : event.x + document.body.scrollLeft;
  y = (navigator.appName=="Netscape") ? e.pageY : event.y + document.body.scrollTop;
	
	if(navigator.appName.substring(0,3) == "Net")
	document.captureEvents(Event.mousemove);
	
}
////// FIN des fonctions de gestion de la confirmation d'ajout au panier d'un article //////


function SetCodePromo(code) 
{
	$.post(
		"/panier", { "do" : "addcodepromo", "reference" : escape(code) },
		function(data) {
			handleResponse('addcodepromo', data);
			AfficherCommande(true);
			ValidCodePromo();
		/*	if(data.getElementsByTagName('messCP')[0].textContent == 'Le code que vous avez saisi n\'est pas valide.')
			{
				
			}*/
		},
		"xml"
	)
	
}

//Cette fonction ne doit normalement plus être utilisée pour Neoshop
/*
function UpdateProduitsRecap(produits) 
{
	for(var i = 0; i < produits.length; i++) 
	{
		var idproduit = produits[i].childNodes[0].firstChild.nodeValue;
		var prixTotal = produits[i].childNodes[4].firstChild.nodeValue;
		
		if($('#totalProduitRecap_' + idproduit).size() == 1)
			RemoveChildNodes($('#totalProduitRecap_' + idproduit)).appendChild(document.createTextNode(prixTotal));
	}
}
*/
