
//drop down menu item-------------------------------------------------
function dropDownMenuItem(id, pid, text, loc){
   this.id   = id;
   this.pid  = pid;
   this.text = text;
   this.loc  = loc;
   this.cnt  = 0;
}

//dropdown menu object------------------------------------------------
function pcDropDown(ind){
   this.cur= -1;
   this.old= -1;

   if ( ind ){
      this.ind = ind;
   }else{
      this.ind = '&nbsp;';
   }

   this.items = new Array();
}

//get index from id---------------------------------------------------
function dropDownGetIndexFromId(id){
   for ( var i=0; i<this.items.length; ++i ){
      if ( this.items[i].id == id ) return (i); 
   }
   return (-1);
}

//add item------------------------------------------------------------
function dropDownAddItem(id, pid, text, loc){
   if ( pid > -1 ){
      pid = this.getIndexFromId(pid);
      if ( this.items[pid] ){
         ++this.items[pid].cnt;
      }
   }
   this.items[this.items.length] = new dropDownMenuItem(id, pid, text, loc);
}

//get sub menu--------------------------------------------------------
function dropDownGetSub(parent){
   var out = "";

   //top spacer row
   out+= "<table class=\"subMenu\" id=\"sub_" + parent + "\" border=0 cellspacing=0 cellpadding=0>" +
         "<tr><td><table width=\"100%\" border=0 cellpadding=0 cellspacing=0><tr>" +
         "<td class=\"subMenuSpacerTopLeft\">&nbsp;</td>" +
         "<td class=\"subMenuSpacerTop\">&nbsp;</td>" +
         "<td class=\"subMenuSpacerTopRight\">&nbsp;</td>" +                  
         "</tr></table></td></tr>";

   for ( var jj=0; jj<this.items.length; ++jj ){
      if ( this.items[jj].pid == parent ){
         if ( this.items[jj].cnt > 0 ){
            ind = '&nbsp;' + this.ind;
         }else{
            ind = '&nbsp;&nbsp;&nbsp;';
         }

         out+= "<tr><td id=\"item_" + jj + "\" class=\"subMenuItem\" align=center valign=middle onmouseover=\"activateMenu(" + jj + ")\" onmouseout=\"deactivateMenu(" + jj + ")\" onclick=\"executeMenu(" + jj + ")\">" + decodeURI(this.items[jj].text) + ind + "</td></tr>"; 
      }
   }

   //bottom spacer row
   out+= "<tr><td><table width=\"100%\" border=0 cellpadding=0 cellspacing=0><tr>" +
         "<td class=\"subMenuSpacerBottomLeft\">&nbsp;</td>" +
         "<td class=\"subMenuSpacerBottom\">&nbsp;</td>" +
         "<td class=\"subMenuSpacerBottomRight\">&nbsp;</td>" +                  
         "</tr></table></td></tr></table>";

   return (out);
}

//drop down to string-------------------------------------------------
function dropDownToString(){
   var out = "";  
   var sub = "";

   //render menu elements
   out+= "<table class=\"dropDownMenu\" id=\"mainMenu\" border=0 cellspacing=0 cellpadding=0><tr>";
   for ( var j=0; j<this.items.length; ++j ){               
      if ( this.items[j].cnt > 0 ){
         sub+= this.getSub(j);
      }

      if ( this.items[j].pid == -1 ){
         if ( this.items[j].cnt > 0 ){
            ind = '&nbsp;' + this.ind;
         }else{
            ind = '&nbsp;&nbsp;';
         }
         out+= "<td id=\"item_" + j + "\" class=\"mainMenuItem\" align=center valign=middle onmouseover=\"activateMenu(" + j + ")\" onmouseout=\"deactivateMenu(" + j + ")\" onclick=\"executeMenu(" + j + ")\">" + decodeURI(this.items[j].text) + ind + "</td>"; 
      }
   }
   out+= "</tr></table>";
   return (out + sub);
}

//--------------------------------------------------------------------
pcDropDown.prototype.getIndexFromId   = dropDownGetIndexFromId;
pcDropDown.prototype.addItem          = dropDownAddItem;
pcDropDown.prototype.getSub           = dropDownGetSub;
pcDropDown.prototype.toString         = dropDownToString;
//--------------------------------------------------------------------
var tout = null;
var old  = -1;

//get absolute x-position---------------------------------------------
function getXpos(obj){
   var curleft = 0;
   if (obj.offsetParent){
      while (obj.offsetParent){
         curleft += obj.offsetLeft
         obj = obj.offsetParent;
      }
   }
   return (curleft);
}

//get absolute y-position---------------------------------------------
function getYpos(obj){
   var curtop = 0;
   if (obj.offsetParent){
      while (obj.offsetParent){
         curtop += obj.offsetTop
         obj = obj.offsetParent;
      }
   }
   return (curtop);
}

//activate menu-------------------------------------------------------
function activateMenu(cur){
   var item = null;

   if ( tout ){
      window.clearTimeout(tout);
   }

   //hidde old trail
   if ( old != -1 ){
      while ( item = dd.items[old] ){
         if ( sm = document.getElementById("sub_" + old) ){
            sm.style.visibility = 'hidden';
         }

         if (it = document.getElementById("item_" + old) ){
            if ( item.pid == -1 ){
               it.className = 'mainMenuItem';
            }else{
               it.className = 'subMenuItem';
            }
         }
         old = item.pid;
      }
   }
   old = cur;        

   while ( item = dd.items[cur] ){
      if ( ritem = document.getElementById("item_" + cur) ){
         if ( item.pid == -1 ){
            ritem.className = 'mainMenuItemOn';
            if ( submenu = document.getElementById("sub_" + cur) ){
               xpos = getXpos(ritem) + 1;
               ypos = getYpos(ritem) + ritem.offsetHeight + 2;
               submenu.style.left = xpos + 'px';
               submenu.style.top  = ypos + 'px';
               submenu.style.visibility = "visible"; 
            }
         }else{
            ritem.className = 'subMenuItemOn';           
            if ( submenu = document.getElementById("sub_" + cur) ){
               xpos = getXpos(ritem) + ritem.offsetWidth + 1;
               ypos = getYpos(ritem) - 10;
               submenu.style.left = xpos + 'px';
               submenu.style.top  = ypos + 'px';
               submenu.style.visibility = "visible"; 
            }
         }
      }
      cur = item.pid
   }
}

//deactivate menu-----------------------------------------------------
function deactivateMenu(index){
   tout = setTimeout("clearMenuBranch()", 200);
}

//clear menu branch---------------------------------------------------
function clearMenuBranch(){
   var item = null;

   //hidde old trail
   if ( old != -1 ){
      while ( item = dd.items[old] ){
         if ( sm = document.getElementById("sub_" + old) ){
            sm.style.visibility = 'hidden';
         }

         if (it = document.getElementById("item_" + old) ){
            if ( item.pid == -1 ){
               it.className = 'mainMenuItem';
            }else{
               it.className = 'subMenuItem';
            }
         }
         old = item.pid;
      }
   }
   old = -1;         
}

//--------------------------------------------------------------------

