var menus={};
var timers={};

function xposbyid(id) {
  var obj=document.getElementById(id);
  if (!obj) return 0;
  var left=0;
  if (obj.offsetParent) {
    while (1) {
      left=left+obj.offsetLeft;
      if (!obj.offsetParent) break;
      obj=obj.offsetParent;
    }
  } else if (obj.x) {
    left=left+obj.x;
  }
  return left;    
}
function xposbyobj(obj) {
  if (!obj) return 0;
  var left=0;
  if (obj.offsetParent) {
    while (1) {
      left=left+obj.offsetLeft;
      if (!obj.offsetParent) break;
      obj=obj.offsetParent;
    }
  } else if (obj.x) {
    left=left+obj.x;
  }
  return left;    
}
function Menu(id) {
  var div=document.createElement("div");
  div.id=id;
  div.style.zIndex=1002;
  div.style.position="absolute";
  div.style.visibility="hidden";
  div.style.display="none";
  div.style.width=170;
  div.style.top=205;
  div.style.left=0;
  div.style.background="#f0f0f0 url(/img/menu.gif) top left no-repeat";
  div.style.borderLeft="1px solid #c0c0c0";
  div.style.borderTop="1px solid #c0c0c0";
  div.style.borderRight="1px solid #808080";
  div.style.borderBottom="1px solid #808080";
  div.style.padding=2;
  div.setAttribute("items",0);
  div.onmouseover=function() {
    if (timers[this.id]) clearTimeout(timers[this.id]);
  }
  div.onmouseout=function() {
    var cmd="killmenu('"+this.id+"')";
    timers[this.id]=setTimeout(cmd,150);
  }
  document.body.appendChild(div);
  var shdiv=document.createElement("div");
  shdiv.className="menushadow";
  shdiv.style.zIndex=1001;
  shdiv.style.top=205;
  shdiv.style.left=0;
  shdiv.style.height=6;
  this.div=div;
  this.shdiv=shdiv;
  document.body.appendChild(shdiv);
  this.additem=function(title,url,wloc) {
    var div=document.createElement("div");
    div.className="menuitem";
    if (title=="-") {
      div.innerHTML="<center><img src=\"/img/menuseparator.gif\"></center>";
    } else {
      div.innerHTML=title;
    }
    div.setAttribute("url",url);
	div.setAttribute("wloc",wloc);
    this.div.setAttribute("items",parseInt(this.div.getAttribute("items"))+1);
    this.shdiv.style.height=parseInt(this.shdiv.style.height)+17;
    if (title!="-") {
      div.onmouseover=function() {
        this.style.color="#ffffff";
        this.style.background="#FF6600";
      }
      div.onmouseout=function() {
        this.style.color="#000000";
        this.style.background="#f0f0f0";
      }
      div.onclick=function() {
        var url=this.getAttribute("url");
        var wloc=this.getAttribute("wloc");
        if (url) window.open(url,wloc);
      }
    } else {
      div.style.cursor="default";
    }
    this.div.appendChild(div);
  }
  this.show=function(trigger) {
    this.trigger=trigger;
    var x=xposbyobj(trigger);
    this.div.style.left=x+10;
    this.shdiv.style.left=x+10;
    this.div.style.display="block";
    this.div.style.visibility="visible";
    this.shdiv.style.display="block";
    this.shdiv.style.visibility="visible";
  }
  this.hide=function() {
    if (this.trigger) {
      this.trigger.style.background="url(/img/divider.gif) top right no-repeat";
      this.trigger.style.color="#606060";
    }
    this.div.style.display="none";
    this.div.style.visibility="hidden";
    this.shdiv.style.display="none";
    this.shdiv.style.visibility="hidden";
  }
  menus[id]=this;
  return this;
}
function killmenu(id) {
  if (menus[id]) menus[id].hide();
}
function initmenus() {
  var xml=rpcget("/menu.xml");
  var ul=document.getElementById("menucontainer");
  var mainmenu=xml.getElementsByTagName("menu");
  for (var i=0; i<mainmenu.length; i++) {
    var menu=mainmenu.item(i);
    var menuname=menu.getAttribute("name");
    var title=menu.getElementsByTagName("title").item(0);
    var li=document.createElement("li");
    li.setAttribute("menu",menuname);
    li.innerHTML=getText(title);
    ul.appendChild(li);
    var thismenu=new Menu(menuname);
    var items=menu.getElementsByTagName("menuitem");
    for (var j=0; j<items.length; j++) {
      var menuitem=items.item(j);
      var type=menuitem.getAttribute("type");
      if (type=="link") {
        var title=menuitem.getElementsByTagName("title").item(0);
        var url=menuitem.getElementsByTagName("url").item(0); 
        var wloc=menuitem.getElementsByTagName("url").item(0).getAttribute("linktype");
        if (wloc=="ownwindow" ) { var wloc="_self" } else { var wloc="_blank" }
        thismenu.additem(getText(title),getText(url),wloc);
      }
      if (type=="separator") {
        thismenu.additem("-","");
      }
    }
  }
  var menuitems=document.getElementById("menu").getElementsByTagName("li");
  for (var i=0; i<menuitems.length; i++) {
    menuitems[i].onmouseover=function() {
      this.style.color="#000000";
      this.style.background="url(/img/selected.gif) top right no-repeat";
      var menu=this.getAttribute("menu");
      if (menu) {
        menus[menu].show(this);
        if (timers[menu]) clearTimeout(timers[menu]);
      }
    }
    menuitems[i].onmouseout=function() {
      var menu=this.getAttribute("menu");
      if (menu) {
        var cmd="killmenu('"+menu+"')";
        timers[menu]=setTimeout(cmd,20);
      }
    }
    menuitems[i].onclick=function() {
      var url=this.getAttribute("url");
      if (url) {
        window.open(url,"");
      }
    }
  }
  if (!document.getElementById("menutab")) return;
  var myitems=document.getElementById("menutab").getElementsByTagName("p");
  for (var i=0; i<myitems.length; i++) {
    myitems[i].onmouseover=function() {
      this.style.color="#ad1b1c";
    }
    myitems[i].onmouseout=function() {
      this.style.color="#606060";
    }
    myitems[i].onclick=function() {
      var url=this.getAttribute("url");
      if (url) { window.location.href=url; }
      var popup=this.getAttribute("popup");
      if (popup) {
        var width=this.getAttribute("popupwidth");
        var height=this.getAttribute("popupheight");
        var name=this.getAttribute("popupname");
        var resizable=this.getAttribute("popupresizable");
        var w=screen.width;
        var h=screen.height;
        var x=(w/2)-(width/2);
        var y=(h/2)-(height/2)-100;
        window.open(popup,name,"left="+x+",top="+y+",width="+width+",height="+height+",location=0,menubar=0,scrollbars=0,status=0,toolbar=0,resizable="+resizable);
      }
    }
  }
}
