/*

  Class de diaporama pour le portail Credit Moderne  
  require prototype.js version 1.6
  
  Realisation : Cosmic Communication - http://agencecosmic.com/
  Contact : jl@agencecosmic.com

*/

/*

  Quand DOM loade, on instancie Diapoteur

*/
document.observe('dom:loaded', function() {
	if ($('illustration'))
		new Diapoteur('illustration', 4, 3);
});

/*
  Classe de diaporama  
  Affiche un diaporama avec fondu sur tous ses enfants  
  ne se joue qu'une fois
  pour boucler, decommenter ligne 55 : afterFinish et commenter ligne 50 a 53 : updator.stop
  @params
  container : id du div conteneur
  pause : pause entre chaque fondu, par defaut = 8
  duration : duree de l'effet, par defaut = 2  
  
*/
var Diapoteur = Class.create({
	// Initialise et lance le diaporama
	initialize: function(container, pause, duration) {
		this.container = $(container);
		this.pause = pause || 8;
		this.duration = duration || 2;		
		if (!this.container) return;
		this.children = this.container.childElements();
		if (this.children.size() == 0) return;
		this.current = 0;
		this.children.invoke('hide').first().show();
		this.updator = new PeriodicalExecuter(this.next.bind(this), this.pause);
	},
	// Passe à la diapo suivante
	next: function() {
		var previous = this.children[this.current];
		this.current = this.next_in_loop();
	  if (this.current == (this.children.size()-1)) {
	   // this.updator.stop(); // arrete la boucle (suffit)
	    // delete this; // supprime obj (suppr car bug ie)
	  }
		new Effect.Appear(this.children[this.current], {
			duration: this.duration,
			afterFinish: function(){ previous.hide();} // permet de boucler
		});
	},
	// Retourne le numero de la diapo suivante en recommençant du debut s'il le faut
	next_in_loop: function() {
		var next = this.current += 1;
		return (next < this.children.size()) ? next : 0;
	}
});


