window.addEvent('domready', function(e) {
	SlideShow.initialize('diaporama')
});

var SlideShow = {
	initialize: function(el){
		this.el = $(el);
		if ($defined(this.el) == false) {
        	throw('SlideShow::initialize l\'element pass� est ind�fini');
        	return false;
        }
        this.currentSlideId = 0;
        this.el.set('morph', {duration: 250, transition: Fx.Transitions.Quad.easeOut});
        
        this.thumbs = new Array();
		$$('.thumb').each(function(el, index) {
			if (window.console) console.log(index + ' : init ');
			//on d�fini l'indesx au premier slide
			if (index == 0)	{
				this.currentSlideId = el.id.replace(/[^0-9]/g, '');
			}
			this.thumbs.push(new Thumb(el, this));
		}, this);
		this.intervalId = 0; //pour le timer
		this.initTimedSlide(20000);
	},
	initTimedSlide: function(duration) {
		$clear(this.intervalId);
		this.intervalId = (function() {
			var index = this.getNextSlideIndex();
			this.thumbs[index].el.fireEvent('mousedown');
		}).periodical(duration, this);
	},
	unselectThumbs: function() {
		this.thumbs.each(function(thumb, index) {
			thumb.setInactive();
		});
	},
	switchSlide: function(slideId) {
		//click sur le visu d�ja s�lectionn�, on s'assure du bon positionnement
		if (this.currentSlideId == slideId) {
			if (window.console) console.log(index + ' : already shown');
			var matchingImage = $('image_' + slideId);
			matchingImage.setStyles({opacity: 1});
			var matchingText = $('article_' + num);
			if ($defined(matchingText)) {
				matchingText.setStyles({opacity: 1});
			}
			return true;
		}
		this.thumbs.each(function(thumb, index) {
			//slide � masquer
			if (thumb.el.id.contains(this.currentSlideId)) {
				if (window.console) console.log(index + ' : hide');
				var matchingImage = $('image_' + this.currentSlideId);
				matchingImage.morph({opacity: 0});
				var matchingText = $('article_' + this.currentSlideId);
				if ($defined(matchingText)) {
					matchingText.morph({opacity: 0});					
				}
			}
			//slide � afficher
			else if (thumb.el.id.contains(slideId)) {
				if (window.console) console.log(index + ' : show');
				var matchingImage = $('image_' + slideId);
				matchingImage.morph.delay(250, matchingImage, {opacity:1});
				var matchingText = $('article_' + slideId);
				if ($defined(matchingText)) {
					matchingText.morph.delay(250, matchingText, {opacity:1});
				}
			}
			//slides masquer : on les maintient masqu�s
			else {
				if (window.console) console.log(index + ' : already hidden');
				var num = thumb.el.id.replace(/[^0-9]/g, '');
				var matchingImage = $('image_' + num);
				matchingImage.setStyles({opacity: 0});
				var matchingText = $('article_' + num);
				if ($defined(matchingText)) {
					matchingText.setStyles({opacity: 0});
				}
			}
			
		}, this);
		this.currentSlideId = slideId;
		return true;
	},
	
	//@deprecated
	getSlideId: function(index) {
		var id = this.currentSlideId; 
		if ($defined(this.thumbs[index])) {
			var id = this.thumbs[index].el.id.replace(/[^0-9]/g, '');
		}
		if (window.console) console.log('getSlideId return ' + id + ' at index ' + index);
		return id;
	},
	
	getNextSlideIndex: function() {
		var foundIndex = -1;
		this.thumbs.each(function(thumb, index) {
			if (window.console) console.log('getNextSlideIndex searching  ' + this.currentSlideId + ' against ' + thumb.el.id);
			if (thumb.el.id.contains(this.currentSlideId)) {
				foundIndex = index;
			}
		}, this);
		
		if (window.console) console.log('getNextSlideIndex found ' + foundIndex + ' total length = ' +  this.thumbs.length) ;
		if (foundIndex < (this.thumbs.length-1)) {
			if (window.console) console.log('getNextSlideIndex returnd +1 ' + (foundIndex+1));
			return foundIndex+1;
		}
		else { //fin du tableau on recommende
			if (window.console) console.log('getNextSlideIndex returnd 0');
			return 0;
		}
		
	}
};

var Thumb = new Class({
    initialize: function(el, parent){
        this.el = $(el);
        this.parent = parent;
        if ($defined(this.el) == false) {
        	throw('Vignette::initialize l\'element pass� est ind�fini');
        }
        
        if (window.console) console.log('init vignette into ' + this.el.id);
        this.registerEventsHandlers();
    },
    registerEventsHandlers: function() {
    	this.el.addEvent('mouseover', this.mouseOver.bind(this));
    	this.el.addEvent('mouseout', this.mouseOut.bind(this));
    	this.el.addEvent('mousedown', this.mouseDown.bind(this));
    },
    mouseOver: function(e) {
    	this.setActive();
    },
    
    mouseOut: function(e) {
    	if (this.el.id.contains(this.parent.currentSlideId)) return false;
    	
    	this.setInactive();
    },
    mouseDown: function(e) {
    	this.parent.unselectThumbs();
    	this.setActive();
    	this.parent.switchSlide(this.el.id.replace(/[^0-9]/g, ''));
    },
    setActive: function() {
   		this.el.addClass('active');
    },
    setInactive: function() {
   		this.el.removeClass('active');
    }
});
