
/*
Constructor Ticker(name, id, shiftBy, interval)

Methods
=======
Ticker.start()
       starts the animation of the ticker
       
Ticker.stop()
       stops the animation of the ticker
       
Ticker.changeInterval(newinterval)
       changes the shifting interval to newinterval

Properties
==========
Ticker.name
	String : name of global variable containing reference to the ticker object

Ticker.id
	String : id of the DIV containing the Ticker data

Ticker.shiftBy
	Number : Number of pixels to shift the Ticker each time it fires.
	
Ticker.interval
    Number : Number of millisecond intervals between times Ticker fires
    
Ticker.runId
    Number : Value returned from setTimeout or null if the Ticker is not 
             running
             
Ticker.div
    HTMLElement : Reference to DIV containing the Ticker data.

-- Added by Freek 07-12-2004
Ticker.startPos
    Number : Value that specifies the starting posistion of the ticker

Ticker.endPos
    Number : Value that specifies the ending posistion of the ticker
  
*/

function Ticker(name, id, shiftBy, interval, startPos)
{
	if( document.getElementById(id) != null )
	{
		this.name     = name;
		this.id       = id;
		this.shiftBy  = shiftBy ? shiftBy : 1;
		this.interval = interval ? interval : 100;
		this.startPos = startPos ? startPos : 520;
		this.runId		= null;

		this.div = document.getElementById(id);
	  
		if( this.div != null )
		{
			var node = this.div.firstChild;
			var next;

			// remove extra textnodes that may separate the child nodes of the ticker div
			while (node)
			{
				next = node.nextSibling;
				if (node.nodeType == 3)
					this.div.removeChild(node);
				node = next;
			}
			//end of extra textnodes removal
			
			this.left = startPos; // 520
			this.div.style.height	= this.div.firstChild.offsetHeight;
			//this.div.style.width = 2 * screen.availWidth;
			this.div.style.visibility = 'visible';
		}
  }
}

Ticker.prototype.start = function()
{
	// Make sure we're attached to a div
	if( this.div != null )
	{
		// Reset the ticker
		this.stop();
		
		// Shift the ticker
		this.left -= this.shiftBy;
		
		// If the left margin reaches the limit, wrap around
		if (this.left <= -this.div.firstChild.offsetWidth)
		{
			this.left = this.startPos; // 520
			
			// Reset the ticker content to it's original position
			//this.div.appendChild(this.div.firstChild);
		}

		// Adjust the style sheet to set the left position
		this.div.style.left = (this.left + 'px');
		
		// Get the rightmost child after which to append scrolled items
		//var itemContainer = this.getItemContainer();
		//this.wrapAround(itemContainer);

		// Start ticking
		if (!this.runId)
			this.runId = setTimeout(this.name + '.start()', this.interval);
  }
}

Ticker.prototype.wrapAround = function(itemContainer)
{
	if (itemContainer)
	{
		var rightMostChild = this.getRightMostItem(itemContainer);
		if (rightMostChild && rightMostChild.childNodes)
		{
			// Walk all child nodes
			for (var childNodeIndex = 0; childNodeIndex < rightMostChild.childNodes.length; childNodeIndex++)
			{
				var childNode = rightMostChild.childNodes[childNodeIndex];
				if (child.offsetRight < 0)
				{
					//alert("Shifting " + child + " after " + rightMostChild);
					childNode.left = rightMostChild.offsetRight;
				}
			}
		}
	}
}

// Get the first inner child with more than 1 child items itself
Ticker.prototype.getItemContainer = function(parent)
{
	if (parent == null)
		parent = this.div;

	if (parent.childNodes)
	{
		if (parent.childNodes.length > 1)
			return parent;
		
		for (var childNodeIndex = 0; childNodeIndex < parent.childNodes.length; childNodeIndex++)
		{
			var childNode = parent.childNodes[childNodeIndex];
			var innerItemContainer = this.getItemContainer(childNode);
			if (innerItemContainer)
				return innerItemContainer;
		}
	}
	
	return null;
}

Ticker.prototype.getRightMostItem = function(parent)
{
	var rightMostItem = null;
	for (var childNodeIndex = 0; childNodeIndex < parent.childNodes.length; childNodeIndex++)
	{
		var childNode = parent.childNodes[childNodeIndex];
		if (rightMostItem && childNode.offsetRight > rightMostItem.offsetRight)
			rightMostItem = childNode;
	}
	return rightMostItem;
}

Ticker.prototype.stop = function()
{
  if (this.runId)
    clearTimeout(this.runId);
    
  this.runId = null;
}

Ticker.prototype.changeInterval = function(newinterval)
{

  if (typeof(newinterval) == 'string')
    newinterval =  parseInt('0' + newinterval, 10); 
	
  if (typeof(newinterval) == 'number' && newinterval > 0)
    this.interval = newinterval;
    
    this.stop();
    this.start();
}

