function showMenu(menu) {
    stopTimeout();
    if (menu) {
        doHideMenu(menu);
        var ref = xGetElementById('menu');
        var obj = xGetElementById('mask');
        if (ref && obj) {
            document.activeMask = obj;
            xMoveTo(obj, xPageX(ref) + xWidth(ref), xPageY(ref) + xHeight(ref) + 37);
            xShow(obj);
        }
        var ref = xGetElementById('m' + menu);
        var obj = xGetElementById('menu' + menu);
        if (ref && obj) {
            document.activeMenu = obj;
            var offset = (menu == 5) ? 200 : 0;
            xMoveTo(obj, xPageX(ref) - offset, xPageY(ref) + xHeight(ref));
            xShow(obj);
        }
    }
}

function hideMenu(menu) {
    if (menu) {
        doHideMenu();
    } else {
        startTimeout();
    }
}

function doHideMenu(menu) {
    stopTimeout();
    if (document.activeMenu) {
        xHide(document.activeMenu);
        xMoveTo(document.activeMenu, -500, -500);
        document.activeMenu = null;
    }
    if (document.activeMask && !menu) {
        xHide(document.activeMask);
        xMoveTo(document.activeMask, -500, -500);
        document.activeMask = null;
    }
}

function startTimeout() {
    stopTimeout();
    if (document.activeMenu || document.activeMask) {
        document.hideTimer = setTimeout("doHideMenu()", 500);
    }
}

function stopTimeout() {
    if (document.hideTimer) {
        clearTimeout(document.hideTimer);
        document.hideTimer = null;
    }
}

function writeMenus() {
    var lang = (window.location.href.indexOf('/en/') >= 0) ? 'en' : 'de';
    var menu3 = new Array(
        'software.html',    'Software-Entwicklung',     'Software Development',
        'web.html',         'Web-Entwicklung',          'Web Development',
        'project.html',     'Projekt-Management',       'Project Management',
        'consult.html',     'Beratung / Schulung',      'Consulting / Training'
    );
    var menu4 = new Array(
        'facts.html',       'Fakten und Zahlen',        'Facts and Figures',
        'owner.html',       'Inhaber',                  'Owner',
        'credits.html',     'Impressum',                'Credits'
    );
    var menu5 = new Array(
        'rolotec.html',     'Rolotec AG',               'Rolotec AG',
//        'redtoo.html',      'redtoo / caatoosee AG',    'redtoo / caatoosee AG',
        'therastrat.html',  'TheraSTrat AG',            'TheraSTrat AG',
        'klubschule.html',  'Klubschule Migros',        'Klubschule Migros',
        'telekurs.html',    'Telekurs Luxembourg SA',   'Telekurs Luxembourg SA',
        'commtec.html',     'Comm-Tec GmbH',            'Comm-Tec GmbH',
        'beso.html',        'Besuchsdienst Solothurn',  'Besuchsdienst Solothurn'
    );
    var menu6 = new Array(
        'index.html',       'Adresse',                  'Address',
        'map.html',         'Stadtplan',                'Location',
        'car.html',         'Anreise mit Auto',         'Arrival by Car',
        'train.html',       'Anreise mit Zug',          'Arrival by Train'
    );
    var level = getLevel(lang);
    var pimg = makePath('', '../', level, 'img/');
    var purl = makePath(lang + '/', '', level);
    writeMask(pimg);
    writeMenu('menu3', pimg, purl + 'srv/', menu3, lang);
    writeMenu('menu4', pimg, purl + 'prf/', menu4, lang);
    writeMenu('menu5', pimg, purl + 'cst/', menu5, lang);
    writeMenu('menu6', pimg, purl + 'cnt/', menu6, lang);
}

function getLevel(lang) {
    var level = -1;
    var pos = window.location.href.indexOf('/' + lang + '/');
    if (pos >= 0) {
        level = 0;
        var str = window.location.href.substring(pos + lang.length + 2);
        while ((pos = str.indexOf('/')) >= 0) {
            level++;
            str = str.substring(pos + 1);
        }
    }
    return level;
}

function makePath(def, pfx, level, sfx) {
    var path = def;
    if (level >= 0) {
        path = pfx;
        for (var i = 0; i < level; i++) {
            path += '../';
        }
    }
    if (sfx) {
        path += sfx;
    }
    return path;
}

function writeMask(pimg) {
    document.write('<div class="layer1" id="mask">\n');
    document.write('<table cellspacing="0" cellpadding="0" border="0">\n');
    document.write('<tr><td><img src="' + pimg + 'm2.gif" alt="" width="425" height="5" border="0"></td></tr>\n');
    document.write('<tr><td><img src="' + pimg + 'white.gif" alt="" width="425" height="56" border="0"></td></tr>\n');
    document.write('</table>\n');
    document.write('</div>\n');
}

function writeMenu(id, pimg, purl, items, lang) {
    document.write('<div class="layer2" id="' + id + '">\n');
    document.write('<table class="menu" cellspacing="0" cellpadding="0" border="0">\n');
    document.write('<tr>\n');
    if (items.length > 12) {
        document.write('<td background="' + pimg + 'm2.gif"><img src="' + pimg + 'empty.gif" alt="" width="200" height="5" border="0"></td>\n');
    }
    document.write('<td><img src="' + pimg + 's1.gif" alt="" width="7" height="5" border="0"></td>\n');
    document.write('<td background="' + pimg + 'm2.gif"><img src="' + pimg + 'empty.gif" alt="" width="10" height="5" border="0"></td>\n');
    document.write('</tr>\n');
    lang = (lang == 'en') ? 2 : 1;
    if (items.length <= 12) {
        for (var i = 0; i < items.length; i += 3) {
            writeMenuItem1(pimg, purl, i == (items.length - 3), items[i], items[i + lang]);
        }
    } else if (items.length <= 24) {
        for (var i = 0; i < 12; i += 3) {
            writeMenuItem2(pimg, purl, i == 9, items[i], items[i + lang], items[i + 12], items[i + 12 + lang]);
        }
    }
    document.write('</table>\n');
    document.write('</div>\n');
}

function writeMenuItem1(pimg, purl, last, url, text) {
    document.write('<tr>\n');
    var sep = !last ? 's2' : 's3';
    document.write('<td><img src="' + pimg + sep + '.gif" alt="" width="7" height="14" border="0"></td>\n');
    document.write('<td><a href="' + purl + url + '" onmouseover="showMenu()" onmouseout="hideMenu()">' + text + '</a></td>\n');
    document.write('</tr>\n');
}

function writeMenuItem2(pimg, purl, last, url1, text1, url2, text2) {
    document.write('<tr>\n');
    if (url2 && text2) {
        document.write('<td align="right"><a href="' + purl + url2 + '" onmouseover="showMenu()" onmouseout="hideMenu()">' + text2 + '</a></td>\n');
        var sep = !last ? 's4' : 's5';
    } else {
        document.write('<td>&nbsp;</td>\n');
        var sep = !last ? 's2' : 's3';
    }
    document.write('<td><img src="' + pimg + sep + '.gif" alt="" width="7" height="14" border="0"></td>\n');
    document.write('<td><a href="' + purl + url1 + '" onmouseover="showMenu()" onmouseout="hideMenu()">' + text1 + '</a></td>\n');
    document.write('</tr>\n');
}

function lang() {
    var location = window.location.href;
    if (location.indexOf('/de/') >= 0) {
        window.location.href = location.replace(new RegExp('/de/'), '/en/');
    } else if (location.indexOf("/en/") >= 0) {
        window.location.href = location.replace(new RegExp('/en/'), '/de/');
    }
}
