






writeMenu(parseMenuItems());

function writeMenu(sHTML) {  
  var method = 0; //values below
  switch(method) {
    case 0: document.write(sHTML); break; // IE/PC yes, NS/PC yes
    case 1: initContainer(); Container.html(sHTML); break; // IE/PC yes, NS/PC no
    case 2: initContainer(); Container.text(sHTML); break; // debug only
    case 3: alert(sHTML); break;
  }
}

var Container;
function initContainer() {
  Container = new cContainer();
  function cContainer() {
    document.write("<div id='Container' style='position:relative; Height:0px;FONT-FAMILY: Verdana; font-size: 10px; background:#cecece;visibility:visible;'>&nbsp;</div>");
    this.box = document.getElementById("Container");
    this.html = write_html;
    this.text = write_text;
  }
  function write_html(msg) {
    Container.box.innerHTML = msg;
  }
  function write_text(msg) {
    Container.box.innerText = msg;
  }
}

function makeDivID(level, count) {
  return "L" + level + "_" + count;
}
function makeTrID(DivID, TrID) {
  return  DivID + "_" + TrID;
}
function parseMenuItems() {
  var cMaxMenLevel = 10;
  var curLevel = -1, cntTrID = 0;  
  var aDivID = new Array(); //track IDs levelwise
  var cntDivLevel = new Array(); //track div count per level  
  var cntLineLevel = new Array(); //track line count per level
  var asLineLevel = new Array(); //lines are pooled levelwise  
  
  // init arrays
  for (i=0; i < cMaxMenLevel; i++) {
    cntDivLevel[i] = 0;
    cntLineLevel[i] = 0;
    asLineLevel[i] = new Array();  //2-d array
  }
  
  for (i=0; i < menuitems.length; i++) {
    var sOut = "";
    // 0=level, 1=caption, 2=link, 3=target
    var aItem = menuitems[i];
    // level change
    var newLevel = aItem[0];
    if (newLevel != curLevel) {
      if (newLevel > curLevel) {
        //down: open new DIV
        cntDivLevel[newLevel]++;
        aDivID[newLevel] = makeDivID(newLevel,cntDivLevel[newLevel]);
        sOut += "<div id=\"" + aDivID[newLevel] + "\" class=nav style=\"position:absolute; visibility: hidden;\" onMouseOver=\"menOver();\" onMouseOut=\"menOut();\">";
        sOut += "\n<table class=nav cellspacing=\"0\" cellpadding=\"0\">";
      } else {
        //up: close any open DIV
        for (openLevel=curLevel; openLevel > newLevel; openLevel--) {
          // add to current level pool
          asLineLevel[openLevel][cntLineLevel[openLevel]] = "</table></div>\n\n";
          // set captionlast class to the previous item
          asLineLevel[openLevel][cntLineLevel[openLevel]-1] = asLineLevel[openLevel][cntLineLevel[openLevel]-1].replace(/class=caption/g, "class=captionlast");    
          cntLineLevel[openLevel]++;
        }
      }
      curLevel=newLevel;
    } else {
      // special for 0-level
      if (newLevel==0) cntDivLevel[1]++;
    }
    
    // build item
    var sTdImg="<img src=\"/CM/images/b.gif\" width=19 height=14>";
    var nextDivID = "";
    cntTrID++;
    var sTrID=" id=\"" + makeTrID(aDivID[curLevel] , cntTrID)+ "\"";
    var sTrOnC="";
    var sTrCls=" class=item";
    // find out about the next item
    if (menuitems[i+1]) {
      // if subfolders -> onMouseOver
      if (menuitems[i+1][0] == curLevel+1) {
        nextDivID = makeDivID(curLevel+1, cntDivLevel[curLevel+1]+1);
        sTdImg="<img src=\"/CM/images/img_arrow.gif\" width=19 height=14>";
      }
    }
    // escape ' needs double-escape \\' 
    var sStatus = aItem[1].replace(/'/,"\\'");
    sStatus = sStatus.replace(/\"/g,"&quot;");
    sTrOnMO = " onMouseOver=\"menOverTR(this.id,'"+nextDivID+"'); window.status='"+sStatus+"'; return true;\"";
    // if link -> onClick
    if (aItem[2]) {
      // target
      if (aItem[3]===1) { //sic!
        sTrOnC=" onClick=\"window.open('"+aItem[2]+"');\"";  //_blank
      } else {
        sTrOnC=" onClick=\"document.location='"+aItem[2]+"';\"";  //_self
      }
      sTrCls=" class=itemlink";
    }
    sOut += "\n<tr"+sTrID+sTrCls+ sTrOnC+sTrOnMO+">";    
    sOut += "<td class=caption>"+aItem[1]+"</td>";    
    sOut += "<td class=caption width=17>"+sTdImg+"</td></tr>";
    
    // add to current level pool
    // skip level 0 (no need to generate it)
    if (curLevel > 0) {
      asLineLevel[curLevel][cntLineLevel[curLevel]] = sOut;
      cntLineLevel[curLevel]++;
    }
  }

  // close any open DIV
  for (openLevel=curLevel; openLevel > 0; openLevel--) {
    // add to current level pool
    asLineLevel[openLevel][cntLineLevel[openLevel]] = "</table></div>\n\n";
    // set captionlast class to the previous item
    asLineLevel[openLevel][cntLineLevel[openLevel]-1] = asLineLevel[openLevel][cntLineLevel[openLevel]-1].replace(/class=caption/g, "class=captionlast");    
    cntLineLevel[openLevel]++;
  }

  // collect the levels
  sOut="<!-- " + cntTrID + " menu items -->\n";
  for (i=0; i < cMaxMenLevel; i++) {  
    sOut += asLineLevel[i].join("");
  }
  return sOut;
}
