/*
 © 2004 Struppi
 http://home.nexgo.de/struebig/computer/javascript
 Datum: 31.08.04

 dom_layer.js

 Beschreibung:

 Mit getObjById() wird ein Objekt erzeugt, mit dem grundlegende
 Funktionen zur Layer Manipulation zu Verfügung stehen.

 Das Modul ist nur für einfachste Fälle ausgelegt.
 Es funktioniert nicht mit ineinander verschachtelten Layern.

 Folgende Funktionen existieren:

 .top( [top] )
 .left( [left] )
 .width( [width] )
 .height( [height] )
 .moveTo(top, left)
 .sizeTo(width, height)
 .getProp( prop)
 .setProp( prop, value)
 .tags( name )
 .info()
*/


/////////////////////////////////////////////////
function getObjById(id, win)
{
    if(!id) return null;
    if(!win) win = window;
    var obj = null;

    if( document.getElementById ) obj = win.document.getElementById(id);
    if( document.all ) obj = win.document.all[id];
    if( win.document[id] ) obj = win.document[id];
    if( document.layers ) obj = win.document.layers[id];

    return obj ? new DOM_Layer(obj, win) : null;
}

function DOM_Layer(obj, win)
{
    this.win = win;
    this.obj = obj;
}
DOM_Layer.prototype.height = function(p) { if( p > 0) set_height(this.obj, p - this.borderV()); return get_height(this.obj); };
DOM_Layer.prototype.width  = function(p) { if( p > 0 ) set_width(this.obj, p - this.borderH()); return get_width(this.obj); };
DOM_Layer.prototype.top    = function(p) { if(defined(p)) set_top(this.obj, p); return get_top(this.obj); };
DOM_Layer.prototype.left   = function(p) { if(defined(p)) set_left(this.obj, p); return get_left(this.obj); };
DOM_Layer.prototype.resizeTo   = function(w, h) { this.width(w); this.height(h);  };
DOM_Layer.prototype.moveTo     = function(t, l) { set_top(this.obj, t); set_left(this.obj, l); };
DOM_Layer.prototype.info = function (){ return 'top/left: ' + this.top() + ' / ' + this.left() + '\nwidth x heigth: ' + this.width() +  ' x ' + this.height();    };
DOM_Layer.prototype.setProp = function (p, v){  var s = this.obj.style || this.obj; if(defined(v) ) s[p] = v; return s[p];};
DOM_Layer.prototype.getProp = function (p){  var s = this.obj.style || this.obj; return s[p];};
DOM_Layer.prototype.display = function(m){ return this.setProp('display', m);}
DOM_Layer.prototype.hide = function() { this.show(false); }
DOM_Layer.prototype.show = function(m)
{
    var v = document.layers ? 'show' : 'visible';
    if(m == false) v = 'hidden';

    return this.setProp('visibility', v);
}
DOM_Layer.prototype.borderH = function (  )
{
    var s = this.obj.style || this.obj;
    return  parseInt( s['borderLeftWidth'] || 0 )
    + parseInt( s['borderRightWidth'] || 0)
;
};
DOM_Layer.prototype.borderV = function (  )
{
    var s = this.obj.style || this.obj;
    return  parseInt( s['borderTopWidth'] || 0 )
    + parseInt( s['borderBottomWidth'] || 0)
    ;
};

DOM_Layer.prototype.marginH = function (  )
{
    var s = this.obj.style || this.obj;
    return parseInt( s['marginLeft'] || 0) + parseInt( s['marginRight'] || 0);
};
DOM_Layer.prototype.marginV = function (  )
{
    var s = this.obj.style || this.obj;
    return parseInt( s['marginTop']  || 0) + parseInt( s['marginBottom'] || 0);

};
DOM_Layer.prototype.innerHTML = function ( n )
{
    if(typeof this.obj.innerHTML != 'undefined')
    {
         if(typeof n != 'undefined') this.obj.innerHTML = n;
         return this.obj.innerHTML;
    }
    else if(document.layers)
    {
       this.obj.document.open();
       this.obj.document.write(n);
       this.obj.document.close();
       return n;
    }
}
DOM_Layer.prototype.tags = function ( n )
{
    if(this.obj.all)
    {
         return this.obj.all.tags(n);
    }
};


var undef = 'undefined';
function defined(arg) { return (typeof arg != undef) };
var Px = document.childNodes ? 'px' : '';
var NL = '\n';
/////////////////////////////////////////////////////////////////////
// Zugriffsfunktion
//
// Hier werden die verschiedenen Browsermodelle
// ab den 4'er Versionen initialisiert.
// Diese Variante hat den Vorteil, dass die Funktionen
// immer nur einmal beim einbinden erzeugt werden.


// Die get Methoden
if(document.getElementById || document.all)
{
// Das DOM Modell
get_width  = function(o) { return o.offsetWidth;};
get_height = function(o) { return o.offsetHeight; };
get_top    = function(o) { var y = 0; while (o) { y += parseInt(o.offsetTop );  o = o.offsetParent;  } return y; };
get_left   = function(o) { var x = 0; while (o) { x += parseInt(o.offsetLeft ); o = o.offsetParent;  } return x; };

}
else if(document.layers)
{
// Netscape 4
get_width  = function(o) { return o.clip.width || o.width ;};
get_height = function(o) { return o.clip.height  || o.height; };
get_top    = function(o) { return o.pageY || o.top; };
get_left   = function(o) { return o.pageX || o.left; };
}

// Die set Methoden
if(!document.layers)
{
// DOM bzw. das style Attribut
set_top = function(o, x) { o.style.top = x + Px; };
set_left = function(o, x) { o.style.left = x + Px; };
set_width  = function(o, x) {  o.style.width = x + Px; };
set_height = function(o, x) {  o.style.height = x + Px; };
}
else
{
// Netscape 4
set_top    = function(o,p) { o.pageY = p; };
set_left   = function(o,p) { o.pageX = p; };
set_width  = function(o,p) { o.clip.width = p;};
set_height = function(o,p) { o.clip.height  = p;};
}

function addLayer(id,win)
{
    if(!win) win = window;
    if(document.layers)
    {
         // Netscape 4
         win.document.layers[id] = new Layer( );

    }
    else if (win.document.body.appendChild)
    {
         var test = document.createElement('div');
         test.id = id;
         win.document.body.appendChild(test);
    }
    // Es muss immer heißen document.body egal ob im strict oder Quirks Mode
    else if (document.body.insertAdjacentHTML)
    {
         win.document.body.insertAdjacentHTML("afterBegin", '<div id="' + id + '"></div>');
    }
    else if (win.body.innerHTML) win.body.innerHTML += '<div id="' + id + '"></div>';

}
