// GaleriePhotos.js





// Crée un nouvel objet GaleriePhotos
function GaleriePhotos ( xmlURI, eThumbnails, fade ) {
	// Propriétés
	this.xmlURI  = xmlURI;
	this.config  = new Object();
	this.photos  = new Array();
	this.photoCourante = null;
	this.visible = false;
	this.fade = fade ? true : false;
	this.thumbnails;
	this.contenant;
	this.fenetre;
	this.fond;
	this.blocPhoto;
	this.btPrecedent, this.btSuivant, this.btFermer;
	this.langue = new String();
	
	if ( this.thumbnails = document.getElementById(eThumbnails) ) {
		
		// Méthodes
		this.creer           = GaleriePhotos_Creer;
		this.creerThumbnails = GaleriePhotos_CreerThumbnails;
		this.creerContenant  = GaleriePhotos_CreerContenant;
		this.creerFenetre    = GaleriePhotos_CreerFenetre;
		this.creerFond       = GaleriePhotos_CreerFond;
		this.creerBoutons    = GaleriePhotos_CreerBoutons;
		this.creerContenu    = GaleriePhotos_CreerContenu;
		this.positionner     = GaleriePhotos_Positionner;
		this.afficher        = GaleriePhotos_Afficher;
		this.afficherPhoto   = GaleriePhotos_AfficherPhoto;
		this.parser          = GaleriePhotos_Parser;
		this.parserConfig    = GaleriePhotos_ParserConfig;
		this.parserPhotos    = GaleriePhotos_ParserPhotos;
		
		
		// Définit la langue si possible
		var eBody = document.documentElement.getElementsByTagName("body")[0];
		if (eBody.className.length)
		{
		    this.langue = eBody.className.substring(0, 2);
		}
		else
		{
		    var eForm = document.documentElement.getElementsByTagName("form")[0];
		    if (eForm.className.length)
		    {
		        this.langue = eForm.className.substring(0, 2);
		    }
		}
		
		new XMLLoader(xmlURI, "parser", this, true);
	}
}





// Crée la galerie
function GaleriePhotos_Creer () {
	this.creerThumbnails();
	this.creerContenant();
	this.creerFenetre();
	this.creerFond();
	this.creerBoutons();
	this.creerContenu();
	
	// Ajoute les listener à l'objet
	var me = this;
	if ( window.addEventListener ) {
		window.addEventListener("resize", function(event) { me.positionner(); }, false);
		window.addEventListener("scroll", function(event) { me.positionner(); }, false);
	} else if ( window.attachEvent ) {
		window.attachEvent("onresize", function(event) { me.positionner(); } );
		window.attachEvent("onscroll", function(event) { me.positionner(); } );
	}
}





// Crée la galerie en thumbnails
function GaleriePhotos_CreerThumbnails() {
	for ( var cPhotos = 0; cPhotos < this.photos.length; cPhotos++ ) {
		var photo = this.photos[cPhotos];
		
		var ancre = document.createElement("a");
			ancre.href = "#";
			ancre.oGaleriePhotos = this;
			ancre.idPhoto = cPhotos;
			ancre.onclick = function() {
					this.oGaleriePhotos.afficherPhoto(this.idPhoto);
					return false;
				};
		var img = document.createElement("img");
			img.src = photo.thumbnail;
		
		ancre.appendChild(img);
		this.thumbnails.appendChild(ancre);
	}
}





// Crée le contenant de la galerie
function GaleriePhotos_CreerContenant() {
	this.contenant = document.createElement("div");
	this.contenant.refObject      = this;
	this.contenant.className      = "GaleriePhotos_Contenant";
	this.contenant.style.display  = "none";
	this.contenant.style.position = "absolute";
	this.contenant.style.top      = "0";
	this.contenant.style.left     = "0";
	this.contenant.style.zIndex   = "10000000";
	
	document.documentElement.getElementsByTagName("body")[0].appendChild(this.contenant);
}





// Crée la fenêtre de la galerie
function GaleriePhotos_CreerFenetre() {
	this.fenetre = document.createElement("div");
	this.fenetre.className = "GaleriePhotos_Fenetre";
	this.fenetre.style.position = "absolute";
	this.fenetre.style.zIndex   = "10000002";
	
	this.contenant.appendChild(this.fenetre);
}





// Crée le fond du contenant (celui à qui on peut mettre une couleur ou une image en transparence, genre)
function GaleriePhotos_CreerFond() {
	this.fond = document.createElement("div");
	this.fond.className = "GaleriePhotos_Fond";
	this.fond.style.position = "absolute";
	this.fond.style.top      = "0";
	this.fond.style.left     = "0";
	this.fond.style.zIndex   = "10000001";
	
	this.contenant.appendChild(this.fond);
}





// Crée les boutons de navigation et de fermeture
function GaleriePhotos_CreerContenu() {
	this.blocPhoto = document.createElement("div");
	this.blocPhoto.className = "GaleriePhotos_BlocPhoto";
	
	
	for ( var cPhotos = 0; cPhotos < this.photos.length; cPhotos++ ) {
		var photo = this.photos[cPhotos]
		
		var conteneurImage = document.createElement("div");
			conteneurImage.style.display = "none";
		
		var img = document.createElement("img");
			img.idPhoto = cPhotos;
			img.src = photo.src;
			
		var description = document.createElement("p");
			description.appendChild(document.createTextNode(photo.description));
		
		
		conteneurImage.appendChild(img);
		conteneurImage.appendChild(description);
		
		this.blocPhoto.appendChild(conteneurImage);
	}
	
	this.fenetre.appendChild(this.blocPhoto);
}





// Crée les boutons de navigation et de fermeture
function GaleriePhotos_CreerBoutons() {
	// Bouton Fermer
	this.btFermer = document.createElement("a");
	this.btFermer.href = "#";
	this.btFermer.className = "GaleriePhotos_Fermer";
	this.btFermer.oGaleriePhotos = this;
	this.btFermer.onclick = function() { this.oGaleriePhotos.afficher(false); return false; };
	var btFermerSpan = document.createElement("span");
		btFermerSpan.appendChild(document.createTextNode(this.config.fermer ? this.config.fermer : libelle = "X"));
	this.btFermer.appendChild(btFermerSpan);
	
	// Bouton Précédent
	this.btPrecedent = document.createElement("a");
	this.btPrecedent.href = "#";
	this.btPrecedent.className = "GaleriePhotos_Precedent";
	this.btPrecedent.oGaleriePhotos = this;
	this.btPrecedent.onclick = function() {
			this.oGaleriePhotos.afficherPhoto(this.oGaleriePhotos.photoCourante - 1);
			return false;
		};
	var btPrecedentSpan = document.createElement("span");
		btPrecedentSpan.appendChild(document.createTextNode(this.config.precedent ? this.config.precedent : libelle = "«"));
	this.btPrecedent.appendChild(btPrecedentSpan);
	
	// Bouton Suivant
	this.btSuivant = document.createElement("a");
	this.btSuivant.href = "#";
	this.btSuivant.className = "GaleriePhotos_Suivant";
	this.btSuivant.oGaleriePhotos = this;
	this.btSuivant.onclick = function() {
			this.oGaleriePhotos.afficherPhoto(this.oGaleriePhotos.photoCourante + 1);
			return false;
		};
	var btSuivantSpan = document.createElement("span");
		btSuivantSpan.appendChild(document.createTextNode(this.config.suivant ? this.config.suivant : libelle = "»"));
	this.btSuivant.appendChild(btSuivantSpan);
	
	this.fenetre.appendChild(this.btFermer);
	this.fenetre.appendChild(this.btPrecedent);
	this.fenetre.appendChild(this.btSuivant);
}





// Positionne les éléments de la galerie
function GaleriePhotos_Positionner() {
	if ( this.visible ) {
		this.contenant.style.width  = String(document.documentElement.clientWidth) + "px";
		this.contenant.style.height = String(document.documentElement.clientHeight) + "px";
		this.contenant.style.top    = window.pageYOffset != undefined ? String(window.pageYOffset) + "px" : document.documentElement.scrollTop + "px";
		this.contenant.style.left   = window.pageXOffset != undefined ? String(window.pageXOffset) + "px" : document.documentElement.scrollLeft + "px";
		
		this.fond.style.width  = this.contenant.style.width;
		this.fond.style.height = this.contenant.style.height;
		
		this.fenetre.style.left = String((document.documentElement.clientWidth / 2) - (this.fenetre.offsetWidth / 2)) + "px";
		this.fenetre.style.top  = String((document.documentElement.clientHeight / 2) - (this.fenetre.offsetHeight / 2)) + "px";
	}
}





// Affiche ou masque la galerie
function GaleriePhotos_Afficher( etat ) {
	if ( etat == undefined ) etat = true;
	this.visible = etat;
	
	if ( this.visible ) {
		this.contenant.style.display = "block";
		this.positionner();
	} else
		this.contenant.style.display = "none";
}





// affiche une image demandée
function GaleriePhotos_AfficherPhoto(idPhoto) {
	if ( !this.visible ) this.afficher();
	
	if ( idPhoto >= 0 && idPhoto < this.photos.length ) {
		if ( this.photoCourante != null )
			this.blocPhoto.getElementsByTagName("div")[this.photoCourante].style.display = "none";
		
		this.photoCourante = idPhoto;
		var ePhoto = this.blocPhoto.getElementsByTagName("div")[this.photoCourante];
		
		// Ajoute un style inactif au bouton Précédent s'il s'agit de la première photo
		if ( this.photoCourante == 0 )
			this.btPrecedent.className = "GaleriePhotos_Inactif " + this.btPrecedent.className;
		else
			this.btPrecedent.className = this.btPrecedent.className.replace(/GaleriePhotos_Inactif/gi, "");
			
		
		// Ajoute un style inactif au bouton Suivant s'il s'agit de la dernière photo
		if ( this.photoCourante == this.photos.length - 1 )
			this.btSuivant.className = "GaleriePhotos_Inactif " + this.btSuivant.className;
		else
			this.btSuivant.className = this.btSuivant.className.replace(/GaleriePhotos_Inactif/gi, "");
			
		
		if ( this.fade ) {
			ePhoto.style.opacity = "0";
			ePhoto.style.filter = "alpha(opacity=0)";
			ePhoto.style.display = "block";
			var fx = new Fx.Styles(ePhoto, {duration: 400, wait:false});	
			fx.start({'opacity': 1});
		} else
			ePhoto.style.display = "block";
	}	
}









// Parsers XML ---------------------------------------- //

// Parser XML
function GaleriePhotos_Parser(xmlData) {
	// Lance la création de la galerie
	for ( var cTreeNodes = 0; cTreeNodes < xmlData.childNodes.length; cTreeNodes++ ) {
		var treeNode = xmlData.childNodes[cTreeNodes];
		if ( treeNode.nodeType == 1 ) {
			switch ( treeNode.nodeName ) {
				case "config":
					this.parserConfig(treeNode);
					break;
				case "photos":
					this.parserPhotos(treeNode);
					break;
			}
		}
	}
	
	// Lance la création de l'objet GaleriePhotos
	this.creer();
}





// Parse les configurations de la galerie
function GaleriePhotos_ParserConfig(xmlData)
{
	for ( var cNodes = 0; cNodes < xmlData.childNodes.length; cNodes++ ) {
		var configNode = xmlData.childNodes[cNodes];
		
		if ( configNode.nodeType == 1 ) {
			var configName  = configNode.attributes.getNamedItem("name").value;
			var configValue = configNode.attributes.getNamedItem("value").value;
			
			if ( this.langue.length && configName.indexOf("_") == configName.length - 3 ) {
				if ( configName.indexOf("_" + this.langue) == configName.length - 3 )
					this.config[configName.substring(0, configName.length - 3)] = configValue;
			} else
				this.config[configName] = configValue;
		}
	}
}





// Parse les photos de la galerie
function GaleriePhotos_ParserPhotos(xmlData) {
	for ( var cPhotos = 0; cPhotos < xmlData.childNodes.length; cPhotos++ ) {
		var photoNode = xmlData.childNodes[cPhotos];
		
		if ( photoNode.nodeType == 1 ) {
			var photo = new Object();
			
			for ( var cAttributes = 0; cAttributes < photoNode.attributes.length; cAttributes++ ) {
				var attr = photoNode.attributes[cAttributes];
				var attrName  = attr.name;
				var attrValue = attr.value;
				
				if ( this.langue.length && attrName.indexOf("_") == attrName.length - 3 ) {
					if ( attrName.indexOf("_" + this.langue) == attrName.length - 3 )
						photo[attrName.substring(0, attrName.length - 3)] = attrValue;
				} else
					photo[attrName] = attrValue;
			}
			
			this.photos.push(photo);
		}
	}
}
