//
// Description:	Roll board javascript module
// File:		rollerBoard.js
// Author:		Guy Fernando (Copyright Informatix Ltd)
// Date:		29/10/2001
//

function Roller(
	x, y, width, height, border, padding,
	fgcolor, bgcolor, bdcolor,
	fontFamily, fontStyle, fontWeight, fontSize, textDecoration)
{
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	this.border = border;
	this.padding = padding;

	this.fgcolor = fgcolor;
	this.bgcolor = bgcolor;
	this.bdcolor = bdcolor;
	
	this.fontFamily     = fontFamily;
	this.fontStyle      = fontStyle;
	this.fontWeight     = fontWeight;
	this.fontSize       = fontSize;
	this.textDecoration = textDecoration;
	
	this.speed = 50;
	this.pauseTime = 2000;

	this.items = new Array();
	this.created = false;
}
Roller.prototype = new Roller;

Roller.prototype.setSpeed = function roller_setSpeed(speed)
{
	this.speed = speed;
}

Roller.prototype.setPause = function roller_setPause(ms)
{
	this.pauseTime = ms;
}

Roller.prototype.addItem = function roller_addItem(s)
{
	this.items[this.items.length] = s;
}

Roller.prototype.create = function roller_create()
{
	var start, end;
	var s;
	var i, j;
	var x, y;
	
	if (!isMinNS4 && !isMinIE4 && !isMinNS6)
		return;
	
	if (rollerList.length == 0)
		setInterval('rollerGo()', rollerInterval);

	this.created = true;
	this.items[this.items.length] = this.items[0];
	
	start = '<table border=0' + ' cellpadding=' + (this.padding + this.border)
		  + ' cellspacing=0'
		  + ' width=' + this.width
		  + ' height=' + this.height + '>'
		  + '<tr><td>'
		  + '<span style="'
		  + ' color:' + this.fgcolor + ';'
		  + ' font-family:' + this.fontFamily + ';'
		  + ' font-style:' + this.fontStyle + ';'
		  + ' font-weight:' + this.fontWeight + ';'
		  + ' font-size:' + this.fontSize + ';'
		  + ' text-decoration:' + this.textDecoration + '">';
		
	end = '</span></td></tr></table>';

	if (isMinNS4)
	{
		this.baseLayer = new Layer(this.width);
		this.scrollLayer = new Layer(this.width, this.baseLayer);
		this.scrollLayer.visibility = "inherit";
		this.itemLayers = new Array();
		for (i = 0; i < this.items.length; i++)
		{
			this.itemLayers[i] = new Layer(this.width, this.scrollLayer);
			this.itemLayers[i].document.open();
			this.itemLayers[i].document.writeln(start + this.items[i] + end);
			this.itemLayers[i].document.close();
			this.itemLayers[i].visibility = "inherit";
		}
	
		setBgColor(this.baseLayer, this.bdcolor);
		setBgColor(this.scrollLayer, this.bgcolor);
	}

	if (isMinIE4 || isMinNS6)
	{
		i = rollerList.length;

		s = '<div id="roller' + i + '_baseLayer"'
		+ ' style="position:absolute;'
		+ ' background-color:' + this.bdcolor + ';'
		+ ' width:' + this.width + 'px;'
		+ ' height:' + this.height + 'px;'
		+ ' visibility:hidden;">\n'
		+ '<div id="roller' + i + '_scrollLayer"'
		+ ' style="position:absolute;'
		+ ' background-color: ' + this.bgcolor + ';'
		+ ' width:' + this.width + 'px;'
		+ ' height:' + (this.height * this.items.length) + 'px;'
		+ ' visibility:inherit;">\n';
		
		for (j = 0; j < this.items.length; j++)
		{
			s += '<div id="roller' + i + '_itemLayers' + j + '"'
			  +  ' style="position:absolute;'
			  +  ' width:' + this.width + 'px;'
			  +  ' height:' + this.height + 'px;'
			  +  ' visibility:inherit;">\n'
			  +  start + this.items[j] + end
			  +  '</div>\n';
		}
		s += '</div>\n</div>\n';
	
		if (!isMinIE5)
		{
			x = getPageScrollX();
			y = getPageScrollY();
			
			window.scrollTo(getPageWidth(), getPageHeight());
		}

		if (isMinIE4)
			document.body.insertAdjacentHTML("beforeEnd", s);
		else if (isMinNS6)
		{
			var r = document.createRange();
			r.setStartBefore(document.body);
			var html = r.createContextualFragment(s);
			document.body.appendChild(html);
		}
		
		if (!isMinIE5)
			window.scrollTo(x, y);
	
		this.baseLayer = getLayer("roller" + i + "_baseLayer");
		this.scrollLayer = getLayer("roller" + i + "_scrollLayer");
		this.itemLayers = new Array();

		for (j = 0; j < this.items.length; j++)
			this.itemLayers[j] = getLayer("roller" + i + "_itemLayers" + j);
	}

	moveLayerTo(this.baseLayer, this.x, this.y);
	clipLayer(this.baseLayer, 0, 0, this.width, this.height);
	moveLayerTo(this.scrollLayer, this.border, this.border);
	clipLayer(this.scrollLayer, 0, 0,
			  this.width - 2 * this.border, this.height - 2 * this.border);
	x = 0;
	y = 0;
	for (i = 0; i < this.items.length; i++)
	{
		moveLayerTo(this.itemLayers[i], x, y);
		clipLayer(this.itemLayers[i], 0, 0, this.width, this.height);
		y += this.height;
	}

	this.stopped = false;
	this.currentY = 0;
	this.stepY = this.speed / (1000 / rollerInterval);
	this.stepY = Math.min(this.height, this.stepY);
	this.nextY = this.height;
	this.maxY = this.height * (this.items.length - 1);
	this.paused = true;
	this.counter = 0;

	rollerList[rollerList.length] = this;

	showLayer(this.baseLayer);
}

Roller.prototype.show = function roller_show()
{
	if (this.created)
		showLayer(this.baseLayer);
}

Roller.prototype.hide = function roller_hide()
{
	if (this.created)
		hideLayer(this.baseLayer);
}

Roller.prototype.moveTo = function roller_moveTo(x, y)
{
	if (this.created)
		moveLayerTo(this.baseLayer, x, y);
}

Roller.prototype.moveBy = function roller_moveBy(dx, dy)
{
	if (this.created)
		moveLayerBy(this.baseLayer, dx, dy);
}

Roller.prototype.getzIndex = function roller_getzIndex()
{
	if (this.created)
		return(getzIndex(this.baseLayer));
	else
		return(0);
}

Roller.prototype.setzIndex = function roller_setzIndex(z)
{
	if (this.created)
		setzIndex(this.baseLayer, z);
}

Roller.prototype.stop = function roller_stop()
{
	this.stopped = true;
}

Roller.prototype.start = function roller_start()
{
	this.stopped = false;
}

var rollerList = new Array();
var rollerInterval = 20;

function rollerGo()
{
	var i;

	for (i = 0; i < rollerList.length; i++)
	{
		if (rollerList[i].stopped)
			;
		else if (rollerList[i].paused)
		{
			rollerList[i].counter += rollerInterval;
			if (rollerList[i].counter > rollerList[i].pauseTime)
				rollerList[i].paused = false;
		}

		else
		{
			rollerList[i].currentY += rollerList[i].stepY;

			if (rollerList[i].currentY >= rollerList[i].nextY)
			{
				rollerList[i].paused = true;
				rollerList[i].counter = 0;
				rollerList[i].currentY = rollerList[i].nextY;
				rollerList[i].nextY += rollerList[i].height;
			}

			if (rollerList[i].currentY >= rollerList[i].maxY)
			{
				rollerList[i].currentY -= rollerList[i].maxY;
				rollerList[i].nextY = rollerList[i].height;
			}
			scrollLayerTo(
				rollerList[i].scrollLayer,
				0, Math.round(rollerList[i].currentY),
				false);
		}
	}
}

if (isMinNS4)
{
	var origWidth = getWindowWidth();
	var origHeight = getWindowHeight();
}

window.onresize = rollerReload;

function rollerReload()
{
	if (isMinNS4 && origWidth == getWindowWidth() && origHeight == getWindowHeight())
		return;

	window.location.href = window.location.href;
}
