// JavaScript Document
var monPanier, articleCourant;

previewImage = function (URLStr) {
  var Hauteur = (screen.availHeight) ? screen.availHeight -42 : 480 ;
  popUpWin = open("../boutique/preview_f.asp?oeuvreID="+URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=1,copyhistory=no,width=628,height='+Hauteur+',left0, top=0,screenX=0,screenY=0');
  popUpWin.focus();
}


initDataPanier = function (){
	monPanier = cookieToString();
	setPanierValue();
	setFeedbackInit();

	//setInterval(showCookieInterval,500);
}

showCookieInterval =  function (){
	window.status = "cookie = " + Get_Cookie("panier") + "  /  monpanier = " + panierToString();
}

ajouterArticle = function (){
	//articleCourant = "g44";
	if(articleCourant){
		var boutonAjouter = top.contenu.description.document.images["ajouterAchats"];
		var combien;
		if(trouverMemeArticleDansPanier(articleCourant) >= 0){
			var dejaCombien = monPanier[trouverMemeArticleDansPanier(articleCourant)].n;
			combien = prompt("Cet article est déjà parmi vos achats.\nCombien d'items de cet article, au total, désirez-vous acheter?", dejaCombien.toString());
			
		}else{
			//combien = prompt("Combien d?items de cet article ? ajouter?", "1");
			combien = 1;
		}
		if(combien){
			if(isNaN(combien) || typeof combien == "null"){
				alert("Entrée incorecte.\nRecommencez.");
				ajouterArticle();
			}else{
				boutonAjouter.src = "../img/panier/b_ajouter_off.png";
				combien =  parseInt(combien,10);
				if(combien > 0){
					boutonAjouter.src = "../img/panier/b_ajouter_anim.gif";
				}else{
					boutonAjouter.src = "../img/panier/b_ajouter_anim_out.gif";
				}
				if(trouverMemeArticleDansPanier(articleCourant) >= 0){
					//alert(monPanier.splice);
					//var memeItem = monPanier.splice(trouverMemeArticleDansPanier(articleCourant), 1);
					var memeItem = monPanierSplice(trouverMemeArticleDansPanier(articleCourant));
					if( combien > 0){
						memeItem.n = combien;
						//monPanier.push(memeItem[0]);
						monPanier[monPanier.length] = memeItem;						
					}
				}else{
					if( combien > 0){
						var nouvelItem = new Object();
						nouvelItem.n = combien;
						nouvelItem.i = articleCourant;
						//monPanier.push(nouvelItem);
						monPanier[monPanier.length] = nouvelItem;
					}
				}
				//alert(monPanier.length);
				setPanierValue();
			}
			modifierCookie();
			setCetArticleDansPanier();
		}
	}
}

modifierCookie = function (){
	var d = new Date();d.setDate(d.getDate()+2);d.setHours("01");d.setMinutes("00");d.setSeconds("00");
	Set_Cookie("panier",panierToString(),d,"/");
}


voirMonPanier = function (){
	monPanier = cookieToString();
	var outHTML = MM_findObj("panier",top.contenu.document);//top.contenu.document.getElementById("panier");
	var out = "<p>";
	for(var x=0; x < monPanier.length; x++){
		out += monPanier[x].n + " ? " + monPanier[x].i + "<br />\n";
	}
	out += "</p>";
	outHTML.innerHTML = out;
	//alert(out);
	//alert(monPanier);
}

checkoutCaisse = function (){
}

trouverMemeArticleDansPanier = function (article){
	var articleIndex = -1;
	for(var x=0; x < monPanier.length; x++){
		if(monPanier[x].i.toString() == article.toString()){
			articleIndex = x;
			break;
		}
	}
	//alert("trouverMemeArticleDansPanier() = return "+articleIndex);
	return articleIndex;
}

monPanierSplice = function (articleIndex){
	var output;
	if(monPanier.length > 0){
		var arrayFront = new Array();
		var arrayEnd = new Array();
		for (var x=0; x < monPanier.length; x++){
			if(x < articleIndex){
				arrayFront[arrayFront.length] = monPanier[x];
			}else if(x > articleIndex){
				arrayEnd[arrayEnd.length] = monPanier[x];
			}
		}
		output = monPanier[articleIndex];
		monPanier = arrayFront.concat(arrayEnd);
		return output;
	}
}

validation = function(qui){
	//alert(Get_Cookie("panier"));
	combiendArticles++
	setPanierValue(combiendArticles);
	//alert(combiendArticles);
	return false;
}

setPanierValue = function(){
	var num = 0;
	if(monPanier.length > 0){
		for(var i=0; i < monPanier.length; i++){
			num += parseInt(monPanier[i].n);
		}
	}

	var field = findID("articles");
	field.innerHTML = num.toString();
}

setFeedbackInit = function(){
	//if(monPanier.length > 0){
		var field;
		var num = 0;
		var terme = "article";
		
		if (typeof feedbackCourant != "undefined") {
			//alert("allFeedbacks.length = "+allFeedbacks.length)
			for (var y=0; y < allFeedbacks.length; y++) {
				//alert(y+"{"+allFeedbacks[y].nom+","+allFeedbacks[y].value+"}")
				field =  MM_findObj(allFeedbacks[y].nom);
				num = 0;
				terme = "article";
				//alert(field.id)
				for(var x=0; x < monPanier.length; x++){
					//alert("monPanier[x].i = " + monPanier[x].i + " & allFeedbacks[y].value = " + allFeedbacks[y].value)
					if (monPanier[x].i == allFeedbacks[y].value){
						num = parseInt(monPanier[x].n);
						terme += (num > 1) ? "s" : "" ;
					}
				}
				field.innerHTML = (num > 0) ? "Qté: " + num.toString() + " " + terme : "&nbsp;&nbsp;&Oslash;&nbsp;&nbsp;";
				
			}
		
		}else{
			if (typeof articleCourant != "undefined") {
				field = MM_findObj("feedback");
				for(var x=0; x < monPanier.length; x++){
					if (monPanier[x].i == articleCourant){
						num = parseInt(monPanier[x].n);
						terme += (num > 1) ? "s" : "" ;
					}
				}
			
				field.innerHTML = (num > 0) ? "Qté: " + num.toString() + " " + terme : "&nbsp;&nbsp;&Oslash;&nbsp;&nbsp;";
				
			}
		}
	//}
}



setCetArticleDansPanier = function(){
	//alert("articleCourant == "+articleCourant)
	//if(monPanier.length > 0){
		var field;
		var num = 0;
		var terme = "article";
		if (typeof articleCourant != "undefined") {
			field = (typeof feedbackCourant != "undefined") ? feedbackCourant : MM_findObj("feedback");
			for(var x=0; x < monPanier.length; x++){
				if (monPanier[x].i == articleCourant){
					num = parseInt(monPanier[x].n);
					terme += (num > 1) ? "s" : "" ;
				}
			}
		
			field.innerHTML = (num > 0) ? "Qté: " + num.toString() + " " + terme : "&nbsp;&nbsp;&Oslash;&nbsp;&nbsp;";
				
		}
	//}
}

panierToString = function(){
	var seq = ["|",","];
	var out = "";
	if(monPanier.length > 0){
		var c = 0;
		for(var i=0; i < monPanier.length; i++){
			c=0;
			for (var x in monPanier[i]){
				out += monPanier[i][x];
				out += seq[c];
				c++;
			}
		}
	}
	return out.substring(0,out.length-1);
}

cookieToString = function(){
	var panier = Get_Cookie("panier");
	if(panier){
		var produit = panier.split(",");
		var out = new Array();
		if(produit.length > 0){
			//var c = 0;
			var donnees;
			for(var i=0; i < produit.length; i++){
				donnees = produit[i].split("|");
				if(donnees.length > 0){
					//alert(typeof donnees);
					var toNumN = parseInt(donnees[0])
					out[i] = {n:toNumN,i:donnees[1]};
				}
			}
		}
	}else{
		//var out = [{n:2,i:"c120"},{n:1,i:"g120"},{n:3,i:"c250"},{n:1,i:"g150"},{n:1,i:"c100"},{n:1,i:"g105"},{n:4,i:"c44"}]
		var out = new Array();
	}
	return out;
}

findID = function (qui){
	var idRef;
	idRef = MM_findObj(qui);
	/*if(!document.getElementById) {
		idRef = document.all[qui];
	}else{
		idRef = document.getElementById(qui);
	}*/
	return idRef;
}

showImage = function (qui){
	top.contenu.description.articleCourant = qui;
	top.contenu.description.detectAfficheImage();
	//top.contenu.description.document.location.href = "description.asp?oeuvre="+escape(qui);
	//top.contenu.description.detectAfficheImage();
}

detectAfficheImage = function (){
	//var oeuvre = unescape(getQueryVariable(top.contenu.description,"oeuvre"));
	var oeuvre = articleCourant;
	var boutonAjouter = top.contenu.description.document.images["ajouterAchats"];
	var preview = top.contenu.description.document.images["previewImg"];//MM_findObj("preview");
	var titreOeuvre = MM_findObj("titreOeuvre");
	var dimensions = MM_findObj("dimensions");
	var prix = MM_findObj("prix");
	
	if(oeuvre){
		boutonAjouter.src = "../img/panier/b_ajouter.png";
		articleCourant = oeuvre;
		preview.src = "../../peintures/preview/"+oeuvre+".jpg";
		titreOeuvre.innerHTML =  "<strong>" + top.contenu.description.oeuvreData[oeuvre].t + "</strong>";
		dimensions.innerHTML =  top.contenu.description.oeuvreData[oeuvre].d;
		prix.innerHTML =  top.contenu.description.oeuvreData[oeuvre].p;
	} else {
		boutonAjouter.src = "../img/panier/b_ajouter_off.png";
		preview.src = "../../img/spacer.gif";
		titreOeuvre.innerHTML = "";
		dimensions.innerHTML =  "";
		prix.innerHTML = "";

	}
}

resetAfficheImage = function (){
	//top.contenu.description.showImage("");
	chemin = top.contenu.description.location.pathname.toString().split("/");
	if(chemin[chemin.length -1] != "description.asp"){
		top.contenu.description.location.href = "description.asp";
	}
}
