/* -----------------------------------------------------------
 © 2004 Struppi
 Datum: 30.03.2005

 base.js V 0.01

 Beschreibung:

 Folgende Methoden existieren:

 .top( [top] )
 .left( [left] )
 .width( [width] )
 .height( [height] )
 .right( [right] )
 .bottom( [bottom] )
 .style( prop)
 .info()
----------------------------------------------------------- */
var Px = document.childNodes ? 'px' : '';
var undef = 'undefined';
function defined(arg) { return (typeof arg != undef) };

/////////////////////////////////////////////////
window.getElementById = function(id, win)
/////////////////////////////////////////////////
{
    if(!id) return null;
    if(!win) win = window;
    return win[id] = 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(!w) w = window;
    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.rect = {
     top:    0,
     left:   0,
     right:  0,
     bottom: 0,
     width:  0,
     height: 0
    };
    this.getRect();
    return this;
};

/////////////////////////////////////////////////
DOM_Layer.prototype.Obj = function() { return this.obj; };
/////////////////////////////////////////////////

/////////////////////////////////////////////////
DOM_Layer.prototype.top = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('top', p + Px);
         this.getRect();
    }
    return this.rect.top;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.left = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('left', p + Px);
         this.getRect();
    }
    return this.rect.left;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.width = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('width', p + Px);
         this.getRect();
    }
    return this.rect.width;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.height = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         this.style('height', p + Px);
         this.getRect();
    }
    return this.rect.height;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.right = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         var doc = getWinSize(this.win);
         this.left(doc.width - this.width() - p );
         this.getRect();
    }
    return this.rect.left + this.rect.width;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.bottom = function(p)
/////////////////////////////////////////////////
{
    if(typeof p != 'undefined')
    {
         var doc = getDocSize(this.win);
         this.top(doc.height - this.height() - p);
         this.getRect();
    }
    return this.rect.top + this.rect.height;
}

/////////////////////////////////////////////////
DOM_Layer.prototype.getRect = function()
/////////////////////////////////////////////////
{
    var o = this.Obj();
    this.rect.left = 0;
    this.rect.top = 0;
    while (o)
    {
         this.rect.top += parseInt(o.offsetTop );
         this.rect.left += parseInt(o.offsetLeft );
         o = o.offsetParent;
    }
    this.rect.height = this.Obj().offsetHeight;
    this.rect.width = this.Obj().offsetWidth;
    return this.rect;
}
/////////////////////////////////////////////////
DOM_Layer.prototype.info = function()
/////////////////////////////////////////////////
{
    this.getRect();
    var text = '____________\n  Layer - Info   \n¯¯¯¯¯¯¯¯¯¯¯¯\n'
    + 'Layer ID: ' + this.Obj().id + '\n'
    + 'Top/Left:' + this.rect.top + '/' + this.rect.left + '\n'
    + 'Höhe/Breite:' + this.rect.height + '/' + this.rect.width + '\n'
    ;
    return text;

}
DOM_Layer.prototype.print = 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))
         {
              this.obj.document.open();
              this.obj.document.write(n);
              this.obj.document.close();
              return n;
       }
    }
    return 'nicht unterstützt';
}

DOM_Layer.prototype.style = function(name, attr)
{
    if(!this.obj) return null;
    var s = this.obj.style ? this.obj.style : this.obj;
    if(  attr != 'null' && typeof attr != 'undefined')
    {
         if(document.all && !window.opera ) s.setAttribute(name, attr);
         else s[name] = attr;
    }
    return s[name];
}
DOM_Layer.prototype.borderH = function (  )
{
    return this.boxBug  ? 0 :// beim falschen Boxmodell muss der Rahmen nicht abgezogen weren
    parseInt( this.style('borderLeftWidth') || 0 )
    + parseInt( this.style('borderRightWidth') || 0)
    + parseInt( this.style('marginLeft')  || 0)
    + parseInt( this.style('marginRight') || 0)
    + parseInt( this.style('paddingLeft')  || 0)
    + parseInt( this.style('paddingRight') || 0)
    ;
}


////////////////////////////////////////////////////////////
// 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);
        var test1 = document.body.scrollHeight;
        var test2 = document.body.offsetHeight || document.body.clientHeight;
        if (test1 > test2) // all but Explorer Mac
        {
              s.width = document.body.scrollWidth;
              s.height = document.body.scrollHeight;
        }
        else // Explorer Mac;
             //would also work in Explorer 6 Strict, Mozilla and Safari
        {
             s.width = document.body.offsetWidth || document.body.clientWidth;
             s.height = document.body.offsetHeight || document.body.clientHeight;
        }
        _alert(
        'offset:' + document.body.offseHeight + '\n' +
        'client:' + document.body.clientHeight + '\n' +
        'scroll:' + document.body.scrollHeight + '\n' +
        'scroll:' + document.body.scrollTop + '\n' +
        ''
        );

    }
    return s;
}
function _alert() {}
////////////////////////////////////////////////////////////
// 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;
}

window.onload = function()
{
    var all;
    if(document.getElementsByName)
    {
         all = document.getElementsByTagName('*');
    }
    else if(document.all)
    {
         all = window.document.all;
    }

    for(var i = 0; i < all.length; i++)
    {
         if(all[i].id)
         {
            getElementById(all[i].id);
            //window[all[i].id].style('position', 'absolute');
            // all[i].onclick = function() { alert( window[this.id].info() ); };
         }
    }
};
