/* -----------------------------------------------------------
 © 2004 Struppi
 http://javascript.jstruebig.de/lib/dom_layer.html
 Datum: 10.01.05

 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 zuverlässig bei
 ineinander verschachtelten Layern (Netscape 4).

 Folgende Methoden existieren:

 .top( [top] )
 .left( [left] )
 .width( [width] )
 .height( [height] )
 .right( [right] )
 .bottom( [bottom] )
 .moveTo(top, left)
 .sizeTo(width, height)
 .clip(t, r, b, l)
 .innerHTML( html )
 .getStyle( prop)
 .setStyle( prop, value)
 .attr( prop, value)
 .tags( name )
 .info()
----------------------------------------------------------- */


/////////////////////////////////////////////////
function getObjById(id, win)
/////////////////////////////////////////////////
{
    if(!id) return null;
    if(!win) win = window;
    return new DOM_Layer(id, win);
}

function DOM_Layer(obj, win)
{
    if(obj) this.ini(obj, win);
}

/////////////////////////////////////////////////
DOM_Layer.prototype.ini = function(o, w)
/////////////////////////////////////////////////
{
    if(typeof o == 'string')
    {
         if( document.getElementById ) o = w.document.getElementById(o);
         else if( document.all ) o = w.document.all[o];
         else if( w.document[o] ) o = w.document[o];
         else if( document.layers ) o = w.document.layers[o];
    }
    else if(typeof o != 'object')
    {
         return alert('Falscher Parameter: ' + o);
    }
    this.obj = o;
    this.win = w;
    this.id = o ? o.id : null;
    // IE Boxmodell Bug im Quirks Mode (Opera macht es auch)
    this.boxBug = !!(!(this.win.document.compatMode && this.win.document.compatMode == "CSS1Compat") && document.all);

}
;

/////////////////////////////////////////////////
DOM_Layer.prototype.height   = function(p) { if( p > 0 && p != get_height(this.obj)) set_height(this.obj, p); return get_height(this.obj); };
/////////////////////////////////////////////////
DOM_Layer.prototype.width    = function(p) { if( p > 0  && p != get_width(this.obj)) set_width(this.obj, p); 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.bottom   = function(p) { if(defined(p)) this.top( p - this.height());return this.top() + this.height();};
/////////////////////////////////////////////////
DOM_Layer.prototype.right    = function(p) { if(defined(p) ) this.left( p - this.width()); return this.left() + this.width();};
/////////////////////////////////////////////////
DOM_Layer.prototype.resizeTo = function(w, h) { if(this.obj.resizeTo) this.obj.resizeTo(w, h); else this.width(w); this.height(h);  };
/////////////////////////////////////////////////
DOM_Layer.prototype.moveTo   = function(t, l) { if(this.obj.moveTo) this.obj.moveTo(t, l); else set_top(this.obj, t); set_left(this.obj, l); };
/////////////////////////////////////////////////
DOM_Layer.prototype.attr     = function (p, v){  return defined(v) ? this.setStyle(p, v) : this.getStyle(p);};
/////////////////////////////////////////////////
DOM_Layer.prototype.display  = function(m){ return this.setStyle('display', m);}
/////////////////////////////////////////////////
DOM_Layer.prototype.hide     = function() { return this.show(false); }
/////////////////////////////////////////////////
DOM_Layer.prototype.setStyle = function (p, v){  var s = this.obj.style || this.obj; s[p] = v;  return s[p];};
/////////////////////////////////////////////////
DOM_Layer.prototype.getStyle = function (p){  var s = this.obj.style || this.obj; return s[p];};
/////////////////////////////////////////////////
DOM_Layer.prototype.show = function(m)
/////////////////////////////////////////////////
{
    if(!defined(m)) return this.getStyle('visibility').indexOf('hid') == -1;

    var v = document.layers ? 'show' : 'visible';
    if(m == false) v = 'hidden';

    return this.setStyle('visibility', v).indexOf('hid') != -1;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.innerHTML = function ( n )
/////////////////////////////////////////////////
{
    if(typeof this.obj.innerHTML != 'undefined')
    {
         if(defined(n) ) this.obj.innerHTML = n;
         return this.obj.innerHTML;
    }
    else if(document.layers)
    {
       if(defined(n))
       {
              with(this.obj.document)
              {
                   open();
                   write(n);
                   close();
              }
              return n;
       }
    }
    return 'nicht unterstützt';
}
/////////////////////////////////////////////////
DOM_Layer.prototype.tags = function ( n )
/////////////////////////////////////////////////
{
    if(this.obj.all)
    {
         return this.obj.all.tags(n);
    }
    else if(this.obj.getElementsByTagName)
    {
         return this.obj.getElementsByTagName( n );
    }
    return null;
};
/////////////////////////////////////////////////
DOM_Layer.prototype.clip = function ( t, r, b, l )
/////////////////////////////////////////////////
{
    if(!defined(t)) t = 0;
    if(!defined(r)) r = 0;
    if(!defined(l)) l = this.width();
    if(!defined(b)) b = this.height();

    this.setStyle('clip', 'rect(' + t + 'px, ' + r + 'px, ' + b + 'px, ' + l + 'px)');
}
/////////////////////////////////////////////////
DOM_Layer.prototype.info = function ()
/////////////////////////////////////////////////
{
    return 'Layer:' + this.id + NL + NL
    + 'top/left: '
    + this.top() + ' / ' + this.left()  + NL
    + 'width x heigth: ' + this.width() +  ' x ' + this.height() + NL
    + 'sichtbar:' + this.show() + NL
    + NL + 'innerHTML:' + this.innerHTML()
    ;
};
DOM_Layer.prototype.borderH = function (  )
{
    return this.boxBug  ? 0 :// beim falschen Boxmodell muss der Rahmen nicht abgezogen weren
    parseInt( this.getStyle('borderLeftWidth') || 0 )
    + parseInt( this.getStyle('borderRightWidth') || 0)
    + parseInt( this.getStyle('marginLeft')  || 0)
    + parseInt( this.getStyle('marginRight') || 0)
    + parseInt( this.getStyle('paddingLeft')  || 0)
    + parseInt( this.getStyle('paddingRight') || 0)
    ;
}

/////////////////////////////////////////////////
// alte Funktionen, um Fehler zu vermeiden
DOM_Layer.prototype.setProp = function (p, v){alert('Funktion nicht mehr verwenden nimm stattdessen setStyle()'); return this.setStyle(s,p); };
DOM_Layer.prototype.getProp = function (p){    alert('Funktion nicht mehr verwenden nimm stattdessen setStyle()'); return this.getStyle(s); };

var Px = document.childNodes ? 'px' : '';
var NL = '\n';
var undef = 'undefined';
function defined(arg) { return (typeof arg != undef) };



/////////////////////////////////////////////////////////////////////
// Zugriffsfunktion
//
// Hier werden die verschiedenen Browsermodelle
// ab den 4'er Versionen initialisiert.
// Diese Variante hat den Vorteil, dass die Funktionen
// immer nur einmal erzeugt werden.

// Die get Methoden
if(document.getElementById || document.all)
{
    // Das DOM Modell
    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; };

    if(window.getComputedStyle)
    {
         // Mozilla
         get_width  = function(o) { return parseInt( window.getComputedStyle(o, null).getPropertyValue('width'));};
         get_height = function(o) { return parseInt( window.getComputedStyle(o, null).getPropertyValue('height')); };
    }
    else
    {
         get_width  = function(o) { return o.offsetWidth;};
         get_height = function(o) { return o.offsetHeight; };
    }
}
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.resizeTo(p, get_height(o) ); };
    set_height = function(o,p) { o.resizeTo(get_width(o), p ); };
}


/////////////////////////////////////////////////////////////////////
// Globale Fensterfunktionen
// um die Größe des Fensters oder Dokumentes zu bestimmen
//


////////////////////////////////////////////////////////////
// getDocSize(window)

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

    if (typeof document.height != 'undefined')
    {
        s.width =  w.document.width;
        s.height = w.document.height;
    }
    else
    {
        var obj = getBody(w);
        s.width = obj.scrollWidth;
        s.height = obj.scrollHeight;
    }
    return s;
}

////////////////////////////////////////////////////////////
// getWinSize(window)
function getWinSize(win)
{
    if(!win) win = window;
    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);
    }
    return s;
}
////////////////////////////////////////////////////////////
// offset(window)
function pageOffset(win)
{
    if(!win) win = window;
    var pos = {left:0,top:0};

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


////////////////////////////////////////////////////////////
// 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;
}
