/*
ticker.js 

this script animates a ticker consisting of a div containing a sequence of 
left floated divs.	The div is shifted to the left by shiftBy pixels every interval ms.
As the second visible div reaches the left of the screen, the first is appended to 
the end of the div's children.

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.
  
*/

function Ticker(name, id, shiftBy, interval){
  this.name     = name;
  this.id       = id;
  this.shiftBy  = shiftBy||1;
  this.interval = interval||100;
  this.runId	= null;

  this.div = document.getElementById(id);

  // remove extra textnodes that may separate the child nodes
  // of the ticker div, compute the real width for proper floating (+250 px)

  var node = this.div.firstChild;
  var next;

  var tickerWidth = 250;
  while (node){
    next = node.nextSibling;
    if (node.nodeType == 3)
      this.div.removeChild(node);
	tickerWidth += parseInt(node.offsetWidth);
    node = next;
  }
  this.div.style.width = tickerWidth + "px";
  //end of extra textnodes removal
 
  this.left = 0;
  this.shiftLeftAt = this.div.firstChild.offsetWidth;
  this.div.style.visibility = 'visible';
}

function startTicker(){
  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';

  this.runId = setTimeout(this.name + '.start()', this.interval);
}

function stopTicker(){
  if (this.runId)
    clearTimeout(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();
}

// Ticker's prototypes 
Ticker.prototype.start = startTicker;
Ticker.prototype.stop = stopTicker;
Ticker.prototype.changeInterval = changeTickerInterval;

