/*
Usage:
new SimpleSlideShow(wrapperElement, arrayOfImagesSources [, options]);

Options:
fadeTime: time in ms for the fade between images
stayTime: time between two fades

License:
MIT-Style-License
Copyright: Jan Kassens <janATkassens.net>
*/

var SimpleSlideShow = new Class({

	Implements: Options,

	options: {
		fadeTime: 1000,
		stayTime: 3000
	},

	initialize: function(wrapper, images, options) {
		var params = Array.link(arguments, {wrapper: $defined, images: Array.type, options: Object.type});
		this.wrapper = $(params.wrapper);
		this.setOptions(params.options);

		this.images = (params.images || JSON.decode(this.wrapper.get('images'))).map(function(image){
			return new Element('img', {
				src: image, width: '100%', height: '100%',
				tween: {duration: this.options.fadeTime}
			});
		}, this);
		this.index = 0;
		this.topImage = this.images[0].inject(this.wrapper);
		this.fade.delay(this.options.stayTime, this);
	},

	fade: function() {
		this.index = (this.index + 1) % this.images.length;
		this.bottomImage = this.topImage;
		this.topImage = this.images[this.index]
			.fade('hide')
			.inject(this.wrapper)
			.fade('in');
		this.fade.delay(this.options.stayTime + this.options.fadeTime, this);
	}

});

var tableau_images = ['theme/images/slideshow/slide-01.jpg','theme/images/slideshow/slide-02.jpg','theme/images/slideshow/slide-03.jpg','theme/images/slideshow/slide-04.jpg','theme/images/slideshow/slide-05.jpg'];
window.addEvent('domready', function() {
	//Gestion des slideshow(s)
	$$('.slideshow').each(function(slideshow){
		new SimpleSlideShow(slideshow, tableau_images);
	});
	
	//Gestion des accordéon(s)
	var toggler = $$('div.texte h3');
	var toggled = $$('div.texte .toggled');
	
	if(toggler.length==toggled.length && toggler.length > 2){ //on affiche que si il y a plus de 2 elements
		new Accordion(toggler, toggled, {
			display: 0,
			alwaysHide: false,
			opacity: false,
			onActive: function(t,e){
				t.toggleClass('actif');
				t.removeClass('hand');
			},
			onBackground: function(t,e){
				t.removeClass('actif');
				t.addClass('hand')
			}
		});
	}
});