ie5 = (document.getElementById && document.all);
ns6 = (document.getElementById && !document.all);

maxDepth       = 5;
doShow         = true;
menuIdentifier = "contextmenu";
shdIdentifier  = "menushadow";


for (i = 0; i < maxDepth; i++)
  {
   document.getElementById("menucontainer").innerHTML += "<DIV ID=\""+shdIdentifier+"_"+i+"\" CLASS=\"menushadow\" STYLE=\"z-index:"+(99+10*i)+";visibility:hidden\" DISPLAY:none></DIV>\n<DIV ID=\""+menuIdentifier+"_"+i+"\" DEPTH="+i+" CLASS=\"menu\" STYLE=\"width:200px;z-index:"+(100+10*i)+"\" onMouseover=\"highlightEntry(event,true)\" onMouseout=\"highlightEntry(event,false)\" onClick=\"chooseEntry(event)\" DISPLAY:none></DIV>\n\n";
  }

function Rectangle(x1,y1,x2,y2)
  {
   this.coord = new Array();

   this.coord["left"]   = x1;
   this.coord["right"]  = x2;
   this.coord["top"]    = y1;
   this.coord["bottom"] = y2;
  }


function MenuNode(parent,id,label,menuwidth)
  {
   this.type       = "node";

   if (parent != null)
     {
      this.path    = parent.path+","+parent.itemID;
      this.itemID  = id;
     }
   else
     {
      this.path    = "";
      this.itemID  = 0;
     }

   this.width      = menuwidth;
   this.label      = label;
   this.children   = new Array();

   this.addEntry   = addEntry;
   this.addNode    = addNode;
   this.addDivider = addDivider;
  }

function MenuEntry(parent,id,label,url,target,alignment)
  {
   this.type       = "entry";
   this.path       = parent.path+","+parent.itemID;
   this.itemID     = id;

   this.label      = label;
   this.url        = url;
   this.target     = target;
   this.alignment  = alignment;
  }

function MenuDivider(parent,width)
  {
   this.type       = "divider";
   if (width == null) this.width = parent.width-28;
   else               this.width = width;
  }

function addNode(id,label,width)
  {
   if (id == null) id = this.children.length;

   this.children[id] = new MenuNode(this,id,label,width);
   return this.children[id];
  }

function addEntry(id,label,url,target,alignment)
  {
   if (id == null) id = this.children.length;

   this.children[id] = new MenuEntry(this,id,label,url,target,alignment);
   return this.children[id];
  }

function addDivider(width)
  {
   this.children[this.children.length] = new MenuDivider(this,width);
  }

function confirmAction(text,url)
  {
   if (confirm(text)) document.location.href = url;
  }

function finalizeMenu(menu,menuObj)
  {
   menucontent = "";
   menudepth   = menu.path.split(",").length;

   document.getElementById(menuIdentifier+"_"+depth).style.width = menu.width;

   for (i = 0; i < menu.children.length; i++)
     {
      curElement = menu.children[i];

      if (curElement.type == "divider")
        {
         menucontent += "<HR"+((curElement.width > 0)?(" WIDTH="+curElement.width):(""))+">";
        }
      else
        {
         if (curElement.type == "entry")
           {
            menucontent += "<DIV CLASS=\"menuitem\" ITEMID=\""+curElement.itemID+"\" ITEMPATH=\""+curElement.path+"\" onMouseover=\"hideSubMenus("+menudepth+")\" ALIGN=\""+curElement.alignment+"\">"+curElement.label+"</DIV>\n";
           }
         else
         if (curElement.type == "node")
           {
            menucontent += "<DIV CLASS=\"menuitem\" ITEMID=\""+curElement.itemID+"\" ITEMPATH=\""+curElement.path+"\" onMouseover=\"highlightEntry(event,true);showMenu(event)\"><DIV CLASS=\"menuarrow\">&nbsp;<IMG SRC=\"pics/arrow_normal.gif\" ALIGN=\"absbottom\"></DIV>"+curElement.label+"</DIV>\n";
           }
        }
     }

   menuObj.innerHTML = menucontent;
  }



function showMenu(e,fillfunction)
  {
   if (doShow && (ie5 || ns6))
     {
      if (!ie5) event = e;
      source = (ie5)?(event.srcElement):(event.target);

      if (source.parentNode.className != "menuarrow")
        {
         while (source.className != "menuitem" && source.className != "menu" && source.className != "")
           {
            source = source.parentNode;
           }

         if (source.className != "menuitem")
           {
            depth = 0;
            hideAllMenus();
            menuContent = eval(fillfunction);

            menu = menuContent;
           }
         else
           {
            depth = parseInt(source.parentNode.getAttribute("depth"))+1;
            hideSubMenus(depth);

            path     = source.getAttribute("itempath").toString().split(",");
            selEntry = parseInt(source.getAttribute("itemid"));
            menu     = menuContent;
            i        = 2;

            while (i < path.length)
              {
               menu = menu.children[path[i]];
               i++;
              }

            menu = menu.children[selEntry];
           }

         menuObj = document.getElementById(menuIdentifier+"_"+depth);
         shdObj  = document.getElementById(shdIdentifier+"_"+depth);

         finalizeMenu(menu,menuObj);

         map_newmenu = new Rectangle(0,0,menuObj.offsetWidth,menuObj.offsetHeight);

         if (depth == 0)
           {
            map_creation  = new Rectangle(((ie5)?(document.body.scrollLeft):(window.pageXOffset+1))+event.clientX,((ie5)?(document.body.scrollTop):(window.pageYOffset+1))+event.clientY,((ie5)?(document.body.scrollLeft):(window.pageXOffset+1))+event.clientX,((ie5)?(document.body.scrollTop):(window.pageYOffset+1))+event.clientY);

            rel_creationX = event.clientX;
            rel_creationY = event.clientY;
           }
         else
           {
            map_creation  = new Rectangle(parseInt(source.parentNode.style.left)+7,parseInt(source.parentNode.style.top)+source.offsetHeight*selEntry+7,parseInt(source.parentNode.style.left)+source.parentNode.offsetWidth-7,parseInt(source.parentNode.style.top)+source.offsetHeight*(selEntry+1));

            rel_creationX = map_creation.coord["right"]-((ie5)?(document.body.scrollLeft):(window.pageXOffset));
            rel_creationY = map_creation.coord["top"]-((ie5)?(document.body.scrollTop):(window.pageYOffset));
           }
   
         //NS bug that allows menu to reach out of screen

         avail_spaceX = ((ie5)?(document.body.clientWidth):(window.innerWidth-20))-rel_creationX-5;
         avail_spaceY = ((ie5)?(document.body.clientHeight):(window.innerHeight))-rel_creationY-5;

         pos_left = (avail_spaceX >= map_newmenu.coord["right"]) ?(map_creation.coord["right"]):(map_creation.coord["left"]-map_newmenu.coord["right"]);
         pos_top  = (avail_spaceY >= map_newmenu.coord["bottom"])?(map_creation.coord["top"]):(map_creation.coord["bottom"]-map_newmenu.coord["bottom"]);

         menuObj.style.left  = pos_left;
         menuObj.style.top   = pos_top;
         shdObj.style.left   = pos_left;
         shdObj.style.top    = pos_top;
         shdObj.style.width  = map_newmenu.coord["right"];
         shdObj.style.height = map_newmenu.coord["bottom"];

         menuObj.style.visibility = "visible";
         shdObj.style.visibility  = "visible";

         event.returnValue  = false;
         event.cancelBubble = true;

         return false;
        }
     }
  }

function highlightEntry(e,over)
  {
   source = (ie5)?(event.srcElement):(e.target);

   while (source.className != "menuitem" && source.className != "menu")
     {
      source = source.parentNode;
     }

   if (source.className == "menuitem")
     {
      img = source.getElementsByTagName("IMG");
      if (img[0] != null) img[0].src = "pics/arrow_"+((over)?("inverted.gif"):("normal.gif"));

      source.style.backgroundColor = (over)?("#316AC5"):("");
      source.style.color           = (over)?("white"):("black");
     }
  }

function chooseEntry(e)
  {
   source = (ie5)?(event.srcElement):(e.target);

   while (source.className != "menuitem" && source.className != "menu")
     {
      source = source.parentNode;
     }

   if (source.className == "menuitem")
     {
      if (source.getElementsByTagName("IMG").length == 0) // entries with arrow (extending entries) may not react to clicking
        {
         path = source.getAttribute("itempath").toString().split(",");
         menu = menuContent;
         i    = 2;

         while (i < path.length)
           {
            menu = menu.children[path[i]];
            i++;
           }

         menu = menu.children[source.getAttribute("itemid")];
         url  = menu.url;

         if (url.substr(0,11) == "javascript:") eval(url.substr(11));
         else window.location = url;
        }
      else
        {
         // but dont make the menu disappear then
         if (ie5)
           {
            event.returnValue  = false;
            event.cancelBubble = true;
           }
         else
           {
            e.returnValue  = false;
            e.cancelBubble = true;
           }
        }
     }
  }

function hideAllMenus()
  {
   for (i = 0; i < maxDepth; i++)
     {
      document.getElementById(menuIdentifier+"_"+i).style.visibility = "hidden";
      document.getElementById(shdIdentifier+"_"+i).style.visibility  = "hidden";
     }
  }

function hideSubMenus(level)
  {
   for (i = level; i < maxDepth; i++)
     {
      document.getElementById(menuIdentifier+"_"+i).style.visibility = "hidden";
      document.getElementById(shdIdentifier+"_"+i).style.visibility  = "hidden";
     }
  }

function highlightObj(obj,over)
  {
   obj.style.backgroundColor = (over)?("#452525"):("");
  }

if (ie5 || ns6) document.onclick = hideAllMenus;