function Marquee(name, id, shiftBy, interval, trid)
{
	this.name     = name;
	this.id       = id;
	this.shiftBy  = shiftBy ? shiftBy : 1;
	this.interval = interval ? interval : 100;
	this.runId	= null;
	this.div = document.getElementById(id);
	this.tr = document.getElementById(trid);

	RemoveTextObjects(this.div)
	this.div.style.width = 2 * screen.availWidth;
}

function initv()
{
	var nodes = this.tr.childNodes;
	if(nodes.length > 0)
	{
		this.left = 400;
		this.shiftLeftAt = this.tr.firstChild.offsetWidth;
		this.div.style.height	= this.tr.firstChild.offsetHeight;
	}
}

function RemoveTextObjects(ele)
{
	var node = ele.firstChild;
	var next;
	
	while (node)
	{
		next = node.nextSibling;
		if (node.nodeType == 3)
			ele.removeChild(node);
		node = next;
	}
}

function startMarquee()
{
	var ftr = this.tr.firstChild;
	this.stop();
	this.left -= this.shiftBy;
	if (this.left <= -this.shiftLeftAt)
	{
	    this.stop();
		this.left = 0;
		this.tr.appendChild(this.tr.firstChild);
		this.shiftLeftAt = this.tr.firstChild.offsetWidth;
	}

  this.div.style.left = (this.left + 'px');
  this.runId = setTimeout(this.name + '.start()', this.interval);
}

function stopMarquee()
{
	if (this.runId)
		clearTimeout(this.runId);
	this.runId = null;
}

function changeMarqueeInterval(newinterval)
{

  if (typeof(newinterval) == 'string')
    newinterval =  parseInt('0' + newinterval, 10); 
	
  if (typeof(newinterval) == 'number' && newinterval > 0)
    this.interval = newinterval;
    
    this.stop();
    this.start();
}

function UpdateValues(newvals)
{
	var lastu = 'first';
	for(var i in newvals)
	{
		var td = document.getElementById('update_'+i);
		if(td != null)
		{
			td.innerHTML = newvals[i];
		}
		else
		{
			td = document.createElement("TD");
			if(lastu == 'first')
			{
				this.tr.appendChild(td);
			}
			else
			{
				lastUnode = document.getElementById(lastu);
				var place = lastUnode.nextSibling;
				this.tr.insertBefore(td, place)
			}
			td.innerHTML = newvals[i];
			td.id = 'update_'+i;
			td.name = 'update_'+i;
		}
		lastu = 'update_'+i; 
	}
	var nodes = this.tr.childNodes;
	var next;
	if(nodes.length > 0)
	{
		var node = this.tr.firstChild
		while (node)
		{
			var nm = node.id;
			next = node.nextSibling;
			if (newvals[nm.substr(7)] == undefined)
			{
				this.tr.removeChild(node);
			}
			node = next;
		}
	}
}

function Restart()
{
	this.left = 400;
//	Clean();
}

function Clean()
{
	var next;
	if(this.tr.hasChildNodes())
	{
		var node = this.tr.firstChild
		while (node)
		{
			var nm = node.id;
			next = node.nextSibling;
			this.tr.removeChild(node);
			node = next;
		}
	}
}

Marquee.prototype.start = startMarquee;
Marquee.prototype.stop = stopMarquee;
Marquee.prototype.update = UpdateValues;
Marquee.prototype.changeInterval = changeMarqueeInterval;
Marquee.prototype.initvariables = initv;
Marquee.prototype.restartticker = Restart;