// Gmenu - fixed by Andreas.Rehm@ibitar.de - allowing custom menu-width
// keep last width of top element
var swid = 0;
function findposx(s,level)
{
    if (level == 0)
    {
        var cnt = 2;
        var idx = 0;
    }
    else
    {
        var cnt = 2;
        var idx = 0;
    }
    var curLeft = 0;
    if (s.offsetParent)
    {
        while (s.offsetParent && (idx < cnt))
        {
            curLeft += s.offsetLeft;
            s = s.offsetParent;
            idx+=1;
        }
    }
    else if (s.x) { curLeft += s.x; }
    return curLeft;
}

function findposy(s,level)
{
    if (level == 0)
    {
        var cnt = 2;
        var idx = 0;
    }
    else
    {
        var cnt = 2;
        var idx = 0;
    }
    var curTop = 0;
    if (s.offsetParent)
    {
        while (s.offsetParent && (idx < cnt))
        {
            curTop += s.offsetTop;
            s = s.offsetParent;
            idx +=1;
        }
    }
    else if (s.x) { curTop += s.x; }
    return curTop;
}

function setmenus(s)
{
    curmenu = s;
}

function setsubmenus(s)
{
    submenu = s;
    var x = submenu.substring(0,3)
    x = x + '0';
    curmenu = x;
}

function expand(s,wid)
{
    curmenu = s.id;
    var x = s.id;
    var count = parseInt(x,10);
    count += 100;
    if (count < 1000)
    {
        x = '0' + count;
    }
    else
    {
        x = count;
    }
    var td = document.getElementById(x);
    var posx = findposx(s,0);
    var posy = findposy(s,0) + menuheight;
    if (td != null)
    {
        td.style.left = "" + posx + "px";
        td.style.top = "" + posy + "px";
        td.style.display = 'block';
    }
}

function checkpos(s)
{
    stop = window.setTimeout("collapse('"+s+"')",005);
}

function checksubpos(s)
{
    stopsub = window.setTimeout("collapsesub('"+s+"')",005);
}

function collapsesub(s)
{
    if (s.substring(0,3) != submenu.substring(0,3))
    {
        var td = document.getElementById(s);
        if (td != null)
        {
            var x = s;
            var count = parseInt(x,10);
            count += 1;
            if (count < 1000)
            {
                x = '0' + count;
            }
            else
            {
                x = count;
            }
            td = document.getElementById(x);
            if (td != null)
            {
                td.style.display = 'none';
            }
        }
    }
    if (s.substring(0,1) != curmenu.substring(0,1) || curmenu == '0')
    {
        var td = document.getElementById(s);
        if (td != null)
        {
            var x = s;
            var y = x.substring(0,2);
            y = y + '00';
            td = document.getElementById(y);
            if (td != null)
            {
                td.style.display = 'none';
            }
        }
    }
}

function collapse(s)
{
    if (s.substring(0,1) != curmenu.substring(0,1))
    {
        var td = document.getElementById(s);
        if (td != null)
        {
            var x = s;
            var count = parseInt(x,10);
            count += 100;
            if (count < 1000)
            {
                x = '0' + count;
            }
            else
            {
                x = count;
            }
            td = document.getElementById(x);
            if (td != null)
                { td.style.display = 'none';}
        }
    }
}

function expandsub(s,direction,wid)
{
    submenu = s.id;
    var x = s.id;
    var count = parseInt(x,10);
    count += 1;
    if (count < 1000)    {
        x = '0' + count;
    }
    else
    {
        x = count;
    }
    var td = document.getElementById(x);
    if (direction == 'right')
    {
        var posx = findposx(s,1) + wid;
    }
    else
    {
        var posx = findposx(s,1) - wid;
    }
    var posy = findposy(s,1);
    if (td != null)
    {
        td.style.left = "" + posx + "px";
        td.style.top = "" + posy + "px";
        td.style.display = 'block';
    }
}

function Menu()
{
    this.addItem = addItem;
    this.addsubmenu = addsubmenu;
    this.addsubmenuitem = addsubmenuitem;
    this.addlevel2menu = addlevel2menu;
    this.addlevel2menuitem = addlevel2menuitem;
    this.showMenu = showMenu;
    thousands = 0;
    hundreds = 0;
    tens = 0;
    digits = 0;
    htmlstr="";
    htmlstr += "<!--GMENU Version 2.1-AR-->";
    htmlstr += "<table cellpadding=0 cellspacing=0 border=0 class='gmenu'><tr><!--GMENUITEMS--></tr></table><!--SUBMENU-->";
    htmlstr += "<!--End of Gmenu-->";
}

function addItem(menuname,lnk,wid)
{
    var id = "";
    hundreds = 0;
    tens = 0;
    digits = 0;
    id = "" + thousands + hundreds + tens + digits + "";
    menustr = "";
    widthstr = "";
    if (wid>0){
       widthstr = "width=\""+wid+"\"";
    }
    if (lnk!='')
    {
      mylink = "<a href='"+lnk+"' class=\"gmenu\">"+menuname+"</a>";
    } else {
      mylink = "<font class=\"link\">"+menuname+"</font>";
    }
     menustr+= "<td class=\"gmenu\" "+widthstr+" id=\""+id+"\" onmouseover=\"expand(this,"+wid+")\" onmouseout=\"setmenus('');checkpos('"+id+"')\">"+mylink+"</td>";
    menustr+="<!--GMENUITEMS-->";
    htmlstr = htmlstr.replace("<!--GMENUITEMS-->",menustr);
    thousands+=1;
}

function addsubmenu(wid)
{
    // keep width for children
    swid = wid;
    var id = "";
    hundreds = hundreds + 1;
    currentthousands = thousands - 1;
    htmlstr = htmlstr.replace("<!--SUBMENUITEMS-->",'');
    id = "" + currentthousands + hundreds + tens + digits + "";
    pid = "" + currentthousands + "000";
    menustr = "";
    menustr+="<div id=\""+id+"\" class=\"submenu\" style=\"position:absolute;left:0;top:0;display:none;z-index:10;width: "+wid+"px\" onmouseover=\"setmenus('"+id+"')\" onmouseout=\"setmenus('');checkpos('"+pid+"')\"><!--SUBMENUITEMS--></div>";
    menustr+="<!--SUBMENU-->";
    htmlstr = htmlstr.replace("<!--SUBMENU-->",menustr);
}

function addsubmenuitem(name,lnk,direction)
{
    var id = "";
    tens = tens + 1;
    digits = 0;
    currentthousands = thousands - 1;
    id = "" + currentthousands + hundreds + tens + digits + "";
    menustr = "";
    if (lnk!='')
    {
        if (direction == 'left')
        {
            menustr+="<a id=\""+id+"\" href=\""+lnk+"\" onmouseover=\"setsubmenus('"+id+"');expandsub(this,'left',swid)\" onmouseout=\"setsubmenus('');checksubpos('"+id+"')\">"+name+"</a>";
        }
        else
        {
            menustr+="<a id=\""+id+"\" href=\""+lnk+"\" onmouseover=\"setsubmenus('"+id+"');expandsub(this,'right',swid)\" onmouseout=\"setsubmenus('');checksubpos('"+id+"')\">"+name+"</a>";
        }
    } else {
        if (direction == 'left')
        {
            menustr+="<font class=\"link2\" id=\""+id+"\" href=\""+lnk+"\" onmouseover=\"setsubmenus('"+id+"');expandsub(this,'left',swid)\" onmouseout=\"setsubmenus('');checksubpos('"+id+"')\">"+name+"</font>";
        }
        else
        {
            menustr+="<font class=\"link2\" id=\""+id+"\" href=\""+lnk+"\" onmouseover=\"setsubmenus('"+id+"');expandsub(this,'right',swid)\" onmouseout=\"setsubmenus('');checksubpos('"+id+"')\">"+name+"</font>";
        }
    } 
    menustr+="<!--SUBMENUITEMS-->";
    htmlstr = htmlstr.replace("<!--SUBMENUITEMS-->",menustr);
}

// Aufruf: Breite und Versatz
function addlevel2menu(wid,vers)
{
    var id = "";
    currentthousands = thousands - 1;
    digits+=1;
    htmlstr = htmlstr.replace("<!--LEVEL2ITEMS-->",'');
    id = "" + currentthousands + hundreds + tens + digits + "";
    pid = "" + currentthousands + hundreds + tens + "0";
    menustr = "";
    menustr+="<div id=\""+id+"\" class=\"level2menu\" style=\"position:absolute;display:none;z-index:10;width: "+wid+"px;margin-left:"+vers+"px\" onmouseover=\"setsubmenus('"+id+"')\" onmouseout=\"setsubmenus('');checksubpos('"+pid+"')\"><!--LEVEL2ITEMS--></div>";
    menustr+="<!--SUBMENU-->";
    htmlstr = htmlstr.replace("<!--SUBMENU-->",menustr);
}

function addlevel2menuitem(name,lnk)
{
    menustr = "";
    menustr+="<a href='"+lnk+"'>"+name+"</a>";
    menustr+="<!--LEVEL2ITEMS-->";
    htmlstr = htmlstr.replace("<!--LEVEL2ITEMS-->",menustr);
}

function showMenu()
{
//    alert(htmlstr);
    document.writeln(htmlstr);
}


var stop;
var stopsub;
var curmenu="";
var submenu="";
var menuheight=15;
var menu = new Menu();



menu.addItem('&nbsp;Startseite&nbsp;&nbsp;<font color="#ffffff">|</font>','index.html',0);//90
menu.addItem('&nbsp;Wir &uuml;ber uns&nbsp;&nbsp;<font color="#ffffff">|</font>','about.html',0);//120
menu.addItem('&nbsp;Vertretungen&nbsp;&nbsp;&nbsp<font color="#ffffff">|</font>','vertretung.html',0);//105
menu.addItem('&nbsp;Produkte&nbsp;&nbsp;<font color="#ffffff">|</font>','',0);//85

menu.addsubmenu(185);
menu.addsubmenuitem('&nbsp;AC-Motoren&nbsp;','ac-index.html','left');
menu.addsubmenuitem('&nbsp;High Torque Motoren<br/>&nbsp;und Generatoren&nbsp;','high-torque.html','right');
menu.addsubmenuitem('&nbsp;Servomotoren SBM,SHJ&nbsp;','servomotoren.html','right');
menu.addsubmenuitem('&nbsp;DC-Motoren&nbsp;','dc.html','right');
menu.addsubmenuitem('&nbsp;Frequenzumrichter&nbsp;','frequenz.html','right');
menu.addsubmenuitem('&nbsp;Servodrive XVy&nbsp;','servodrive.html','right');
menu.addsubmenuitem('&nbsp;Softstarter&nbsp;','softstarter.html','right');
menu.addsubmenuitem('&nbsp;Stromrichter&nbsp;','stromrichter.html','right');

menu.addItem('&nbsp;Referenzen&nbsp;&nbsp;<font color="#ffffff">|</font>','referenz.html',0);//105
menu.addItem('&nbsp;Download&nbsp;&nbsp;<font color="#ffffff">|</font>','download.html',0);//95
menu.addItem('&nbsp;Kontakte&nbsp;&nbsp;<font color="#ffffff">|</font>','kontakt.html',0);//80
menu.addItem('&nbsp;Impressum&nbsp;&nbsp;','impressum.html',0);//100
menu.showMenu();
