/*
   Ticker
   15.01.2007
   jstruebig@gmx.net
   
   V 1.0
*/
window.onload = ticker_ini;

function ticker_ini()
{
    var obj = document.getElementsByTagName('div');
    for(var i = 0; i < obj.length; i++) if(obj[i].className == 'ticker') new Ticker( obj[i] );
}

function Ticker(obj, trennzeichen)
{
    this.obj = obj;

    // Attribute holen (muss man nicht so machen)
    this.freq = this.obj.getAttribute('freq') || 100;
    this.dir = 1* ( this.obj.getAttribute('ticker_dir') || Ticker.LEFT );

    this.text = this.obj.innerHTML;
    this.trennzeichen = trennzeichen || ' +++ ';

    /* Ein Element einfügen */
    this.span = document.createElement('span');
    this.span.innerHTML = this.trennzeichen + this.text + this.trennzeichen + this.text;
    this.span.style.position = 'relative';

    this.obj.innerHTML = '';
    this.obj.appendChild( this.span) ;

    this.width = this.span.offsetWidth / 2;
    this.isStopped = true;
    this.aktPos = 1 * ( this.dir == Ticker.LEFT ? 0 : -this.width );
    this.run = function()
    {
         this.aktPos += this.dir;
         this.span.style.left = this.aktPos + 'px';
         if( this.dir < 0 && -this.aktPos > this.width) this.aktPos = 0;
         else if( this.dir > 0 && this.aktPos > 0) this.aktPos = -this.width;
    }
    this.start_stop = function()
    {
         if(this.isStopped) this.start(); else this.stop()
    }
    this.stop = function()
    {
         if(this.isStopped) return;
         window.clearInterval( this.timer);
         this.isStopped = true;
    }
    this.start = function()
    {
         if(!this.isStopped) return;
         var self = this;
         this.timer = window.setInterval( function() { self.run();}, this.freq );
         this.isStopped = false;
    }
    var self = this;
    this.obj.onmouseover = function() { self.stop(); };
    this.obj.onmouseout = function() { self.start(); };
    this.start();
}
// Konstanten für Links und Rechts
Ticker.LEFT = -1;
Ticker.RIGHT = 1;
