var Diaporama = Class.create();
Diaporama.prototype = {
	initialize: function() {
		var me = this;
		
		this.auto = true;
		this.timer = 6;
		this.pos = 1;
		this.first = 1;
		this.last = $('ecran').childNodes.length;
		this.loading = false;
		
		this.photoPrecedente = 'photoPrecedente';
		this.photoSuivante = 'photoSuivante';
		this.photoPrefixe = 'photo_';
		
		this.photoPrecedente = $(this.photoPrecedente);
		this.photoSuivante = $(this.photoSuivante);
		
		new PeriodicalExecuter(function(pe) {
			if (me.auto)
				me.nextPicture();
			else
				pe.stop();
		}, me.timer);
		
		this.initBoutons();
	},
	
	estAuDebut: function() {
		return this.pos == this.first;
	},
	
	estALaFin: function() {
		return this.pos == this.last;
	},
	
	previousPicture: function() {
		var me = this;
		
		if (!this.loading)
		{
			this.loading = true;
			Effect.Fade(this.photoPrefixe + this.pos,
				{ afterFinish: function()
					{
						if (!me.estAuDebut())
						{
							Effect.Appear(me.photoPrefixe + --me.pos, { afterFinish: function() { me.loading = false; } });
						}
						else
						{
							me.pos = me.last;
							Effect.Appear(me.photoPrefixe + me.pos, { afterFinish: function() { me.loading = false; } });
						}
					}
				}
			);
		}
	},
	
	nextPicture: function() {
		var me = this;
		
		if (!this.loading)
		{
			this.loading = true;
			Effect.Fade(this.photoPrefixe + this.pos,
				{ afterFinish: function()
					{
						if (!me.estALaFin())
						{
							Effect.Appear(me.photoPrefixe + ++me.pos, { afterFinish: function() { me.loading = false; } });
						}
						else
						{
							me.pos = me.first;
							Effect.Appear(me.photoPrefixe + me.pos, { afterFinish: function() { me.loading = false; } });
						}
					}
				}
			);
		}
	},
	
	initBoutons: function() {
		var me = this;

		Event.observe(this.photoPrecedente, EVENT_CLICK, function(event) {
			Event.stop(event);
			me.auto = false;
			me.previousPicture();
		});
		
		Event.observe(this.photoSuivante, EVENT_CLICK, function(event) {
			Event.stop(event);
			me.auto = false;
			me.nextPicture();
		});
	}
};

Event.observe(window, EVENT_LOAD, function() { if ($('ecran')) new Diaporama(); });