// vim: set filetype=javascript :
var Ticker = Class.create();

Ticker.prototype =
{
    ticker: new Array(),

    initialize:
    function()
    {
        var tickerManager = this;

        // Find our elements that have the class "Ticker"
        $$('div.Ticker', 'p.Ticker').each(
        function(el)
        {
            className = el.className;
            pattern   = new RegExp("speed\-([0-9]+)");
            matches   = className.match(pattern);

            if (matches != null)
            {
                speed = new Number(matches[1]);
            }
            else
            {
                speed = new Number(200);
            }

            tickerManager.ticker.push(new TickerElement(el, speed));
        });
    }
}

var TickerElement = Class.create();

TickerElement.prototype =
{
    initialize:
    function(parentElement, speed)
    {
        this.parentElement = parentElement;

        this.prepareElements();

        this.position = {start: this.parentElement.offsetWidth,
                         end:   (-1 * this.scrollable.offsetWidth)};

        // The speed in pixels per millisecond
        this.speed         = speed / 1000;
        this.distance      = Math.abs(this.position.end - this.position.start)

        this.timing       = new Class.create();
        this.timing.start = new Date().getTime();
        this.timing.end   = this.timing.start + (this.distance / this.speed);

        this.start();
    },


    prepareElements:
    function()
    {
        // Clear out what's in our parent, because we are going to add
        // another tag we can animate within it
        var innerHtml                = this.parentElement.innerHTML;
        this.parentElement.innerHTML = '';

        // Init the object that we'll be animating
        scrollable                  = document.createElement('div');
        scrollable.innerHTML        = innerHtml;
        scrollable.style.position   = 'absolute';
        scrollable.style.whiteSpace = 'pre';
        scrollable.style.lineHeight = this.parentElement.offsetHeight + 'px';
        scrollable.style.margin     = '0px';
        scrollable.style.padding    = '0px';
        scrollable.style.left       = this.parentElement.offsetWidth + 'px';
        
        this.scrollable = scrollable;

        // Tweak the main parent container, this is the div the user made
        this.parentElement.style.display    = 'block';
        this.parentElement.style.overflow   = 'hidden';
        this.parentElement.style.position   = 'relative';

        // Add the new scrollable element we've created
        this.parentElement.appendChild(scrollable);
    },
        

    start:
    function()
    {
        this.intervalId = setInterval(this.step.bind(this), 40);
    },


    stop:
    function()
    {
        clearInterval(this.intervalId);
    },


    step:
    function()
    {
        var rightNow = new Date().getTime();

        if (rightNow > (this.timing.end + 500))
        {
            var nextEnd = (this.timing.end - this.timing.start) + rightNow;

            this.timing.start = rightNow;
            this.timing.end   = nextEnd;
        }

        var elapsed = rightNow - this.timing.start;

        if (elapsed == 0)
        {
            this.scrollable.style.left = this.parentElement.offsetWidth + 'px';
        }
        else
        {
            this.scrollable.style.left = this.parentElement.offsetWidth - (elapsed * this.speed) + 'px';
        }
    }
}
    
/**
 * Creates the new ticker element when the page has loaded
 */
function createTicker()
{
    var pageTicker = new Ticker;
}

// Fires off the ticker creation when the DOM is ready
Event.onDOMReady(createTicker);
