var scrollers	=	new Array();

function scroller( scid, scwidth, scheight, scspeed )
{
	if( scspeed == '' )
		scspeed	=	1;

	this.scid		=	scid;
	this.scwidth	=	scwidth;
	this.scheight	=	scheight;
	this.scspeed	=	parseInt( scspeed );
	this.cpos		=	0;
	
	this.init		=	scInit;
	this.doScroll	=	scDoscroll;
}

function scDoscroll()
{
	if( this.cpos > ( this.scheight + this.cheight ) )
	{
		this.cpos			=	this.cpos - this.cheight;
		var oldy			=	this.c.style.top;
		var newy			=	parseInt(oldy.substring(0, oldy.length-2 ))+this.cheight;
		this.c.style.top	=	newy+"px";
	}
		
	this.cpos			=	this.cpos+this.scspeed;
	var oldy			=	this.c.style.top;
	var newy			=	oldy.substring(0, oldy.length-2 )-this.scspeed;
	this.c.style.top	=	newy+"px";
	
	var newch	=	this.cpos - this.scheight;
	
	this.c.style.clip	=	"rect("+newch+"px "+this.scwidth+"px "+this.cpos+"px 0px)";
}

function scInit()
{
	this.posimg	=	document.getElementById( "scroller"+this.scid+"p" );	// position img
	this.m		=	document.getElementById( "scroller"+this.scid+"m" );	// main layer wrapped around content to hide scrollbars
	this.c		=	document.getElementById( "scroller"+this.scid+"c" );	// content
	this.ft		=	document.getElementById( "scroller"+this.scid+"ft" );	// fader top
	this.fb		=	document.getElementById( "scroller"+this.scid+"fb" );	// fader bottom
	
	this.xpos	=	getImageX( this.posimg );
	this.ypos	=	getImageY( this.posimg );

	this.cheight	=	this.c.offsetHeight;
	this.amountc	=	Math.ceil( this.scheight/this.cheight )+2;	// amount of times to duplicate content for endless scrolling

	var single	=	this.c.innerHTML;
	var	content	=	"";
	for( var j=0; j < this.amountc; j++ )
		content	+=	single;
	this.c.innerHTML	=	content;
	
	this.c.style.top		=	this.ypos + this.scheight + "px";
	this.c.style.left		=	this.xpos + "px";
	this.c.style.visibility	=	"visible";

	this.ft.style.top			=	this.ypos + "px";
	this.ft.style.left			=	this.xpos + "px";
	this.ft.style.visibility	=	"visible";
	
	this.fb.style.top			=	(this.ypos + this.scheight - 42) + "px";
	this.fb.style.left			=	this.xpos + "px";
	this.fb.style.visibility	=	"visible";
}

function initScrollers()
{
	for( var i=0; i < scrollers.length; i++ )
		scrollers[i].init();
	
	manageScrolling();
}

function manageScrolling()
{
	for( var i=0; i < scrollers.length; i++ )
		scrollers[i].doScroll();
	
	setTimeout( "manageScrolling()", 50 );
}

function getImageX( imgobj )
{
	var x = 0;
	var obj = imgobj;
	while (obj.offsetParent != null)
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	x += obj.offsetLeft;
	return x;
}

function getImageY( imgobj )
{
	var x = 0;
	var obj = imgobj;
	while (obj.offsetParent != null)
	{
		x += obj.offsetTop;
		obj = obj.offsetParent;
	}
	x += obj.offsetLeft;
	return x;
}

function addScroller( scid, scwidth, scheight, scspeed )
{
	scrollers[scrollers.length]	=	new scroller( scid, scwidth, scheight, scspeed );
}
