var fMenuBar = new Array(
  new Array("ABOUT US", "69", "compania/nuestra_empresa.html"),
  new Array("R & D", "44", "i_d/plataformas_tecnologicas.html"),
  new Array("BIOCONTROL & AGRONOMY", "168","biocontrol/overview.html"),
  new Array("GENOMICS", "70", "genomica/overview.html"),
  new Array("GENE-TECH", "75", "gene_tech/overview.html"),
  new Array("PARTNERSHIP", "90", "partners/i_d.html"),
  new Array("RESOURCES", "80",  "recursos/documentacion.html"),
  new Array("CONTACT US", "79", "contacto/contacto.html")
);

var fPopups = new Array(
  new Array( // Compania
    "0px",
    new Array("THE COMPANY", "compania/nuestra_empresa.html"),
    new Array("OUR PEOPLE", "compania/equipo_humano.html"),
    new Array("ACTIVITIES", "compania/actividades.html")
  ),
  
  new Array( // I D
    "40px",
    new Array("TECHNOLOGY PARTNERS", "i_d/plataformas_tecnologicas.html"),
    new Array("PARTNERS", "partners/i_d.html"),
    new Array("R&D AREAS", "i_d/areas.html")
  ),
  
  new Array( // BIOCONTROL
    "130px",
    new Array("INTRODUCTION", "biocontrol/overview.html"),
    new Array("PRODUCTS", "biocontrol/productos.html"),
    new Array("SERVICES", "biocontrol/servicios.html")

  ),

  new Array( // GENOMICA
    "250px",
    new Array("INTRODUCTION", "genomica/overview.html"),
    new Array("SERVICES", "genomica/servicios.html"),
	new Array("BIOINFORMATICS", "genomica/bioinformatica.html"),
	new Array("DIAGNOSTICS", "genomica/diagnostico.html")
  ),

  new Array( // GENE-TECH
    "370px",
    new Array("INTRODUCTION", "gene_tech/overview.html"),
	new Array("TECHNOLOGY OFFER", "gene_tech/oferta_tecnologica.html")
  ),

  new Array( // PARTNERS
    "342px",
    new Array("R & D NETWORK", "partners/i_d.html"),
    new Array("TECHNOLOGIES PARTNERS", "partners/tecnologicos.html"),
    new Array("ALLIANCE OPPORTUNITIES", "partners/alliance_op.html")
  ),

  new Array( // RECURSOS
    "450px",
    new Array("LITERATURE", "recursos/documentacion.html"),
    new Array("APPLICATION FORMS",  "recursos/formularios.html"),
    new Array("EMPLOYMENT OPPORTUNITIES", "recursos/careers.html")
  ),

  new Array( // CONTACTO
    "620px",
    new Array("CONTACT DETAILS", "contacto/contacto.html"),
	new Array("DIRECTIONS TO NBT", "contacto/ubicacion.html")
  )
);

function printMenuBar(base, popup, subitem) {
  document.writeln('<table width="742" cellspacing="0" cellpadding="0" border="0" id="menu">');
  document.writeln('<tr class="header">');
  for (var i = 0; i < fMenuBar.length; i++) {
    var item = fMenuBar[i];
    if (i > 0) {
      document.writeln('<td width="9"><img src="', base,
                       'images/common/arrow.gif" border="0" width="9" height="10"></td>');
    }
    document.writeln('<td width="', item[1], '" height="20"> ',
                     '<div align="center">',
                       '<a onmouseover="openclose(\'dropdown', i+1,
                       '\')" href="', base, item[2], '" class="header">',
                       item[0], '</a> </div></td>');
  }
  document.writeln('</table>');

  // Prints the popup menus
  for (var i = 0; i < fPopups.length; i++) printPopup(base, i, (i == popup-1), subitem);
}

function printPopup(base, pos, sel, subitem) {
  var popup = fPopups[pos];

  document.writeln('<div id="dropdown', pos + 1, '" class="drop" style="left:', popup[0], '">');
  document.writeln('<table cellpadding="2" cellspacing="0" class="popup"><tr>');
  document.writeln('<td nowrap align="center">&nbsp;');
  for (var i = 1; i < popup.length; i++) {
    var item = popup[i];
	
    if (i > 1) document.writeln('<img src="', base, 'images/common/arrow.gif" border="0" width="9" height="10">');
    if (sel && subitem == i) {
      document.writeln('<span class="subheader2">', item[0], '</span>');
    }
    else {
      document.writeln('<a href="', base, item[1], '" class="subheader">', item[0], '</a>');
    }
  }
  document.writeln('&nbsp;</td></tr></table></div>');
}

//======================================================================================
// Behaviours
var agent = navigator.userAgent;
var isMac = (agent.indexOf("Mac") != -1);
var isSafari = (agent.indexOf("Safari") != -1);
var isExplorer = (typeof(document.all) != "undefined");
var isDOM = (typeof(document.getElementById) != "undefined");
var firstTime = true;

function reloadPage(init) {
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage; }}
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}

reloadPage(true);
drop1state=0
drop2state=0
drop3state=0
drop4state=0
drop5state=0
drop6state=0
drop7state=0
drop8state=0

n = (document.layers) ? 1:0
ie = (document.all) ? 1:0
gecko = (typeof(document.getElementById) == "function");

function _findObj(id) {
  var elem = null;
  if (gecko) elem = document.getElementById(id);
  else if (ie) elem = document.all[id];
  else if (n) elem = document.layers[id];

  return elem;
}

function findObjStyle(id) {
  var elem = _findObj(id);
  if (elem) {
    return (n) ? elem : elem.style;
  }
  else return null;
}

function openclose(drop){
  if (drop=="dropdown1") { dropstate=drop1state }
  if (drop=="dropdown2") { dropstate=drop2state }
  if (drop=="dropdown3") { dropstate=drop3state }
  if (drop=="dropdown4") { dropstate=drop4state }
  if (drop=="dropdown5") { dropstate=drop5state }
  if (drop=="dropdown6") { dropstate=drop6state }
  if (drop=="dropdown7") { dropstate=drop7state }
  if (drop=="dropdown8") { dropstate=drop8state }
  var value = (n) ? "hide" : "hidden";
  var d;
  for (var i = 1; i <= 8; i++) {
    d = findObjStyle("dropdown" + i);
    //d.visibility = value;
    if (d) d.display = "none";
  }

  drop1state=0; drop2state=0; drop3state=0; drop4state=0; drop5state=0; drop6state=0; drop7state=0; drop8state=0

  d = findObjStyle(drop);
  if (d) d.display="block";

  if (drop=="dropdown1") { drop1state=1; adjustSubmenu(1); }
  if (drop=="dropdown2") { drop2state=1; adjustSubmenu(2); }
  if (drop=="dropdown3") { drop3state=1; adjustSubmenu(3); }
  if (drop=="dropdown4") { drop4state=1; adjustSubmenu(4); }
  if (drop=="dropdown5") { drop5state=1; adjustSubmenu(5); }
  if (drop=="dropdown6") { drop6state=1; adjustSubmenu(6); }
  if (drop=="dropdown7") { drop7state=1; adjustSubmenu(7); }
  if (drop=="dropdown8") { drop8state=1; adjustSubmenu(8); }

  //value = (n) ? "shown" : "visible";
  //d.visibility = value;
}

function getWidth(i) {
  var drop = _findObj("dropdown" + i);
  if (drop) {
    var w;
    if (isDOM) {
      // The DIV does not define the width. Looks into the table
      for (var j = 0; j < drop.childNodes.length; j++) {
        if (drop.childNodes[j].nodeName == "TABLE") {
          // Obtain table width
          w = drop.childNodes[j].offsetWidth;
          break;
        }
      }
    }
    else w = drop.offsetWidth;
    return w;
  }
}

function adjustSubmenus() {
  for (var i = 1; i <= 8; i++) {
    adjustSubmenu(i);
  }
}

function adjustSubmenu(i) {
  var drop = _findObj("dropdown" + i);
  if (drop) {
    var w = getWidth(i);
    if (drop.style) drop = drop.style;
    var x = drop.left
    x = parseInt(x.substring(0, x.length - 2));

    var xx = (x + w > 743) ? 743 - w : x;
    drop.left = xx + "px";
        drop.width = w + "px";
  }
}

function dump(e) {
  var s = "";
  var count = 0;
  for (i in e) {
    s = s + i + ": " + e[i] + "\n";
    if (++count == 5) {
      if (!confirm(s)) return;
      s = "";
      count = 0;
    }
  }
  if (count > 0) alert(s);
}

window.onresize=function() {
  adjustSubmenus();
}
