/**
 * @author darius
 */
function ListTicker(tickerName, elementId, shiftBy, interval)
{
	this.name     = tickerName;
	this.id       = elementId;
	this.shiftBy  = shiftBy ? shiftBy : 1;
	this.shiftByManual  = this.shiftBy;
	this.interval = interval ? interval : 100;
	this.intervalManual = this.interval / 2;
	this.runId    = null;
	
	this.container = document.getElementById(elementId);
	
	// remove extra textnodes that may separate the child nodes
	// of the ticker div
	var node = this.container.firstChild;
	var next;
	while (node)
	{
		next = node.nextSibling;
		if (node.nodeType == 3)
	       this.container.removeChild(node);
		node = next;
	}
	//end of extra textnodes removal
    
    this.top = 0;
    this.shiftLeftAt = this.container.firstChild.offsetWidth;
    this.container.style.height = this.container.firstChild.offsetHeight;
    this.container.style.width = 2 * screen.availWidth;
    this.container.style.visibility = 'visible';
    
    // make the list stop on mouseover, and resume on mouseout
    this.container.className = this.name;
	this.container.onmouseover = function(){eval(this.className+'.stop();');};
	this.container.onmouseout = function(){eval(this.className+'.start();');};
	
	this.start = function()
	{
		
		if (this.container.childNodes.length < 2)
		  return;
		
		this.stop();
		
        this.top -= this.shiftBy;
        if (this.top <= -this.container.firstChild.nextSibling.offsetTop)
        {
            this.top = 0;
            this.container.appendChild(this.container.firstChild);
        }
        this.container.style.top = (this.top + 'px');
        this.runId = setTimeout(this.name + '.start()', this.interval);
        
	}
	
	this.stop = function()
    {
        
		if (this.runId)
            clearTimeout(this.runId);
		
		this.runId = null;
        
    }
    
    this.setBackScroller = function(elementId)
    {
    	
    	var scroller = document.getElementById(elementId);
    	var scrollerClassSuffix = 'BackScroller';
    	scroller.className = this.name+scrollerClassSuffix;
    	scroller.onmouseover = function(){
    		eval(this.className.replace(scrollerClassSuffix,'')+'.stop();');};
    	scroller.onmouseout = function(){
    		eval(this.className.replace(scrollerClassSuffix,'')+'.start();');};
    	
    	scroller.onmousedown = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.backScroll();');};
        scroller.onmouseup = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.stop();');};
        
    }
    
    this.setFrontScroller = function(elementId)
    {
        
        var scroller = document.getElementById(elementId);
        var scrollerClassSuffix = 'FrontScroller';
        scroller.className = this.name+scrollerClassSuffix;
        scroller.onmouseover = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.stop();');};
        scroller.onmouseout = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.start();');};
        
        scroller.onmousedown = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.frontScroll();');};
        scroller.onmouseup = function(){
            eval(this.className.replace(scrollerClassSuffix,'')+'.stop();');};
        
    }
    
    this.backScroll = function()
    {
    	
        this.top += this.shiftByManual;
        if (this.top >= this.shiftByManual)
        {
            this.container.insertBefore(this.container.lastChild,this.container.firstChild);
            this.top -= (this.container.firstChild.offsetHeight
                +this.container.firstChild.style.marginBottom
                +this.container.firstChild.style.borderBottomWidth
                +this.container.firstChild.style.borderTopWidth);
            this.top -=18;
        }
        this.container.style.top = (this.top + 'px');
        this.runId = setTimeout(this.name + '.backScroll()', this.intervalManual);
    	
    }
    
    this.frontScroll = function()
    {
        
        this.top -= this.shiftByManual;
        if (this.top <= -this.container.firstChild.nextSibling.offsetTop)
        {
            this.top = 0;
            this.container.appendChild(this.container.firstChild);
        }
        this.container.style.top = (this.top + 'px');
        this.runId = setTimeout(this.name + '.frontScroll()', this.intervalManual);
        
    }

    this.setManualInterval = function(interval)
    {
    	this.intervalManual = interval;
    }
    
    this.setManualShift = function(pixels)
    {
    	this.shiftByManual = pixels;
    }
}

