var clsNewScroller = new Class({
	initialize: function(elID,elUpArrow,elDownArrow) {
		this.delay = null;
		this.moving = false;
		this.auto = false;
		this.direction = 1;		
		this.el = $(elID);
		if(this.el == null)return;
		this.box = this.el.getElement('.scroller_box');
		if(this.box == null)return;
		this.fx = new Fx.Scroll(this.box,{
					link: 'cancel',
					fps: 25,
					wheelStops: false,
					transition: Fx.Transitions.linear
				});
					
		this.curNews = 0;
		this.newsEls = this.box.getElements('.scroller_el');	
		if(this.newsEls.length <= 2)return;
		
						
		// EVENTS
		
		this.el.addEvent('mouseover', this.OnMouseOver.bindWithEvent(this));
		this.el.addEvent('mouseleave', this.OnMouseOut.bindWithEvent(this));
		
		this.upEl = $(elUpArrow);
		this.downEl = $(elDownArrow);		
		if(this.upEl != null) {
			this.upEl.addEvent('click', this.OnUpClick.bindWithEvent(this));
		}
		if(this.downEl != null) {
			this.downEl.addEvent('click', this.OnDownClick.bindWithEvent(this));
		}
		
		this.fx.addEvent('complete', this.OnScrollComplete.bindWithEvent(this));
		this.fx.addEvent('start', this.OnScrollStart.bindWithEvent(this));
		
		this.StartAuto();
    },
	StartAuto : function() {
		this.auto = true;
		this.MoveNext();
	},
	StopAuto : function() {
		if(!this.auto)return;
		this.auto = false;
		this.fx.cancel();
		$clear(this.delay);
	},	
	MoveNext : function() {	
		if(this.direction == -1 && this.curNews <= 1)
		{
			this.direction = 1;
			return;
		}
		if(this.auto)
		{
			// La vitesse de défilement est reltive a la taille de la news actuelle
			var size = this.newsEls[this.curNews].getSize();
			this.fx.options.duration = size.y * 15;
			this.fx.options.link = 'cancel';
			if(this.curNews+1 == this.newsEls.length)
			{
				this.curNews = 0;
				this.fx.set(0,0);
			}
		} else {
			this.fx.options.link = 'ignore';
			this.fx.options.duration = 100;
			if(this.curNews+1 == this.newsEls.length && this.direction == 1)
			{
				this.curNews = 0;
				this.fx.set(0,0);
			}
		}		
		this.fx.toElement(this.newsEls[this.curNews+this.direction]);
	},
	OnScrollStart : function(e) {		
		this.moving = true;
	},
	OnScrollComplete : function(e) {	
		if(!this.moving)return;
		this.curNews += this.direction;		
		if(this.curNews+1 == this.newsEls.length) {
			this.fx.options.duration = 0;
			this.curNews = 1;
			this.fx.toElement(this.newsEls[this.curNews]);
		}
		this.moving = false;
		if(this.auto)
		{			
			this.delay = this.MoveNext.delay(3000,this);
		}
	},
	OnMouseOver : function(e) {
		if(this.auto)
		{
			this.fx.pause();
		}
	},
	OnMouseOut : function(e) {		
		if(this.auto)
		{
			this.fx.resume();
		}
		else
		{
			this.StartAuto();
		}
	},
	OnDownClick : function(e) {		
		this.StopAuto();
		this.MoveNext();
	},
	OnUpClick : function(e) {	
		this.StopAuto();
		this.direction = -1;
		this.MoveNext();
	}
});

