/* This script is developed by Christian Hammer for COMM-WEB */
/* All rights are reserved. Copyright 2004 COMM-WEB			 */

Thread = function(id, interval)
{
	if (document.threads==null) document.threads = [];
	document.threads[id] = this;
	this.execute = "document.threads['"+id+"'].runHelper()";

	this.id = id;
	this.interval = interval;
	this.intervalObject = null;

	this.start = function(initialRun) {
		if (initialRun) this.run();
		if (this.intervalObject!=null) this.clear();
		this.intervalObject = window.setInterval(this.execute,this.interval);
	}
	this.stop = function() {
		this.clear();
		this.finalizer();
	}
	this.clear = function() {
		window.clearInterval(this.intervalObject);
		document.threads[this.id] = null;
		delete this.intervalObject;
		this.intervalObject = null;
	}
	this.runHelper = function() {
		if (this.intervalObject!=null) this.run();
	}
	this.finalizer = function() {}
}
Thread.prototype.run = function() {}
Thread.prototype.finalizer = function() {}

TimedDisplayThread = function(id, interval)
{
	this.Thread = Thread;
	this.Thread(id,interval);

	this.repeat = true;

	this.containerNode = null;
	this.currentNode = null;
	this.classSelector = null;

	this.actionFirst = function(item) { item.style.display = "block"; }
	this.actionLast = function(item) { item.style.display = "none"; }
	this.next = function(currentNode) {	return currentNode.nextSibling;	}
}
TimedDisplayThread.prototype.run = function()
{
	if (this.currentNode) {
		this.actionLast(this.currentNode);
		this.currentNode = this.next(this.currentNode);
		if (!this.repeat && this.currentNode==null) {
			this.stop();
			return;
		}
	}

	if (this.currentNode==null)	this.currentNode = this.containerNode.firstChild;
	while (this.currentNode!=null && (this.currentNode.style==null || (this.classSelector!=null && this.currentNode.className!=this.classSelector)))
		this.currentNode = this.next(this.currentNode);

	if (this.currentNode) this.actionFirst(this.currentNode);
	else if (this.repeat) this.run();
}

function startTimedDisplay(parentNodeID,interval)
{
	return startTimedDisplay2(parentNodeID,null,interval);
}
function startTimedDisplay2(parentNodeID,classID,interval)
{
	var thread = new TimedDisplayThread(parentNodeID+"TimedDisplay",interval);
	thread.containerNode = document.getElementById(parentNodeID);
	thread.classSelector = classID;
	thread.start(true);
	return thread;
}
