var menus={};
var timers={};

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=275;
  div.style.left=0;
  div.style.background="#ef2525";
  div.style.border="1px solid #ef2525";
  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);
  this.div=div;
  this.additem=function(title,url,linktype) {
    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("linktype",linktype);
    this.div.setAttribute("items",parseInt(this.div.getAttribute("items"))+1);
    if (title!="-") {
      div.onmouseover=function() {
        this.style.color="#ffffff";
        this.style.background="#000000";
      }
      div.onmouseout=function() {
        this.style.color="#ffffff";
        this.style.background="#ef2525";
      }
      div.onclick=function() {
        var url=this.getAttribute("url");
        var linktype=this.getAttribute("linktype");
        if (url) {
          if (linktype=="newwindow") window.open(url);
          if (linktype=="ownwindow") window.location.href=url;
          if (linktype=="javascript") eval(url);
        }
      }
    } else {
      div.style.cursor="default";
    }
    this.div.appendChild(div);
  }
  this.show=function(trigger) {
    this.trigger=trigger;
    var x=getpagex(trigger);
    this.div.style.left=x;
    this.div.style.display="block";
    this.div.style.visibility="visible";
  }
  this.hide=function() {
    if (this.trigger) {
      this.trigger.style.backgroundPosition="0 0";
      this.trigger.style.color="#000000";
    }
    this.div.style.display="none";
    this.div.style.visibility="hidden";
  }
  menus[id]=this;
  return this;
}
function killmenu(id) {
  if (menus[id]) menus[id].hide();
}
function initmenus() {
  var xml=rpcget("/xml/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); 
        thismenu.additem(getText(title),getText(url),url.getAttribute("linktype"));
      }
      if (type=="separator") {
        thismenu.additem("-","");
      }
    }
  }
  var menuitems=document.getElementById("menucontainer").getElementsByTagName("li");
  for (var i=0; i<menuitems.length; i++) {
    menuitems[i].onmouseover=function() {
      this.style.color="#ffffff";
      this.style.backgroundPosition="160 0";
      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,"");
      }
    }
  }
}
function memberservice(idx) {
  if (coreuserid==0) {
    window.location.href="/register.html";
    return;
  }
  if (idx==1) {
    window.location.href="/uploadvideo.html";
  }
  if (idx==2) {
    window.location.href="/uploadphoto.html";
  }
  if (idx==3) {
    window.location.href="/managevideo.html";
  }
  if (idx==4) {
    window.location.href="/managephoto.html";
  }
  if (idx==5) {
    window.open("http://www.offroadracingmail.com/main","Mail","width=1024,height=768,resizable=1");
  }
}
function headerlogin() {
  var username=document.getElementById("username").value;
  var password=document.getElementById("password").value;
  var target=document.getElementById("target");
  if (target) target=target.value; else target="";
  var xml=rpcget("/core/login?action=login&username="+username+"&password="+password);
  var error=xml.getElementsByTagName("error");
  if (error.length>0) {
    alert(getText(error.item(0)));
    return;
  } else {
    var hash=getText(xml.getElementsByTagName("hash").item(0));
    if (target&&target.length) {
      window.location.href="/core/login?"+hash+"&"+target;
    } else {
      window.location.href="/core/login?"+hash;
    }
  }
}

