/*
    Datei: layer.js
    Datum: 14.01.2004
    Autor: J. Strübig <jstruebig@web.de>

    Beschreibung: Funktionen um Layer mit Hilfe von Javascript zu manipulieren.

    Funktionsliste:

     getById(id [, win]);
        Sucht den Layer mit der id im Dokument doc (oder im aktuellen Dokument).

     fixLayer(id [,window])
        fixiert einen Layer an seiner Position.

     setVis(obj, mode)
        zeigt/versteckt einen Layer.

     pagePos(obj [, x, y])
        Die Position eines Layer.

     getSize(obj [, win])
        Die Größe eines Layers.

     pageOffset([window])
        aktuelle Scrollposition der Seite.

     getWinSize(window)
        Größe des Anzeigebreiches

     getDocSize(window)
        Größe des Dokumentes

*/
var DEBUG = false;


///////////////////////////////////////////////////////////
// getStyle(id)
function getStyle(id, win)
{
    if(!id) return null;
    if(typeof id == "string") id = getById(id, win);
    if(typeof id.style == 'undefined') return id;
    return id.style;
}
///////////////////////////////////////////////////////////
// getById(id [, win])

function getById(id, win)
{
    var doc = window.document;
    if(win) doc = win.document;

    if(typeof id == 'undefined') return null;

    var obj = null;
    if(document.getElementById) obj = doc.getElementById(id);
    else if(typeof document.layers != 'undefined')  obj = _findObj_(id, doc);
    else if(document.all) obj = doc.all[id];

    return obj;
}
////////////////////////////////////////////////////////////
// Ein Layer beschreiben
function print(obj, text, win)
{
    if(!obj) return;
    if(typeof obj == "string") obj = getById(obj, win);

    if(typeof obj.innerHTML != 'undefined')
    {
       obj.innerHTML = text;
    }
    else if(typeof document.layers != 'undefined')
    {

         obj.document.open('text/html');
         obj.document.write(text);
         obj.document.close();
    }

}
////////////////////////////////////////////////////////////
// Ein Layer fixieren

function fixLayer(id, win)
{
    if(!win) win = window;
    var obj = getById(id, win);
    if(!obj) return alert('Fehler\n\nID:' + id + ' ist nicht zu finden.');

    _STICKY_OBJ_[_STICKY_OBJ_.length] = new Sticky(obj, win);

    if (typeof win.onscroll != 'undefined') win.onscroll = _fixLayer_;
    else if(typeof document.layers != 'undefined')  setInterval ('_fixLayer_()', 50);
    else window.setInterval ('_fixLayer_()', 50);
    _fixLayer_();
    return obj
}

////////////////////////////////////////////////////////////
// setVis
function setVis(obj, mode, win)
{
    if(!obj) return null;
    var vis;
    if(typeof obj == "string") obj = getById(obj, win);

    if(typeof document.layers != 'undefined')
    {
         vis = mode ? 'show' : 'hidden';
         obj['visibility'] = vis;
         return obj['visibility'];
    }
    vis = mode ? 'visible' : 'hidden';
    obj.style['visibility'] =  vis;
    //alert(debugObj(obj));
    return obj.style['visibility']
}
////////////////////////////////////////////////////////////
// getSize(obj)
function getSize(obj, win)
{
    if(!obj) return null;
    if(typeof obj == "string") obj = getById(obj, win);

    var size = {width:0, height:0};

    if(typeof document.layers != 'undefined')
    {
         size.width = obj.clip.width;
         size.height = obj.clip.height;
    }
    else if(obj.offsetWidth)
    {
         size.width = parseInt(obj.offsetWidth);
         size.height = parseInt(obj.offsetHeight);
    }


    return size;
}

////////////////////////////////////////////////////////////
// pagePos(obj [,y, x])

function pagePos(obj, y, x, win)
{
    if(typeof obj == "string") obj = getById(obj, win);
    if(!win) win = window;
    if(!obj) return null;
    var pos;
    if(typeof x != 'undefined' && typeof y != 'undefined')
    {
         pos = pageOffset(win);

         if(typeof document.layers != 'undefined')
         {
              obj.moveTo(x ,y );
         }
         else
         {
              obj.style.left = x + 'px';
              obj.style.top = y + 'px';
         }
    }
    pos = {x:0, y:0};

    if(typeof obj.offsetLeft != 'undefined')
    {
         while (obj)
         {
             pos.x += obj.offsetLeft;
             pos.y += obj.offsetTop;
             obj = obj.offsetParent;
         }
    }
    else
    {
        pos.x = obj.left ;
        pos.y = obj.top ;
    }
    if(DEBUG) window.status = 'Pos:' + pos.x + ':'+ pos.y;

    return pos;
}
////////////////////////////////////////////////////////////
// offset(window)
function pageOffset(win)
{
    if(!win) win = window;
    var pos = {x:0,y:0};
    pos.x = typeof win.pageXOffset != 'undefined' ? win.pageXOffset : win.document.body.scrollLeft;
    pos.y = typeof win.pageYOffset != 'undefined' ? win.pageYOffset : win.document.body.scrollTop;
    return pos;
}
////////////////////////////////////////////////////////////
// getDocSize(window)
////////////////////////////////////////////////////////////
// getDocSize(window)
function getDocSize(w)
{
    if(!w) w = window;
    var pos = {w:0,h:0};
    if (document.height)
    {
        pos.w = w.document.width;
        pos.h = w.document.height;
    }
    else if (document.body && typeof document.body.scrollHeight != 'undefined')
    {
        pos.w = w.document.body.scrollWidth;
        pos.h = w.document.body.scrollHeight;
    }
    return pos;
}
////////////////////////////////////////////////////////////
// getWinSize(window)
function getWinSize(win)
{
    if(!win) win = window;
    var pos = {x:0,y:0};
    if(typeof win.innerWidth != 'undefined')
    {
        pos.w = win.innerWidth;
        pos.h = win.innerHeight;
    }
    else if (win.document.body)
    {
       pos.w = parseInt(document.body.clientWidth);
       pos.h = parseInt(document.body.clientHeight);
    }
    return pos;
}

/* Globale Hilfsfunktionen */

////////////////////////////////////////////////////////////
// Nur für den NC 4.x um Layer zu finden.

function _findObj_(n, doc)
{
    if(doc[n]) return doc[n];

    for(var i = 0; i < doc.layers.length; i++)
    {
         if(typeof doc.layers[i].document != 'undefined')
         {
              var obj = _findObj_(n, doc.layers[i].document);
              if(obj) return obj;
         }
    }
    return null;
}


////////////////////////////////////////////////////////////
// Sticky -> Hilfsstruktur um die Startwerte zu sichern

function Sticky(obj, win)
{
    var pos = pagePos(obj);

    this.x = pos.x;
    this.y = pos.y;
    this.obj = obj;
    this.win = win;
}
////////////////////////////////////////////////////////////
// Die eigentliche Funktion um einen Layer zu fixieren
var c = 0;

function _fixLayer_()
{
    for(var i = 0; i < _STICKY_OBJ_.length; i++)
    {
       var l = _STICKY_OBJ_[i];
       if(!l) continue;
       var offset = pageOffset(l.win);
       pagePos(l.obj, l.y + offset.y, l.x + offset.x, l.win);
    }
}
var _STICKY_OBJ_ = new Array(); // benötigt fixLayer