/****************************************************************************
 RNAi_menukit.js - routines associated with the DRSC website main pulldown
 menu.
 ****************************************************************************/

function hide() {
  var i;

  for (i = 0; i < menuCount; ++i) {
    var ElIdStr = menuNames[i];
    var menuDiv = document.getElementById("menuDiv" + menuNames[i]);
    var menuDivTd = document.getElementById("menuDiv" + menuNames[i] +
                                            "-td");
    if (null == menuDiv || null == menuDivTd) continue;

    if (menuDiv.style.visibility == 'visible') {
      menuDiv.innerHTML = "<div></div>";
      menuDiv.style.visibility = 'hidden';
      menuDivTd.style.backgroundColor=colorMenuBg;
    }
  }
}



/* Handling of mouse info is not standard across browsers.  MouseEvent
 * gives us a standardized object to work with.
 */
function MouseEvent(e) {
  if (e) {
    this.e = e;
  } else {
    this.e = window.event;
  }

  if (e.pageX) {
    this.x = e.pageX;
  } else {
    this.x = e.clientX;
  }
  if (e.pageY) {
    this.y = e.pageY;
  } else {
    this.y = e.clientY;
  }

  if (e.target) {
    this.target = e.target;
  } else {
    this.target = e.srcElement;
  }
}

/* standard colors - should always match those in
 *                   cgi-bin/RNAi_menu_routines.pl
 */
var colorTan = "#c9caa7";
var colorHappy = "#79c7c7"; // Happy Blue
var colorPlum = "#5c223b";
var colorOlive = "#757a5b";
var colorLSBlue = "#f1f8f9"; // Light Soft Blue
var colorMenuBg = colorTan;
var colorHilite = colorHappy;
var colorMainBg = colorLSBlue;
var colorText = "#000000";


/* list of objects that have popup menus and the information for those
 * menus
 */
var menuCount = 0;
var menuNames = new Array(); // name to use w/menu array & calling parent
var menuWidth = new Array(); // pixel width of the menu
var menuData = new Array(); // (text, destination) pairs for each menu
                            // item
//ABOUT - points to home
menuNames[menuCount] = "About";
menuWidth[menuCount] = 155;
menuData[menuCount] = [
  ["Contact Us", "/DRSC-PEO.html"],
  ["Facility Location", "/DRSC-LOC.html"],
  ["Funding", "/DRSC-FUN.html"],
  ["Staying in Boston", "/DRSC-STA.html"],
  ["Newsletter", "/cgi-bin/RNAi_newsletter.pl"]
  //["Job Opportunities", "#"]
];
++menuCount;

//ASSAYS
menuNames[menuCount] = "Assays";
menuWidth[menuCount] = 285;
menuData[menuCount] = [
  ["Overview", "/DRSC-ASO.html"],
  ["Platereader", "/DRSC-GTA.html"],
  ["Platereader (Infrared)", "/DRSC-ICW.html"],
  ["High-Content Imaging (Confocal)", "/DRSC-EVO.html"],
  ["High-Content Imaging", "/DRSC-DIS.html"],
  ["Fly to Human", "/DRSC-FTH.html"]
];
++menuCount;

//REAGENTS
menuNames[menuCount] = "Reagents";
menuWidth[menuCount] = 230;
menuData[menuCount] = [
  ["Overview", "/DRSC-REA.html"],
  ["RNAi Libraries", "/DRSC-DRS.html"],
  ["RNAi Sub-Libraries", "/DRSC-SUB.html"],
  ["Over-Expression Libraries", "/DRSC-OEX.html"],
  ["RNAi Rescue", "/cgi-bin/RNAi_find_rescue_compl.pl"],
  ["TRiP", "TRiP-HOME.html"],
  ["Cell Lines", "/DRSC-CEL.html"]
];
++menuCount;

//PROTOCOLS
menuNames[menuCount] = "Protocols";
menuWidth[menuCount] = 190;
menuData[menuCount] = [
  ["Protocols Overview", "/DRSC-PRO.html"],
  ["dsRNA Synthesis", "/DRSC-PRS.html"],
  ["RNAi Protocols", "/DRSC-PRR.html"],
  ["Primary Cells", "/DRSC-PCP.html"],
  ["Drosophila Cell Culture", "/DRSC-PRC.html"]
];
++menuCount;

//APPLY
menuNames[menuCount] = "Apply";
menuWidth[menuCount] = 220;
menuData[menuCount] = [
  ["Policies & Application", "/DRSC-APP.html"],
  ["Costs", "/DRSC-COS.html"],
  ["Downloadable Application", "http://flyrnai.org/RNAi_ScreenAppForm.doc"],
  ["Data Sharing Agreement", "/DRSC-DSA.html"]
];
++menuCount;

//SCREENS - points to by topic
menuNames[menuCount] = "Screens";
menuWidth[menuCount] = 260;
menuData[menuCount] = [
  ["Published Screens (by Year)", "/DRSC-PRY.html"],
  ["Published Screens (by Topic)", "/DRSC-PTO.html"],
  ["DRSC Analyses & Methods", "/DRSC-PDR.html"]
];
++menuCount;

//TOOLS
menuNames[menuCount] = "Tools";
menuWidth[menuCount] = 110;
menuData[menuCount] = [
  ["Overview", "/DRSC-TOO.html"],
  ["SnapDragon", "/cgi-bin/RNAi_find_primers.pl"],
  ["Heat Map", "/cgi-bin/RNAi_heatmap_public.pl"],
  ["Gene Lookup", "/cgi-bin/RNAi_gene_lookup_public.pl"],
  ["RNAi Rescue", "/cgi-bin/RNAi_find_rescue_compl.pl"],
  ["RNAiCut", "http://groups.csail.mit.edu/cb/RNAiCut/"],
  ["Links", "/DRSC-LIN.html"],
//(etc. direct links to the most commonly used tools for any user)
];
++menuCount;

//TRiP
menuNames[menuCount] = "TRiP";
menuWidth[menuCount] = 200;
menuData[menuCount] = [
  ["Home", "/TRiP-HOME.html"],
  ["Approach", "/TRiP-TTR.html"],
  ["Nominations &amp; the Queue", "/TRiP-NOM.html"],
  ["TRiP Stocks", "/TRiP-ACC.html"],
  ["Screening Center", "/TRiP-SCC.html"],
  ["FAQ", "/TRiP-QNA.html"]
];
++menuCount;


//ENTER
menuNames[menuCount] = "Enter";
menuWidth[menuCount] = 145;
menuData[menuCount] = [
                       //  ["Database Log-In ", "http://134.174.160.115/cgi-bin/RNAi_login.pl"],
  ["Database Log-In ", "/cgi-bin/RNAi_login.pl"],
  ["Equipment Sign-Up", "/cgi-bin/RNAi_device_use.pl"]
];
++menuCount;


//DRSC - menu for TRiP site
menuNames[menuCount] = "DRSC";
menuWidth[menuCount] = 70;
menuData[menuCount] = [
  ["Home", "/DRSC-HOME.html"],
  ["Assays", "/DRSC-ASO.html"],
  ["Reagents", "/DRSC-REA.html"],
  ["Apply", "/DRSC-APP.html"],
  ["Screens", "/DRSC-PTO.html"],
  ["Tools", "/DRSC-TOO.html"]
];
++menuCount;


//SITE MAP





function check(evt) {
  var i;
  var e = new MouseEvent(evt);
  var targetDiv = document.getElementById("targetDiv");
  var debugMsg = "Menus checked:";

  // debug code
  var text1 = document.getElementById("text1");
  if (null != text1) {
    text1.value = e.x;
    document.getElementById("text2").value = e.y;
  }

  for (i = 0; i < menuCount; i++) {
    var parent = document.getElementById("menuDiv" + menuNames[i] +
                                         "-td");
    //var parent = document.getElementById("menu_" +
    //                                     menuNames[i].toLowerCase());
    var target = document.getElementById("menuDiv" + menuNames[i]);
    if (null == parent || null == target) continue;
    var parOffSt = getOs(parent); /* [x, y] */
    //debugMsg += " menuDiv" + menuNames[i] + "(" + parOffSt[0] + "," +
    //            parOffSt[1] + ") - (" +
    //            (parOffSt[0] + parseInt(parent.style.width)) + "," + 
    //            (parOffSt[1] + parseInt(parent.style.height)) + ")";

    if (parOffSt != null) {
      if ((e.x > parOffSt[0]) &&
          (e.y > parOffSt[1]) &&
          (e.x < (parOffSt[0] + parseInt(parent.style.width))) &&
          (e.y < (parOffSt[1] + parseInt(parent.style.height)))) {
        //targetDiv.innerHTML = "img x=" + parent.x +
        //                      ", img y=" + parent.style.top;
        showDRSCmenu(menuNames[i]);
        //debugMsg += " (IN!)";
      } else {
        if (target.style.visibility == 'visible' &&
            ((e.x < parseInt(target.style.left)) ||
             (e.y < parseInt(target.style.top)) ||
             (e.x > (parseInt(target.style.left) +
                     parseInt(target.style.width))) ||
             (e.y > (parseInt(target.style.top) +
                     parseInt(target.style.height))))) {
          hide();
        }
      }
    }
  }
  //document.getElementById("dbgTxt").value = debugMsg;
  //targetDiv.innerHTML = debugMsg;
  //targetDiv.style.visibility = "visible";
}


// Populate a DRSC menu, put it in the right place and make it visible
function showDRSCmenu(name) {
  // name - the string naming the menu, it will be the unique part of
  //        the menu and parent link ids
  var menuIdx = -1; // index of this menu in menuNames, menuWidth
                    // and menuData
  var i;

  // find this menu in the arrays
  for (i = 0; i < menuNames.length; ++i) {
    if (menuNames[i] == name) {
      menuIdx = i;
      break;
    }
  }
  if (-1 == menuIdx) {
    alert("showDRSCmenu() called with invalid menu name (" + name + ")");
    return;
  }

  var data = "<table width='100%' cellpadding='2' cellspacing='1'" +
                    "bgcolor='#000000'>";
  var target;
  var dbgMsg = "";
  var belowEle = document.getElementById("menuDiv" + name + "-td");
  if (menuData[menuIdx].length) {
    for (i = 0; i < menuData[menuIdx].length; ++i) {
      //dbgMsg += "menuData[" + menuIdx + "][" + i + "][1] = " +
      //          menuData[menuIdx][i][1] + "\n";
      //document.getElementById("dbgTxt").value = dbgMsg;
      //document.getElementById("dbgTxt").value =
      //    "name = '" + name + "', menuIdx = " + menuIdx + ", i = " + i;

      var linkText = menuData[menuIdx][i][0];
      var linkDest = menuData[menuIdx][i][1];

      data += "<tr><td bgcolor='" + colorMenuBg + "' onclick='hide()' " +
              "onmouseover='this.style.backgroundColor=" + '"' +
              colorHilite + '"' + 
              "' onmouseout='this.style.backgroundColor=" + '"' + 
              colorMenuBg + '"' + "'>"
      data += "<a href='" + 
              linkDest + "' class='menuLink'>" + 
              linkText + "</a>"
      data += "</td></tr>";
    }
  }

  data += "</table>";

  target = document.getElementById("menuDiv" + name);
  if (target.style.visibility == "hidden") {
    /* find the element target should go below and try to put it there */
    var OffSt = getOs(belowEle); /* [x, y] */
    OffSt[1] += parseInt(belowEle.style.height)
    target.style.position = "absolute";
    target.style.left = OffSt[0] + "px";
    target.style.top = OffSt[1] + "px";
    target.style.height = 1 + menuData[menuIdx].length * 21 + "px";
    target.style.width = menuWidth[menuIdx] + "px";

    target.innerHTML = data;
    target.style.visibility = "visible";
  }
  //document.getElementById("dbgTxt").value = dbgMsg;
}


function getOs(theThing) {
  var osL = theThing.offsetLeft;
  var osT = theThing.offsetTop;
  var i = 0;
  var parentEl=theThing.offsetParent;

  if ("absolute" == theThing.style.position) {
    return [parseInt(theThing.style.left),
            parseInt(theThing.style.top)];
  }
  while (parentEl!=null) {
    osL += parentEl.offsetLeft;
    osT += parentEl.offsetTop;
    parentEl = parentEl.offsetParent;
  }
  return [osL, osT];
}

function getposOffset(what, offsettype){
  var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
  var parentEl=what.offsetParent;
  while (parentEl!=null){
    totaloffset=(offsettype=="left")?
        totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

