/*
	scroller - a JavaScript 1.2 class

	Phil Glanville phil@zeddcomm.com

	6 August 2001
*/

function scroller(name, timer, p1, p2, div, x, y, width, height, direction, amount, speed, pause, textColor)
{
	this.name= name;					// name of the scroller
	this.scrollTimer= timer;			// timer to use with this scroller
	this.parent1Div= p1;				// relative positioned parent div
	this.parent2Div= p2;				// absolutely positioned parent div
	this.scrollDiv= div;				// div for the scroller
	this.startx= x;						// where the scroller's x coordinate starts
	this.starty= y;						// where the scroller's y coordinate starts
	this.scrollItemWidth= width;		// width of each scroll item
	this.scrollItemHeight= height;		// height of each scroll item
	this.scrollDirection= direction;	// direction in which to scroll [left|right|up|down]
	this.scrollAmount= amount;			// larger numbers = coarser scrolling
	this.scrollSpeed= speed;			// larger numbers = slower scroll
	this.pauseTime= pause;				// number of milliseconds to pause
	this.textColor= textColor;			// colour of text
	this.scrollItemArray= new Array();

	this.loadScroller= _LoadScroller;
	this.doScroller= _DoScroller;
}

// writes the contents of scrollItemArray to the scroller div
function _LoadScroller()
{
	var contents= '';
	var startTag;
	var endTag;

	// loop through the scroll items
	for (var i=0; i<this.scrollItemArray.length; i++)
	{
		// if we're going left or right
		if (this.scrollDirection== 'left' || this.scrollDirection== 'right')
		{
			startTag= '<span style="position:absolute; left:' + (this.scrollItemWidth * i) + 'px; top:0px; width:' + this.scrollItemWidth + 'px; height:' + this.scrollItemHeight + 'px; color:' + this.textColor + ';">';
			endTag= '</span>';
		}
		// if we're going up or down
		else
		{
			startTag= '<div style="position:absolute; left:0px; top:' + (this.scrollItemHeight * i) + 'px; width:' + this.scrollItemWidth + 'px; height:' + this.scrollItemHeight + 'px; color:' + this.textColor + ';">';
			endTag= '</div>';
		}

		// Netscape 4 hack
		if (document.layers)
		{
			// force the font and force no line break
			startTag+= '<font face="verdana, arial, helvetica" size="1" color="' + this.textColor + '">';

			endTag= '</font>' + endTag;

			if (this.scrollDirection== 'left' || this.scrollDirection== 'right')
			{
				startTag+= '<nobr>';
				endTag= '</nobr>' + endTag;
			}
		}

		// add this entry to the contents
		contents+= startTag + this.scrollItemArray[i] + endTag;
	}

	// DOM1 browsers
	if (document.getElementById) document.getElementById(this.scrollDiv).innerHTML= contents;

	// Microsoft IE4 DOM
	else if (document.all) eval("document.all." + this.scrollDiv + ".innerHTML= " + contents);

	// Netscape 4 DOM
	else if (document.layers)
	{
		contents= contents.replace(/"/g, '\\\"');
		eval("document.layers." + this.parent1Div + ".document.layers." + this.parent2Div + ".document.layers." + this.scrollDiv + ".document.write('" + contents + "')");
		eval("document.layers." + this.parent1Div + ".document.layers." + this.parent2Div + ".document.layers." + this.scrollDiv + ".document.close()");
	}
}

function _DoScroller(reset)
{
	if (eval("window." + this.scrollTimer)) clearTimeout(eval("window." + this.scrollTimer));

	var doScroll=0;
	var maxx= this.scrollItemWidth * this.scrollItemArray.length;
	var maxy= this.scrollItemHeight * this.scrollItemArray.length;

	// DOM1 browsers
	if (document.getElementById)
	{
		var s= document.getElementById(this.scrollDiv);

		if (reset)
		{
			s.style.left= this.startx + "px";
			s.style.top= this.starty + "px";
		}

		switch(this.scrollDirection)
		{
			case 'left':
				s.style.left= parseInt(s.style.left) - this.scrollAmount + "px";
				carryOn= parseInt(s.style.left) % this.scrollItemWidth;
				doScroll= (parseInt(s.style.left) > -maxx);
			break;
			case 'right':
				s.style.left= parseInt(s.style.left) + this.scrollAmount + "px";
				carryOn= parseInt(s.style.left) % this.scrollItemWidth;
				doScroll= (parseInt(s.style.left) < maxx);
			break;
			case 'up':
				s.style.top= parseInt(s.style.top) - this.scrollAmount + "px";
				carryOn= parseInt(s.style.top) % this.scrollItemHeight;
				doScroll= (parseInt(s.style.top) > -maxy);
			break;
			case 'down':
				s.style.top= parseInt(s.style.top) + this.scrollAmount + "px";
				carryOn= parseInt(s.style.top) % this.scrollItemHeight;
				doScroll= (parseInt(s.style.top) < maxy);
			break;
		}
	}

	// Microsoft IE4 DOM
	else if (document.all)
	{
		var s= eval("document.all." + this.scrollDiv);

		if (reset)
		{
			s.style.pixelLeft= this.startx;
			s.style.pixelTop= this.starty;
		}

		switch(this.scrollDirection)
		{
			case 'left':
				s.style.pixelLeft-= this.scrollAmount;
				carryOn= s.style.pixelLeft % this.scrollItemWidth;
				doScroll= (s.style.pixelLeft > -maxx);
			break;
			case 'right':
				s.style.pixelLeft+= this.scrollAmount;
				carryOn= s.style.pixelLeft % this.scrollItemWidth;
				doScroll= (s.style.pixelLeft < maxx);
			break;
			case 'up':
				s.style.pixelTop-= this.scrollAmount;
				carryOn= s.style.pixelTop % this.scrollItemHeight;
				doScroll= (s.style.pixelTop > -maxy);
			break;
			case 'down':
				s.style.pixelTop+= this.scrollAmount;
				carryOn= s.style.pixelTop % this.scrollItemHeight;
				doScroll= (s.style.pixelTop < maxy);
			break;
		}
	}

	// Netscape 4 DOM
	else if (document.layers)
	{
		var s= eval("document.layers." + this.parent1Div + ".document.layers." + this.parent2Div + ".document.layers." + this.scrollDiv);

		if (reset)
		{
			s.left= this.startx;
			s.top= this.starty;
		}

		switch(this.scrollDirection)
		{
			case 'left':
				s.left-= this.scrollAmount;
				carryOn= s.left % this.scrollItemWidth;
				doScroll= (s.left > -maxx);
			break;
			case 'right':
				s.left+= this.scrollAmount;
				carryOn= s.left % this.scrollItemWidth;
				doScroll= (s.left < maxx);
			break;
			case 'up':
				s.top-= this.scrollAmount;
				carryOn= s.top % this.scrollItemHeight;
				doScroll= (s.top > -maxy);
			break;
			case 'down':
				s.top+= this.scrollAmount;
				carryOn= s.top % this.scrollItemHeight;
				doScroll= (s.top < maxy);
			break;
		}
	}

	if (!carryOn || reset) eval("window." + this.scrollTimer + "= setTimeout('" + this.name + ".doScroller()', " + this.pauseTime + ")");
	else if (doScroll) eval("window." + this.scrollTimer + "= setTimeout('" + this.name + ".doScroller()', " + this.scrollSpeed + ")");
	else eval(this.name + ".doScroller(1)");
}



