
MenuActivated=false;
VisibleMenu=null;
VisibleSender=null;

function ShowMenu(Sender) {
  if(VisibleMenu!=null) if(HideMenu(Sender)) return;

  var c=Sender.id.substring(9);
  var m=document.getElementById("menu"+c);
  if(m==null) return;

  VisibleSender=Sender;
  m.style.top="";

// non-IE menu
  if(!document.all) {
    m.style.position="fixed";
    m.style.visibility="";
    VisibleMenu=m;
    MenuDocument=document;

// stores possibly existing mousedown event and replaces with new
//    document.oldmousedown=document.onmousedown;
//    document.onmousedown=HideMenu;

    return;
  }

// a bug in Internet Explorer renders select elements above layers. also, IE does not understand "display:fixed;". IE-specific popup object must be used for correct display.
  if(VisibleMenu==null) VisibleMenu=window.createPopup();
  VisibleMenu.document.body.innerHTML="<div>"+m.innerHTML+"</div>";
  VisibleMenu.document.body.style.border="1px solid #888888";
  VisibleMenu.document.body.style.backgroundColor="#f0f0f0";

// duplicate style sheets from current document
  var css=document.getElementsByTagName("link");

  for(var i=0;i<css.length;i++) {
    var ss=css[i];
    if(ss.rel!="stylesheet") continue;

    var L=VisibleMenu.document.createElement("link");
    L.rel="stylesheet";
    L.type="text/css";
    L.href=ss.href;
    VisibleMenu.document.body.insertAdjacentElement("beforeEnd", L); 
  }

// duplicate function references of document object
//  VisibleMenu.document.MenuItemDivHilight=MenuItemDivHilight;
  VisibleMenu.document.HideMenu=HideMenu;
  VisibleMenu.document.MenuLinkAction=MenuLinkAction;
  VisibleMenu.document.MenuAction=MenuAction;
  VisibleMenu.document.MenuItemHilight=MenuItemHilight;
  VisibleMenu.document.MenuItemDivClick=MenuItemDivClick;
  VisibleMenu.document.body.className="menu";

  VisibleMenu.document.body.style.bgColor="#aaaaaa";
  VisibleMenu.document.MainWindow=this.document;

  CopyFunctions(m);

  VisibleMenu.show(0, 0, m.clientWidth+2, m.clientHeight-4, m);
  MenuDocument=VisibleMenu.document;

  window.setTimeout("CheckPopup()", 500);
}

function PopupClick() {
  window.parent.MenuPostback(this);
}

// copies href functions from current menu to popup clone menu. removes other mouse click events.

function CopyFunctions(Source) {
  for(var i=0;i<Source.children.length;i++) {
    var source=Source.children[i];
    CopyFunctions(source);

    if(source.nodeName.toLowerCase()!="a") continue;

    var target=VisibleMenu.document.getElementById(source.id);
    target.MainWindow=this;
    target.onclick=source.onclick;
    target.onmousedown=null;
    target.onmouseup=null;
    target.ondblclick=null;
  }
}

function ListProperties(o) {
  var s=window.open();
  var d=s.document;
  d.write("<table>");
  for(ss in o) {
    d.write("<tr><td>"+typeof(o[ss])+"</td><td>"+ss+"</td><td>"+o[ss]+"</td></tr>");
  }
  d.write("</table>");
  d.close();
}

function MenuPostback(Sender) {
  if(typeof(Page_ClientValidate)!="function" || Page_ClientValidate()) __doPostBack(MenuBaseId+":"+Sender.id, "");
}

function CheckPopup() {
  if(VisibleMenu==null) return;

  if(VisibleMenu.isOpen) {
    window.setTimeout("CheckPopup()", 500);
    return;
  }
  if(VisibleSender!=null) VisibleSender.className="menulabel";
  if(!document.all) {
    alert("2 deleting visiblemenu");
    VisibleMenu=null;
  }
}



// sometimes IE fails to show dialogs emerging from the onclick attribute javascript. however, calling these from the same attribute works. weird.

function MenuConfirm(s) {
  return window.confirm(s);
}

function MenuPrompt(s, d) {
  return window.prompt(s, d);
}

function MenuAlert(s) {
  window.alert(s);
}

function MenuOpenWindow(s) {
  window.open(s);
}




function HideMenu(Sender) {

  MenuActivated=true;
  window.setTimeout("MenuActivated=false;", 500);

  if(document.all) {  // IE
    if(VisibleMenu!=null) VisibleMenu.hide();
  } else { // others
    document.onmousedown=document.oldmousedown;
    if(VisibleMenu!=null) {
      VisibleMenu.style.visibility="hidden";

// mozilla and firefox fail to blink the cursor in rich text edit iframe under a layer (even hidden layer). this remedies the problem.
      VisibleMenu.style.top="-1000px";
    }
    VisibleMenu=null;
  }

  if(VisibleSender!=null) VisibleSender.className="menulabel";
  if(VisibleSender==Sender) {
    VisibleSender=null;
    return true;
  }
  return false;
}

function MenuLinkAction(Sender, Action) {
  MenuAction(document.getElementById(Sender), Action);
}

function MenuAction(Sender, Action) {
  if(Action=="hover") {
    if(VisibleSender!=null) VisibleSender.blur();
    if(Sender==VisibleSender) return;

    var Down=false;

    if(document.all) {
      if(VisibleMenu!=null && VisibleMenu.isOpen) Down=true;
    } else {
      if(VisibleMenu!=null) Down=true;
    }

    if(Down) {
      ShowMenu(Sender);
      Sender.className="menulabel_down";
    } else {
      Sender.className="menulabel_hover";
    }
    return;
  }

  if(Action=="basic") {
    if(Sender==VisibleSender) return;
    Sender.className="menulabel";
    return;
  }

  if(Action=="down") {
    Sender.className="menulabel_down";
    ShowMenu(Sender);
    return;
  }
}

function MenuItemHilight(Sender, Light) {
  if(MenuActivated) return;
  Sender.className="menuitem"+(Light ? "_hover" : "");

  if(MenuDocument==null) return;
  var p=MenuDocument.getElementById(Sender.id+"_c");
  if(p==null) return;
  p.className="menuitem"+(Light ? "_hover" : "");
}

/*

function MenuItemDivHilight(Sender, Light) {
  if(MenuActivated) return;
  Sender.className="menuitem"+(Light ? "_hover" : "");
  var cn=Sender.id.substr(0, Sender.id.length-2);
  if(MenuDocument==undefined) return;

  var p=MenuDocument.getElementById(cn);
  if(p==null) {
    cn=cn.replace(new RegExp(":", "gi"), "_");
    p=MenuDocument.getElementById(cn);
  }
  if(p==null) {
//    alert("could not find "+Sender.id+" "+cn+", sorry");
    return;
  }
  p.className="menuitem"+(Light ? "_hover" : "");
}
*/

function MenuItemDivClick(Sender, Light) {
  if(MenuDocument==undefined) return;

  var cn=Sender.id.substr(0, Sender.id.length-2);
  var p=MenuDocument.getElementById(cn);
  if(p==null) {
    cn=cn.replace(new RegExp(":", "gi"), "_");
    p=MenuDocument.getElementById(cn);
  }
  if(p==null) {
//    alert("could not find "+cn+", sorry");
    return;
  }
  Sender.className="menuitem_active";
  p.className="menulink_active";

  LastSender=Sender;
  LastLink=p;

  window.setTimeout("HideMenu(null);LastSender.className=\"menuitem\";LastLink.className=\"menuitem\";", 500);
  window.setTimeout("MenuActivated=false;", 500);

  if(MenuActivated) return;

  MenuActivated=true;

  if(typeof(p.onclick)=="function") {
    p.onclick(null);
    return;
  }

  var h=p.href;
  if(h.indexOf("javascript:")==0) h=h.substring(11);
//  alert(h);

  eval(h);
}
