/**
 * Caroussel - alterne des images
 *
 * @version		1.0
 *
 * @license		MIT-style license
 * @author		Guilhem Achikbache <guilhem [at] answeb.net>
 * @copyright	Author
 */

var Caroussel = new Class({

	Implements: Options,

	options: {
		duration: 3000,
		container: 'slider',
		element: 'div'
	},

	initialize: function(options) {
		this.setOptions(options);
		this.container = $(this.options.container);
		if (this.container) {
			this.container.setStyles({'position': 'relative'});
			this.items = this.container.getElements(this.options.element);
			this.items.setStyles({ 'position': 'absolute', 'opacity': 0 });
			this.current = 0;
			$(this.items[this.current]).setStyle('opacity', 1);
			this.timer = this.nextflash.periodical(this.options.duration, this);
		}
	},

	setHeight: function() {
		// Hauteur de l'image dessous
		if (this.container) {
			var h = this.container.getElement('img').getStyle('height');
			if (h != null) this.container.setStyles({'position': 'relative', 'height': h});
		}
	},

	nextflash : function() {
		$(this.items[this.current]).tween('opacity', 1, 0);
		this.current++;
		if (this.current >= this.items.length) { this.current = 0; }
		$(this.items[this.current]).tween('opacity', 0, 1);
	}

});
window.addEvent('domready', function() {
	c = new Caroussel({});
	window.addEvent('load', function() { c.setHeight(); } );
});
