/* bellajones.com
 * 2011 Luc NOTTÉ luc.notte@gmail.com
 * requires mootools-core.js
 */
//*************
Galerie = new Class({
	Implements: [Options, Events],
	options: {
		autoPlay: true,
		idxDeb: 0,
		afficheDuree: 4000,
		slidingDuree: 1400
	},

	initialize: function(elt, options){
		this.setOptions(options);
		this.elt = $(elt);
		this.idxCur = this.options.idxDeb;
	},

	slidesContenuEvents: {},
	eltEvents: {},

	setupSlides: function(){
		this.slides = this.elt.getElements('.diaporama>li');
		this.addSlides();
		this.playId = this.startPlay.delay(200, this);
		this.slides[this.options.idxDeb].addClass('curSel');
	},

	setupEvents: function(){
		this.elt.addEvents(this.eltEvents);
	},

	addSlides: function(){
		this.slides.each(function(slide){
			slide.ressource = slide.getElement('.ressource');
			slide.set('tween', {
				property: 'opacity',
				duration: this.options.slidingDuree,
				transition: Fx.Transitions.linear.easeIn,
				link: 'cancel'
			});
			this.fireEvent('slideAdded');
		}, this);
	},

	addSlide: function(){
		var slide = this.slides[0];
		slide.ressource = slide.getElement('.ressource');
		this.fireEvent('loadSlideRequest', 1);
		this.fireEvent('loadSlideComplete', 1);
		this.fireEvent('loadSlidesComplete');
	},

	showSlide: function(idxNxt){
			var slideCur = this.slides[this.idxCur];
			var animIn = function(s){
				this.sliding = true;
				s.addClass('curSel').get('tween').start(1).chain(function(){
					this.sliding = false;
				}.bind(this));
			}.bind(this);
			if(this.idxCur != idxNxt){
				this.slides[this.idxCur].get('tween').start(0).chain(function(){
					slideCur.removeClass('curSel');
				});
				animIn(this.slides[idxNxt]);
			}
			this.idxCur = idxNxt;
	},

	showSlideNextPlay: function(){
		this.stopPlay();
		this.showSlideNext();
		this.startPlay();
	},

	showSlideNextStop: function(){
		this.stopPlay();
		this.showSlideNext();
	},

	showSlideNext: function(){
		this.showSlide((this.idxCur+1)%this.slides.length);
	},

	startPlay: function(){
		this.playId = this.showSlideNext.periodical(this.options.afficheDuree, this);
	},

	stopPlay: function(){
		if (this.playId > 0) {
			clearInterval(this.playId);
			this.playId = 0;
		}
	},

	togglePlay: function(){
		if (this.playId > 0) {
			this.stopPlay();
		} else {
			this.showSlideNextPlay();
		}
	}
});
//*/

