/*
	Classe de gestion de diaporama simpliste
	©2007 - DynamicNet - www.dynamicnet.fr
*/

/*

	les images a faire défiler dans le slideshow doivent être présentes dans un element #slideshow
	ensuite le slide sera construit et les images commenceront a défiler

*/

var SimpleSlideshow = Class.create();


SimpleSlideshow.prototype = {

	/**
		id: #id de la zone contenant les images
	*/
	initialize: function( id ) {
		this.SlideShowContainer = $(id);
		
		Object.extend( this, arguments[1] || {} );
		
		this.create();
	},

	create: function() {

		//__ récup des images composant le slide show
		this.SlideShowElements = $A(this.SlideShowContainer.getElementsByTagName( "img" ));


		//__ suppression des noeuds déjà présents dans la zone d'affichage
			while (this.SlideShowContainer.firstChild) {
			  this.SlideShowContainer.removeChild(this.SlideShowContainer.firstChild);
			}
			
			this.launch();

	},

	launch: function() {
		//__ affiche la première image du slide
		this.SlideShowImage = document.createElement( "img" );
		this.SlideShowImage.src = this.SlideShowElements[0].src;

		this.SlideShowContainer.appendChild(this.SlideShowImage);

		this.currentIndex = 0;

		this.oPE = new PeriodicalExecuter(this.next , this.interval);
	},

	next: function() {

		if( sld.currentIndex+1 < sld.SlideShowElements.length ) {
			sld.currentIndex++;
		} else if( sld.loop == true ) {
			sld.currentIndex = 0;
		} else {
			sld.oPE.stop;
			return;
		}

		new Effect.Fade( sld.SlideShowContainer , {
			duration: 0.75,
			fps: 50,
			to: 0.01,
			afterFinish: function() {
				sld.SlideShowImage.src = sld.SlideShowElements[ sld.currentIndex ].src,

				new Effect.Appear( sld.SlideShowContainer , {
				duration: 1,
				fps: 50,
				queue: 'end'
				})

			}
		})

	}



};

function initSimpleSlideShow() { if( $("slideshow") ) {sld = new SimpleSlideshow( "slideshow", {loop: true, interval:5} ); } }
Event.observe( window , 'load', initSimpleSlideShow, false);