var menu = [];
var m_idx = 0;

//parentid, id, title, image, mode, level, link
menu[0] = [-1,"ROMS Nowcast","ROMS Nowcast","qplus.gif","open",0,""];
menu[1] = [-1,"ROMS Forecast","ROMS Forecast","qplus.gif","open",0,""];
menu[2] = [-1,"WRF","WRF","qplus.gif","open",0,""];
menu[3] = [-1,"ROMS vs. Data","ROMS vs. Data","qplus.gif","open",0,""];
menu[4] = [-1,"Drifter Trajectory","Drifter Trajectory","qplus.gif","open",0,""];
//menu[5] = [-1,"Satellite SST","Satellite SST","qplus.gif","open",0,""];

m_idx = 5;
menu[m_idx++] = [0,"Temperature","Temperature","NowcastTemp","close",1,"ROMSNowcast"];
menu[m_idx++] = [0,"Salinity","Salinity","NowcastSalinity","close",1,"ROMSNowcast"];
menu[m_idx++] = [0,"Current","Current","NowcastCurrent","close",1,"ROMSNowcast"];
menu[m_idx++] = [0,"Sea Surface Height","Sea Surface Height","NowcastSSH","close",1,"ROMSNowcast"];

menu[m_idx++] = [1,"3D Output","3D Output","ROMSForecast","close",1,"ROMSForecast"];

menu[m_idx++] = [2,"Wind","Wind","WRFWind","close",1,"WRFWind"];

menu[m_idx++] = [3,"Tide Gauge","Tide Gauge","ROMSDataTide","close",1,"ROMSDataTide"];
menu[m_idx++] = [3,"Glider Profile","Glider Profile","GliderProfile","close",1,"GliderProfile"];
menu[m_idx++] = [3,"Ship CTD","Ship CTD","ShipCTD","close",1,"ShipCTD"];
menu[m_idx++] = [3,"REMUS","REMUS","Remus","close",1,"Remus"];
menu[m_idx++] = [3,"HF Radar","HF Radar","HFRadar","close",1,"HFRadar"];
menu[m_idx++] = [3,"Sea Surface Temperature","Sea Surface Temperature","G1SST","close",1,"G1SST"];

menu[m_idx++] = [4,"Observation","Observation","DrifterObserved","close",1,"DrifterObserved"];
menu[m_idx++] = [4,"Prediction","Prediction","DrifterSimulated","close",1,"DrifterSimulated"];
menu[m_idx++] = [4,"Ensemble Prediction","Ensemble Prediction","DrifterEnsemble","close",1,"DrifterEnsemble"];

function selectMenu(pindex,mindex)
{
  var subproj = menu[mindex][3];
  var proj = menu[mindex][6];
  //alert('selectMenu(): proj = ' + proj + '; pindex = ' + pindex + '; subproj = ' + subproj);
  //selectProject(proj, pindex-5, subproj, false, true);
  lastID = pindex; 
  selectProject(proj, subproj, false, true);
}

function doClick(idx)
{
  //alert(idx);
  if (idx == lastID) return;  //currently has a selected child
  if (menu[idx][4] == "open")
  {
    menu[idx][4] = "hide";
    menu[idx][3] = "qplus.gif";
  }
  else
  {
    menu[idx][4] = "open";
    menu[idx][3] = "qminus.gif";
  }
  addTopMenu();
}

function addTopMenu() 
{       
  //alert('addTopMenu()');
  var txt = '<table>\n';
  for (var i=0; i<menu.length; i++)
  { 
    if (menu[i][0] == -1)  //toplevel
    { 
      //txt = txt + '<tr><td>&nbsp;</td></tr>\n';
      if (menu[i][6] != "NA")
      {
        txt = txt + '<tr>\n';
        //indenting
        txt = txt + '<td class="bold8">\n';
        for (var space=0; space<menu[i][5]; space++)
          txt = txt + '&nbsp;&nbsp;&nbsp;';
        //image expanding or hiding
        txt = txt + '<img src="images/' + menu[i][3] + '" width="10" height="10" border="0" onclick="doClick(' + i + ')"> \n';
        //have children node?
        txt = txt + '&nbsp;';
        txt = txt + menu[i][2];
        //alert('1 - ' + txt);
        txt = txt + '</td>\n';
        txt = txt + '</tr>';
      }
      if (menu[i][4] == "open")
      {
        /* if (menu[i][6] != "NA")
        { 
          txt = txt + '<tr>';
          txt = txt + '<td>\n';
          txt = txt + '<img src="images/' + menu[i][6] + '"> \n';
          txt = txt + '</td>\n';
          txt = txt + '</tr>';
        } */
        txt = txt + addmenu(i);
      }
      //alert('2 - ' + txt);
    }
  }
  txt = txt + '</table>';
  //alert(txt);
  if (document.all)
    document.all("lmenu").innerHTML = txt;
  else if (document.getElementById)
    document.getElementById('lmenu').innerHTML = txt;
}

function addmenu(idx)
{
  //alert('addmenu()');
  var rtxt = '';
  for (var i=0; i<menu.length; i++)
  {
    if (menu[i][0] == idx)
    {
      rtxt = rtxt + '<tr>\n';
      //indenting
      rtxt = rtxt + '<td nowrap class="bold8">\n';
      for (var space=0; space<menu[i][5]; space++)
        rtxt = rtxt + '&nbsp;';
      //i mage expanding or hiding
      rtxt = rtxt + '<input type="radio" name="project" value="' + menu[i][3] + '" onclick="selectMenu(' + idx + ',' + i + ')"';
      if (menu[i][3] == lastValue)
        rtxt = rtxt + ' checked';
      rtxt = rtxt + '>' + menu[i][2] + '\n';
      rtxt = rtxt + '</td>\n';
      rtxt = rtxt + '</tr>\n';
      //if it is opening (expanding)
      if (menu[i][4] == "open")
      {
        rtxt = rtxt + addmenu(i);
      }
    }
  }     
  rtxt = rtxt + '<tr><td>&nbsp;</td></tr>\n';
  return rtxt; 
}     
