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

 dom_layer.js V 3.01

 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);
    if( !this.obj ) return null;
}

/////////////////////////////////////////////////
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;
    this.t = this.top();
    this.l = this.left();
    this.w = this.width();
    this.h = this.height();
}
;

/////////////////////////////////////////////////
DOM_Layer.prototype.height   = function(p)
{
    if( p > 0 && p != this.h) set_height(this.obj, p);
    return this.h = get_height(this.obj);
};
/////////////////////////////////////////////////
DOM_Layer.prototype.width    = function(p)
{
    if( p > 0  && p != this.w) set_width(this.obj, p);
    return this.w = get_width(this.obj);
};
/////////////////////////////////////////////////
DOM_Layer.prototype.top      = function(p)
{
    if(defined(p) && p != this.t) set_top(this.obj, p);
    return this.t = get_top(this.obj);

};
/////////////////////////////////////////////////
DOM_Layer.prototype.left     = function(p)
{
    if(defined(p) && p != this.l) set_left(this.obj, p);
    return this.l = get_left(this.obj);
};
/////////////////////////////////////////////////
DOM_Layer.prototype.bottom   = function(p)
{
    if(defined(p) && this.b != p) this.top( p - this.height());
    return this.b + this.h;
};
/////////////////////////////////////////////////
DOM_Layer.prototype.right    = function(p)
{
    if(defined(p) && p != this.r ) this.left( p - this.width());
    return this.l + this.w;
};
/////////////////////////////////////////////////
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)
    {
         if( !n ) n = '*';
         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 = this.w;
    if(!defined(l)) l = 0;
    if(!defined(b)) b = this.h;

    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()
    ;
};

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.

if(document.getElementById || document.all)
{
    // set mit dem style Attribut
    // Browser IE 4+, MZ, FF, OP
    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; };

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

    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_weight(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;
}