/**
 * 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);
		if (options) this.options = 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.container.addEvents({
				'mouseenter': this.stop.bind(this),
				'mouseleave': this.start.bind(this)
			});

		}
	},

	start: function() {
		// Forcer le changement
		$clear(this.timer);
		this.timer = this.nextflash.periodical(this.options.duration, this);
	},
	
	stop: function() {
		$clear(this.timer);
	},
	
	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() {
		f = new Fx.Style($(this.items[this.current]), 'opacity');
		f.start(1, 0);
		this.current++;
		if (this.current >= this.items.length) { this.current = 0; }
		f = new Fx.Style($(this.items[this.current]), 'opacity');
		f.start(0, 1);
	}

});

