var mbars=new Array();

function XMenuItem(pid,ptitle,plink){
  this.id=pid;
  this.title=ptitle;
  this.link=plink;
}

function XMenu(pid,ptitle,script,image){
  this.id=pid;
  this.title=ptitle;
  this.expanded=0;
  this.owner="";
  this.script=script;
  this.image=image;
  this.items=new Array();
}

XMenu.prototype.addItem=function(item){
  this.items.push(item);
}

XMenu.prototype.expand=function(id){
  if (this.items[id] instanceof XMenuItem) {
    this.expanded=1-this.expanded;
    this.owner.render();
  }
}

var xid;

function click(id,link){
 if (xid != null) {
   var elem=document.getElementById(xid);
   
   if (elem!=null)
   	 elem.style.color="#ffffff";
 }
 
 xid=id;
 document.getElementById(xid).style.color="#ffff00";
 parent.Hauptframe.location.href=link;
}

XMenu.prototype.render=function(){
  var d=document;
  var m=d.getElementById(this.id);

  for (k=0; k<m.childNodes.length; k++)
    m.removeChild(m.childNodes[k]);

  var table=d.createElement("table");
  table.style.backgroundColor="#5A94CE";
  //table.style.border="1 solid #000000";
  var tr=table.insertRow(0);
  var td=d.createElement("td");
  var attr=d.createAttribute("colspan");
  attr.nodeValue="2";
  td.setAttributeNode(attr);
  tr.appendChild(td);
  var a=d.createElement("a");
  a.id="_"+this.id;
  var mlink="javascript:mbars[\""+this.owner.id+"\"].menus["+j+"].expand(0)";
  if (this.script!=null)
    mlink+=";click(\""+a.id+"\",\""+this.script+"\")";
  a.href=mlink;
  
  if (this.image!=null) {
    var img=d.createElement("img");
    img.setAttribute("src",this.image);
    img.setAttribute("border",0);

    if (this.owner.wimage!=null)
      img.setAttribute("width",this.owner.wimage);

    if (this.owner.himage!=null)
      img.setAttribute("height",this.owner.himage);

    a.appendChild(img);
  }
  a.appendChild(d.createTextNode(this.title));
  td.appendChild(a);

  if (this.expanded==1) {
    for(i=0; i<this.items.length; i++) {
      tr=table.insertRow(1+i);
      td=d.createElement("td");
      td.width=20;
      tr.appendChild(td);
      td=d.createElement("td");
      td.id=this.items[i].id;

      if (this.items[i] instanceof XMenu)
        td.onclick="javascript:mbars[\""+this.owner.id+"\"].menus["+j+"].expand("+i+")";

      a=d.createElement("a");
      a.id="_"+this.items[i].id;
      a.appendChild(d.createTextNode(this.items[i].title));
      a.href="javascript:click(\""+a.id+"\",\""+this.items[i].link+"\")";
      td.appendChild(a);
      tr.appendChild(td);
    }
  }

  m.appendChild(table);
}

function XMenuBar(id,x,y,w,h){
  this.id=id;
  this.px=x;
  this.py=y;
  this.width=w;
  this.height=h;
  this.menus=new Array();
  this.prepared=0;
  this.wimage=null;
  this.himage=null;
  mbars[this.id]=this;
}

XMenuBar.prototype.addMenu=function(menu){
  menu.owner=this;

  if (menu instanceof XMenu)
    this.menus.push(menu);
}

XMenuBar.prototype.setImageSize=function(width,height){
  this.wimage=width;
  this.himage=height;
}

XMenuBar.prototype.render=function(){
  if (this.prepared==1) {
    for(j=0; j<this.menus.length; j++)
      this.menus[j].render();
  } else {
    document.write('<table style="position:absolute; top:'+this.py+"px; left:"+this.px+"px; width="+this.width+"px; height:"+this.height+'">');

    for(j=0; j<this.menus.length; j++) {
      document.write('<tr><td id="'+this.menus[j].id+'" valign="top"></td></tr>');
      this.menus[j].render();
    }

    document.write("</table>");
    this.prepared=1;
  }
}
