var SlideShow = new Class({
	
	getOptions: function(){
		return {
			effect: 'fade', //fade|wipe|slide|random
			duration: 2000,
			wait: 5000,
			loop: false,
			onClick: false
		};
	},

	initialize: function(container, liste, options){
		this.setOptions(this.getOptions(), options);
		
		this.container = $(container);
			
		this.listService = $(liste).getElements('li');
		
		if (this.listService.length>0) {
			this.listService.each(function(el,i){
				el.addEvent('mouseover',function(){
					this.stop();
					this.show(i);				 
				}.bind(this,el,i));	
				
				el.addEvent('mouseout',function(){
					this.play(i);									 
				}.bind(this,el,i));	
			},this);
			$('linkIllu').addEvent('mouseover',function(){
				show.stop();
				show.show(show.image);				 
			});
			$('linkIllu').addEvent('mouseout',function(){
				show.play(show.image);			 
			});
		}
		
		this.timer = 0;
		this.image = -1;
		this.imageLoaded = 0;
		this.stopped = true;
		this.started = false;
		this.animating = false;
	},
	
	load: function(){
		$clear(this.timer);
		this.image++;		
		this.show(-1);		
	},

	show: function(num){		
		if (this.listService.length>0) {
			if(num > -1){this.image = num};
			var src = imagesService[this.image].src;
			var link = imagesLink[this.image];
		
			this.listService.each(function(el,i){
				el.removeClass('active');
				if (i == this.image) {				
					el.addClass('active');
				}
			},this);
			
			this.container.setProperty('src', src);	
			$('linkIllu').setProperty('href', link['lien']);
			$('linkIllu').setProperty('target', link['target']);
			
			if(!this.stopped){
				this.next.delay(this.options.duration,this,true);
			}
		}
	},
	
	wait: function(){
		this.timer = this.load.delay(this.options.wait,this);
	},
	
	play: function(num){
		if(num > -1){this.image = num-1};
		if(this.stopped){			
			if(this.image < this.listService.length && this.listService.length>0) {
				this.stopped = false;
				if(this.started){
					this.next();
				}else{
					this.load();
				}
				this.started = true;
			}
		}
	},
	
	stop: function(){		
		this.listService.each(function(el,i){
			el.removeClass('active');
		});
		$clear(this.timer);
		this.stopped = true;
	},
	
	next: function(wait){
		var doNext = true;
		if(wait && this.stopped){
			doNext = false;
		}
		if(this.animating){
			doNext = false;
		}
		if(doNext){			
			$clear(this.timer);
			if(this.image < this.listService.length-1){
				if(wait){
					this.wait();
				}else{
					this.load();	
				}
			}else{
				if(this.options.loop){
					this.image = -1;
					if(wait){
						this.wait();
					}else{
						this.load();	
					}
				}else{
					this.stopped = true;
				}
			}
		}
	},
	
	resetAnimation: function(){
		this.animating = false;
	}
	
});
SlideShow.implement(new Options);
SlideShow.implement(new Events);


/*************************************************************/

window.addEvent('domready',function(){
	var obj = {
		wait: 3000,
		duration: 9000, // ancien : 1000
		loop: true		
	}
	show = new SlideShow('serviceIllu','services',obj);
	show.play();
});