//
// SIDE EFFECTS:
// - expects a div (or layer for Netscape 4.x) with the id "menuLayer"
//   (this layer can be created via createMenuLayer())
// - expects the images "menuWeiss0", "menuWeiss1", ...
//   to determine the x position of the menu
//
var menuColors = new Array(
    "#17548C",
    "#17548C",
    "#17548C",
    "#17548C"
);

var menuItemHiColors = new Array(
    "#010C68",
    "#010C68",
    "#010C68",
    "#010C68"
);

var menuBarHiColors = new Array(
    "#bb0000",
    "#BB0000",
    "#BB0000",
    "#BB0000"
);

var spacerImage = '../Bilder/0.gif'; // transparent spacer image
var menuWidth = 160;
var menuItemHeight = 16;

var menuCurrent = null;
var menuPathPrefix = 'menu';

//
// get page objects...
//
function getImage(id, layer, parentLayer) {
        var img = null;
        if (isNetscape4) {
        img = (layer ? getLayer(layer, parentLayer).obj.document.images[id] : document.images[id]);
        }
    else if (document.all) {
            img = document.all[id];
        img.x = img.offsetLeft ? img.offsetLeft : 0;
        img.y = img.offsetTop ? img.offsetTop : 0;
    }
    else if (document.getElementById) {
        img = eval('document.getElementById("' + id + '")')
        img.x = img.offsetLeft;
        img.y = img.offsetTop;
    }
    return img;
}

function getMenuLayer() {
        var tempObj = getLayer("menuLayer");
    return tempObj.obj;
}

//
// build html helper functions...
//
function attr(name, val) { // wrapper for tag attribute
    if (val && val != "") return ' ' + name + '="' + val + '"';
    return "";
}

function a(href, cls, onMouseOver, onMouseOut, onClick) { // wrapper for <a> tag
    return "<a" + attr("href", href) + attr("class", cls) + attr("onMouseOver", onMouseOver) + attr("onMouseOut", onMouseOut) + attr("onClick", onClick) + ">";
}

function e_a() { // wrapper for </a> tag
    return "</a>";
}

function img(src, id, width, height) { // wrapper for <img> tag
        return "<img" + attr("src", src) + attr("name", id) + attr("id", id) + attr("width", width) + attr("height", height) + attr("border", "0") + ">";
}

function div(id, x, y, w, h, bgcolor, visibility, onMouseOver, onMouseOut) { // wrapper for <div> (<layer>) tag
        if (isNetscape4) {
                return "<layer" + attr("name", id) + attr("position", "absolute") + attr("pagex", x) + attr("pagey", y) + attr("width", w) + attr("height", h) + attr("bgcolor", bgcolor) + attr("visibility", visibility) + attr("onMouseOver", onMouseOver) + attr("onMouseOut", onMouseOut) + ">";
        }
    else {
                return "<div" + attr("id", id) + attr("style", "position:absolute;left:" + x +"px;top:" + y + "px;width:" + w + "px;height:" + h + "px;background-color:" + bgcolor + ";visibility:" + visibility) + attr("onMouseOver", onMouseOver) + attr("onMouseOut", onMouseOut) + ">";
        }
}

function e_div () { // wrapper for </div> (</layer>) tag
        if (isNetscape4) {
        return "</layer>";
    }
    else {
            return "</div>\n";
    }
}

//
// build html...
//
function createMenuBar() {
    var html = "";
    for (var i = 0; i < menuArray.length; ++i) {
        html += img(spacerImage, "menuWeiss" + i, 1, 10)
        var label = menuArray[i][0]
            var url = menuArray[i][1];
        var onMouseOver = "menuClearTimeout();menuBarItemHiLo(this,menuBarHiColors[" + i + "]);openMenu(" + i + ")";
        var onMouseOut = "menuInstallTimeout(250);menuBarItemHiLo(this,'#feb705')";
        var onClick = "openMenu(" + i + ")";
        html += a(url, "menuBar", onMouseOver, onMouseOut, onClick) + "&nbsp;&nbsp;&nbsp;" + label + "&nbsp;&nbsp;&nbsp;" + e_a();
    }
    return html;
}

function createMenu(array, bgColor) {
    var html = "";
    for (var i = 2; i < array.length; ++i) {
        var label = array[i][0]
            var url = array[i][1];
        array[i].path = menuPathPrefix + '_' + i;
        var div_onMouseOver = 'menuClearTimeout();menuItemHiLo(this,true)'
        var div_onMouseOut = 'menuItemHiLo(this,false);menuInstallTimeout(250);'
        var a_onMouseOver = 'menuClearTimeout();'
        var a_onMouseOut = null
        var a_onClick = null;
        // die layer sind zunächst 'hidden', weil sie sonst in Netscape 4 schon
        // gerendert werden würden, obwohl der eigentliche menu layer noch unsichtbar ist...
                html += div(array[i].path, 0, (i-2)*menuItemHeight, menuWidth, menuItemHeight, bgColor, 'hidden', div_onMouseOver, div_onMouseOut)
                + img(spacerImage, null, 11, 11)
                + a(url, "menuItem", a_onMouseOver, a_onMouseOut, a_onClick) + label + e_a()
              + e_div();
        }
    array.height = (array.length-2)*menuItemHeight;
        return html;
}

//
// timer functions (for delayed closing of the menu)...
//
var menuTimeout

function menuClearTimeout () {
        if (menuTimeout) {
                clearTimeout(menuTimeout);
        }
}

function menuInstallTimeout(timeoutTime) {
        menuClearTimeout();
        menuTimeout = setTimeout("closeMenu()", timeoutTime > 0 ? timeoutTime : 200);
}

//
// action helper functions...
//
function setVisibility(obj, visibility) {
        if (isNetscape4) {
        if (obj && obj.visibility) obj.visibility = visibility;
    }
    else {
        if (obj && obj.style && obj.style.visibility){
                        obj.style.visibility = visibility;
                }
    }
}

function showLayer(layer) {
    setVisibility(layer, 'visible');
}

function hideLayer(layer) {
    setVisibility(layer, 'hidden');
}

function setLayerPosDim(obj, x, y, w, h) {
        if (isNetscape4) {
                if (x != null) { obj.left = Number(x); }
                if (y != null) { obj.top = Number(y); }
        if (w != null) { obj.width = Number(w); }
        if (h != null) { obj.height = Number(h); }
        }
    else {
                if (x != null) { obj.style.left = x; }
                if (y != null) { obj.style.top = y; }
                if (w != null) { obj.style.width = w; }
                if (h != null) { obj.style.height = h; }
        }
}

function setMenuLayer(html) {
    var menuLayer;
    if (document.layers) {
        menuLayer = document.layers['menuLayer'];
        if (menuLayer) {
            menuLayer.document.open();
                    menuLayer.document.writeln(html);
                    menuLayer.document.close();
        }
    }
    else if (document.all) {
        menuLayer = document.all.menuLayer;
        if (menuLayer) {
                    menuLayer.innerHTML = html;
        }
    }
    else if (document.getElementById) {
        menuLayer = document.getElementById('menuLayer');
        if (menuLayer) {
            var range = document.createRange();
            range.selectNodeContents(menuLayer);
            range.deleteContents();
            menuLayer.appendChild(range.createContextualFragment(html));
        }
    }
}

//
// actions...
//
function menuBarItemHiLo(menuBarItem, color) {
    if (menuBarItem.style) {
        menuBarItem.style.color = color;
    }
}

function menuItemHiLo(menuItemLayer, highlight) {
    if (!menuCurrent) return;
        if (menuItemLayer.style) {
                if (highlight) {
                        menuItemLayer.style.backgroundColor = menuCurrent.hiBgColor;
        }
                else {
                        menuItemLayer.style.backgroundColor = menuCurrent.bgColor;
        }
        }
    else {
                if (highlight) {
                        menuItemLayer.bgColor = menuCurrent.hiBgColor;
        }
                else {
                        menuItemLayer.bgColor = menuCurrent.bgColor;
        }
    }
}

function openMenu(index) {
        if (menuCurrent == menuArray[index]) return; // menu already open, done
    if (menuCurrent) closeMenu(); //another menu is still open, close it
        menuCurrent = menuArray[index];
    menuCurrent.bgColor = menuColors[index];
    menuCurrent.hiBgColor = menuItemHiColors[index];
    setMenuLayer(createMenu(menuCurrent, menuColors[index]));
        var img = getImage("menuWeiss" + index);
    var x = img.x
    if (isNetscape4) x+= 5; else if (isNetscape6) x+= 0; else x+= 0;
    var y = y
    if (isNetscape4) y=90; else if (isNetscape6) y=90; else y=91;
        setLayerPosDim(getMenuLayer(), x, y, menuWidth, menuCurrent.height);
    for (var i = 2; i < menuCurrent.length; ++i) {
        showLayer(getLayer(menuCurrent[i].path, getMenuLayer()).obj);
    }
    showLayer(getMenuLayer());
}

function closeMenu () {
    if (!menuCurrent) return; // menu already closed, done
    hideLayer(getMenuLayer());
    setMenuLayer("");
        setLayerPosDim(getMenuLayer(), 0, 0, 1, 1);
        menuCurrent = null;
}