/*
   Datei: scroller.js
   Datum: 10.01.2005
   Version: 0.9

   J. Strübig
   http://javascript.jstruebig.de/source/endlosscroll.html

   Ein Skript das einen Bereich endlos scrollen läßt.

   Benötigt die Bibliothek: http://javascript.jstruebig.de/lib/dom_layer.js


*/
Scroller.H = true;
Scroller.V = !Scroller.H;
function Scroller(id, w, h, scrolldir, name, show)
{
    this.o = getObjById(id);
    this.jump = false;

    if(!this.o) return alert('Fehler!');
    if(!name) return alert('Kein Name!');
    if(!w) return alert('Keine Breite!');
    if(!h) return alert('Kein Höhe!');

    this.name = name;
    this.show = show;

    // alten Inhalt auslesen
    var tmp = this.o.innerHTML();

    // und löschen
    this.o.innerHTML('');

    var name1 = this.name + 's1';
    var name2 = this.name + 's2';
    var name3 = this.name + 's3';

    this.o.width(2000);
    this.o.height(2000);

    addLayer(name1, this.o.obj);
    addLayer(name2, this.o.obj);
    addLayer(name3, this.o.obj);

    this.s1 = getObjById(name1);
    this.s2 = getObjById(name2 );
    this.s3 = getObjById(name3 );

    this.s1.innerHTML( tmp);
    this.s2.innerHTML( tmp);
    this.s3.innerHTML( tmp);

    this.s1.setStyle('padding', 0);
    this.s1.setStyle('margin', 0);

    this.s2.setStyle('padding', 0);
    this.s2.setStyle('margin', 0);

    this.s3.setStyle('padding', 0);
    this.s3.setStyle('margin', 0);

    this.realWidth = this.s1.width();
    this.o.width( w ) ;

    this.realHeight = this.s1.height();

    this.o.height( h ) ;

    this.scrollPos = 0;
    this.scroll = null;
    this.scrollDir = scrolldir;
    this.dir = 1;
    this.timer = 50;

    this.o.obj.p = this;
    this.o.obj.onmouseover = function(){ this.p.stop(); }
    this.o.obj.onmouseout = function() { this.p.start();}
    this.s2.innerHTML( tmp );
    this.set();
	this.scroll = window.setInterval( this.name + '.doScroll()', this.timer);
}
Scroller.prototype.stop = function()
{
    if(this.scroll) window.clearInterval(this.scroll);
    this.scroll = null;
}
Scroller.prototype.start = function(scrolldir, time, dir)
{
    if(this.scroll) return;
    if(typeof scolldir != 'undefined') this.scrollDir = scrolldir;
    if(time) this.timer = timer;
    if(typeof dir != 'undefined') this.dir = dir;
    this.scroll = window.setInterval( this.name + '.doScroll()', this.timer);
}
Scroller.prototype.doScroll = function()
{
    if(this.aktiv) alert('');
    this.aktiv = true;
    if(!this.scroll) return;

    this.scrollPos += this.dir ;

    if(this.scrollDir == Scroller.H )
         this.scrollV();
    else
         this.scrollH();
    this.aktiv = false;
}

Scroller.prototype.set = function()
{
    this.s1.top(0);
    this.s1.left(0);

    this.s2.top(0);
    this.s2.left(0);

    this.w = this.s1.width();
    this.h = this.s1.height();

    if(this.scrollDir)
    {
         this.s1.height( this.realHeight );
         this.s2.height( this.realHeight );
    }
    else
    {
         this.s1.width( this.w );
         this.s2.width( this.w );
    }

    if(!this.show) this.o.setStyle('overflow', 'hidden');
    if(!this.scrollDir) this.o.setStyle('whiteSpace', 'nowrap');
}
Scroller.prototype.scrollV = function()
{
    var h = this.dir > 0 ? -this.realHeight: this.realHeight;
    if(Math.abs(this.scrollPos) > this.realHeight)
    {
         if(this.jump)
         {
         this.dir = -this.dir;
         }

         this.s1.top( h );
         var tmp = this.s1;
         this.s1 = this.s3;
         this.s3 = tmp;
         this.scrollPos = 0;
    }
    this.s1.top( this.scrollPos - h);
    this.s2.top( this.scrollPos );
    this.s3.top( this.scrollPos + h );
}
Scroller.prototype.scrollH = function()
{
    var w = this.dir > 0 ? -this.realWidth : this.realWidth;

    if(Math.abs(this.scrollPos) > this.realWidth)
    {
         this.s1.left(w);
         var tmp = this.s1;
         this.s1 = this.s3;
         this.s3 = tmp;
         this.scrollPos = 0;
    }
    this.s1.left( this.scrollPos -w );
    this.s2.left( this.scrollPos  );
    this.s3.left( this.scrollPos + w);
}

function addLayer(id, o)
{
    if(!o) o = window;

    if (window.document.body.appendChild)
    {
         var test = document.createElement('div');
         test.id = id;
         test.style.position = 'absolute';
         o.appendChild(test);
    }
}