// GalerieSimple.js
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Historique de mise à jour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 2008-04-18 : Vincent
//				Création du script

function nouvelleGalerie() {
	var proprietes = new Object();
		proprietes.galerie       = "maGalerie";            // String  - Identifiant du contenant des images | OBLIGATOIRE
		proprietes.btSuivant     = "maGalerie-suivant";    // String  - Identifiant du bouton Précédent | OBLIGATOIRE
		proprietes.btPrecedent   = "maGalerie-precedent";  // String  - Identifiant du bouton Suivant | OBLIGATOIRE
		proprietes.imageCourante = "maGalerie-current";    // String  - Identifiant de l'élément ou sera indiqué le numéro de l'image actuellement affichée | FACULTATIF
		proprietes.imagesTotal   = "maGalerie-count";      // String  - Identifiant de l'élément ou sera indiqué le nombre total d'images | FACULTATIF
		proprietes.boucler       = true                   // Boolean - Si la navseq de la galerie doit boucler | FACULTATIF (défaut: false)
		proprietes.startIndex    = 0;                      // Number  - Position de l'image à afficher au chargement de la galerie | FACULTATIF (défaut: 0)	
	new GalerieSimple(proprietes); }
	
if ( window.addEventListener ) window.addEventListener("load", nouvelleGalerie, false);
else if ( window.attachEvent ) window.attachEvent("onload", nouvelleGalerie);


function GalerieSimple( obj ) {
	// Ajoute les propriétés de l'objet passée en argument au nouvel objet GalerieSimple
	for ( var prop in obj ) this[prop] = obj[prop];
	
	// Redéfinit les propriétés reliées aux éléments HTML
	if ( this.galerie ) this.galerie = document.getElementById(this.galerie);
	if ( this.btSuivant ) this.btSuivant = document.getElementById(this.btSuivant);
	if ( this.btPrecedent ) this.btPrecedent = document.getElementById(this.btPrecedent);
	if ( this.imageCourante ) this.imageCourante = document.getElementById(this.imageCourante);
	if ( this.imagesTotal ) this.imagesTotal = document.getElementById(this.imagesTotal);
		
	// Propriétés
	this.images;
	this.current = this.startIndex ? this.startIndex : 0;
	
	// Méthodes
	this.init                = GalerieSimple_Init;
	this.precedent           = GalerieSimple_Precedent;
	this.suivant             = GalerieSimple_Suivant;
	this.afficherImage       = GalerieSimple_AfficherImage;
	this.ecrireImageCourante = GalerieSimple_EcrireImageCourante;
	this.ecrireImagesTotal   = GalerieSimple_EcrireImagesTotal;
	this.activerElement      = GalerieSimple_ActiverElement;
		
	// Lance le programme si tous les éléments semblent en place
	if ( this.galerie && this.btSuivant && this.btPrecedent )
		this.init();
	else {
		var msg = "Erreur: GalerieSimple\nImpossible d'initialiser la galerie...\n";
		if ( !this.galerie ) msg += "- 'galerie' n'est pas définit ou est nul\n";
		if ( !this.btPrecedent ) msg += "- 'btPrecedent' n'est pas définit ou est nul\n";
		if ( !this.btSuivant ) msg += "- 'btSuivant' n'est pas définit ou est nul\n";
		delete this;
		alert(msg);
	}
}


function GalerieSimple_Init() {
	this.images = this.galerie.getElementsByTagName("img");
	
	// Masque toutes les images de la galerie
	for ( var cImg = 0; cImg < this.images.length; cImg++ )
		this.images[cImg].style.display = "none";
	
	// Bouton Précédent
	this.btPrecedent.actif     = true;
	this.btPrecedent.refObject = this;
	this.btPrecedent.onclick   = function() { this.refObject.precedent(); return false; };
	
	// Bouton Suivant
	this.btSuivant.actif     = true;
	this.btSuivant.refObject = this;
	this.btSuivant.onclick   = function() { this.refObject.suivant(); return false; };
	
	
	this.ecrireImagesTotal();
	this.afficherImage(this.current);
}


function GalerieSimple_EcrireImageCourante() {
	if ( this.imageCourante )
		this.imageCourante.innerHTML = String("Image " + (this.current + 1));
}


function GalerieSimple_EcrireImagesTotal() {
	if ( this.imagesTotal )
		this.imagesTotal.innerHTML = String(this.images.length);
}


function GalerieSimple_Precedent() {
	if ( this.btPrecedent.actif ) {
		this.activerElement(this.btSuivant, true);
		this.afficherImage(this.current - 1);
	}
}


function GalerieSimple_Suivant() {
	if ( this.btSuivant.actif ) {
		this.activerElement(this.btPrecedent, true);
		this.afficherImage(this.current + 1);
	}
}


function GalerieSimple_ActiverElement(element, etat) {
	element.actif = etat;
	if ( etat ) {
		element.style.cursor  = "pointer";
		element.style.opacity = "1";
		element.style.filter  = "alpha(opacity=100)";
	} else {
		element.style.cursor  = "default";
		element.style.opacity = ".4";
		element.style.filter  = "alpha(opacity=40)";
	}
}


function GalerieSimple_AfficherImage( imageIndex ) {
	// Si la galerie doit se faire en boucle, nous redéfinissons la variable si nous atteignons les limites
	if ( this.boucler ) {
		if ( imageIndex == -1 ) imageIndex = this.images.length - 1;
		if ( imageIndex == this.images.length ) imageIndex = 0;
	} else {
		if ( imageIndex == 0 ) this.activerElement(this.btPrecedent, false);
		if ( imageIndex == this.images.length - 1 ) this.activerElement(this.btSuivant, false);
	}
	
	if ( this.current != imageIndex )
		this.images[this.current].style.display = "none";
		
	this.current = imageIndex;
	this.images[this.current].style.display = "block";
	
	this.ecrireImageCourante();
}
