// Diapoo - 1.1 - 6 Juin 2007
// Librairie sous license MIT

// Copyright (c) 2007 Antoine Pultier (http://www.blogjaune.fr/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

liste_objets = [];
function Diaporama(nom_diapositives, reglages){
	this.nom_diapositives = nom_diapositives;
	this.reglages = reglages;//Nom de la diapositive (peut aussi etre un objet)
	
	//Definition des variables en fonction des reglages donnes.
	if (!this.reglages.nom_diaporama){	this.reglages.nom_diaporama = "diaporama";	};//Nom du diaporama (utile quand il y en plusieurs)
	if (!this.reglages.delai_actions){	this.reglages.delai_actions = 2500;		};//Delai en mini-secondes, avant que les actions se masquent
	if (!this.reglages.temps_diapo){	this.reglages.temps_diapo = 6000;		};//Delai en mini-secondes, avant que la diapositive suivante s'affiche
	if (!this.reglages.depart_auto){	this.reglages.depart_auto = false;		;};//Si le diaporama commence tout seul ou non
	if (!this.reglages.duree_transition){	this.reglages.duree_transition = 1.0;		;};//Duree de la transition en mini secondes
	if (!this.reglages.chemin_boutons){	this.reglages.chemin_boutons = '';		;};//Chemin des boutons
	//if (!this.reglages.transition_a){	this.reglages.transition_a = "Appear";		;};//Duree de la transition en mini secondes
	//if (!this.reglages.transition_m){	this.reglages.transition_m = "DropOut";		;};//Duree de la transition en mini secondes
	
	//Sauvegardons l'objet :)
	liste_objets[this.reglages.nom_diaporama] = this;
	//necessaire au fonctionnement du script
	this.liste_diapositives = [];
	this.diapositive_affiche = null;
	this.delai = null;
	
	this.initialiser();
}

Diaporama.prototype.initialiser = function(){
	//On masque les diapositives, et on les ajoutes a la liste
	diapositives = $(this.nom_diapositives).getElementsByClassName('diapo');
	for (i=0;i<diapositives.length;i++){
		Element.hide(diapositives[i]);
		this.liste_diapositives.push(diapositives[i]);
	}
	
	//Barre des actions
	new Insertion.Bottom(this.nom_diapositives, '<div class="actions" id="'+this.reglages.nom_diaporama+'_actions"></div>');
	Element.hide(this.reglages.nom_diaporama+'_actions');//Barre d'actions masquee au debut
	this.actions_affiche = false;
	
	//creation des boutons
	bouton_revenir = this.bouton('revenir','Revenir au debut du diaporama')
	Element.hide(bouton_revenir);
	
	bouton_prec = this.bouton('precedente','Diapositive precedente');
	Element.hide(bouton_prec);
	
	bouton_start = this.bouton('demarrer','Demarrer le diaporama');
	Element.hide(bouton_start);
	
	bouton_pause = this.bouton('pause','Mettre en pause le diaporama')
	Element.hide(bouton_pause);
	
	bouton_stop = this.bouton('stop','Stopper le diaporama')
	Element.hide(bouton_stop);
	
	bouton_suiv = this.bouton('suivante','Diapositive suivante');
	Element.hide(bouton_suiv);
	
	//masque automatiquement les actions
	//eval("objet_fonction = function(){clearTimeout(liste_objets['"+this.reglages.nom_diaporama+"'].delai);if (!Element.visible('"+this.reglages.nom_diaporama+"_actions')&&liste_objets['"+this.reglages.nom_diaporama+"'].actions_affiche==true){Effect.Appear('"+this.reglages.nom_diaporama+"_actions');};liste_objets['"+this.reglages.nom_diaporama+"'].delai = setTimeout(\"if(liste_objets['"+this.reglages.nom_diaporama+"'].diapositive_affiche!=null){Effect.Fade('"+this.reglages.nom_diaporama+"_actions')}\","+this.reglages.delai_actions+");};");
	//new Event.observe(document.body,'mousemove',objet_fonction);
	
	//pour le css des diapositives
	$(this.nom_diapositives).addClassName('diaporama_js');
	
	this.diapositive_affiche=-1;
	if (this.reglages.depart_auto==true){
		this.demarrer();
	}
	
	//Creer le bouton pour demarrer le diaporama
	if (this.reglages.bouton){
		eval("function_click = function(){liste_objets['"+this.reglages.nom_diaporama+"'].demarrer();}");
		Event.observe(this.reglages.bouton, 'click', function_click);
		Element.show(this.reglages.bouton);
	}//else{
		//new Insertion.Before(this.nom_diapositives, '<a href="#" id="'+this.reglages.nom_diaporama+'_bouton" onclick="liste_objets[\''+this.reglages.nom_diaporama+'\'].demarrer();return false;">Démarrer le diaporama</a>');
	//}
}

Diaporama.prototype.demarrer = function(){
	this.actions_affiche = true;
	this.suivante();
	this.m_lecture = setInterval("liste_objets['"+this.reglages.nom_diaporama+"'].lecture();",this.reglages.temps_diapo);
	this.lecture_automatique = true;
	//Element.hide(this.reglages.nom_diaporama+'_bouton_demarrer');
	//Element.show(this.reglages.nom_diaporama+'_bouton_pause');
}

Diaporama.prototype.lecture = function(){
	if (this.diapositive_affiche<=this.liste_diapositives.length-2){
		this.suivante();
	}else{
		this.pause_lecture();
	}
}

Diaporama.prototype.stop = function(){
	this.diapositive_affiche=-1;
	this.masquer();
	this.pause_lecture();
	this.affichage_boutons();
}

Diaporama.prototype.pause = function(){
	this.pause_lecture();
	this.affichage_boutons();
}

Diaporama.prototype.suivante = function(){
	this.diapositive_affiche+=1;
	this.masquer();
	this.affichage_boutons();
}

Diaporama.prototype.precedente = function(){
	this.diapositive_affiche-=1;
	this.masquer();
	this.pause_lecture();
	this.affichage_boutons();
}

Diaporama.prototype.revenir = function(){
	this.diapositive_affiche=0;
	this.masquer();
	this.pause_lecture();
	this.affichage_boutons();
}

Diaporama.prototype.bouton = function(id, nom){
	new Insertion.Bottom(this.reglages.nom_diaporama+'_actions', '<a href="#" id="'+this.reglages.nom_diaporama+"_bouton_"+id+'"><img src="'+this.reglages.chemin_boutons+id+'.png" alt="'+nom+'"/></a>');
	eval("objet_fonction = function(event){ liste_objets['"+this.reglages.nom_diaporama+"']."+id+"(); return false; };");
	bouton = $(this.reglages.nom_diaporama+"_bouton_"+id);
	bouton.onclick = objet_fonction;
	return bouton;
}

Diaporama.prototype.masquer = function(){
	this.actions_affiche = false;
	o = 0;
	for (i=0;i<this.liste_diapositives.length;i++){
		if (Element.visible(this.liste_diapositives[i])&&i!=this.diapositive_affiche){
			//eval("new Effect."+this.reglages.transition_m+"(liste_objets['"+this.reglages.nom_diaporama+"'].liste_diapositives["+i+"],{duration:"+this.reglages.duree_transition+"})");
			new Effect.DropOut(this.liste_diapositives[i],{duration:this.reglages.duree_transition});
			o=1;
		}else if(this.diapositive_affiche==i&&!Element.visible(this.liste_diapositives[i])){
			new Effect.Appear(this.liste_diapositives[i],{duration:this.reglages.duree_transition});
			//setTimeout("new Effect."+this.reglages.transition_a+"(liste_objets['"+this.reglages.nom_diaporama+"'].liste_diapositives["+i+"],{duration:"+this.reglages.duree_transition+"})",o*this.reglages.duree_transition*1000);
		}
	}
	setTimeout("liste_objets['"+this.reglages.nom_diaporama+"'].actions_affiche=true",this.reglages.duree_transition*1000+100);
}

Diaporama.prototype.affichage_boutons = function(){
	o = this.diapositive_affiche;
	demarrer=$(this.reglages.nom_diaporama+'_bouton_demarrer');
	stop=$(this.reglages.nom_diaporama+'_bouton_stop');
	precedente=$(this.reglages.nom_diaporama+'_bouton_precedente');
	suivante=$(this.reglages.nom_diaporama+'_bouton_suivante');
	pause=$(this.reglages.nom_diaporama+'_bouton_pause');
	revenir=$(this.reglages.nom_diaporama+'_bouton_revenir');
	Element.hide(demarrer);
	Element.hide(stop);
	Element.hide(precedente);
	Element.hide(suivante);
	Element.hide(pause);
	Element.hide(revenir);
	if (o==0){
		Element.show(suivante);
		Element.show(stop);
		if (this.lecture_automatique){
			Element.show(pause);
		}else{
			Element.show(demarrer);
		}
	}else if(o==this.liste_diapositives.length-1){
		this.pause_lecture();
		Element.show(precedente);
		Element.show(stop);
		Element.show(revenir);
	}else if(o==-1){
		this.pause_lecture();
		Element.show(suivante);
		Element.show(demarrer);
	}else{
		Element.show(precedente);
		Element.show(stop);
		Element.show(suivante);
		if (this.lecture_automatique){
			Element.show(pause);
		}else{
			Element.show(demarrer);
		}
	}
}

Diaporama.prototype.pause_lecture = function(){
	clearTimeout(this.m_lecture);
	this.lecture_automatique = false;
}