
/* Version modifiée de la Class "Scroller" de mootools pour que le slide tourne tout seul */

var Runner = new Class({

	Implements: [Events, Options],
	timer:null,
	scroller:null,
	options: {		
		active :false,
		duration:1000,
		area:20,
		period:5000,
		current_index:0,
		mode : 'V'
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = document.id(element);
		this.sub_elements = this.element.getElements('div.box');
		this.sub_elements[0].getParent().adopt(this.sub_elements[0].clone());
		this.sub_elements = this.element.getElements('div.box');
		this.prevbt = new Element('div').set('id','runner_previous').setStyles({
			'position':'absolute',
			'z-index':'6000',
			'border':'0'				
		});
		this.nextbt = new Element('div').set('id','runner_next').setStyles({
			'position':'absolute',
			'z-index':'6000',
			'border':'0'
		});	
		if(this.options.mode=='V'){
			this.prevbt.setStyles({
				'height':this.options.area+'px',
				'width':this.options.area+'px',
				'margin-left':(this.element.getParent().getSize().x/2)-(this.options.area/2)+'px',
				'cursor':'pointer',
				'background':'transparent url("images/bt/up.gif") no-repeat top center'			
			});
			this.nextbt.setStyles({
				'height':this.options.area+'px',
				'width':this.options.area+'px',
				'margin-top':this.element.getParent().getSize().y-(this.options.area/2)-2+'px',
				'margin-left':(this.element.getParent().getSize().x/2)-(this.options.area/2)+'px',
				'cursor':'pointer',
				'background':'transparent url("images/bt/down.gif") no-repeat bottom center'		
			});	
		}else{
			this.prevbt.setStyles({
				'height':this.element.getParent().getSize().y+'px',
				'width':this.options.area+'px',
				'margin-top':(this.element.getParent().getSize().y/2)-(this.options.area/2)+'px',
				'cursor':'pointer'			
			});
			this.nextbt.setStyles({
				'height':this.options.area+'px',
				'width':this.options.area+'px',
				'margin-left':this.element.getParent().getSize().x-this.options.area+'px',
				'margin-top':(this.element.getParent().getSize().y/2)-(this.options.area/2)+'px',
				'cursor':'pointer'			
			});
		}
		this.nextbt.inject(this.element.getParent(),'top');
		this.prevbt.inject(this.element.getParent(),'top');
		if(this.options.area>0){
			this.nextbt.addEvents({
				'click':function(){
					this.go_next();
				}.bind(this),
				'mouseleave':function(){
					this.nextbt.setStyle('cursor','pointer');
					this.go_stop();
				}.bind(this)
			});
			this.prevbt.addEvents({
				'click':function(){
					this.go_prev();
				}.bind(this),
				'mouseleave':function(){
					this.prevbt.setStyle('cursor','pointer');
					this.go_stop();
				}.bind(this)
			});
		}
		this.resize();
		this.listener = ($type(this.element) != 'element') ? document.id(this.element.getDocument().body) : this.element;
		if(this.options.duration<this.options.period)
			this.options.duration=this.options.period;
		if(this.options.active==true) this.timer = this.move.delay(this.options.period,this);		
	},
	
	go_stop:function(){
		if(null!==this.timer)
			$clear(this.timer);
		if(null!==this.scroller)
			this.scroller.cancel();
		this.timer = this.move.delay(this.options.period,this);
	},

	go_next:function(){
		if(null!==this.timer)
			$clear(this.timer);
		if(null!==this.scroller)
			this.scroller.cancel();
		if(this.options.current_index<(this.sub_elements.length-1)){
			this.nextbt.setStyle('cursor','pointer');
			var next_element = this.get_next(this);
			this.scroller = new Fx.Scroll(this.element,{
				wait: false,
				duration: '200',
				transition: Fx.Transitions.Linear
			});
			this.scroller.toElement(next_element);
		}else {
			this.nextbt.setStyle('cursor','not-allowed');
			this.timer = this.go_next.delay(500,this);
		}	
	},
	
	go_prev:function(){
		if(null!==this.timer)
			$clear(this.timer);
		if(null!==this.scroller)
			this.scroller.cancel();
		if(this.options.current_index>0){
			this.prevbt.setStyle('cursor','pointer');
			var next_element = this.get_prev(this);
			this.scroller = new Fx.Scroll(this.element,{
				wait: false,
				duration: '200',
				transition: Fx.Transitions.Linear
			});
			this.scroller.toElement(next_element);
		}else {
			this.prevbt.setStyle('cursor','not-allowed');
			this.timer = this.go_prev.delay(500,this);
		}	
	},

	resize:function(){
		var width = 0;
		var height = 0;
		for(i=0;i<this.sub_elements.length;i++){
			width+=this.sub_elements[i].getSize().x;
			height+=this.sub_elements[i].getSize().y;			
		}
		if('H'==this.options.mode){
			this.element.getFirst().setStyle('width',width+'px');
		}else{
			this.element.getFirst().setStyle('height',height+'px');
		}		
	},
	
	next:function(){
		if(this.options.current_index<(this.sub_elements.length-1)){
			this.options.current_index++;
		}else{
			this.options.current_index=0;
			this.element.scrollTo(0,0);
		}	
		return this.sub_elements[this.options.current_index];	
	},
	
	get_prev:function(){
		if(this.options.current_index>0){
			this.options.current_index=this.options.current_index-1
			return this.sub_elements[this.options.current_index];	
		}else{
			this.options.current_index=0
			return this.sub_elements[this.options.current_index];
		}		
	},
	
	get_next:function(){
		if(this.options.current_index<this.sub_elements.length){
			this.options.current_index=this.options.current_index+1
			return this.sub_elements[this.options.current_index];	
		}else{
			this.options.current_index=this.sub_elements.length-1
			return this.sub_elements[this.options.current_index];
		}		
	},

	move:function(){
		var next_element = this.next(this);
		if(null!==this.timer)
			$clear(this.timer);
		if(null!==this.scroller)
			this.scroller.cancel();
		this.scroller = new Fx.Scroll(this.element,{
			wait: false,
			duration: this.options.duration,
			transition: Fx.Transitions.Linear,
			onComplete:function(){
				this.timer = this.move.delay(this.options.period,this);
			}.bind(this)
		});
		this.scroller.toElement(next_element);
	}

});