/**
 * 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: '.produit'
	},

	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);
			if (this.items.length > 1) {
				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);
				this.container.addEvent('mouseenter', function() {
					$clear(this.timer);
				}.bind(this));
				this.container.addEvent('mouseleave', function() {
					this.nextflash();
					this.timer = this.nextflash.periodical(this.options.duration, this);
				}.bind(this));
			}
		}
	},

	setHeight: function() {
		// Hauteur de l'image dessous
		if (this.container) {
			var h = null;
			if (this.container.getElement('img'))
			{
				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);
	}

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