/*
	Funcion : checkNavegador
	Parametros : Ninguno
	Devuelve : Objeto que representa el navegador
	Descripcion : Obtiene toda la información necesaria sobre el navegador actual
*/
function checkNavegador()
{
	this.ver = navigator.appVersion;
	this.agent = navigator.userAgent;
	this.dom = document.getElementById?1:0;
	this.opera5 = (navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0;
	this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6 = (this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4 = (document.all && !this.dom && !this.opera5)?1:0;
	this.ie = this.ie4 || this.ie5 || this.ie6;
	this.mac = this.agent.indexOf("Mac")>-1;
	this.ns6 = (this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4 = (document.layers && !this.dom)?1:0;
	this.bw = this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5;
	
	return this;
}

var navegador = checkNavegador();
//var NumeroCarpetas = 9;			// Numero de carpetas principales -> ver head.php

// Unidad de medida de la posicion de los objetos en la pantalla
var unidad_px = (navegador.ns4 || navegador.opera)?"":"px";

var menuTop, menuSub;

/*
	Funcion : makeMenu
	Parametros : 
	Devuelve : Objeto menu
	Descripcion : Genera una rama del menu
*/
function makeMenu(obj, padre, num)
{
	obj_ns4 = 'document.'+padre+'.document.'+obj;
	
	var item = navegador.ie4?document.all[obj]:navegador.ns4?eval(obj_ns4):document.getElementById(obj);
	var aux = navegador.ns4?item.document:0;
	
	this.css = navegador.ns4?item:item.style;	
	this.x = (navegador.ns4 || navegador.opera5)?this.css.left:item.offsetLeft;
	this.y = (navegador.ns4 || navegador.opera5)?this.css.top:item.offsetTop;
	this.h = (navegador.ie || navegador.ns6)?item.offsetHeight:navegador.ns4?aux.height:navegador.opera5?this.css.pixelHeight:0;
		
	this.ocultar = ocultar;
	this.mostrar = mostrar;
	this.esVisible = esVisible;
	this.mover = mover;
	
	this.num = num;
	
	return this;
}

/*
	Funcion : mostrar
	Parametros : Ninguno
	Devuelve : Nada
	Descripcion : Muestra una rama del menu
*/
function mostrar()
{
	this.css.visibility = 'visible';
//	this.css.display = 'block';
	
	var img = document.getElementById('img_divTop'+this.num);
	if(img)
	{
		if(img.src.substring(img.src.length-13, img.src.length-6) == "carpeta")
			img.src = img.src.substring(0, img.src.length-5) + "a.gif";
		else if(img.src.substring(img.src.length-11, img.src.length-6) == "libro")
			img.src = img.src.substring(0, img.src.length-5) + "a.gif";
	}
}

/*
	Funcion : ocultar
	Parametros :  Ninguno
	Devuelve : Nada
	Descripcion : Oculta una rama del menu
*/
function ocultar()
{
	this.css.visibility='hidden';
//	this.css.display = 'none';
	
	var img = document.getElementById('img_divTop'+this.num);	
	if(img)
	{
		if(img.src.substring(img.src.length-13, img.src.length-6) == "carpeta")
			img.src = img.src.substring(0, img.src.length-5) + "c.gif";
		else if(img.src.substring(img.src.length-11, img.src.length-6) == "libro")
			img.src = img.src.substring(0, img.src.length-5) + "c.gif";
	}
}

/*
	Funcion : esVisible
	Parametros : Nada
	Devuelve : Booleano
	Descripcion : Es visible una rama del menu?
*/
function esVisible()
{
	if(this.css.visibility=='hidden' || this.css.visibility=='HIDDEN' || this.css.visibility=='hide')
		return false;
		
	return true;
}

/*
	Funcion : mover
	Parametros : Posición X, Y 
	Devuelve : Nada
	Descripcion : Mueve un objeto del menu a la posición x,y indicada
*/
function mover(x, y)
{

	this.x = x;
	this.y = y;
	this.css.left = this.x + unidad_px;
	this.css.top = this.y + unidad_px
}

/*
	Funcion : menu
	Parametros : Rama del menu a plegar/desplegar
	Devuelve : Nada
	Descripcion : Expande/contrae una rama del menu
*/
function menu(num)
{
/*
	// Primero que nada, plegamos todas las demas ramas
	// a) Ocultar
	for(var i=0; i<menuSub.length; i++)
	{
		if(i != num)
			menuSub[i].ocultar();
	}
	// b) Reubicar los elementos del menu
	for(var i=1; i<menuTop.length; i++)
		menuTop[i].mover(0, menuTop[i-1].y - menuSub[i-1].h);
*/
	// Ahora desplegamos/plegamos la rama según su estado
	// a) Mostar/ocultar
	if(!menuSub[num].esVisible())
		menuSub[num].mostrar();
	else
		menuSub[num].ocultar();
		
	// b) Reubicar los elementos del menu
	for(var i=1; i<menuTop.length; i++)
	{ 
		if(menuSub[i-1].esVisible())
			menuTop[i].mover(0, menuTop[i-1].y);
		else
			menuTop[i].mover(0, menuTop[i-1].y - menuSub[i-1].h);
	}
}

/*
	Funcion : initMenu
	Parametros : Ninguno
	Devuelve : Nada
	Descripcion : Inicializa las posiciones y objetos del menu
*/
function initMenu()
{	
	menuTop = new Array();
	menuSub = new Array();

	// Generar los objetos del menu
	for (var i=0; i<NumeroCarpetas; i++)
	{
		menuTop[i] = new makeMenu('divTop'+i,'divCont', i);
		menuSub[i] = new makeMenu('divSub'+i,'divCont.document.divTop'+i, i);
		
		menuSub[i].ocultar();
	}

	// Posicionar los objetos
	menuTop[0].mover(0, 0);	
	for (var i=1; i<NumeroCarpetas; i++)	
		menuTop[i].mover(0, menuTop[i-1].y - menuSub[i-1].h);

	// Mostrar el elemento contenedor

	var item = navegador.ie4?document.all['divCont']:navegador.ns4?eval('document.divCont'):document.getElementById('divCont');
	var css = navegador.ns4?item:item.style;
	css.visibility = 'visible';	
}

// Si se ha detectado un navegador compatible, generar el menu
/*Esto ahora lo hacemos en el index.php
if(navegador.bw)
	onload = initMenu;
*/