/******************************************************************************
* NostriScroller.js                                                           *
* v2.0                                                                        *
*                                                                             *
* (c) 2005 Copyright by Nostri,Lda.                                           *
* http://www.nostri.pt                                                        *
*                                                                             *
* Última Revisão: 2005-02-22                                                  *
* Actualizações: Compatibilidade Firefox 1.0                                  *
* Autor: Joaquim Ponte                                                        *
******************************************************************************/


//*****************************************************************************
// Constructor do objecto 'NostriScroller'
//*****************************************************************************

function NostriScroller(x, y, width, height, align) {
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	this.align = align;

	this.items = new Array();
	this.AlreadyCreated = false;

	// Atribuição dos parâmetros da instância
	
	// Velocidade do scroll e pausa por defeito
	this.speed = 50;
	this.pauseTime = 2000;

	// Métodos do objecto.
	this.setBGColor = NostriScrollerBGColor;

	this.addItem = NostriScrollerAddItem;
	this.create = NostriScrollerCreate;
	this.show = NostriScrollerShow;
	this.hide = NostriScrollerHide;
	this.setzIndex = NostriScrollerSetzIndex;
	this.stop = NostriScrollerStop;
	this.start = NostriScrollerStart;
}

//*****************************************************************************
// Definição dos Métodos do objecto 'NostriScroller'
//*****************************************************************************

function NostriScrollerBGColor(color) {
	if (!this.AlreadyCreated)
		this.bgColor = color;
}

function NostriScrollerAddItem(str) {
	if (!this.AlreadyCreated) {
		this.items[this.items.length] = str;
	}
}
function NostriScrollerCreate() {
	var start, end;
	var str;
	var i, j;
	var x, y;

	if (!browserNS4 && !browserIE4 && !browserFF1)
		return;

	if (scrollerList.length == 0)
		setInterval('NostriScrollerGo()', scrollerInterval);
	if (!this.AlreadyCreated) {
		this.AlreadyCreated = true;
	}

	this.items[this.items.length] = this.items[0];

	ini = '<table border=0 cellpadding=0 cellspacing=0 width=' + this.width
			+ ' height=' + this.height + '><tr><td align=' + this.align + '>';
			
	fim   = '</td></tr></table>';

	if (browserNS4) {
		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(ini + this.items[i] + fim);
			this.itemLayers[i].document.close();
			this.itemLayers[i].visibility = "inherit";
		}
		setBgColor(this.baseLayer, this.bgColor);
		setBgColor(this.scrollLayer, this.bgColor);

		this.baseLayer.onmouseover=stopRoll;
		this.baseLayer.onmouseout=startRoll;

	}

	if (browserIE4) {
		i = scrollerList.length;
		str = '<div id="NostriScroller' + i + '_baseLayer"'
			+ ' style="position:absolute;'
			+ ' background-color:' + this.bgColor + ';'
			+ ' width:' + this.width + 'px;'
			+ ' height:' + this.height + 'px;'
			+ ' overflow:hidden;'
			+ ' visibility:hidden;" onmouseover="stopRoll()" onmouseout="startRoll()">\n'
			+ '<div id="NostriScroller' + 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++) {
			str += '<div id="NostriScroller' + i + '_itemLayers' + j + '"'
				+  ' style="position:absolute;'
				+  ' width:' + this.width + 'px;'
				+  ' height:' + this.height + 'px;'
				+  ' visibility:inherit;">\n'
				+  ini + this.items[j] + fim
				+  '</div>\n';
		}
		str += '</div>\n'
		    +  '</div>\n';

		if (!browserIE5) {
			x = getPageScrollX();
			y = getPageScrollY();
			window.scrollTo(getPageWidth(), getPageHeight());
		}
		document.body.insertAdjacentHTML("beforeEnd", str);
		if (!browserIE5)
			window.scrollTo(x, y);
	
		this.baseLayer = getLayer("NostriScroller" + i + "_baseLayer");
		this.scrollLayer = getLayer("NostriScroller" + i + "_scrollLayer");
		this.itemLayers = new Array();
		for (j = 0; j < this.items.length; j++)
			this.itemLayers[j] = getLayer("NostriScroller" + i + "_itemLayers" + j);
	}
	if (browserFF1) {
		i = scrollerList.length;
		str = '<div id="NostriScroller' + i + '_baseLayer"'
			+ ' style="position:absolute;'
			+ ' background-color:' + this.bgColor + ';'
			+ ' width:' + this.width + 'px;'
			+ ' height:' + this.height + 'px;'
			+ ' overflow:hidden;'
			+ ' visibility:hidden;" onmouseover="stopRoll()" onmouseout="startRoll()">\n'
			+ '<div id="NostriScroller' + 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++) {
			str += '<div id="NostriScroller' + i + '_itemLayers' + j + '"'
				+  ' style="position:absolute;'
				+  ' width:' + this.width + 'px;'
				+  ' height:' + this.height + 'px;'
				+  ' visibility:inherit;">\n'
				+  ini + this.items[j] + fim
				+  '</div>\n';
		}
		str += '</div>\n'
		    +  '</div>\n';
			if (canCreateContextualFragment = (typeof document.body.ownerDocument != 'undefined' && document.body.ownerDocument.createRange)){
				r = document.body.ownerDocument.createRange();
				r.setStartBefore(document.body);
				f = r.createContextualFragment(str);
				document.body.appendChild(f);
			}

		this.baseLayer = getLayer("NostriScroller" + i + "_baseLayer");
		this.scrollLayer = getLayer("NostriScroller" + i + "_scrollLayer");
		this.itemLayers = new Array();
		for (j = 0; j < this.items.length; j++)
			this.itemLayers[j] = getLayer("NostriScroller" + i + "_itemLayers" + j);
	}

	moveLayerTo(this.baseLayer, this.x, this.y);
	clipLayer(this.baseLayer, 0, 0, this.width, this.height);
	moveLayerTo(this.scrollLayer, 0, 0);
	clipLayer(this.scrollLayer, 0, 0, this.width, this.height);

	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 / scrollerInterval);
	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;

	scrollerList[scrollerList.length] = this;
	showLayer(this.baseLayer);
}

function NostriScrollerShow() {
	if (this.AlreadyCreated)
		showLayer(this.baseLayer);
}

function NostriScrollerHide() {
	if (this.AlreadyCreated)
		hideLayer(this.baseLayer);
}

function NostriScrollerSetzIndex(z) {
	if (this.AlreadyCreated)
		setzIndex(this.baseLayer, z);
}

function stopRoll(){
	oNostriScroller.stop();
	return false;
}

function startRoll(){
	oNostriScroller.start();
	return false;
}


function NostriScrollerStart() {
	this.stopped = false;
}

function NostriScrollerStop() {
	this.stopped = true;
}

//*****************************************************************************
// Código para o scroll
//*****************************************************************************

var scrollerList = new Array();
var scrollerInterval = 20;

function NostriScrollerGo() {
	var i;

	for (i = 0; i < scrollerList.length; i++) {
		if (scrollerList[i].stopped);
		else if (scrollerList[i].paused) {
				scrollerList[i].counter += scrollerInterval;
				if (scrollerList[i].counter > scrollerList[i].pauseTime)
					scrollerList[i].paused = false;
			 }
			 else {
				scrollerList[i].currentY += scrollerList[i].stepY;
				if (scrollerList[i].currentY >= scrollerList[i].nextY) {
					scrollerList[i].paused = true;
					scrollerList[i].counter = 0;
					scrollerList[i].currentY = scrollerList[i].nextY;
					scrollerList[i].nextY += scrollerList[i].height;
				}
				if (scrollerList[i].currentY >= scrollerList[i].maxY) {
					scrollerList[i].currentY -= scrollerList[i].maxY;
					scrollerList[i].nextY = scrollerList[i].height;
				}
				scrollLayerTo(scrollerList[i].scrollLayer, 0, Math.round(scrollerList[i].currentY), false);
			 }
	}
}

//*****************************************************************************
// Código para corrigir o bug do window resize no Netscape.
//*****************************************************************************

var origWidth;
var origHeight;

if (browserNS4 || browserFF1) {
	origWidth  = window.innerWidth;
	origHeight = window.innerHeight;
}
window.onresize = NostriScrollerReload;

function NostriScrollerReload() {
	if ((browserNS4 || browserFF1) && origWidth == window.innerWidth && origHeight == window.innerHeight)
		return;
	if (browserFF1) window.location.href = window.location.href;
	else history.go(0);
}

