var moz = (typeof document.implementation != 'undefined') && 
          (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
var main=new Array();
var xmlDoc=new XMLHttpRequest();
var menuWidth=780;
var center=screen.width/2;
var timer;


function hideMenu(){
  var extras=document.getElementById('menuextras');
  extras.innerHTML="&#160;";
}

function setTimer(evt){
  if(ie){
    if(event.srcElement.tagName=="DIV")
      timer=setTimeout("hideMenu('"+event.srcElement.id+"');",1000);
  } else {
    timer=setTimeout("hideMenu('"+evt.target.id+"');",1000);
  }
}

function clearTimer(evt){
  if(moz){
    clearTimeout(timer);
  }
}

function clearHighlight(evt){
  evt.style.backgroundColor="#ccccff";
  evt.style.color="black";
}

function highlightRow(evt){
  evt.style.backgroundColor='#ccffcc';
  evt.style.color="green";
}

function reCenter(){
  var x, itm=0, subItm;
  center=document.body.clientWidth/2;
  x=(center)-(menuWidth/2); 
  if(x<0) x=25;
  
  subItm=document.getElementById("subItems"+itm);
  while(subItm!=null){
    subItm.style.left=x+"px";
    itm++;
    x=x+150;
    subItm=document.getElementById("subItems"+itm);
  }
}

function readXML(){  
  var tbl, bdy, row, col;
  var subLoop=0, itmLoop=0;
  var title, link; 
  var items;
  var item, sub;
  var addCnt=0;

  if(xmlDoc.readyState==4 && xmlDoc.status==200){ 
    items=xmlDoc.responseXML.getElementsByTagName("menu");
    if(items != null){
      item=items[0].getElementsByTagName("itm");
      for(itmLoop=0;itmLoop<item.length;itmLoop++){
        main[itmLoop]=new menu();
        main[itmLoop].title=item[itmLoop].getAttribute("title");
        main[itmLoop].link=item[itmLoop].getAttribute("link");
        sub=item[itmLoop].getElementsByTagName("sub");
        if(sub.length>0){
          for(subLoop=0; subLoop<sub.length;subLoop++){
            main[itmLoop].items[subLoop]=new menu();
            main[itmLoop].items[subLoop].title=sub[subLoop].getAttribute("title");
            main[itmLoop].items[subLoop].link=sub[subLoop].getAttribute("link");
          }   
        }
      }
    }
  }
}

function initMenu(){
  center=document.body.clientWidth/2;
  window.onresize=reCenter;
  //if(xmlDoc == null){	 	   
  //  if(moz){
  //    xmlDoc=document.implementation.createDocument("", "doc", null);        
  //    xmlDoc.onload = readXML;
  //  } else if (ie) {
  //    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  //    xmlDoc.async = false;
  //    xmlDoc.onreadystatechange=function () {	if (xmlDoc.readyState == 4) readXML();	};
      //xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      //xmlDoc.async = false;
      //xmlDoc.onreadystatechange=function(){if (xmlDoc.readyState == 4)readXML();};
  //  }
  //  xmlDoc.load("menu.xml");
  //} else {
  //  readXML();
  //} 
  xmlDoc.onreadystatechange = readXML;
  xmlDoc.open("GET", "/menu.xml");
  xmlDoc.send(null); 
}

function showMenu(mnu){
  var loop=0;
  var itm=mnu.id.substring(1);
  var menu, oMenu;
  var extras=document.getElementById('menuextras');
  var tbl, row, col, tbdy;

  if(timer != null)
    clearTimeout(timer);
  
  for(loop=0;loop<7;loop++){
    oMenu=document.getElementById('z'+loop);
    oMenu.style.backgroundColor="blue";
    oMenu.style.color="white";
  }
    
    
  extras.innerHTML="";
  menu=mnu.id.substring(1);
  mnu.style.backgroundColor="#ccffcc";
  mnu.style.color="green";
  extras.style.backgroundColor="#ccffcc";

  tbl=document.createElement("TABLE");
  tbdy=document.createElement("TBODY");
  tbl.appendChild(tbdy);
  row=document.createElement("TR");
  col=document.createElement("TD");
  col.innerHTML="<b>Additional links for this item:</b>";
  col.style.color="black";
  row.appendChild(col);
  if(main[menu] != null){
    if(main[menu].items.length){
      for(loop=0;loop<main[menu].items.length;loop++){
        col=document.createElement("TD");
        col.innerHTML="<li><a href='"+main[menu].items[loop].link+"'><b><font color='green'>"+main[menu].items[loop].title+"</font></b></a></li>";
        row.appendChild(col);
      }
    } else {
      col=document.createElement("TD");
      col.innerHTML="None";
      row.appendChild(col);
    }
  } else {
    col=document.createElement("TD");
    col.innerHTML="None";
    row.appendChild(col);
  }
  tbdy.appendChild(row);
  extras.appendChild(tbl);
}
