//-------------------------------------------------
// VARIABLES GLOBALES
//-------------------------------------------------

var menuActivo = null;
var hoy = new Date();
activar = false;
esExplorer = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);

//-------------------------------------------------
// CLASES
//-------------------------------------------------

function Menu(opciones)
{
  this.opciones = opciones;
}

function OpcionMenu(texto, enlace)
{
  this.texto  = texto;
  this.enlace = enlace;
}

//-------------------------------------------------
// FUNCIONES
//-------------------------------------------------

function elegidaOpcion(opcionElegida)
{
  cabecera = parent.frames.cabecera;
  opciones = cabecera.document.getElementsByTagName("a");
  for (i = 0; i < opciones.length; i++)
  {
    opcion = opciones[i];
    opcion.style.color = (opcion.id == opcionElegida)? "#ffff99" : "#993333";
  }
}

function esBisiesto()
{
  anio = new Date().getFullYear();
  return (anio % 4 == 0) && ((!anio % 100 == 0) || (anio % 400 == 0));
}

function esHoy(dia)
{
  return (dia.getFullYear() == hoy.getFullYear()) &&
         (dia.getMonth   () == hoy.getMonth   ()) &&
		 (dia.getDate    () == hoy.getDate    ());
}

function esAyer(dia)
{
  var probableHoy = new Date(dia);
  probableHoy.setDate(dia.getDate() + 1);
  return esHoy(probableHoy);
}

function esPasado(dia)
{
  return (dia.getFullYear() < hoy.getFullYear()) ||
         (dia.getMonth   () < hoy.getMonth   ()) ||
		 (dia.getDate    () < hoy.getDate    ());
}

function esFinDeSemana(dia)
{
  return (dia.getDay() == 0) || (dia.getDay() == 6);
}

function textoFecha(fecha)
{
  return fecha.getFullYear() + '-' + dosCifras(fecha.getMonth() + 1) + '-' + dosCifras(fecha.getDate());
}

function dosCifras(numero)
{
  var cifras = new String(numero);
  if (cifras.length == 1) cifras = "0" + cifras;
  return cifras;
}

function capturarClics(objeto, funcion, funcionFuera)
{
  if (!esExplorer)
  {
    document.captureEvents(Event.CLICK); // Netscape
  }
  document.onclick = funcionFuera;
  objeto.onclick = funcion;
}

function crearMenu(menu, objeto)
{
  var estilo = 'display:none; position:absolute; top:0; left:0;';
  document.write('<table id="menu_' + objeto.id + '" border="0" style="' + estilo + '" class="menuEmergente">');
  var opciones = menu.opciones;
  for (i = 0; i < opciones.length; i++)
  {
    var opcion = opciones[i];
    document.write('<tr><td onclick="activar=false; ocultarMenu()" class="textoMenuEmergente">');
    document.write('<a href="' + opcion.enlace + '">' + opcion.texto + '</a></td></tr>');
  }
  document.write('</table>');
  capturarClics(objeto, activarMenu, ocultarMenu);
}

function activarMenu(evento)
{
  ocultarMenu();
  activar = !activar;
  var e  = esExplorer? event : evento;
  var id = esExplorer? e.srcElement.id : e.target.id;
  var menu = document.getElementById("menu_" + id);
  menu.style.top  = e.clientY + "px";
  menu.style.left = e.clientX + "px";
  menu.style.display = "block";
  menuActivo = menu;
}

function ocultarMenu()
{
  if (!activar)
  {
    if (menuActivo != null) menuActivo.style.display = "none";
	menuActivo = null;
  }
  else
  {
    activar = false;
  }
}

function ponerImagen(id, imagen)
{
  document.getElementById(id).src = imagen;
}
