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

var Runner = new Class({

	Implements: [Events, Options],
	n_show:1,
	options: {
		area: 20,
		velocity: 1,
		onChange: function(x, y){
			this.element.scrollTo(x, y);
		},
		fps: 50,
		run:{
			active :false,
			duration:2000,
			period:5000,
			current_index:0,
			mode : 'V'
		}
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = document.id(element);
		this.sub_elements = this.element.getFirst().getChildren('div.box');
		var ln = this.sub_elements.length;
		if('H'==this.options.run.mode){
			var count = this.element.getSize().x/this.sub_elements[0].getSize().x;
		}else{
			var count = this.element.getSize().y/this.sub_elements[0].getSize().y;
		}
		this.n_show = Math.round(count-1),
		this.n_show = (0<this.n_show)?this.n_show:1;
		var z=0;
		if(this.n_show==1){
			this.sub_elements[0].getParent().adopt(this.sub_elements[0].clone());
		}else{
			for(i=0;i<((this.n_show*ln)-ln);i++){
				this.sub_elements[0].getParent().adopt(this.sub_elements[z].clone());
				if(z==ln-1)
					z=0;
				else
					z++;
			}
		}
		this.sub_elements = this.element.getElements('div.box');
		this.resize();
		this.listener = ($type(this.element) != 'element') ? document.id(this.element.getDocument().body) : this.element;
		this.timer = null;
		this.run_timer = null;
		if(this.options.run.period<this.options.run.duration)
			this.options.run.period=this.options.run.duration;
		this.bound = {
			attach: this.attach.bind(this),
			detach: this.detach.bind(this),
			getCoords: this.getCoords.bind(this)
		};
		if(this.options.run.active==true) this.run.delay((this.options.run.period),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.run.mode){
			this.element.getFirst().setStyle('width',width+'px');
		}else{
			this.element.getFirst().setStyle('height',height+'px');
		}		
	},

	start: function(){
		this.listener.addEvents({
			mouseover: this.bound.attach,
			mouseout: this.bound.detach
		});
	},

	stop: function(){
		this.listener.removeEvents({
			mouseover: this.bound.attach,
			mouseout: this.bound.detach
		});
		this.detach();
		this.timer = $clear(this.timer);
	},

	run:function(){
		this.run_timer = this.move.periodical(this.options.run.period,this);
	},
	
	halt:function(){
		this.run_timer = $clear(this.run_timer);
	},
	
	next:function(){
		if(this.options.run.current_index<(this.sub_elements.length-this.n_show)){
			this.options.run.current_index++;
		}else{
			this.options.run.current_index=1;
			this.element.scrollTo(0,0);
		}	
		return this.sub_elements[this.options.run.current_index];	
	},
	
	attach: function(){
		this.listener.addEvent('mousemove', this.bound.getCoords);
		this.halt(this);
	},

	detach: function(){
		this.listener.removeEvent('mousemove',this.bound.getCoords);
		this.timer = $clear(this.timer);
		this.run(this);
	},

	getCoords: function(event){
		this.page = (this.listener.get('tag') == 'body') ? event.client : event.page;
		if (!this.timer) this.timer = this.scroll_element.periodical(Math.round(1000 / this.options.fps), this);
	},

	move:function(){
		var next_element = this.next(this);
		this.scroller = new Fx.Scroll(this.element,{
			wait: false,
			duration: this.options.run.duration,
			transition: Fx.Transitions.Linear
		});
		this.scroller.toElement(next_element);
	},
	
	scroll_element: function(){
		var size = this.element.getSize(), 
		e_scroll = this.element.getScroll(), 
		pos = this.element.getOffsets(), 
		scrollSize = this.element.getScrollSize(), 
		change = {x: 0, y: 0};
		for (var z in this.page){
			if (this.page[z] < (this.options.area + pos[z]) && e_scroll[z] != 0)
				change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
			else if (this.page[z] + this.options.area > (size[z] + pos[z]) && e_scroll[z] + size[z] != scrollSize[z])
				change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
		}
		if (change.y || change.x) this.fireEvent('change', [e_scroll.x + change.x, e_scroll.y + change.y]);
	}

});
