var allTickers = new Array();

function Ticker(divId)
{
    // store div ids
    this.containerId = divId;
    this.bodyId = divId + '_Body';
    
    // get the container element (so it doesn't have to repeatedly
    this.container = document.getElementById(divId);
    
    // grab the ticker contents and set defaults values
    this.content = this.container.innerHTML;
    this.container.innerHTML = "";
    this.container.style.display = "block";
    this.rightToLeft = true;
    this.speed = 1;
    this.paused = false;
    this.pauseOnMouseOver = true;
    
    // set default width and position
    this.width = parseInt(this.container.offsetWidth);
    this.contentWidth = this.width;
    this.x = this.rightToLeft ? this.width + 1 : 0;
    
    // set functions
    this.start = Ticker_start;
    this.tick = Ticker_tick;
    this.update = Ticker_updateContent;
    
    // store a copy of this ticker object to be used for interval timeouts
    allTickers[this.containerId] = this;
}

function Ticker_start() {
    // Initialize the ticker
	var tickerSupported = false;
	var tickerid = this.containerId;

    // add event listeners to container for pausing
    if (this.pauseOnMouseOver)
    {
        addListener(this.container, 'mouseover', function(e) { allTickers[tickerid].paused = true; }, false);
        addListener(this.container, 'mouseout', function(e) { allTickers[tickerid].paused = false; }, false);
    }
    
	// Firefox, Safari, IE
	if (!checkBrowserSupported()) 
	    this.container.outerHTML = "";
	else
	{
	    // Create a new div to house the contents in a single group for scrolling
	    this.scrollBody = document.createElement("div");
	    this.scrollBody.setAttribute("id", this.bodyId);
	    this.scrollBody.style.position = "absolute";
	    this.scrollBody.style.whiteSpace = "nowrap";
		this.scrollBody.style.left = String(this.x) + 'px';
	    this.container.appendChild(this.scrollBody);
		
		// Set contents of div to original contents
		this.update();
		
		// Start scrolling
		this.tick();
	}
}

function Ticker_updateContent()
{
	// Set contents of div to original contents
	this.scrollBody.innerHTML = this.content;
	// Update dimensions
	this.contentWidth = this.scrollBody.offsetWidth;
	this.container.style.height = this.scrollBody.offsetHeight + 'px';
}

function Ticker_tick(container) {
	if(!this.paused) 
	{
	    // Update position of the content
	    this.x += this.speed * (this.rightToLeft ? -1 : 1);
	    
	    if(this.rightToLeft && this.x <= (-1 * this.contentWidth - 1)) { this.update(); this.x = this.width + 1; }
	    if(!this.rightToLeft && this.x >= this.width+1) { this.update(); this.x = (-1 * this.contentWidth - 1); }
        
        this.scrollBody.style.left = String(this.x) + 'px';
    }	
	setTimeout("allTickers['" + this.containerId + "'].tick()", 25);
}



// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;
	
	if(window.addEventListener)	{ // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}

// Test browser support to see if ticker is supported
function checkBrowserSupported()
{
    var browserSupported = false;

    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) // Test for Firefox
    { 
         var version=new Number(RegExp.$1) // get version
         
         if (version>=2)
          browserSupported = true;
    }
    else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) // Test for Internet Explorer
    { 
         var version=new Number(RegExp.$1) // get version
         
         if (version>=6)
          browserSupported = true;
    }
    else if (navigator.userAgent.indexOf("Safari")!=-1)
        browserSupported = true;
    
    return browserSupported;
}
