/** @class
  * Sur-Charge des images de la galerie
  * @constructor
  * @param idImg id HTML de l'image
  */
function ImgGallerie (affichericones,idImg, present, gal_name,isFolder, folderName)
{
	/** L'element HTML racine de la boite d'information @type HTMLTable */
	this.box = document.getElementById(idImg);
	this.isFolder=isFolder;
	this.folderName=folderName;
	this.checkStructure(idImg);
	/** L'image @type HTMLImage */
	this.adresse = Element.getChildElements(this.box, "span")[0].innerHTML;
	this.vignette = Element.getChildElements(this.box, "span")[1];
	this.srcimg = this.vignette.innerHTML;
	/** Le corps @type HTMLDiv */
	this.id = Element.getChildElements(this.box, "span")[2].innerHTML;
	this.gallerie_nom = gal_name;
	/** Zone conteant l'icone sur laquelle cliquer @type HTMLSpan */
	this.icon = document.createElement("span");
	/** Icone montrant que le contenu est visible @type HTMLImage */
	this.presentImg = document.createElement("img");
	/** Icone montrant que le contenu est masqué @type HTMLImage */
	this.absentImg = document.createElement("img");
	/** Image */
	this.imgToShow = document.createElement("img");
	/** Lien */
	this.lien = document.createElement("a");
	// Initialiser la structure HTML
	this.setStructure(affichericones);
	// Mettre les styles
	this.setStyle(affichericones);
	// Faire réagir aux clics
	if(affichericones)
	{
		this.setOnClick();
	}
	//this.setOnMouseOver();
	//this.setOnMouseOut();
	// Au départ, réduire a l'en-tete ou afficher le contenu
	if (affichericones && present)
	{
		this.absent();
	}
	else
	{
		this.present();
	}
	//this.cacher();
}

ImgGallerie.prototype = {
	absentImgUrl: "./img/panier/basket_delete.png",
	presentImgUrl: "./img/panier/basket_add.png",
	/** Couleur de fond du span englobant les icones @type String */
	iconBackground: "transparent",
	/** Icone par défaut représentant un vide */
	emptyImgUrl: "./img/vign/empty.gif",
	

	/** Initialiser la structure HTML */
	setStructure: function (affichericones)
	{
		// Réorganiser l'en-tete : ajouter les images devant
		this.box.innerHTML = "";
		this.box.appendChild(this.lien);
		this.lien.href = this.adresse;
		this.lien.appendChild(this.imgToShow);
		this.imgToShow.src = this.srcimg;
		if(!this.isFolder)
			this.imgToShow.className = this.vignette.className;
		if(this.isFolder)
			this.lien.innerHTML += "<br />"+this.folderName;
		
		if(affichericones)
		{
			this.box.appendChild(this.icon);
			this.icon.appendChild(this.presentImg);
			this.presentImg.src = this.presentImgUrl;
			this.icon.appendChild(this.absentImg);
			this.absentImg.src = this.absentImgUrl;
		}
	},

	/** Initialiser l'apparence */
	setStyle: function (affichericones)
	{
		// Les icones
		this.box.lastChild.style.cursor = "pointer";
		if(affichericones)
		{
			if(!this.isFolder)
			{
				this.icon.style.background = this.iconBackground;
				this.icon.style.cursor = "pointer";
				this.icon.style.left = "1px";
				this.icon.style.top = "-16px";
				this.icon.style.position = "relative";
				this.icon.style.margin = "0px";
				this.icon.style.verticalAlign = "middle";
			}
			else
			{
				this.icon.style.background = this.iconBackground;
				this.icon.style.cursor = "pointer";
				this.icon.style.left = "1px";
				this.icon.style.top = "-16px";
				this.icon.style.position = "relative";
				this.icon.style.margin = "0px";
				this.icon.style.verticalAlign = "middle";
			}
		}
	},

	/** Définir la réaction au clic sur l'icone */
	setOnClick: function ()
	{
		var current = this;
		this.icon.onclick = function ()
		{
			if (current.presentImg.style.display == "none")
			{
				current.present();
				if(!current.isFolder)
					ImagePanier("panier_photo", "suppr", current.id, current.gallerie_nom);
				else
					ImagePanier("panier_photo", "suppralbum", current.id, current.gallerie_nom);
			}
			else
			{
				current.absent();
				if(!current.isFolder)
					ImagePanier("panier_photo", "add", current.id, current.gallerie_nom);
				else
					ImagePanier("panier_photo", "addalbum", current.id, current.gallerie_nom);
			}
		}
	},
	
	/** Définir la réaction au survol 
	setOnMouseOver: function ()
	{
		var current = this;
		this.box.onmouseover = function ()
		{
			current.montrer();
		}
	},*/
	
	/** Définir la réaction à la sortie de la sourie
	setOnMouseOut: function ()
	{
		var current = this;
		this.box.onmouseout = function ()
		{
			current.cacher();
		}
	},*/
	
	/** Montrer le corps (les infos) */
	montrer: function ()
	{
		this.icon.style.display = "";
	},

	/** Cacher le corps */
	cacher: function ()
	{
		this.icon.style.display = "none";
	},
	
	/** Montrer le corps (les infos) */
	present: function ()
	{
		this.presentImg.style.display = "";
		this.absentImg.style.display = "none";
	},

	/** Cacher le corps */
	absent: function ()
	{
		this.presentImg.style.display = "none";
		this.absentImg.style.display = "";
	},

	/** Vérifier que la boite a uniquement 2 enfants div */
	checkStructure: function (idBox)
	{
		if (this.box == null || this.box.nodeName != "LI")
		{
			Log.error("Element <li id='"+ idBox +"'> non trouvé dans le document");
		}
		else
		{
			Element.cleanWhiteSpace(this.box);
			var spans = Element.getChildElements(this.box, "span");
			var children = this.box.childNodes;
			var msg = new Array();
			if (spans.length != 3)
			{
				msg.push("doit contenir 3 span ("+spans.length+" trouvé)");
			}
			if ((children.length - spans.length) > 0)
			{
				msg.push("doit contenir seulement 3 span ("+ (children.length - spans.length) +" autres éléments trouvés)");
			}
			if (msg.length > 0)
			{
				Log.error("Element " + idBox + "\n" + msg.join("\n"));
			}
		}
	}
}

ImgGallerie.prototype.constructor = ImgGallerie;

function ImagePanier(idPanier, action, idImage, nomGallerie)
{
	var panier = document.getElementById(idPanier);
	this.url = "mod/panier_photo.php5";
	this.params = "action="+action;
	if(nomGallerie!="")
	{
		this.params+="&galerie_nom="+nomGallerie;
	}
	if(idImage!="")
	{
		this.params+="&photo="+idImage+"";
	}
	request = new XMLHttpRequest();
	request.open("POST",this.url);
	request.onreadystatechange = function ()
	{
		if(request.readyState == 4 && request.status == 200)
		{
			results=request.responseText.split("[causes]");
			if(results[0].replace(/^\s+/, '') == "OK")
			{
				reponse = results[1].split("[liste]");
				var nombre = reponse[1].split("[details]");
				if(nombre[0] == "Nombre")
				{
					panier.innerHTML = "<table class=\"login\"><tr><th><a href=\"page-mod__panier.html\">Contenu du Panier</a></th></tr><tr><td>"+nombre[1]+" photos sélectionnées</td></tr>"+(nombre[1]!=0 ? "<tr><td><a onclick=\"ImagePanier('panier_photo', 'make_zip', '','');\">Télécharger le panier <img src=\"./img/panier/picture_save.png\" border=\"0\" alt=\"Téléchargez le panier\" /></a></td></tr>":"")+"</table>";
				}
				else
				{
					var contenu="<tr><td><a href=\""+nombre[1]+"\">Téléchargez le fichier <img src=\"./img/panier/picture_save.png\" border=\"0\" alt=\"Téléchargez le panier\" /></a></td></tr>";
					panier.innerHTML = "<table class=\"login\"><tr><th>Fichier créé:</th></tr>"+contenu+"</table>";
				}
			}
			else
			{
				panier.innerHTML = "<table class=\"login\"><tr><th><a href=\"page-mod__panier.html\">Panier</a></th></tr>";
				panier.innerHTML += "<tr><td>Erreur:"+results[1]+"</td></tr></table>";
			}
		}
	}
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");
	request.send(params);
}
