Element.display = function(element, show) {
    if (element) Element[(show) ? 'show': 'hide'](element);
}

var Slideshow = Class.create();
Slideshow.prototype = {
	initialize: function( slideshowElement, settings ) {
    	this.loaded = false;

		if (!settings) settings = {};
        this.settings = Object.extend({
			delaiAppear:1850,
			delaiSlide:2000,
			delaiMoveSlide:-1200,
			thumbWidth:70,
			autostart:true
        }, settings);
        
		this.timeout1 = 0 ;
    	this.nbslide = 0 ;
		this.itemsBanner = [];
		this.itemsMark = [];
		this.currslide = 0;
		this.thumbWidth = 0;
		this.nbthumbs = 0 ;

		this.slideshowElement = $(slideshowElement);
        if (!this.slideshowElement) { alert('Warning: Invalid slideshow element: ' + slideshowElement); return; }
       
		this.slideshowBanner = this.slideshowElement.down('.slideshowBanner');
        if (this.slideshowBanner) {
        	this.itemsBannerElement = this.slideshowBanner.down('.items');
			if (!this.itemsBannerElement) { alert('Warning: Class \'items\' does not exist as a child element in slideshowBanner : ' + slideshowElement); return; }
		}
       
		this.backElement = this.slideshowElement.down('.navButton.previous');
        if (this.backElement) {
        	Event.observe(this.backElement, 'click', this.back_slide.bind(this));
			this.backElement.show();
		}
        this.forwardElement = this.slideshowElement.down('.navButton.next');
        if (this.forwardElement) {
        	Event.observe(this.forwardElement, 'click', this.forward_slide.bind(this));
        	this.forwardElement.show();
    	}
        
        this.playElement = this.slideshowElement.down('.navButton.play');
        if (this.playElement) Event.observe(this.playElement, 'click', this.start_slide.bind(this));
        
        this.pauseElement = this.slideshowElement.down('.navButton.pause');
        if (this.pauseElement) Event.observe(this.pauseElement, 'click', this.stop_slide.bind(this));
        
        this.marksElement = this.slideshowElement.down('.navButton.marks');
        if (this.marksElement) this.marksElement.show();
	},
	load: function() {
        if ( this.slideshowBanner ) {
        	var eList = this.itemsBannerElement;
        	this.itemsBanner.clear();
        	var itemwidth = false ;
        	eList.select('.item').each(function(item, index) {
        		if ( !itemwidth ) {
        			var slideshowBanner = this.slideshowBanner.getStyle('width') ;
        			var ind = slideshowBanner.indexOf("px");
        			slideshowBanner = slideshowBanner.substring(0, ind );
        			itemwidth = item.getStyle('width'); 
					ind = itemwidth.indexOf("px");
        			itemwidth = itemwidth.substring(0, ind );
        			
        			var itemmarginright = item.getStyle('margin-right')
        			ind = itemmarginright.indexOf("px");
        			itemmarginright = itemmarginright.substring(0, ind );
        			this.thumbWidth = parseInt(itemwidth) + parseInt(itemmarginright); 
        			
        			this.nbthumbs = Math.round(slideshowBanner / this.thumbWidth );
				}
        		this.itemsBanner.push(item);
			} .bind(this));
		}

		this.loaded = true;
        this.nbslide = this.itemsBanner.length ; 
		if (this.nbslide == 0) {
            alert('Warning: No Slideshow Exist');
            return;
        }
		if (this.nbslide == 1 && this.forwardElement)
        	this.forwardElement.hide();

        if (this.settings.autostart)
        	this.start_slide();
        else {
        	this.pauseElement.hide();
        	this.playElement.show();
        }
        if ( this.nbslide > this.nbthumbs ) this.forwardElement.show();
        this.backElement.hide();
        
        if ( this.marksElement ) {
        	var eList = this.marksElement;
        	this.itemsMark.clear();
        	eList.select('.mark').each(function(item, index) {
        		var self = this ;
				item.onclick = function() {
        			self.currslide = index-1 ;
        			self.back_forward_slide(1, 1);
				};
        		this.itemsMark.push(item);
			} .bind(this));
			this.marks();
        }
   	},
	marks : function(){
		if ( !this.marksElement ) return ;
		var first = this.currslide ;
		var last = first + this.nbthumbs ;
		for ( var i = 0 ; i < this.nbslide ; i++ )
		{
			if ( i >= first && i < last ){
				this.itemsMark[i].addClassName('markOn');
				this.itemsMark[i].removeClassName('markOff');
			}
			else {
				this.itemsMark[i].addClassName('markOff');
				this.itemsMark[i].removeClassName('markOn');
			}
		}
	},
	back_slide : function() {
		this.back_forward_slide(-1, this.nbthumbs);
	},
    forward_slide : function() {
		this.back_forward_slide(1, this.nbthumbs) ;
   	},
    back_forward_slide : function(offset, pas) {
		this.stop_slide();
		var slide = this.calcul_slide(this.currslide, offset, pas);
		this.afterAppear( slide, offset ) ;
   	},
    start_slide : function() {
		this.move_slide(1,this.settings.delaiSlide);
	},
	move_slide : function(offset,delay) {
		this.clearAllTimeout();
		if (this.pauseElement) this.pauseElement.show();
		if (this.playElement) this.playElement.hide();
    	this.timeout1 = this.nextprev_slide.delay(delay ? delay : 0, this, offset, this.currslide, delay ? delay : this.settings.delaiMoveSlide);
    },
	calcul_slide : function(currslide, offset, pas){
		var slide = eval(currslide + (offset * pas) );
		//console.log("slideAV="+slide);
   		if ( offset == 1 && ( slide + this.nbthumbs > this.nbslide ) )
			slide = this.nbslide - this.nbthumbs ;
		else if ( offset == -1 && slide < 0 )
			slide = 0 ;
		//console.log("slideAP="+slide);
   		return slide;
	},
	nextprev_slide : function(slideshow, offset, currslide, delay) {
		var slide = slideshow.calcul_slide(currslide, offset, 1); 
			
		slideshow.afterAppear( slide, offset ) ;
   			
   		if ( offset == 1 && ( slide + slideshow.settings.nbthumbs == slideshow.nbslide ) )
			offset = -1 ;
		else if ( offset == -1 && slide == 0 )
			offset = 1 ;
		
		slideshow.timeout1 = slideshow.nextprev_slide.delay(delay + slideshow.settings.delaiAppear, slideshow, offset, slideshow.currslide, delay);
	},
    stop_slide : function() {
		this.clearAllTimeout();
		if (this.pauseElement) this.pauseElement.hide();
		if (this.playElement) this.playElement.show();
	},
	stopmove_slide : function() {
		this.start_slide();
 	},
	afterAppear : function(slide, offset) {
		this.affBanner( slide, offset ) ;
		this.currslide = slide ;
		if ( this.currslide + this.nbthumbs == this.nbslide) 
			this.forwardElement.hide();
		else this.forwardElement.show();
		if ( this.currslide == 0 ) this.backElement.hide();
		else this.backElement.show();
		this.marks();
	},
	affBanner : function(slide, offset) {
		if(!this.slideshowBanner) return; 
		
		var iLeft = this.getLeft(slide);
		var iCurrentLeft = parseInt(Element.getStyle(this.itemsBannerElement, 'left')) || 0;
		var move = iLeft - iCurrentLeft;
       	
      	var ef = new Effect.Move(this.itemsBannerElement, {
			'x': move,
			'afterFinish': function() {
				Element.setStyle(this.itemsBannerElement, { 'left': iLeft + 'px' });
			} .bind(this)
		});
		ef = null;
	},
    getLeft: function(slide) {
        if(!this.slideshowBanner) return;
        return slide * (-this.thumbWidth);
    },
	clearAllTimeout : function() {
		clearTimeout( this.timeout1 );
		this.timeout1 = 0 ;
	}
}

