document.observe("dom:loaded", function(e) {
	SlideShow.initialize('bandeau')
});

var SlideShow = {
	initialize: function(el){
		this.el = $(el);
		if (!this.el) {
        	return false;
        }
    this.currentSlideId = 0;
    this.thumbs = new Array();
		$$('#num_slide a').each(function(el, index) {
			var num = new Thumb(el, this)
			if (index == 0)	{
				this.currentSlideId = el.id.replace(/[^0-9]/g, '');
				num.setActive();
			}
			this.thumbs.push(num);
		}, this);
		//suivant / pr�cedant
    $('slide_prev').observe('mousedown', (function(e) {
			this.thumbs[this.getPrevSlideIndex()].mouseDown();
		}).bindAsEventListener(this));
		$('slide_next').observe('mousedown', (function(e) {
			this.thumbs[this.getNextSlideIndex()].mouseDown();
		}).bindAsEventListener(this));

		this.intervalId = 0; //pour le timer
		this.initTimedSlide();
	},
	initTimedSlide: function() {

		//$clear(this.intervalId);
    //this.intervalId.stop();
    if ( this.intervalId != 0) this.intervalId.stop();
    this.intervalId = new PeriodicalExecuter((function(pe) {
			var index = this.getNextSlideIndex();
			this.thumbs[index].mouseDown();//fire('mousedown');
		}).bind(this), 3);
	},
	unselectThumbs: function() {
		this.thumbs.each(function(thumb, index) {
			thumb.setInactive();
		});
	},
	getLeft: function(slideIndex) {
		return $('slide_' + slideIndex).getDimensions().width * slideIndex;
	},
	switchSlide: function(slideId) {
		var slider = $('slider');
		if (this.currentSlideId == slideId) {
			slider.setStyle({'margin-left': '-' + this.getLeft(slideId)});
			return true;
		}
		else {
			var myMorph = slider.morph({'margin-left': '-' + this.getLeft(slideId) + 'px'});
		}
		this.currentSlideId = slideId;
	},
	
	//@deprecated
	getSlideId: function(index) {
		var id = this.currentSlideId; 
		if (this.thumbs[index]) {
			var id = this.thumbs[index].el.id.replace(/[^0-9]/g, '');
		}
		return id;
	},
	
	getPrevSlideIndex: function() {
		var foundIndex = 0;
		if (this.currentSlideId > 0) {
			foundIndex = this.currentSlideId -1;
		}
		
		return foundIndex;
	},
	getNextSlideIndex: function() {
		var foundIndex = -1;
		this.thumbs.each(function(thumb, index) {
			if (thumb.el.id.include(this.currentSlideId)) {
				foundIndex = index;
			}
		}, this);
		
		if (foundIndex < (this.thumbs.length-1)) {
			return foundIndex+1;
		}
		else { //fin du tableau on recommende
			return 0;
		}
		
	}
};

var Thumb = new Class.create({
    initialize: function(el, parent){
        this.el = $(el);
        this.parent = parent;
        if (!this.el) {
        	throw('Vignette::initialize l\'element pass� est ind�fini');
        }
        
        this.registerEventsHandlers();
    },
    registerEventsHandlers: function() {
      this.el.observe('mouseover', this.mouseOver.bind(this));
    	this.el.observe('mouseout', this.mouseOut.bind(this));
    	this.el.observe('mousedown', this.mouseDown.bind(this));
    },
    mouseOver: function(e) {
    	//this.setActive();
    },
    
    mouseOut: function(e) {
    	if (this.el.id.include(this.parent.currentSlideId)) return false;
    	this.setInactive();
    },
    mouseDown: function(e) {
    	this.parent.unselectThumbs();
    	this.parent.initTimedSlide();
    	this.setActive();
    	this.parent.switchSlide(this.el.id.replace(/[^0-9]/g, ''));
    },
    setActive: function() {
   		this.el.addClassName('select');
    },
    setInactive: function() {
   		this.el.removeClassName('select');
    }
});
