var itemBuffer = new Array();
var itemParent = new Array();
var itemCommand = new Array();
var itemIncrement = 1;
var itemCount = 0;
var itemActual;

var itemWidth = 150;
var itemHeight = 20;
var itemOffset = 0;

var imagePath ='img/menu';
                        
function addMenuItem(parent,caption,command){
  itemBuffer[itemIncrement]=caption;
  itemParent[itemIncrement]=parent;
  itemCommand[itemIncrement]=command;
  itemIncrement=itemIncrement+1;
  itemCount=itemIncrement;
}

function drawItemList(top,left,parent){
      
      itemId = ("submenu" + parent);
      
      document.write("<span style='left:"+left+";top:"+top+";position:absolute'>");
      document.write("<div id="+ itemId + " style='display:none;'>"); 
      document.write("<table border=0 cellspacing=0 cellpadding=0 onMouseOver='openSubMenu("+itemId+");' onMouseOut='closeSubMenu("+ itemId + ");'>");
      var i = 0;

      var itemsCount = getItemsCount(parent);
      var itemsIndex = 0;
      
      for(i=1;i<itemCount;i++){
          if (itemParent[i] == parent){
            if (itemsIndex == itemsCount){
              // last item //
              document.write("<tr><td nowrap  width="+itemWidth+" height="+itemHeight+" background='"+imagePath+"/menu00.png' id="+itemCommand[i]+" style='cursor:hand;opacity:.8;filter: alpha(opacity=100);' onMouseOver='changeButtonFont(this,1);' onMouseOut='changeButtonFont(this,0);' onClick='commandExecute(this)'>&nbsp; &nbsp; "+itemBuffer[i]+"</td></tr>");
            }else{
              // non last item //
              document.write("<tr><td nowrap  width="+itemWidth+" height="+itemHeight+" background='"+imagePath+"/menu00.png' id="+itemCommand[i]+" style='cursor:hand;opacity:.8;filter: alpha(opacity=100);' onMouseOver='changeButtonFont(this,1);' onMouseOut='changeButtonFont(this,0);' onClick='commandExecute(this)'>&nbsp; &nbsp; "+itemBuffer[i]+"</td></tr>");
            }
          itemsIndex = itemsIndex + 1;
          };
      }
      document.write("</table>");
      document.write("</div>");
      document.write("</span>");
}

function getItemsCount(parent)
{
      var i = 0;
      var c = 0;
      for(i=1;i<itemCount;i++){
          if (itemParent[i] == parent)
          c = c + 1;
      }
      return c-1;
}

function drawItemRoot(top,left){
      
      document.write("<span style='left:"+left+";top:"+top+";position:absolute'>");
      document.write("<table id='RootMenu' border=0 cellspacing=0 cellpadding=0><tr>");


      var i = 0;

      for(i=1;i<itemCount;i++){
          if (itemParent[i] == 0){
          var itemId = ("submenu" + i);
          document.write("<td nowrap width="+itemWidth+" height="+itemHeight+" background='"+imagePath+"/menu00.png' onMouseOver='openSubMenu("+itemId+");' onMouseOut='resetRoot();' style='cursor:hand;'>&nbsp;&nbsp; <FONT color='#FFBC1B' size='2'>"+itemBuffer[i]+"</FONT></td>");
          };
      }
      document.write("</tr></table>");
      document.write("</span>");

      var depth = 0;
      for(i=1;i<itemCount;i++){
          if (itemParent[i] == 0){
          drawItemList(top+itemHeight,left+itemOffset+(depth*itemWidth),i);
          depth = depth + 1;
}}}

function openSubMenu(item) {
 
    closeAllSubMenu(item);
  
    if (itemCount > 0) {
      itemActual = item;
      item.style.display='';
}}

function closeSubMenu(item){
     
    if (itemCount > 0) {
      item.style.display='none';
}}

function closeAllSubMenu(item){
    
    if (itemCount > 0) {
      var i = 0; 
      for(i=1;i<itemCount;i++){
      if (itemParent[i] > 0){
      var itemId = ("submenu" + itemParent[i]);
      var exitem = document.getElementById(itemId);
            if (itemId != item.id) 
            { 
        exitem.style.display = 'none';
}}}}}

function resetRoot(){
    
    if (itemCount > 0) {
    var i = 0; 
      for(i=1;i<itemCount;i++){
      if (itemParent[i] > 0){
      var itemId = ("submenu" + itemParent[i]);
      var exitem = document.getElementById(itemId);
          exitem.style.display = 'none';
}}}}

function commandExecute(element){
    
    document.location.href = ("?content="+element.id);
  
}

function changeButtonFont(element,focus){
  if(focus==1)
  {
    element.style.color='#FFB80F';
  }
  else
  {
    element.style.color='#a0a0a0';
  }
}

