// GaleriePhotos.js
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Historique de mise Ã  jour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 2009-01-11 : Vincent
//				CrÃ©ation du script


function GaleriePhotos() {
	this.init = GaleriePhotos_Init;
}



// CrÃ©e un nouvel objet GaleriePhotos
function GaleriePhotos_Init ( eThumbnails, oConfig ) {
	// PropriÃ©tÃ©s
	this.config  = oConfig;
	this.photos  = new Array();
	this.photoCourante = null;
	this.visible = false;
	this.fade = this.config.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.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.ajouterPhotos   = GaleriePhotos_AjouterPhotos;
		
		this.trackView       = GaleriePhotos_TrackView;
		
		
		// DÃ©finit la langue si possible
		var eBody = document.documentElement.getElementsByTagName("body")[0];
		if ( eBody.className.length )
			this.langue = eBody.className.substring(0, 2);
			
		
		this.ajouterPhotos();
	}
}



// CrÃ©e la galerie
function GaleriePhotos_Creer () {
	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 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Ã©ee 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.fond.style.opacity  = ".85";
	this.fond.style.filter   = "alpha(opacity=85)";
	
	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.innerHTML = 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();
		this.trackView("image", "open");
	} else
		this.contenant.style.display = "none";
}


// Affiche l'image demandÃ©e
function GaleriePhotos_AfficherPhoto(idPhoto) {
	
	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;
			this.btPrecedent.style.opacity = ".5";
			this.btPrecedent.style.filter = "alpha(opacity=50)";
		} else {
			this.btPrecedent.className = this.btPrecedent.className.replace(/GaleriePhotos_Inactif/gi, "");
			this.btPrecedent.style.opacity = "1";
			this.btPrecedent.style.filter = "alpha(opacity=100)";
		}
		
		
		// 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;
			this.btSuivant.style.opacity = ".5";
			this.btSuivant.style.filter = "alpha(opacity=50)";
		} else {
			this.btSuivant.className = this.btSuivant.className.replace(/GaleriePhotos_Inactif/gi, "");
			this.btSuivant.style.opacity = "1";
			this.btSuivant.style.filter = "alpha(opacity=100)";
		}
		
		
		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";
	}
	
	if ( !this.visible ) this.afficher();
	
	//this.trackView("image", "view");
}



// Ajoute les photos Ã  la galerie
function GaleriePhotos_AjouterPhotos() {
	var aImg = this.thumbnails.getElementsByTagName("img");
	for ( var cImg = 0; cImg < aImg.length; cImg++ ) {
		aImg[cImg].parentNode.galerieObject = this;
		aImg[cImg].parentNode.photoID = cImg;
		aImg[cImg].parentNode.onclick = function() { this.galerieObject.afficherPhoto(this.photoID); };
		this.photos.push({src: aImg[cImg].src.replace(/_thumb/gi, ""), description: aImg[cImg].parentNode.getElementsByTagName("p")[0].innerHTML });
	}
	
	this.creer();
}





// Tracks picture viewing with GoogleAnalytics
function GaleriePhotos_TrackView(name, action) {
	var imageName = this.photos[this.photoCourante].src;
	if ( imageName.indexOf("/") != -1 )
		imageName = imageName.substring(imageName.lastIndexOf("/") + 1);
		
	try {
		pageTracker._trackEvent(name, action, this.photos[this.photoCourante].src);
	} catch (err) {}
}
