function nnwAnimbox(n){
	this.aHtmlNavi = [];
	this.oHtmlContent = null;
	this.iHtmlContentChilds = 0;
	this.iHtmlContentWidth = 0;
	this.iMoveWidth = 0;
	this.iCurrentDirection = 0;
	this.iCurrentIndex = 0;
	this.iTimeOutValue = 0;
	this.iPauseDuration = 3000;
	this.bPause = false;
	this.init(n);
}
nnwAnimbox.prototype.init = function(n, p){
	var oContainer = document.getElementById(n);
	if (oContainer && oContainer.hasChildNodes()){
		var len = oContainer.childNodes.length - 1;
		var thisObj = this;
		if (1 < len){
			while (len >= 0){
				if ('undefined' != typeof(oContainer.childNodes[len].className) && '' != oContainer.childNodes[len].className){
					if ('animbox-cont' == oContainer.childNodes[len].className){
						this.oHtmlContent = oContainer.childNodes[len];
						this.onlyRealNodes(this.oHtmlContent);
						this.iHtmlContentChilds = this.oHtmlContent.childNodes.length;
					}else if (0 == oContainer.childNodes[len].className.indexOf('animbox-nav')){
						this.aHtmlNavi.push(oContainer.childNodes[len]);
						oContainer.childNodes[len].firstChild.onclick = function(){
							thisObj.navigate(this);
							return false;
						}
						oContainer.childNodes[len].firstChild.onmouseover = function(){
							thisObj.pause();
						}
						oContainer.childNodes[len].firstChild.onmouseout = function(){
							thisObj.restart();
						}
					}
				}
				len--;
			}
		}
		if (this.oHtmlContent && 1 < this.iHtmlContentChilds){
			this.iCurrentDirection = 1;
			this.iCurrentIndex = 0;
			this.iTimeOutValue = 0;
			this.bPause = false;
			this.iHtmlContentWidth = this.oHtmlContent.offsetWidth;
			this.iMoveWidth = Math.ceil(this.iHtmlContentWidth/10);

			this.oHtmlContent.onmouseover = function(){
				thisObj.pause();
			}
			this.oHtmlContent.onmouseout = function(){
				thisObj.restart();
			}
		}
	}
}
nnwAnimbox.prototype.onlyRealNodes = function(o){
	for (var i = o.childNodes.length - 1; i >= 0; i--){
		if (1 != o.childNodes[i].nodeType){
			o.removeChild(o.childNodes[i]);
		}
	}
}
nnwAnimbox.prototype.setNavi = function(b){
	for (var i in this.aHtmlNavi){
		this.aHtmlNavi[i].style.display = b ? 'block' : 'none';
	}
}
nnwAnimbox.prototype.animate = function(){
	if (0 != this.iCurrentDirection){
		this.setNavi(true);
		this.moveInit();
	}
}
nnwAnimbox.prototype.setMoveInterval = function(p){
	this.iPauseDuration = p;
}
nnwAnimbox.prototype.moveInit = function(noTimeout){
	window.clearTimeout(this.iTimeOutValue);
	for (var i = 0; i < this.iHtmlContentChilds; i++){
		this.oHtmlContent.childNodes[i].style.display = 'none';
		this.oHtmlContent.childNodes[i].style.left = '0px';
	}
	var pos = 0;
	var idx = this.iCurrentIndex;
	for (i = 0; i < 2; i++){
		this.oHtmlContent.childNodes[idx].style.left = pos + 'px';
		this.oHtmlContent.childNodes[idx].style.display = 'block';
		pos += this.iHtmlContentWidth * this.iCurrentDirection;
		idx = this.neighbour(this.iCurrentDirection);
	}
	if ('undefined' == typeof(noTimeout) || 0 == noTimeout){
		this.iTimeOutValue = this.moveTimeout(this.iPauseDuration);
	}
}
nnwAnimbox.prototype.moveTimeout = function(delay){
	var thisObj = this;
	var move = function(){
		thisObj.move();
	}
	return window.setTimeout(move, delay);
}
nnwAnimbox.prototype.move = function(){
	var i, bFinished;
	window.clearTimeout(this.iTimeOutValue);
	var idx = this.iCurrentIndex;
	for (i = 0; i < 2; i++){
		this.oHtmlContent.childNodes[idx].style.left = (this.oHtmlContent.childNodes[idx].offsetLeft - (this.iMoveWidth * this.iCurrentDirection)) + 'px';
		idx = this.neighbour(this.iCurrentDirection);
	}
	if (1 == this.iCurrentDirection && this.oHtmlContent.childNodes[this.iCurrentIndex].offsetLeft + this.oHtmlContent.childNodes[this.iCurrentIndex].offsetWidth <= 0){
		bFinished = 1;
	}else if (-1 == this.iCurrentDirection && this.iHtmlContentWidth <= this.oHtmlContent.childNodes[this.iCurrentIndex].offsetLeft){
		bFinished = 1;
	}else{
		bFinished = 0;
	}
	if (0 == bFinished){
		this.iTimeOutValue = this.moveTimeout(40);
	}else if (0 == this.bPause){
		this.iCurrentIndex = this.neighbour(this.iCurrentDirection);
		this.moveInit();
	}
}
nnwAnimbox.prototype.neighbour = function(dir){
	var d = this.iCurrentIndex + dir;
	if (0 > d){
		d = this.iHtmlContentChilds - 1;
	}else if (d >= this.iHtmlContentChilds){
		d = 0;
	}
	return d;
}
nnwAnimbox.prototype.pause = function(){
	window.clearTimeout(this.iTimeOutValue);
	this.bPause = true;
}
nnwAnimbox.prototype.restart = function(){
	if (true == this.bPause){
		this.bPause = false;
		this.iTimeOutValue = this.moveTimeout(500);
	}
}
nnwAnimbox.prototype.navigate = function(el){
	if (-1 != el.parentNode.className.indexOf('-fwd')){
		this.iCurrentDirection = 1;
		this.iCurrentIndex = this.neighbour(1);
	}else{
		this.iCurrentDirection = -1;
		this.iCurrentIndex = this.neighbour(-1);
	}
	this.moveInit(1);
}

/*
The following codelines are subject to the Netscape Public
License Version 1.1 (the "License"); you may not use this
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.

The Initial Developer of the Original Code is Doron Rosenberg

Alternatively, the contents of this file may be used under the
terms of the GNU Public License (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only
under the terms of the GPL and not to allow others to use your
version of this file under the NPL, indicate your decision by
deleting the provisions above and replace them with the notice
and other provisions required by the GPL.  If you do not delete
the provisions above, a recipient may use your version of this
file under either the NPL or the GPL.

Contributor(s):  Doron Rosenberg, doronr@gmx.net
                 Bob Clary, Netscape Communications, Copyright 2001
*/


function Ticker(name, id, shiftBy, interval) {
	this.name     = name;
	this.id       = id;
	this.shiftBy  = shiftBy ? shiftBy : 1;
	this.interval = interval ? interval : 50;
	this.runId	= null;
	this.div = document.getElementById(id);
	if (!this.div) {
		return false;
	}
	this.animate = !document.all || 0 >= navigator.userAgent.indexOf("Mac");
	if (this.animate) {
		// remove extra textnodes that may separate the child nodes
		// of the ticker div
		var node = this.div.firstChild;
		var next;
		var maxNodeWidth = 0;

		while (node) {
			next = node.nextSibling;
			if (node.nodeType == 3) {
				this.div.removeChild(node);
			} else if (node.offsetWidth > maxNodeWidth) {
				maxNodeWidth = node.offsetWidth;
			}
			node = next;
		}
		//end of extra textnodes removal

		if (this.div.childNodes.length == 0) {
			this.animate = false;
			return false;
		}

		var i = 0;
		var currWidth = this.div.offsetWidth;
		var maxWidth = this.div.parentNode.offsetWidth + maxNodeWidth;
		var nChildren = this.div.childNodes.length;
		while (currWidth < maxWidth && ++i <= 5) {
			for (var j = 0; j < nChildren; j++) {
				node = this.div.childNodes[j].cloneNode(true);
				this.div.appendChild(node);
				currWidth += node.offsetWidth;
			}
		}

	 	this.left = 20;
		this.shiftLeftAt = this.div.firstChild.offsetWidth;
		this.div.style.height = this.div.firstChild.offsetHeight + 'px';
		this.div.style.width = (2 * screen.availWidth) + 'px';
	}
	this.div.style.visibility = 'visible';
}

function startTicker() {
	if (!this.animate) {
		return;
	}
	//this.stop();
	this.left -= this.shiftBy;
	if (this.left <= -this.shiftLeftAt) {
		this.left = 0;
		this.div.appendChild(this.div.firstChild);
		this.shiftLeftAt = this.div.firstChild.offsetWidth;
	}
	this.div.style.left = this.left + 'px';
	if (!this.runId) {
		this.runId = setInterval(this.name + '.start()', this.interval);
	}
}

function stopTicker() {
	if (this.runId) {
		clearInterval(this.runId);
	}
	this.runId = null;
}

function changeTickerInterval(newinterval) {
	if (typeof(newinterval) == 'string') {
		newinterval =  parseInt('0' + newinterval, 10);
	}
	if (typeof(newinterval) == 'number' && newinterval > 0) {
		this.interval = newinterval;
		this.stop();
		this.start();
	}
}

/* Prototypes for Ticker */
Ticker.prototype.start = startTicker;
Ticker.prototype.stop = stopTicker;
Ticker.prototype.changeInterval = changeTickerInterval;
