$G = Class.create();
$G.debug = function(t) {
	var d = $("debug");
	d.update(t+"<br />"+d.innerHTML);
}
$G.Ticker = Class.create();
Object.extend($G.Ticker.prototype, {
	initialize: function(el, options) {
		this.options = Object.extend(
				{ 
					tickersToShow:2,
					speed:5,
					direction:1,
					fps:30,
					waittime:1000,
					autostart:true
				}, options || {});
		
		this.el = $(el);		
		this.tickers = this.el.getElementsBySelector('div.ticker');
				
		this.stations = new Array();
		this.stationdelta = parseInt(this.el.clientHeight/this.options.tickersToShow);
		
		if (this.tickers.length < this.options.tickersToShow) {
			var viewer = this.el.down("div");
			viewer.setStyle ({
				height:parseInt(this.stationdelta*this.tickers.length)+"px"
			});
		}
		
		this.stations.push({id:0, y:(this.stationdelta*-1), ticker:new Array(), next:null, prev:1, visible:true});
		
		for (var i=1; i <= this.tickers.length; i++) {
			var vis = (i <= this.options.tickersToShow+1) ? true : false;
			this.stations.push({id:i, y:this.stationdelta*(i-1), ticker:new Array(), next:(i-1), prev:(i+1), visible : vis});
		}
		
		this.stations[0].next = this.stations.length - 1;
		this.stations[this.stations.length - 1].prev = 0;
		
		
		var counter = 0;
		this.tickers.each((function (t) {
				t.uid = counter;
				t.controller = this;
				t.target = null;
				
				t.next = (counter == this.tickers.length -1) ? null : counter +1;
				t.prev = (counter == 0) ? null : counter -1;
				
				var sid = ((counter +1) < this.stations.length) ? counter +1 : this.stations.length -1;
				
				this.linkTickerToStation(t, this.stations[sid]);
				
				t.setStyle ({
					position:"absolute",
					overflow:"hidden",
					width:this.el.clientWidth+"px",
					height:this.stationdelta+"px",
					top:t.target.y+"px"
				});
				counter ++;
			}).bind(this)
		);
		
		Event.observe($("tickerup"), "click", (this.directionup).bind(this), false);
		Event.observe($("tickerdn"), "click", (this.directiondn).bind(this), false);
		this.mspf = parseInt(1000/this.options.fps);
		
		this.setdirection(this.options.direction);
		if (this.options.autostart) setTimeout((this.start).bind(this), 500);
	},
	
	directionup: function() {
		this.setdirection(1);
	},
	
	directiondn: function() {
		this.setdirection(-1);
	},
		
	setdirection: function(d) {
		this.options.nextdirection = d;
		this.firstticker = (d == 1) ? this.tickers[0] : this.tickers[this.tickers.length -1];
		this.lastticker = (d == -1) ? this.tickers[0] : this.tickers[this.tickers.length -1];
	},
	
	wait: function(){
		//$G.debug("Lancio pausa ");
		this.timerID = setTimeout((this.start).bind(this), this.options.waittime);
	},
	
	start: function() {
		//$G.debug("Parte il giro con "+this.firstticker.uid);
		if (this.options.nextdirection != this.options.direction) {
			this.options.direction = this.options.nextdirection;
		}
		this.startMove(this.firstticker);
	},
	
	startMove: function(t) {
		//$G.debug("Inizio movimento "+t.uid);
		var n = this.stations[t.target.next];
		var p = this.stations[t.target.prev];
		var ns = (this.options.direction > 0) ? n : p;
		if (t != undefined) {
			this.linkTickerToStation(t, ns);
			t.interval = setInterval((this.move).bind(t), this.mspf);
		}
	},
	
	move: function() {
		//$G.debug("Muovo "+this.uid);
		var y;

		if (this.target.visible) {
			y = this.offsetTop - ((this.controller.options.speed * this.controller.options.direction));
		} else {
			y =  this.target.y;
		}
		
		if ((y <= this.target.y && this.controller.options.direction > 0) || (y >= this.target.y && this.controller.options.direction < 0)) {
			y =  this.target.y;

			clearInterval(this.interval);
			this.interval = 0;
			//delete this.interval;			
			var nt = (this.controller.options.direction > 0) ? this.controller.tickers[this.next] : this.controller.tickers[this.prev];

			if (nt != undefined) {
				this.controller.startMove(nt);
			} else {
				//$G.debug("Mi fermo perchè sono l'ultimo "+this.uid);
				this.controller.wait();
			}
		}
			
		this.setStyle({top:y+"px"});
		//this.update(this.uid+" "+this.target.id+" "+y);
	},
	
	linkTickerToStation: function(t, s) {
		this.unlinkTicker(t);
		s.ticker.push(t);
		t.target = s;
	},
	
	unlinkTicker: function(t) {
		if (t.target != null) t.target.ticker = t.target.ticker.without(t);
		t.target = null;
	},
	
	unlinkStation: function(s) {
		s.ticker.each(function(t) {
			t.target = null;
		});
		s.ticker = s.ticker.clear();
	}
}); 



