/*-----------------------------------------------------------------------------------------------*/
//
// switchBloc.js v2.5
//
// Copyright (c) 2008 Agence Clark (http://www.agence-clark.com)
//
/*-----------------------------------------------------------------------------------------------*/

var SwitchBloc = Class.create();
SwitchBloc.prototype =
{
	initialize: function(SwiConteneur, SwiListe, SwiBloc, SwiBtPrecedent, SwiBtSuivant) {
		//-- Récupération des variables optionnelles --//
		var options = Object.extend({SwiConteneur:SwiConteneur,SwiListe:SwiListe,SwiBloc:SwiBloc,SwiBtPrecedent:SwiBtPrecedent,SwiBtSuivant:SwiBtSuivant}, arguments[5] || {});
		if (!options.SwiDirection) 			options.SwiDirection = 'horizontal';
		if (!options.SwiClassInact) 		options.SwiClassInact = 'off';
		if (!options.SwiDuree) 				options.SwiDuree = 1;
		if (!options.SwiIndice) 			options.SwiIndice = "indices";
		if (!options.SwiActIndice) 			options.SwiActIndice = false;
		if (!options.SwiClassOverlay) 		options.SwiClassOverlay = 'overlay';
		
		//-- Récupération des variables --//
		this.SwiDirection  = options.SwiDirection; 							// Direction du défilement
		this.SwiConteneur  = SwiConteneur; 									// Id du conteneur de la liste
		this.SwiListe  = SwiListe; 											// Id du conteneur des elements
		this.SwiBloc  = SwiBloc; 											// Classe d'un bloc
		this.SwiBtPrecedent  = SwiBtPrecedent; 								// ID du bouton précédent
		this.SwiBtSuivant  = SwiBtSuivant; 									// ID du bouton suivant
		this.SwiClassInact  = options.SwiClassInact; 						// Nom de la classe inactive
		this.SwiDuree  = options.SwiDuree; 									// Durée de l'effet
		this.SwiIndice  = options.SwiIndice; 								// Id du conteneur des indices
		this.SwiActIndice  = options.SwiActIndice; 							// Affichage des indices
		this.SwiClassOverlay  = options.SwiClassOverlay; 					// Nom de la classe de l'overlay

		//-- Initialisation des variables --//
		this.SwiPosition = 0;												// Position du curseur
		this.SwiEcran = 1;													// Ecran en cours
		this.SwiNbBloc = $$('#'+this.SwiListe+' .'+this.SwiBloc).size();	// Nombre d'éléments dans la liste
		this.SwiConteneurWidth = $(this.SwiConteneur).getWidth();			// Largeur du conteneur
		this.SwiConteneurHeight = $(this.SwiConteneur).getHeight();			// Hauteur du conteneur
		this.SwiListeWidth = this.SwiNbBloc * $$('#'+this.SwiListe+' .'+this.SwiBloc)[0].getWidth();	// Largeur de la liste
		this.SwiListeHeight = this.SwiNbBloc * $$('#'+this.SwiListe+' .'+this.SwiBloc)[0].getHeight();	// Largeur de la liste
		
		//-- Calcul du nombre d'écran --//	
		if(this.SwiDirection=='vertical') this.SwiNbEcran = Math.ceil(this.SwiListeHeight/this.SwiConteneurHeight);
		else{
			this.SwiNbEcran = Math.ceil(this.SwiListeWidth/this.SwiConteneurWidth);
			//-- Affectation de la largeur de la liste --//
			$(this.SwiListe).setStyle({width:this.SwiListeWidth+'px'});
		}

		//-- Création des indices --//
		if(this.SwiActIndice == true && this.SwiNbEcran > 1){
			this.SwiEvtIndices = new Array();
			new Insertion.Top(this.SwiIndice, '<ul id="'+this.SwiIndice+'_ul"></ul>');
			for(i=0;i<this.SwiNbEcran;i++){
				new Insertion.Bottom(this.SwiIndice+'_ul', '<li><a href="javascript:void(0);" title="'+(i+1)+'/'+this.SwiNbEcran+'" id="'+this.SwiIndice+'_'+(i+1)+'">'+(i+1)+'</a></li>');
				this.SwiEvtIndices.push({element:this.SwiIndice+'_'+(i+1),evt:this.move.bindAsEventListener(this, this.SwiIndice+'_'+(i+1))});
				Event.observe(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
			}
			$(this.SwiIndice).show();
		}
				
		//-- Mise en place des evenements --//
		if($(this.SwiBtPrecedent)) {
			this.SwiEvtPrec = this.precedent.bindAsEventListener(this);
			Event.observe($(this.SwiBtPrecedent), 'click', this.SwiEvtPrec);
		}
		if($(this.SwiBtSuivant)) {
			this.SwiEvtSuiv = this.suivant.bindAsEventListener(this);
			Event.observe($(this.SwiBtSuivant), 'click', this.SwiEvtSuiv);
		}
		
		//-- Création de l'Overlay --//
		var overlay = '<div id="masque_'+this.SwiConteneur+'" class="'+this.SwiClassOverlay+'"></div>';
		new Insertion.Top(this.SwiConteneur, overlay);
		$('masque_'+this.SwiConteneur+'').setStyle({display: 'none',width: $(this.SwiConteneur).getWidth()+'px',height: $(this.SwiConteneur).getHeight()+'px'});
		
		//-- Activation-Désactivation du bouton --//
		this.etat();
	},
	
	before : function() {	
		//-- Gestion de l'overlay --//
		$('masque_'+this.SwiConteneur).show();
		new Effect.Fade($('masque_'+this.SwiConteneur), {duration:1.5*this.SwiDuree});
		
		//-- Désactivation des listeners --//
		if(this.SwiActIndice == true && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++) Event.stopObserving(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
		}
		Event.stopObserving($(this.SwiBtPrecedent), 'click', this.SwiEvtPrec);
		Event.stopObserving($(this.SwiBtSuivant), 'click', this.SwiEvtSuiv);
	},
	
	after : function(type,ecran) {		
		//-- Activation des listeners --//
		if(this.SwiActIndice == true && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++) Event.observe(this.SwiEvtIndices[i].element, 'click', this.SwiEvtIndices[i].evt);
		}
		Event.observe($(this.SwiBtPrecedent), 'click', this.SwiEvtPrec);
		Event.observe($(this.SwiBtSuivant), 'click', this.SwiEvtSuiv);
		
		//-- Mise à jour des positions --//
		if(this.SwiDirection=='vertical'){
			if(type == 'prec') this.SwiPosition += this.SwiConteneurHeight;
			else if(type == 'suiv') this.SwiPosition -= this.SwiConteneurHeight;
			else if(type == 'indice_moins') this.SwiPosition -= (ecran - this.SwiEcran)*this.SwiConteneurHeight;
			else if(type == 'indice_plus') this.SwiPosition += (this.SwiEcran - ecran)*this.SwiConteneurHeight;
		}else{			
			if(type == 'prec') this.SwiPosition += this.SwiConteneurWidth;
			else if(type == 'suiv') this.SwiPosition -= this.SwiConteneurWidth;
			else if(type == 'indice_moins') this.SwiPosition -= (ecran - this.SwiEcran)*this.SwiConteneurWidth;
			else if(type == 'indice_plus') this.SwiPosition += (this.SwiEcran - ecran)*this.SwiConteneurWidth;
		}
		this.SwiEcran = ecran;
		this.etat();
	},
	
	etat : function() {
		//-- Bouton precedent --//
		if(Math.abs(this.SwiPosition) <= 0) $(this.SwiBtPrecedent).addClassName(this.SwiClassInact);
		else $(this.SwiBtPrecedent).removeClassName(this.SwiClassInact);
		
		//-- Bouton suivant --//
		if(this.SwiDirection=='vertical'){
			if(Math.abs(this.SwiPosition) >= (this.SwiListeHeight - this.SwiConteneurHeight)) $(this.SwiBtSuivant).addClassName(this.SwiClassInact);
			else $(this.SwiBtSuivant).removeClassName(this.SwiClassInact);
		}else{
			if(Math.abs(this.SwiPosition) >= (this.SwiListeWidth - this.SwiConteneurWidth)) $(this.SwiBtSuivant).addClassName(this.SwiClassInact);
			else $(this.SwiBtSuivant).removeClassName(this.SwiClassInact);
		}
		
		//-- Indices --//
		if(this.SwiActIndice == true && this.SwiNbEcran > 1){
			for(i=0;i<this.SwiNbEcran;i++){
				if(i+1 == this.SwiEcran) $(this.SwiEvtIndices[i].element).addClassName(this.SwiClassInact);
				else $(this.SwiEvtIndices[i].element).removeClassName(this.SwiClassInact);
			}
		}
	},
	
	move : function(cible){
		var ecran = parseFloat((cible.target.title).substring(0,(cible.target.title).lastIndexOf('/')));
		if(this.SwiDirection=='vertical'){			
			if(this.SwiEcran < ecran){
				new Effect.MoveBy($(this.SwiListe), -(ecran - this.SwiEcran)*this.SwiConteneurHeight, 0, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_moins',ecran)
				});
			}else if(this.SwiEcran > ecran){
				new Effect.MoveBy($(this.SwiListe), (this.SwiEcran - ecran)*this.SwiConteneurHeight, 0, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_plus',ecran)
				});
			}
		}else{
			if(this.SwiEcran < ecran){
				new Effect.MoveBy($(this.SwiListe), 0, -(ecran - this.SwiEcran)*this.SwiConteneurWidth, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_moins',ecran)
				});
			}else if(this.SwiEcran > ecran){
				new Effect.MoveBy($(this.SwiListe), 0, (this.SwiEcran - ecran)*this.SwiConteneurWidth, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'indice_plus',ecran)
				});
			}
		}
	},
	
	precedent : function(){
		if(Math.abs(this.SwiPosition) > 0){
			if(this.SwiDirection=='vertical'){
				new Effect.MoveBy($(this.SwiListe), this.SwiConteneurHeight, 0, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'prec',this.SwiEcran-1)
				});
			}else{
				new Effect.MoveBy($(this.SwiListe), 0, this.SwiConteneurWidth, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'prec',this.SwiEcran-1)
				});
			}
		}
	},
	
	suivant : function(){
		if(this.SwiDirection=='vertical'){
			if(Math.abs(this.SwiPosition) < (this.SwiListeHeight - this.SwiConteneurHeight)){
				new Effect.MoveBy($(this.SwiListe), -this.SwiConteneurHeight, 0, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'suiv',this.SwiEcran+1)
				});
			}
		}else{
			if(Math.abs(this.SwiPosition) < (this.SwiListeWidth - this.SwiConteneurWidth)){
				new Effect.MoveBy($(this.SwiListe), 0, -this.SwiConteneurWidth, {
					duration:this.SwiDuree,
					beforeStart:this.before.bind(this),
					afterFinish:this.after.bind(this,'suiv',this.SwiEcran+1)
				});
			}
		}
	}
};