
var BarackSlideshow = new Class({
	
	Implements: [Options, Events],
	
	options: {
		auto: false,
		autointerval: 2000,
		transition: 'fade',
		tween: { duration: 700 },
		startIndex: 0,
		on: 'mouseover',
		container: null,
		btn_suivant: null,
		btn_precedent: null,
		btn_tournant_infini: false //<-- Pour permettre qu'en appuyant sur un btn suivant ou précédent, quand on arrive en fin de liste, ça reprenne le début ou la fin de la liste suivant le sens de rotation.
	},
	
	initialize: function(menus, panneaux, loader, options){
		this.baliseMenus 	= $(menus);
		this.balisePanneaux = $(panneaux);
		this.balisePanneaux.setStyle('overflow', 'hidden');
		this.setOptions(options);
		if(this.options.container != null) $(this.options.container).setStyle('overflow', 'hidden');
		//--> Ajout des menus
		this.addMenus(menus);
		//--> Ajout des panneaux
		this.addPanneaux(panneaux);
		//--> Ajout des boutons suivant et précédent 
		if(this.options.btn_suivant != null) 	this.btn_suivant 	= $(this.options.btn_suivant);
		if(this.options.btn_precedent != null) 	this.btn_precedent 	= $(this.options.btn_precedent);
		//--> Affichage du premier panneau
		if(this.panneaux.length){
			this.currentIndex = this.options.startIndex;
			this.show(this.currentIndex);
		}
		
	},
	
	panneaux: [],
	
	menus: [],
	
	addPanneaux: function(panneaux){
		$(panneaux).getChildren().each(function(panneau, index){
			this.panneaux.include(panneau);
			panneau.setStyle('position', 'absolute');
			panneau.setStyle('top', 0);
			panneau.setStyle('left', 0);
			panneau.fade('hide');
		}.bind(this));
	},
	
	addMenus: function(menus){
		$(menus).getChildren().each(function(menu, index){
			this.menus.include(menu);
			menu.addEvent(this.options.on, function(event){
				this.change(event, index);
				$clear(this.autotimer);
			}.bind(this));
			menu.addEvent('mouseout', function(event){
				this.auto();
			}.bind(this));
		}.bind(this));
	},
  
	auto: function(){
		if (!this.options.auto) return false;
		$clear(this.autotimer);
		this.autotimer = this.progress.delay(this.options.autointerval, this);
	},
  			
	change: function(event, index){
		event.stop();
		$clear(this.autotimer);
		this.show(index);
	},
  
	show: function(index) {
		this.menus[index].addClass('current');
		if(this.currentIndex != index){
			this.panneaux[index].set('tween', this.options.tween).dispose().inject(this.panneaux[this.currentIndex], 'after');
		} else {
			this.panneaux[index].set('tween', this.options.tween).dispose().inject(this.balisePanneaux, 'top');
		}
		this.panneaux[index].fade('in');
		if(this.currentIndex != index){
			this.menus[this.currentIndex].removeClass('current');
			this.panneaux[this.currentIndex].fade('out');
		}
		this.currentIndex = index;
		this.mise_a_jour_btns();
		this.auto();
	},
  
	progress: function(){
		var curindex = this.currentIndex;
		var index = curindex + 1 < this.panneaux.length ? curindex + 1 : 0;
		this.show(index);
	},
	
	suivant: function(){
		var curindex = this.currentIndex;
		var index = curindex + 1 < this.panneaux.length ? curindex + 1 : 0;
		this.show(index);
	},
	
	precedent: function(){
		var curindex = this.currentIndex;
		var index = curindex - 1 < 0 ? this.panneaux.length - 1 : curindex - 1;
		this.show(index);
	},
	
	mise_a_jour_btns: function(){
		if("btn_suivant" in this){
			this.btn_suivant.removeEvents();
			if(this.currentIndex < (this.panneaux.length -1)){
				this.btn_suivant.removeClass('invalide');
				this.btn_suivant.addEvent('click', function(event){this.change(event, this.currentIndex + 1)}.bind(this));
			} else {
				if(this.options.btn_tournant_infini == false){
					this.btn_suivant.addClass('invalide');
					this.btn_suivant.removeEvent('click');
				} else {
					this.btn_suivant.addEvent('click', function(event){this.change(event, 0)}.bind(this));
				}
			}
		}
		if("btn_precedent" in this){
			this.btn_precedent.removeEvents();
			if(this.currentIndex > 0){
				this.btn_precedent.removeClass('invalide');
				this.btn_precedent.addEvent('click', function(event){this.change(event, this.currentIndex - 1)}.bind(this));
			} else {
				if(this.options.btn_tournant_infini == false){
					this.btn_precedent.addClass('invalide');
					this.btn_precedent.removeEvent('click');
				} else {
					this.btn_precedent.addEvent('click', function(event){this.change(event, this.panneaux.length -1)}.bind(this));
				}
			}
		}
	}
  
});
