// ===================================================================================
// Funciones JavaScript para menus. 
// Necesita 'dhtml.js'
// ===================================================================================

// Constructor
function Menu(id, horizontal, niveles, nombreEstilos)
{
	this.id = id;
	this.horizontal = horizontal;
	this.niveles = niveles;
	this.nombreEstilos = nombreEstilos;
	// Retardo de cierre de submenú, en milisegundos
	this.retardoCierre = 200;
	// Timer usado para cerrar con retardo
	this.timerCierre;
	// Identificador del menú activo (el que contiene el cursor)
	this.menuActivo = null;
	// Array con los identificadores de los menús abiertos
	this.menusAbiertos = new Array();
	// Poner a nulo el array JavaScript con los id de los menús abiertos
	// niveles = máximo número de niveles de menú (se le asigna valor al generar el menú)
	for (var i = 0; i < this.niveles; ++i) {
		this.menusAbiertos[i] = null;
	}
}

// Indices de los nombres de estilos (propiedades de clase)
Menu.ESTILO_SECCION = 0;
Menu.ESTILO_ACTIVO = 1;
Menu.ESTILO_ACTIVO_RES = 2;
Menu.ESTILO_INACTIVO = 3;
Menu.ESTILO_INACTIVO_RES = 4;
Menu.ESTILO_BORDE = 5;

// Cierra todos los submenús abiertos de nivel igual o mayor al indicado
Menu.prototype.cerrarMenu = function(nivel)
{
	// Si no ha quedado ningún nivel activo, cerrar todos los submenús
	if (this.menuActivo == null) {
		nivel_comienzo = 0;
	} else {
		nivel_comienzo = nivel;
	}
	for (var i = nivel_comienzo; i < this.niveles; ++i) {
		var submenu = document.getElementById(this.menusAbiertos[i]);
		if (submenu) {
			ocultar(submenu);
		}
		this.menusAbiertos[i] = null;
	}
}

Menu.prototype.resaltarMenu = function(id_opcion, nivel, id_submenu)
{
	var opcion = document.getElementById(id_opcion);
	if (opcion) {
		if (opcion.className == this.nombreEstilos[Menu.ESTILO_ACTIVO])
			opcion.className = this.nombreEstilos[Menu.ESTILO_ACTIVO_RES];
		else if (opcion.className == this.nombreEstilos[Menu.ESTILO_INACTIVO])
			opcion.className = this.nombreEstilos[Menu.ESTILO_INACTIVO_RES];
	}
	// Cerrar inmediatamente cualquier submenú de menor nivel (excepto si es un sumbenú de la opción actual), cancelando un posible cierre pendiente
	if (this.menusAbiertos[nivel + 1] != null && (arguments.length < 3 || this.menusAbiertos[nivel + 1] != id_submenu)) {
		this.menuActivo = this.menusAbiertos[nivel];	//*** No debería ser necesario, pero este evento se dispara tras un 'salir_menu' que pone a null la variable this.menuActivo, y provoca que se cierren todos los submenús
		clearTimeout(this.timerCierre);
		this.cerrarMenu(nivel + 1);
	}
	// Si hay un submenú, abrirlo
	if (arguments.length == 3) {
		var submenu = document.getElementById(id_submenu);
		if (submenu) {
			// Si el submenú ya está visible (=> el cursor viene del submenú), desactivar el timeout que lo cierra y terminar
			if (submenu.style.visibility == 'visible') {
				clearTimeout(this.timerCierre);
				return;
			}
			// Llamar al evento onMenuOpen para permitir realizar cambios antes de mostrar el submenú
			if (typeof(onMenuOpen) == 'function') {
				onMenuOpen(id_opcion);
			}
			// Calcular posición del submenú
			var posX, posY;
			if (nivel == 0) {
				// Hay que tener en cuenta la posición de la tabla que contiene todo el menú 
				if (this.horizontal) {
					posX = posicionIzquierda(opcion.parentNode.parentNode.parentNode) + posicionIzquierda(opcion);
					posY = posicionSuperior(opcion.parentNode.parentNode.parentNode) + alto(opcion);
				} else {
					posX = posicionIzquierda(opcion.parentNode.parentNode.parentNode) + ancho(opcion);
					posY = posicionSuperior(opcion.parentNode.parentNode.parentNode) + posicionSuperior(opcion);
				}
			} else {
				var capa_padre = document.getElementById(this.menusAbiertos[nivel]);
				posX = posicionIzquierda(opcion) + ancho(opcion) + posicionIzquierda(capa_padre);
				posY = posicionSuperior(opcion) + posicionSuperior(capa_padre);
			}
			situar(submenu, posX, posY);
			mostrar(submenu);
			this.menusAbiertos[nivel + 1] = id_submenu;
		}
	}
}

Menu.prototype.deresaltarMenu = function(id_opcion, nivel, id_submenu)
{
	var opcion = document.getElementById(id_opcion);
	if (opcion) {
		if (opcion.className  == this.nombreEstilos[Menu.ESTILO_ACTIVO_RES])
			opcion.className = this.nombreEstilos[Menu.ESTILO_ACTIVO];
		else if (opcion.className == this.nombreEstilos[Menu.ESTILO_INACTIVO_RES])
			opcion.className = this.nombreEstilos[Menu.ESTILO_INACTIVO];
	}
	// Si hay un submenú, activar el cierre del submenú (se cancelará el cierre si se entra en el submenú)
	if (arguments.length == 3) {
		var submenu = document.getElementById(id_submenu);
		if (submenu) {
			this.timerCierre = setTimeout(this.id + '.cerrarMenu(' + (nivel + 1) + ')', this.retardoCierre);
		}
	}
}

Menu.prototype.entrarMenu = function(id_opcion, nivel)
{
	this.menuActivo = id_opcion;
	// Si estaba activo el timer de cierre del menú, desactivarlo
	clearTimeout(this.timerCierre);
}

Menu.prototype.salirMenu = function(id_opcion, nivel)
{
	this.menuActivo = null;
	// Si no se ha abierto un menú hijo, activar el cierre del menú
	if (this.menusAbiertos[nivel + 1] == null) {
		var menu = document.getElementById(id_opcion);
		if (menu) {
			this.timerCierre = setTimeout(this.id + '.cerrarMenu(' + nivel + ')', this.retardoCierre);
		}
	}
}

// Devuelve true si la opción de menú indicada está marcada
Menu.prototype.menuMarcado = function(id_opcion)
{
	return (document.getElementById(id_opcion).childNodes[0].childNodes[0].attributes['src'] == 'menu-marcado.gif');
}
// Marca (marcar == true) o desmarca (marcar == false) la opción de menú indicada
Menu.prototype.marcarMenu = function(id_opcion, marcar)
{
	var marca = document.getElementById(id_opcion).childNodes[0].childNodes[0];
	if (marca) {
		marca.src = (marcar ? 'menu-marcado.gif' : 'menu-desmarcado.gif');
	}
}
// En el rango de opciones de menú comprendido entre id_primera_opcion e id_ultima_opcion, dejar marcada sólo id_opcion
Menu.prototype.marcarMenuExclusivo = function(id_primera_opcion, id_ultima_opcion, id_opcion)
{
	var opciones_menu = document.getElementById(id_primera_opcion).parentNode.childNodes;
	var en_rango = false;
	for (var i = 0; i < opciones_menu.length; ++i) {
		var opcion = opciones_menu[i];
		if (en_rango || opcion.id == id_primera_opcion) {
			en_rango = true;
			opcion.childNodes[0].childNodes[0].src = (opcion.id == id_opcion ? 'menu-marcado.gif' : 'menu-desmarcado.gif');
			if (opcion.id == id_ultima_opcion) {
				en_rango = false;
				break;
			}
		}
	}
}

Menu.prototype.menuActivo = function(id_opcion)
{
	var opcion = document.getElementById(id_opcion);
	if (opcion) {
		return opcion.className == this.nombreEstilos[Menu.ESTILO_ACTIVO];
	}
	return false;
}

Menu.prototype.ActivarMenu = function(id_opcion, activar)
{
	var opcion = document.getElementById(id_opcion);
	if (opcion)
	{
		opcion.className = (activar ? this.nombreEstilos[Menu.ESTILO_ACTIVO] : this.nombreEstilos[Menu.ESTILO_INACTIVO]);
	}
}

// Funciones de barra de herramientas /////////////////////////////////////////
function resaltar_boton(id_boton, resaltar)
{
	var boton = document.getElementById(id_boton);
	if (boton) {
		if (resaltar) {
			if (boton.className == 'boton-barra-inactivo') {
				boton.className = 'boton-barra-inactivo-resaltado';
			} else {
				boton.className = 'boton-barra-resaltado';
			}
		} else {
			if (boton.className == 'boton-barra-inactivo-resaltado') {
				boton.className = 'boton-barra-inactivo';
			} else {
				boton.className = 'boton-barra';
			}
		}
	}
}
function resaltar_boton2(id_boton, resaltar)
{
	var boton = document.getElementById(id_boton);
	if (boton) {
		if (resaltar) {
			if (boton.className == 'boton-barra-inactivo') {
				boton.className = 'boton-barra-inactivo-resaltado';
			} else {
				boton.className = 'boton-barra-resaltado';
			}
		} else {
			if (boton.className == 'boton-barra-inactivo-resaltado') {
				boton.className = 'boton-barra-inactivo2';
			} else {
				boton.className = 'boton-barra';
			}
		}
	}
}

function pulsar_boton(id_boton, pulsar)
{
	var boton = document.getElementById(id_boton);
	if (boton) {
		if (pulsar) {
			if (boton.className == 'boton-barra-inactivo-resaltado') {
				boton.className = 'boton-barra-inactivo-pulsado';
			} else {
				boton.className = 'boton-barra-pulsado';
			}
		} else {
			if (boton.className == 'boton-barra-inactivo-pulsado') {
				boton.className = 'boton-barra-inactivo-resaltado';
			} else {
				boton.className = 'boton-barra-resaltado';
			}
		}
	}
}
