
var timer=null;

var speed=40;

function runScrollers()
{
	for(var s in scrollers)
	{	
		scrollers[s].initialize();
	}
	startScrollers();
}

function Ticker(id,scroll_type)
{
	this.id=id;
	this.ready=false;
	this.stopped=false;
	this.y=0;
	this.timer=null;
	this.scrollType=scroll_type ? scroll_type : 'stop'; // scroll|stop
}

Ticker.prototype.initialize=function()
{
	var handle=document.getElementById(this.id);
	
	if(handle==null)
		return;
	
	this.scroller=document.getElementById(this.id);
	this.items=this.scroller.getElementsByTagName('div');
	var sum=0;
	for(var j=0;j<this.items.length;j++)
	{
		sum+=this.items.item(j).offsetHeight;
	}
	this.maxHeight=this.scroller.parentNode.offsetHeight;
	this.selfHeight=this.scroller.offsetHeight;
	this.scroller.style.top=this.y+'px';
	if(sum<this.maxHeight)
		return;
	this.ready=true;
}

	
Ticker.prototype.stop=function(s)
{
	if(!this.ready)
		return;
	if(!s)
		window.clearTimeout(this.timer);	
//	alert('stoped');
	this.stopped=true;
}	

Ticker.prototype.start=function()
{
	if(!this.ready)
		return;
	this.stopped=false;
}	

Ticker.prototype.scroll=function()
{
	if(!this.ready)
		return;
		
	if(this.y+this.selfHeight>0)
	{
		if(this.scrollType=='stop' && this.y<=0)
		{
			for(var j=0;j<this.items.length;j++)
			{
				if(this.y+this.items.item(j).offsetTop==0)
				{
					this.stop(true);
					this.timer=window.setTimeout('scrollers["'+this.id+'"].start()',1000);
				}
			}
		}
		this.y=this.y-1;
	}
	else
	{	
		this.stop(true);
		this.y=this.maxHeight+1;
		this.timer=window.setTimeout('scrollers["'+this.id+'"].start()',0);
	}	
	this.scroller.style.visibility='hidden';
	this.scroller.style.top=this.y+'px';
	this.scroller.style.visibility='visible';
}

function startScrollers()
{
//	for(var s in scrollers)
//	{	
//		if(!scrollers[s].stopped)
//			scrollers[s].scroll();
//	}	
	if(timer==null)
		timer=window.setInterval('scroll(true)',speed);
}

function scroll()
{
	for(var s in scrollers)
	{	
		if(!scrollers[s].stopped)
			scrollers[s].scroll();
	}	
}



