/*-----------------------------------------------------------------------------------------------*/
//
// slideShow.js v1.0
//
// Copyright (c) 2008 Agence Clark (http://www.agence-clark.com)
//
/*-----------------------------------------------------------------------------------------------*/

var slideShow = Class.create();
slideShow.prototype =
{
	initialize: function(slideConteneur,slideContenu,slideBloc) {
		// Récupération des variables optionnelles
		var options = Object.extend({slideConteneur:slideConteneur,slideContenu:slideContenu,slideBloc:slideBloc}, arguments[3] || {});
		
		if (!options.slideDuree) 			options.slideDuree = 1;			// Durée des transitions
		if (!options.slideTempo) 			options.slideTempo = 1;			// Temporisation du slideshow
		if (!options.slideControl) 			options.slideControl = false;	// Pas de controle lecture/pause
		if (!options.slideControl2) 		options.slideControl2 = false;	// Pas de controle precedent/suivant
		if (!options.slideIndice) 			options.slideIndice = false;	// Pas d'index
		if (!options.slideAutostart) 		options.slideAutostart = false;	// Lancement automatique du slideshow
		if (options.slideAutostart) 		options.slideLecture = true; else options.slideLecture = false;	// Lancement automatique du slideshow
		
		// Récupération des variables
		this.slideConteneur  = slideConteneur;			// ID du conteneur général
		this.slideContenu  = slideContenu;				// ID du conteneur des contenus
		this.slideBloc  = slideBloc;					// CSS des blocs de contenus + de nav
		this.slideNav  = options.slideNav;				// ID du conteneur de nav
		this.slideDuree  = options.slideDuree;			// Durée des transitions
		this.slideTempo  = options.slideTempo;			// Temporisation du slideshow
		this.slideControl  = options.slideControl;		// Control lecture/pause
		this.slideControl2  = options.slideControl2;	// Control precedent/suivant
		this.slideIndice  = options.slideIndice;		// Indices
		this.slideAutostart  = options.slideAutostart;	// Lancement automatique du slideshow
		this.slideLecture  = options.slideLecture;		// Etat de la lecture
		
		// Initialisation des variables
		this.slidePosition = 0;															// Position du curseur
		this.slideNbBloc = $$('#'+this.slideContenu+' .'+this.slideBloc).size();		// Nombre d'éléments dans la liste

		//Mise en place du controle lecture/pause
		if(this.slideControl == true){
			$(this.slideConteneur).insert({top:'<div id="control_'+this.slideConteneur+'" class="control_'+this.slideConteneur+'"><a href="javascript:void(0);" class="lecture" id="lecture_'+this.slideConteneur+'">Lecture</a><a href="javascript:void(0);" class="pause" id="pause_'+this.slideConteneur+'">Pause</a></div>'});
			this.slideControlEvt = new Array(
				{element:'lecture_'+this.slideConteneur,evt:this.lecturePE.bindAsEventListener(this, 'lecture_'+this.slideConteneur)},
				{element:'pause_'+this.slideConteneur,evt:this.pausePE.bindAsEventListener(this, 'pause_'+this.slideConteneur)}
			);
			this.creerPauseEvt();
		}

		//Mise en place du controle precedent/suivant
		if(this.slideControl2 == true){
			$(this.slideConteneur).insert({top:'<div id="control2_'+this.slideConteneur+'" class="control2_'+this.slideConteneur+'"><a href="javascript:void(0);" class="precedent" id="precedent_'+this.slideConteneur+'">precedent</a><a href="javascript:void(0);" class="suivant" id="suivant_'+this.slideConteneur+'">suivant</a></div>'});
			this.slideControl2Evt = new Array(
				{element:'precedent_'+this.slideConteneur,evt:this.precedent.bindAsEventListener(this, 'lecture_'+this.slideConteneur)},
				{element:'suivant_'+this.slideConteneur,evt:this.suivant.bindAsEventListener(this, 'pause_'+this.slideConteneur)}
			);
			this.creerControl2Evt();
		}

		// Mise en place des indices
		if(this.slideIndice == true){
			this.liIndice = '';
			for(i=0;i<this.slideNbBloc;i++){
				this.liIndice+= '<li><a href="javascript:void(0);" id="indice_'+this.slideConteneur+'_'+i+'">'+(i+1)+'</a></li>';
			}
			$(this.slideConteneur).insert({top:'<div id="indice_'+this.slideConteneur+'" class="indice_'+this.slideConteneur+'"><ul>'+this.liIndice+'</ul></div>'});
			
			this.slideTabIndice = new Array();
			
			for(i=0;i<this.slideNbBloc;i++){
				this.slideTabIndice.push({element:'indice_'+this.slideConteneur+'_'+i,evt:this.affiche.bindAsEventListener(this, i,'indice_'+this.slideConteneur+'_'+i)});
			}
			this.creerIndiceEvt();
		}
		
		// Lancement du slideshow
		if(this.slideAutostart == true){
			this.lecturePE();
			this.slideLecture = true;
		}else{
			if(this.slideControl == true){
				$('pause_'+this.slideConteneur).addClassName('on');
			}
		}
		
		// Bloc de nav
		if(this.slideNav){
			this.tabNav = $$('#'+this.slideNav+' .'+this.slideBloc);
			this.tabNav.first().addClassName('on');
			this.slideNavEvt = new Array();
			this.slideNavEvt2 = new Array();
			this.slideNavEvt3 = new Array();
			for(i=0;i<this.slideNbBloc;i++){
				this.tabNav[i].id = 'nav_'+this.slideConteneur+'_'+i;
				this.slideNavEvt.push({element:'nav_'+this.slideConteneur+'_'+i,evt:this.affiche.bindAsEventListener(this, i,'nav'+this.slideConteneur+'_'+i)});
				this.slideNavEvt2.push({element:'nav_'+this.slideConteneur+'_'+i,evt:this.ajouteCssNav.bindAsEventListener(this, i,'nav'+this.slideConteneur+'_'+i)});
				this.slideNavEvt3.push({element:'nav_'+this.slideConteneur+'_'+i,evt:this.enleveCssNav.bindAsEventListener(this, i,'nav'+this.slideConteneur+'_'+i)});
			}
			this.creerNavEvt();
		}

		// Bloc indice
		if(this.slideIndice){
			this.tabIndice = $$('#indice_'+this.slideConteneur+' li a');
			this.tabIndice.first().addClassName('on');
		}

		// Bloc de contenu
		this.tabContenu = $$('#'+this.slideContenu+' .'+this.slideBloc);
		
		// Calcul Z-index initial;
		this.calcZindex();
	},
	
	creerIndiceEvt : function(){
		for(i=0;i<this.slideNbBloc;i++){
			Event.observe(this.slideTabIndice[i].element, 'click', this.slideTabIndice[i].evt);
		}
	},

	supprimerIndiceEvt : function(){
		for(i=0;i<this.slideNbBloc;i++){
			Event.stopObserving(this.slideTabIndice[i].element, 'click', this.slideTabIndice[i].evt);
		}
	},

	creerNavEvt : function(){
		for(i=0;i<this.slideNbBloc;i++){
			Event.observe(this.slideNavEvt[i].element, 'click', this.slideNavEvt[i].evt);
			Event.observe(this.slideNavEvt2[i].element, 'mouseover', this.slideNavEvt2[i].evt);
			Event.observe(this.slideNavEvt3[i].element, 'mouseout', this.slideNavEvt3[i].evt);
		}
	},
	
	supprimerNavEvt : function(){
		for(i=0;i<this.slideNbBloc;i++){
			Event.stopObserving(this.slideNavEvt[i].element, 'click', this.slideNavEvt[i].evt);
			Event.stopObserving(this.slideNavEvt2[i].element, 'mouseover', this.slideNavEvt2[i].evt);
			Event.stopObserving(this.slideNavEvt3[i].element, 'mouseout', this.slideNavEvt3[i].evt);
		}
	},
	
	creerPauseEvt : function(){
		Event.stopObserving(this.slideControlEvt[0].element, 'click', this.slideControlEvt[0].evt);
		Event.observe(this.slideControlEvt[1].element, 'click', this.slideControlEvt[1].evt);
	},
	
	creerLectureEvt : function(){
		Event.stopObserving(this.slideControlEvt[1].element, 'click', this.slideControlEvt[1].evt);
		Event.observe(this.slideControlEvt[0].element, 'click', this.slideControlEvt[0].evt);
	},
	
	creerControl2Evt : function(){
		for(i=0;i<this.slideControl2Evt.size();i++){
			Event.observe(this.slideControl2Evt[i].element, 'click', this.slideControl2Evt[i].evt);
		}
	},

	supprimerControl2Evt : function(){
		for(i=0;i<this.slideControl2Evt.size();i++){
			Event.stopObserving(this.slideControl2Evt[i].element, 'click', this.slideControl2Evt[i].evt);
		}
	},

	fade: function(id, pos){
		new Effect.Fade(id,{duration:this.slideDuree,
			beforeStart:function(){
				if(this.slideControl2){
					this.supprimerControl2Evt();
				}
				if(this.slidePosition < this.slideNbBloc-1){
					if(this.slideNav){
						this.rollNav(this.tabNav[this.slidePosition+1]);
					}
					if(this.slideIndice){
						this.rollIndice(this.tabIndice[this.slidePosition+1]);
					}
					this.slidePosition++;
				}else{
					this.slidePosition = 0;
					if(this.slideNav){
						this.tabNav.first().addClassName('on');
						this.tabNav.last().removeClassName('on');
					}
					if(this.slideIndice){
						this.tabIndice.first().addClassName('on');
						this.tabIndice.last().removeClassName('on');
					}
				}
			}.bind(this),
			afterFinish:function(){
				if(this.slideControl2){
					this.creerControl2Evt();
				}
				id.setStyle({
					zIndex:(id.style.zIndex-this.slideNbBloc),
					display:'block'
				});
				// Recalcule les index quand on revient au premier
				if(this.slidePosition==0){
					this.calcZindex();	
				}
			}.bind(this)
		});
	},

	rollNav: function(id){
		this.tabNav.each( function(e){
			e.removeClassName('on');					  
		});
		id.addClassName('on');
	},
	
	ajouteCssNav: function(id,i){
		if(this.slidePosition!=i){
			$('nav_'+this.slideConteneur+'_'+i).addClassName('on');
		}
	},

	enleveCssNav: function(id,i){
		if(this.slidePosition!=i){
			$('nav_'+this.slideConteneur+'_'+i).removeClassName('on');
		}
	},

	rollIndice: function(id, pos){
		this.tabIndice.each( function(e){
			e.removeClassName('on');					  
		});
		id.addClassName('on');
	},

	calcZindex: function(){
		// Traitement des blocs de contenu
		$$('#'+this.slideContenu+' .'+this.slideBloc).each(function(e,i){
			this.e = e;
			this.i = i;
			this.e.setStyle({zIndex:(99-this.i)});
		});
	},
	
	lecturePE: function(){
		if(this.slideControl == true){
			this.creerPauseEvt();
		}
		this.slideLecture = true;
		this.pe = new PeriodicalExecuter(
			function(){
				if(this.slidePosition < this.slideNbBloc){
					this.fade(this.tabContenu[this.slidePosition], this.slidePosition);
				}else{
					this.slidePosition = 0;
					this.calcZindex();
				}
			}.bind(this)
		, this.slideTempo);
		
		// CSS des boutons de controle
		if(this.slideControl){
			$('pause_'+this.slideConteneur).removeClassName('on');
			$('lecture_'+this.slideConteneur).addClassName('on');
		}
	},

	pausePE: function(){
		if(this.slideLecture==true){
			this.pe.stop();
		}
		this.slideLecture = false;
		// CSS des boutons de controle
		if(this.slideControl){
			$('pause_'+this.slideConteneur).addClassName('on');
			$('lecture_'+this.slideConteneur).removeClassName('on');
		}
		if(this.slideControl == true){
			this.creerLectureEvt();
		}
	},
	
	precedent: function(){
		if(this.slidePosition == 0){
			this.affiche(this, (this.slideNbBloc-1));
		}else{
			this.affiche(this, (this.slidePosition-1));
		}
	},

	suivant: function(){
		this.fade(this.tabContenu[this.slidePosition], this.slidePosition);
	},

	affiche: function(num,i){
		if(i != this.slidePosition){
			// Pause
			if(this.slideLecture == true){
				this.pausePE();
			}
			// Fade
			new Effect.Fade(this.tabContenu[this.slidePosition], {duration:this.slideDuree,
				beforeStart:function(){
					if(this.slideIndice){
						this.supprimerIndiceEvt();
					}
					if(this.slideNav){
						this.supprimerNavEvt();
					}
					if(this.slideControl2){
						this.supprimerControl2Evt();
					}
					this.tabContenu[this.slidePosition].setStyle({
						zIndex:100
					});
					this.tabContenu[i].setStyle({
						zIndex:99,
						display:'block'
					});
				}.bind(this),
				afterFinish:function(){
					if(this.slideIndice){
						this.creerIndiceEvt();
					}
					if(this.slideNav){
						this.creerNavEvt();
					}
					if(this.slideControl2){
						this.creerControl2Evt();
					}
					// Réaffectation des zIndex des blocs précédents
					this.tabContenu[i].previousSiblings().each(function(e,index){
						e.setStyle({
							zIndex:(99-this.slideNbBloc)+index+1,
							display:'block'
						});								   
					}.bind(this));
					// Réaffectation des zIndex des blocs suivants
					this.tabContenu[i].nextSiblings().each(function(e,index){
						e.setStyle({
							zIndex:98-index,
							display:'block'
						});								   
					}.bind(this));
					// Nouvelle position
					this.slidePosition = i;
					// Relance la lecture
					if(this.slideAutostart == true && this.slideLecture == false){
						this.lecturePE();
					}
				}.bind(this)
			});
			// Roll de la nav
			if(this.slideNav){
				this.rollNav(this.tabNav[i]);
			}
			// Roll des indices
			if(this.slideIndice){
				this.rollIndice(this.tabIndice[i]);
			}
		}
	}
};
