/*
sublime-tapeTicker.js
Sublime Consulting AB, 2009

Description:

Simple horizontal tape ticker script.

Parameters:
    clientId = the id of the text element.
    tickDelay = the delay between ticks in milliseconds.
    scrollFactor = the scroll factor in pixels.

Remarks:

The text element should always have a valid class attribute and the
class should always specify the exact font style, or the string
measurement may fail with unexpected results.

Example usage:

<style type="text/css">
    #ticker { background-color: #D3E3F3; padding: 4px 15px 4px 15px; margin-bottom: 17px; }
    .tickerText { color: #0D3B76; font-family: Arial; font-size: 12px; }
</style>
<div id="ticker">
    <div id="tickertext" class="tickerText">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Nam rutrum mollis sem. Nam at erat ac tellus bibendum congue.
        Nunc ut ante quis est suscipit bibendum. Maecenas velit augue,
        condimentum eget, viverra at, semper at, dolor. Praesent cursus magna.
    </div>
    <script type="text/javascript" language="javascript">
        var ticker = new sublime.tapeTicker('tickertext', 50, 2);
    </script>
</div>

*/


if(typeof sublime=="undefined"){var sublime=new Object();}
if(typeof sublime.tapeTicker=="undefined"){sublime.tapeTicker=new Object();}

sublime.tapeTicker = function(clientId, tickDelay, scrollFactor)
{
    this.tickDelay = tickDelay==undefined ? 80 : tickDelay;
    
    this.scrollFactor = scrollFactor==undefined ? 2 : scrollFactor;

    this.element = document.getElementById(clientId);
    
    if(this.element==undefined)
    {
        alert('Failed to initialize tapeTicker with clientId \'' + clientId + '\'.');
        return;
    }

    this.element.style.display = 'block';
    this.doPausOnMouseOver = false;
    this.width = this.element.offsetWidth;
    this.height = this.element.offsetHeight;
    this.actualWidth = this.measureStringWidth();
    this.actualHeight = this.measureStringHeight();
    this.marginLeft = this.width;
    
    if(!this.sanityCheck(true))
        return;
    
    this.outerElement = document.createElement('div');
    this.outerElement.style.display = 'block';
    this.outerElement.style.width = this.width;
    this.outerElement.style.heigth = this.height;
    this.outerElement.style.overflow = 'hidden';
    this.textElement = document.createElement('div');
    this.textElement.style.whiteSpace = 'nowrap';
    this.textElement.style.display = 'block';
    this.textElement.style.width = this.actualWidth;
    this.textElement.style.height = this.actualHeight;
    this.textElement.innerHTML = this.element.innerHTML;
    this.textElement.style.marginLeft = this.width + 'px';
    this.element.innerHTML = '';
    this.element.appendChild(this.outerElement);
    this.outerElement.appendChild(this.textElement);
    this.moveTimeout(10);
}

sublime.tapeTicker.prototype={
    sanityCheck:function(supressMessages)
    {
        var msg = '';
        
        if(this.width<1)
        {
            msg += 'Width was less than 1.\r\n';
        }
        else if(this.height<1)
        {
            msg += 'Height was less than 1.\r\n';
        }
        else if(this.scrollFactor<1)
        {
            msg += 'tickWidth was less than 1.\r\n';
        }
        else if(this.tickDelay<10)
        {
            msg += 'tickDelay was less than 10.\r\n';
        }
        if(msg.length>0)
        {
            if(!supressMessages)
                alert('Failed to initialize ticker (' + this.element.id + ').\r\n' + msg);
            return false;
        }
        return true;
    },
    moveTimeout:function(ms)
    {
        var self = this;
        var x = setTimeout(function(){self.move();}, ms);
    },
    move:function()
    {
        if(this.marginLeft+this.actualWidth>=0)
            this.marginLeft -= this.scrollFactor;
        else
            this.marginLeft = this.width;
            
        this.textElement.style.marginLeft = this.marginLeft + 'px';
        this.moveTimeout(this.tickDelay);
    },
    measureStringWidth:function()
    {
        var span = document.createElement('span');
        span.className = this.element.className;
        span.style.whiteSpace = 'nowrap';
        span.style.width = '';
        span.style.height = '';
        span.innerHTML = this.element.innerHTML;
        document.body.appendChild(span);
        var width = span.offsetWidth;
        document.body.removeChild(span);
        return width;
    },
    measureStringHeight:function()
    {
        var span = document.createElement('span');
        span.className = this.element.className;
        span.style.whiteSpace = 'nowrap';
        span.style.width = '';
        span.style.height = '';
        span.innerHTML = this.element.innerHTML;
        document.body.appendChild(span);
        var height = span.offsetHeight;
        document.body.removeChild(span);
        return height;
    }
}