/*-----------------------------------------------------------------------------------------------*/
//
// switchBloc.js v3.0
//
// Copyright (c) 2008 Agence Clark (http://www.agence-clark.com)
//
/*-----------------------------------------------------------------------------------------------*/

var SwitchBloc = Class.create();
SwitchBloc.prototype =
{
	initialize: function(SwiConteneur, SwiBloc, options){
		//-- Recuperation des variables optionnelles --//
		this.options = Object.extend({
			SwiConteneur : SwiConteneur, // Id du conteneur de la liste
			SwiBloc : SwiBloc, // Classe d'un bloc
			SwiListe: 'liste_' + SwiConteneur, // Id du conteneur des elements
			SwiBtPrecedent: 'precedent_' + SwiConteneur, // Id du bouton precedent
			SwiBtSuivant: 'suivant_' + SwiConteneur, // Id du bouton suivant
			SwiIndice: 'indices_' + SwiConteneur, // Id du conteneur des indices
			SwiBtLecture: 'lecture_' + SwiConteneur, // Id du bouton lecture
			SwiBtPause: 'pause_' + SwiConteneur, // Id du bouton pause
			
			SwiBt: true, // Affichage des boutons precedent/suivant
			SwiActIndice: false, // Affichage des indices			
			SwiDefilAuto: false, // Defilement automatique
			SwiDefilBts: false, // Affichage des boutons lecture/pause
			
			SwiDirection: 'horizontal', // Direction du defilement
			SwiClassOverlay: 'overlay', // Nom de la classe de l'overlay
			SwiClassInact: 'off', // Nom de la classe inactive
			SwiDuree: 1, // Duree de l'effet
			SwiDefilTempo: 3 // Temporisation du defilement
		}, options || {});

		//-- Initialisation des variables --//
		this.SwiPosition = 0; // Position du curseur
		this.SwiEcran = 1; // Ecran en cours
		this.SwiNbBloc = $$('#'+this.options.SwiListe+' .'+this.options.SwiBloc).size(); // Nombre d'elements dans la liste
		if(this.SwiNbBloc>0) //MODIFICATION UBIQUICK
		{
			this.options.SwiConteneurWidth = $(this.options.SwiConteneur).getWidth(); // Largeur du conteneur
			this.options.SwiConteneurHeight = $(this.options.SwiConteneur).getHeight();	 // Hauteur du conteneur
			this.options.SwiListeWidth = this.SwiNbBloc * $$('#'+this.options.SwiListe+' .'+this.options.SwiBloc)[0].getWidth(); // Largeur de la liste
			this.options.SwiListeHeight = this.SwiNbBloc * $$('#'+this.options.SwiListe+' .'+this.options.SwiBloc)[0].getHeight(); // Hauteur de la liste
			
			//-- Calcul du nombre d'ecran --//	
			if(this.options.SwiDirection=='vertical') this.SwiNbEcran = Math.ceil(this.options.SwiListeHeight/this.options.SwiConteneurHeight);
			else{
				this.SwiNbEcran = Math.ceil(this.options.SwiListeWidth/this.options.SwiConteneurWidth);
				//-- Affectation de la largeur de la liste --//
				$(this.options.SwiListe).setStyle({width:this.options.SwiListeWidth+'px'});
			}
			
			//-- Creation des indices --//
			if(this.options.SwiActIndice && this.SwiNbEcran > 1){
				this.SwiEvtIndices = new Array();
				new Insertion.Top(this.options.SwiIndice, '<ul id="'+this.options.SwiIndice+'_ul"></ul>');
				for(i=0;i<this.SwiNbEcran;i++){
					new Insertion.Bottom(this.options.SwiIndice+'_ul', '<li><a href="javascript:void(0);" title="'+(i+1)+'/'+this.SwiNbEcran+'" id="'+this.options.SwiIndice+'_'+(i+1)+'">'+(i+1)+'</a></li>');
					this.SwiEvtIndices.push({element:this.options.SwiIndice+'_'+(i+1),evt:this.move.bindAsEventListener(this, this.options.SwiIndice+'_'+(i+1))});
					Event.observe(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
				}
				$(this.options.SwiIndice).show();
			}
					
			//-- Mise en place des evenements --//
			if(this.options.SwiBt){
				this.SwiEvtPrec = this.precedent.bindAsEventListener(this);
				Event.observe($(this.options.SwiBtPrecedent), 'click', this.SwiEvtPrec);
				this.SwiEvtSuiv = this.suivant.bindAsEventListener(this);
				Event.observe($(this.options.SwiBtSuivant), 'click', this.SwiEvtSuiv);
			}
			if(this.options.SwiDefilAuto && this.options.SwiDefilBts){
				this.SwiDefil = true;
				this.SwiEvtLec = this.lecture.bindAsEventListener(this);
				this.SwiEvtPause = this.pause.bindAsEventListener(this);
				Event.observe($(this.options.SwiBtPause), 'click', this.SwiEvtPause);
			}
			
			//-- Creation de l'Overlay --//
			var overlay = '<div id="masque_'+this.options.SwiConteneur+'" class="'+this.options.SwiClassOverlay+'"></div>';
			new Insertion.Top(this.options.SwiConteneur, overlay);
			$('masque_'+this.options.SwiConteneur+'').setStyle({display: 'none',width: $(this.options.SwiConteneur).getWidth()+'px',height: $(this.options.SwiConteneur).getHeight()+'px'});
			
			//-- Gestion defilement automatique --//
			if(this.options.SwiDefilAuto) this.lecture();
			
			//-- Activation-Desactivation du bouton --//
			this.etat();
		}
	},
	
	before : function(){	
		//-- Gestion de l'overlay --//
		$('masque_'+this.options.SwiConteneur).show();
		new Effect.Fade($('masque_'+this.options.SwiConteneur),{duration:1.5*this.options.SwiDuree});
		
		//-- Desactivation des listeners --//
		if(this.options.SwiActIndice && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++) Event.stopObserving(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
		}
		if(this.options.SwiBt){
			Event.stopObserving($(this.options.SwiBtPrecedent), 'click', this.SwiEvtPrec);
			Event.stopObserving($(this.options.SwiBtSuivant), 'click', this.SwiEvtSuiv);
		}
	},
	
	after : function(type,ecran){		
		//-- Activation des listeners --//
		if(this.options.SwiActIndice && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++) Event.observe(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
		}
		if(this.options.SwiBt){
			Event.observe($(this.options.SwiBtPrecedent), 'click', this.SwiEvtPrec);
			Event.observe($(this.options.SwiBtSuivant), 'click', this.SwiEvtSuiv);
		}
		
		//-- Mise a jour des positions --//
		if(this.options.SwiDirection=='vertical'){
			if(type == 'prec') this.SwiPosition += this.options.SwiConteneurHeight;
			else if(type == 'suiv') this.SwiPosition -= this.options.SwiConteneurHeight;
			else if(type == 'indice_moins') this.SwiPosition -= (ecran - this.SwiEcran)*this.options.SwiConteneurHeight;
			else if(type == 'indice_plus') this.SwiPosition += (this.SwiEcran - ecran)*this.options.SwiConteneurHeight;
		}else{			
			if(type == 'prec') this.SwiPosition += this.options.SwiConteneurWidth;
			else if(type == 'suiv') this.SwiPosition -= this.options.SwiConteneurWidth;
			else if(type == 'indice_moins') this.SwiPosition -= (ecran - this.SwiEcran)*this.options.SwiConteneurWidth;
			else if(type == 'indice_plus') this.SwiPosition += (this.SwiEcran - ecran)*this.options.SwiConteneurWidth;
		}
		this.SwiEcran = ecran;
		this.etat();
	},
	
	etat : function(){
		if(this.options.SwiBt){
			//-- Bouton precedent --//
			if(Math.abs(this.SwiPosition) <= 0) $(this.options.SwiBtPrecedent).addClassName(this.options.SwiClassInact);
			else $(this.options.SwiBtPrecedent).removeClassName(this.options.SwiClassInact);
			
			//-- Bouton suivant --//
			if(this.options.SwiDirection=='vertical'){
				if(Math.abs(this.SwiPosition) >= (this.options.SwiListeHeight - this.options.SwiConteneurHeight)) $(this.options.SwiBtSuivant).addClassName(this.options.SwiClassInact);
				else $(this.options.SwiBtSuivant).removeClassName(this.options.SwiClassInact);
			}else{
				if(Math.abs(this.SwiPosition) >= (this.options.SwiListeWidth - this.options.SwiConteneurWidth)) $(this.options.SwiBtSuivant).addClassName(this.options.SwiClassInact);
				else $(this.options.SwiBtSuivant).removeClassName(this.options.SwiClassInact);
			}
		}
		
		//-- Indices --//
		if(this.options.SwiActIndice && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++){
				if(i+1 == this.SwiEcran) $(this.SwiEvtIndices[i].element).addClassName(this.options.SwiClassInact);
				else $(this.SwiEvtIndices[i].element).removeClassName(this.options.SwiClassInact);
			}
		}
		
		//-- Bouton lecture/pause --//
		if(this.options.SwiDefilAuto && this.options.SwiDefilBts){
			if(this.SwiDefil){
				$(this.options.SwiBtLecture).addClassName(this.options.SwiClassInact);
				$(this.options.SwiBtPause).removeClassName(this.options.SwiClassInact);
			}else{
				$(this.options.SwiBtPause).addClassName(this.options.SwiClassInact);
				$(this.options.SwiBtLecture).removeClassName(this.options.SwiClassInact);
			}
		}
	},
	
	move : function(cible){
		if(cible.target){
			var ecran = parseFloat((cible.target.title).substring(0,(cible.target.title).lastIndexOf('/')));
			//-- Arret PE --//
			if(this.options.SwiDefilAuto) this.pause();
		}
		else  var ecran = cible;
		
		if(this.options.SwiDirection == 'vertical'){			
			if(this.SwiEcran < ecran){
				new Effect.MoveBy($(this.options.SwiListe), -(ecran - this.SwiEcran)*this.options.SwiConteneurHeight, 0,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_moins',ecran)
				});
			}else if(this.SwiEcran > ecran){
				new Effect.MoveBy($(this.options.SwiListe), (this.SwiEcran - ecran)*this.options.SwiConteneurHeight, 0,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_plus',ecran)
				});
			}
		}else{
			if(this.SwiEcran < ecran){
				new Effect.MoveBy($(this.options.SwiListe), 0, -(ecran - this.SwiEcran)*this.options.SwiConteneurWidth,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_moins',ecran)
				});
			}else if(this.SwiEcran > ecran){
				new Effect.MoveBy($(this.options.SwiListe), 0, (this.SwiEcran - ecran)*this.options.SwiConteneurWidth,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_plus',ecran)
				});
			}
		}
	},
	
	precedent : function(){
		//-- Arret PE --//
		if(this.options.SwiDefilAuto) this.pause();
		
		//-- Traitement --//
		if(Math.abs(this.SwiPosition) > 0){
			if(this.options.SwiDirection=='vertical'){
				new Effect.MoveBy($(this.options.SwiListe), this.options.SwiConteneurHeight, 0,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'prec',this.SwiEcran-1)
				});
			}else{
				new Effect.MoveBy($(this.options.SwiListe), 0, this.options.SwiConteneurWidth,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'prec',this.SwiEcran-1)
				});
			}
		}
	},
	
	suivant : function(){
		//-- Arret PE --//
		if(this.options.SwiDefilAuto) this.pause();
		
		//-- Traitement --//
		if(this.options.SwiDirection=='vertical'){
			if(Math.abs(this.SwiPosition) < (this.options.SwiListeHeight - this.options.SwiConteneurHeight)){
				new Effect.MoveBy($(this.options.SwiListe), -this.options.SwiConteneurHeight, 0,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'suiv',this.SwiEcran+1)
				});
			}
		}else{
			if(Math.abs(this.SwiPosition) < (this.options.SwiListeWidth - this.options.SwiConteneurWidth)){
				new Effect.MoveBy($(this.options.SwiListe), 0, -this.options.SwiConteneurWidth,{
					duration:this.options.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'suiv',this.SwiEcran+1)
				});
			}
		}
	},
	
	lecture : function(){	
		this.SwiPE = new PeriodicalExecuter(
			function(){
				if(this.SwiEcran < this.SwiNbEcran) this.move(this.SwiEcran+1);
				else this.move(1);
			}.bind(this)
		, this.options.SwiDefilTempo);
		this.SwiDefil = true;
		
		//-- Gestion des listeners --//
		if(this.options.SwiDefilAuto && this.options.SwiDefilBts){
			Event.stopObserving($(this.options.SwiBtLecture), 'click', this.SwiEvtLec);
			Event.observe($(this.options.SwiBtPause), 'click', this.SwiEvtPause);
		}
		//-- Activation-Desactivation du bouton --//
		this.etat();
	},
	
	pause : function(){	
		this.SwiPE.stop();
		this.SwiDefil = false;
		
		//-- Gestion des listeners --//
		if(this.options.SwiDefilAuto && this.options.SwiDefilBts){
			Event.stopObserving($(this.options.SwiBtPause), 'click', this.SwiEvtPause);
			Event.observe($(this.options.SwiBtLecture), 'click', this.SwiEvtLec);
		}
		
		//-- Activation-Desactivation du bouton --//
		this.etat();
	}
};