﻿var tickers = [];

function Ticker(tickerContainerId, interval, step)
{
    this.tickerContainer = document.getElementById(tickerContainerId);
    this.interval = interval || 200;
    this.step = step || 5;
    this.isPaused = false;
	if (this.tickerContainer != null)
	{
	    //this.width = this.tickerContainer.style.width;
	    this.width = this.tickerContainer.style.width = this.tickerContainer.offsetWidth;
	    this.tickerContainer.style.overflow = "hidden";
	    this.tickerContentId = "ticker_" + this.tickerContainer.id;
	}
}

Ticker.prototype.start = function()
{
    if (this.tickerContainer != null)
    {
        var tickerContentHTML = this.tickerContainer.innerHTML;
        var tickerContainerId = this.tickerContainer.id;
	    var isTickerSupported = false;
	    var img = "<img src='ClearPixel.gif' width='" + this.width + "' height='0'>";

	    // Firefox
	    if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1)
	    {
	        this.tickerContainer.innerHTML =
	            "<table cellspacing='0' cellpadding='0' width='100%'><tr><td nowrap='nowrap'>" +
                    img + 
                    "<span class='ticker' id='" + this.tickerContentId + "' width='100%'>" +
                        "&nbsp;" +
                    "</span>" +
                    img +
	            "</td></tr></table>";
    	        
		    isTickerSupported = true;
	    }
    	
	    // IE
	    if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1)
	    {
		    this.tickerContainer.innerHTML =
		        "<div nowrap='nowrap' style='width:100%;'>" +
		            img +
		            "<span class='ticker' id='" + this.tickerContentId +"' width='100%'>" +
		            "</span>" +
		            img +
		        "</div>";
		        
		    isTickerSupported = true;
	    }
    	
	    if(!isTickerSupported)
	    {
	        this.tickerContainer.outerHTML = "";
	    }
	    else
	    {
		    this.tickerContainer.scrollLeft = this.ltr ? 0 : this.tickerContainer.scrollWidth - this.tickerContainer.offsetWidth;

		    this.bodyElement = document.getElementById(this.tickerContentId);
		    this.bodyElement.innerHTML = tickerContentHTML;
		    
		    this.tickerContainer.style.display = "block";
		    
		    var tickerIndex = tickers.length - 1;
		    tickers[tickerIndex] = this;
	        tickerIntervalId = setInterval( "tickers[" + tickerIndex + "].tick()", this.interval);
	        window.onUnload = function() { clearInterval(tickerIntervalId); }
		}
	}
}

Ticker.prototype.tick = function()
{
	if(!this.isPaused)
	    this.tickerContainer.scrollLeft += this.step;

	if (this.step < 0)
	{    
	    if (this.tickerContainer.scrollLeft <= 0)
	        this.tickerContainer.scrollLeft = this.tickerContainer.scrollWidth - this.tickerContainer.offsetWidth;
	}
	else
	{
	    if (this.tickerContainer.scrollLeft >= this.tickerContainer.scrollWidth - this.tickerContainer.offsetWidth)
	        this.tickerContainer.scrollLeft = 0;
	}
    
	var _this_ = this;
}

