// Courtesy of SimplytheBest.net http://simplythebest.net/info/dhtml_scripts.html
//Nuevo -
var fMenuBar = new Array(
	new Array("Introduction", "70", "content/intro.html"),
	new Array("Main Personnel", "110", "content/main_personnel.html"),
	new Array("Recent Publications", "140", "content/r_publications.html"),
	new Array("Research Projects", "130", "content/espvirtual.html"),
	new Array("Contact", "100", "content/contact.html")
	//new Array("Exit", "80", "about/careers.html")
);

var fPopups = new Array(
  new Array( // Introduction
	"4px",
	
	new Array("Introduction", "content/intro.html")
  ),
  new Array( // Main Personnel
	"110px",
	
	new Array("Main research personnel", "content/main_personnel.html")
  ),
  new Array( // Recent Publications
	"210px",
	new Array("2009", "content/r_publications.html"),
	new Array("2008", "content/r_publications_2008.html"),
	new Array("2007", "content/r_publications_2007.html"),
	new Array("2006", "content/r_publications_2006.html"),
	new Array("2005", "content/r_publications_2005.html"),
	new Array("2004", "content/r_publications_2004.html"),
	new Array("2003", "content/r_publications_2003.html"),
	new Array("2002", "content/r_publications_2002.html"),
	new Array("Technical reports", "content/technical_reports.html")
  ),
  new Array( // Research projects
	"400",
	
	new Array("España Virtual", "content/espvirtual.html"),
	new Array("Simflowny", "simflowny/index.html"),
	new Array("Other research projects", "content/r_projects.html")
  ),
  new Array( // Contact
	"695",
	
	new Array("Contact", "content/contact.html"),
	new Array("Open positions", "content/openp.html")
  )
);

function printMenuBar(base, popup, subitem) {
  document.writeln('<table width="793" 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="8"><img src="', base,
			'images/common/arrow_up.gif" border="0" width="2" height="20"></td>');
    }
    document.writeln('<td width="', item[1], '" height="24"> ',
			'<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="1" height="8">');
 // El código comentado daba un mal comportamiento en los popups. Creo que es debido al mal uso de los parámetros de printMenuBar en los html.
 //   if (sel && subitem == i) {
 //     document.writeln('<span class="subheader2">', item[0], '</span>');
 //   } else {
      document.writeln('<a href="', (item[1].indexOf("http://") == -1) ? 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 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;

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 }

  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;

  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); }

  //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 > 793) ? 793 - 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 == 7) {
			if (!confirm(s)) return;
			s = "";
			count = 0;
		}
	}
	if (count > 0) alert(s);
}

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