/* -----------------------------------------------------------
 © 2004 Struppi
 Datum:          14.03.05
 Name:           layer.js
 Abhängigkeiten: lib.js
 Version:        1.00
 Status:         Stable
 Änderung:       15.04.05

 Browsertest:
 IE 4 (win95), FF 1.0 (WinXP), IE6 (winXP), OP 7.21 (winXP)

 Beschreibung:

 window.getElementById(id)

 holt das Objekt id aus dem Dokumentenbaum
 und erzeugt ein Layer Objekt, mit den folgenden Methoden:

 .top( [top] )
 .left( [left] )
 .width( [width] )
 .height( [height] )
 .right( [right] )
 .bottom( [bottom] )
 .style( prop [, value])
 .show( true/false )
 .addEvent(event, function, chain);
 .info()

 Darüber hinaus:
 getWinSize( [window] )
 getDocSize( [window] )
 pageOffset( [window] )

 onload:

 window.win_width
 window.win_height
 window.doc_width
 window.doc_height

----------------------------------------------------------- */
Layer.showInfo = true;    // zeigt bei dbl-Klick Infos über den Layer.

/////////////////////////////////////////////////
window.getElementById = function(id, win)
/////////////////////////////////////////////////
{
    if(!id) return null;
    if(!win) win = window;
    return win[id] = new Layer(id, win);
}

/////////////////////////////////////////////////
function Layer(obj, win)
/////////////////////////////////////////////////
{
    this.obj = null;
    this.rect    = new Object;
    this.rect.width = 0;
    this.rect.height = 0;
    this.rect.left = 0;
    this.rect.top = 0;
    if(obj) this.ini(obj, win);
}

/////////////////////////////////////////////////
Layer.prototype.ini = function(o, w)
/////////////////////////////////////////////////
{
    if(!w) w = window;

    if(typeof o == 'string')
    {
         var obj = null;
         if( document.getElementById ) obj = w.document.getElementById(o);
         else if( document.all )       obj = w.document.all[o];
         else if( document.layers )    obj = w.document.layers[o];
         else if( w.document[o] )      obj = w.document[o];

         if(!obj) return alert('Objekt nicht ansprechbar:' + o);

         o = obj;
    }

    if(typeof o != 'object')
    {
         return alert('Falscher Parameter: ' + o);
    }
    this.obj     = o;
    this.win     = w;
    this.id      = o ? o.id : null;
    this.sty_obj = this.obj.style || this.obj;
    this.getRect();

    if(Layer.showInfo) {
       this.obj.parent = this;
       var func = function(e, o)
       {
            var t = o.parent;
            alert( t.info())
       };
       this.addEvent('dblclick', func, true );
    }
    return this.obj;
};

/////////////////////////////////////////////////
Layer.prototype.addEvent = function(e, f, c) { return addEvent(this.obj, e, f, c); };
/////////////////////////////////////////////////
Layer.prototype.Obj = function() { return this.obj; };
/////////////////////////////////////////////////

/////////////////////////////////////////////////
Layer.prototype.top = function(p)
/////////////////////////////////////////////////
{
    if(this.obj.style && typeof p != 'undefined')
    {
         this.style('top', p + Px);
         this.getRect();
    }
    return this.rect.top;
}
/////////////////////////////////////////////////
Layer.prototype.left = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('left', p + Px);
         this.getRect();
    }
    return this.rect.left;
}
/////////////////////////////////////////////////
Layer.prototype.width = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         if(document.layers) { this.obj.resize( p, this.height() ); this.obj.bgColor = '#ffffff'; }
         else this.style('width', p + Px);
         this.getRect();
    }
    return this.rect.width;
}
/////////////////////////////////////////////////
Layer.prototype.height = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         if(document.layers) { this.obj.resize( this.width(), p ); this.obj.bgColor = '#ffffff'; }
         else this.style('height', p + Px);
         this.getRect();
    }
    return this.rect.height;
}
/////////////////////////////////////////////////
Layer.prototype.right = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('left', (this.win.doc_width - this.left() - p ) + Px);
         this.getRect();
    }
    return this.win.doc_width - this.rect.left;
}
/////////////////////////////////////////////////
Layer.prototype.bottom = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('top', (this.win.doc_height - this.height() - p ) + Px);
         this.getRect();
    }
    return this.win.doc_height - this.rect.top + this.rect.height;
}

/////////////////////////////////////////////////
Layer.prototype.getRect = function()
/////////////////////////////////////////////////
{
    var o = this.Obj();
    if(!o) return null;
    if(o.offsetTop)
    {
         this.rect.left = 0;
         this.rect.top = 0;
         this.rect.height = o.offsetHeight;
         this.rect.width = o.offsetWidth;
         while (o)
         {
              this.rect.top += parseInt(o.offsetTop );
              this.rect.left += parseInt(o.offsetLeft );
              o = o.offsetParent;
         }
    }
    else if(o.clip)
    {
         this.rect.left = o.left;
         this.rect.top = o.top;
         this.rect.width = o.clip.width;
         this.rect.height = o.clip.height;
    }
    this.rect.bottom = this.bottom();
    this.rect.right = this.right();
    return this.rect;
}
/////////////////////////////////////////////////
Layer.prototype.info = function()
/////////////////////////////////////////////////
{
    this.getRect();
    /*
    var x = 'Test\n';
    for(var i in this.obj)
    {
        if(this.obj[i] && typeof this.obj[i] != 'object'&& typeof this.obj[i] != 'function') x += i + '=' + this.obj[i] + NL;
    }
    */
    var text = '____________________________\n'
    + '\tLayer - Info   \n'
    + '¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\n'
    + 'Layer ID:\t\t' + this.id + NL
    + 'Top/Left:\t\t' + this.rect.top + '/' + this.rect.left + NL
    + 'Bottom/Right:\t' + this.rect.bottom + '/' + this.rect.right + NL
    + 'Höhe/Breite:\t' + this.rect.height + '/' + this.rect.width + NL
    + 'Dokument:\t' + this.win.doc_height + '/' + this.win.doc_width + NL
    + 'Browserfenster:\t' + this.win.win_height + '/' + this.win.win_width + NL
    + 'ScrollPos:\t' + pageOffset(this.win).top + '/' + pageOffset(this.win).left + NL
    // + x
    ;
    return text;

}
/////////////////////////////////////////////////
Layer.prototype.style = function(name, attr)
/////////////////////////////////////////////////
{
    if( !this.sty_obj ) return null;
    if( typeof attr != 'undefined' && attr != 'null' )
    {
         if(document.all && !window.opera ) this.sty_obj.setAttribute(name, attr);
         else this.sty_obj[name] = attr;
    }
    return this.sty_obj[name];
}
/////////////////////////////////////////////////
Layer.prototype.show = function(mode)
/////////////////////////////////////////////////
{
    if( !this.sty_obj ) return null;
    if(document.layers)
    {
    if(typeof mode != 'undefined') this.obj['visibility'] = mode ? 'show' : 'hide';
       return this.obj['visibility'] == 'hide' ? false : true;
    }
    if(typeof mode != 'undefined') this.style('display', mode ? '' : 'none');
    return this.style('display') != 'none';
}


/////////////////////////////////////////////////
function getDocSize(w)
/////////////////////////////////////////////////
{
    if(!w) w = this;
    var s = new Object();

    if (typeof document.height != 'undefined')
    {
        s.width =  w.document.width;
        s.height = w.document.height;
    }
    else
    {
        var test1 = w.document.body.scrollHeight;
        var test2 = w.document.body.offsetHeight || document.body.clientHeight;
        if (test1 > test2) // all but Explorer Mac
        {
              s.width = w.document.body.scrollWidth;
              s.height = w.document.body.scrollHeight;
        }
        else // Explorer Mac;
             //would also work in Explorer 6 Strict, Mozilla and Safari
        {
             s.width = w.document.body.offsetWidth || w.document.body.clientWidth;
             s.height = w.document.body.offsetHeight || w.document.body.clientHeight;
        }

    }
    w.doc_height = s.height;
    w.doc_width  = s.width;
    return s;
}

/////////////////////////////////////////////////
function getWinSize(win)
/////////////////////////////////////////////////
{

    if(!win) win = this;
    var s = new Object();
    if(typeof win.innerWidth != 'undefined')
    {
        s.width = win.innerWidth;
        s.height = win.innerHeight;
    }
    else
    {
         var obj = getBody(win);
         s.width = parseInt(obj.clientWidth);
         s.height = parseInt(obj.clientHeight);
    }
    win.win_height = s.height;
    win.win_width = s.width;
    win.onresize = function() { getWinSize(this); getDocSize(this);};
    return s;
}
/////////////////////////////////////////////////
function pageOffset(win)
/////////////////////////////////////////////////
{
    if(!win) win = window;
    var s = new Object();

    if(typeof win.pageXOffset != 'undefined')
    {
         // Mozilla/Netscape
         s.left = win.pageXOffset;
         s.top = win.pageYOffset;
    }
    else
    {
         var obj = getBody(win);
         s.left = obj.scrollLeft;
         s.top = obj.scrollTop;
    }
    return s;
}

/////////////////////////////////////////////////
// Der IE hat 2 verschiedene Objekte
// für den strict und quirks Mode.
function getBody(w)
/////////////////////////////////////////////////
{
    return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ?
    w.document.documentElement : w.document.body || null;
}

/////////////////////////////////////////////////
if(typeof addEvent != 'undefined') addEvent(window, 'load', getWinSize, false);
if(typeof addEvent!= 'undefined') addEvent(window, 'load', getDocSize, false);

if(typeof JS_LIB == 'undefined') alert('Fehler!!!\n\n"lib.js" muss zuerst eingebunden werden.');
self.doc_width = 0;
self.doc_height = 0;
self.win_width = 0;
self.win_height = 0;