/* Variables */
var idTimerOcultarSubmenuActivo = null; // Contiene el identificador del temporizador que oculta el submenu activo.
var reglaSubMenuCuandoInactivo = null;  // Contiene la regla del submenu que se muestra cuando no hay ningún submenu activo.
var reglaSubMenuActivo = null;  // Contiene la regla del submenu que Est&aacute; activo en estos momentos.


/*
 * Funciones generales.
 */

// DHTML: La variable contiene un buleano que indica si el navegador soporta DHTML.
var DHTML = (document.getElementById || document.all || document.layers);

// getObj: Devuelve el objeto que corresponde al identificador pasado como argumento.
function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

// establecerHojaEstiloActiva: Establece como activa la hoja de estilo con titulo "title".
function establecerHojaEstiloActiva(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if( (a.getAttribute("rel").indexOf("style") != -1) && a.getAttribute("title") ) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

// obtenerHojaEstiloActiva: Obtine el título de la hoja de estilo activa.
function obtenerHojaEstiloActiva() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if((a.getAttribute("rel").indexOf("style") != -1) && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

/*
 * Menú.
 */
 
/* resaltarLengueta: Resalta/desresalta la lengueta conrrespondiente al índice pasado como argumento */
function resaltarLengueta(indexLengueta, resaltar) {
	if (!DHTML) return;
	if (indexLengueta == null) return;

	var x = null;

	// Resaltamos la lengueta.
	x = document.getElementById( obtenerIdReglaLengueta(indexLengueta) );
	if (x != null) x.className  = (resaltar) ? 'opcionMenuResaltado' : 'opcionMenu';
}

/* obtenerIdReglaLengueta: Obtiene el identificador de la regla correspondiente a la lengueta deseada */
function obtenerIdReglaLengueta(menu) {
	if (menu == 'inicio') return null;
	else if (menu == 'empresa') return 'empresaMenu';
	else if (menu == 'clientes') return null;
	else if (menu == 'servicios') return 'serviciosMenu';
	else if (menu == 'revista') return 'revistaMenu';
	else if (menu == 'curriculum') return 'curriculumMenu';
	else if (menu == 'contacto') return null;
	// else if (menu == 'foro') return null; // YA NO SE UTILIZA, SE MANTIENE POR SI ACASO.
/*	DEBUG: */
	else {
		alert("Menú no válido: '" + menu + "'");
		return null;
	}
}

/*
 * Submenu.
 */
 
// mostrarSubMenu: Permite mostrar/ocultar un submenu dada su descriptor.
function mostrarSubmenu(submenu, visible) {
	if (!DHTML) return; if (!document.styleSheets) return;
	
	var reglaSubmenu = (submenu == null) ? null : obtenerReglaCSSSubmenu(submenu);
	
	// Ocultamos el anterior submenu, en caso que no sea el mismo.
	if ( (reglaSubMenuActivo != null) && (reglaSubMenuActivo != reglaSubmenu) ) reglaSubMenuActivo.style.display = 'none'; 
	
	// Mostramos el submenu deseado.
	reglaSubMenuActivo = reglaSubmenu;
	if ( visible ) {
	
		if (idTimerOcultarSubmenuActivo != null) {
			self.clearTimeout(idTimerOcultarSubmenuActivo);
			idTimerOcultarSubmenuActivo = null;
		}
		
		if (reglaSubMenuActivo != null) reglaSubMenuActivo.style.display = 'block';
		
	} else {
		if (reglaSubMenuCuandoInactivo == null) idTimerOcultarSubmenuActivo = self.setTimeout("ocultarSubMenuActivo()", 0);
		else ocultarSubMenuActivo();
	}
	
	resaltarLengueta(submenu, visible);
}

// ocultarSubmenu: Oculta el submenu.
function ocultarSubMenuActivo() {
	idTimerOcultarSubmenuActivo = null;
	
	if ( (reglaSubMenuCuandoInactivo != null) && (reglaSubMenuActivo == reglaSubMenuCuandoInactivo) ) return;
	
	// Mostramos el submenu de inactividad, si procede.
	if ( reglaSubMenuCuandoInactivo != null ) reglaSubMenuCuandoInactivo.style.display ='block';
	
	// Ocultamos el submenu activo.
	if (reglaSubMenuActivo != null) reglaSubMenuActivo.style.display = 'none';	
	
	reglaSubMenuActivo = reglaSubMenuCuandoInactivo;
}

/*
 * Hojas de estilo.
 */

// obtenerReglaCSSSubmenu: Permite obtener la reglas CSS
function obtenerReglaCSSSubmenu(submenu) {
	if (!DHTML) return null; if (!document.styleSheets) return null;

	var reglas = obtenerReglasCSS( obtenerIndiceCSSSubmenu() ) ;
	if (reglas != null) {
		var indice = obtenerIndiceReglaSubMenu(submenu);
		if (indice != -1) return reglas[indice];
	}
	
	return null
}

// obtenerIndiceCSSSubmenu: Permite obtener el índice del CSS correspondiente al submenú.
function obtenerIndiceCSSSubmenu() { return 0; }

// obtenerIndiceReglaSubMenu: Permite obtener un índice de la regla CSS dado el descriptor del submenu.
function obtenerIndiceReglaSubMenu(submenu) {
	if (submenu == 'inicio') return 0;
	if (submenu == 'empresa') return 1;
	else if (submenu == 'clientes') return 2;
	else if (submenu == 'servicios') return 3;
	else if (submenu == 'revista') return 4;
	else if (submenu == 'contacto') return 5;
	else if (submenu == 'curriculum') return 6;
/*	DEBUG: */
	else {
		alert("Submenu no válido: '" + submenu + "'");
		return -1;
	}
}

// obtenerReglasCSS: Permite obtener las reglas de una CSS dado su índice (en base 0) respecto a su orden de declaración de CSS.
function obtenerReglasCSS(indiceHojaEstilo) {
	var reglas = new Array();
	
	if (document.styleSheets[indiceHojaEstilo].cssRules)  reglas = document.styleSheets[indiceHojaEstilo].cssRules;
	else if (document.styleSheets[indiceHojaEstilo].rules)  reglas = document.styleSheets[indiceHojaEstilo].rules;
	else reglas = null;
	
	return reglas;
}
