/****************************************************************************
Menú desplegable accesible. 05-10-06
Autor: tunait http://javascript.tunait.com/ tunait@yahoo.com
Script de libre uso mientras se mantengan los créditos de autor intactos.
No permitida su re-distribución.
*****************************************************************************/



var retardoEnCerrar = 5000000
var menuActivo = false
var zindex = 1000
var tiempoCierre



function nodoHijo(obj){
	if(obj.hasChildNodes){
		for(m=0; m<obj.childNodes.length; m++){
			if(obj.childNodes[m].nodeType == 1 && obj.childNodes[m].tagName.toLowerCase() == 'ul') {
				return obj.childNodes[m]
				break
			}
		}
	}
}



function recorre(elems, obj){
	for(s=0; s<elems-1; s++){
		if(obj.childNodes[s].nodeType == 1 && obj.childNodes[s].tagName.toLowerCase() == 'li'){
			alert(obj.childNodes[s].tagName + ": " + obj.childNodes[s].childNodes.length)
		}
	}
}




H = document.documentElement.childNodes[0]
tagLink = document.createElement('link')
tagLink.type = "text/css"
tagLink.rel = "stylesheet"
tagLink.href = "../_css/menu_desplegable.css"
H.appendChild(tagLink)

	

	
function iniciaMenu(){
	for(s in document.getElementsByTagName('link')){
		hoja = document.getElementsByTagName('link')[s]
		if(hoja.title == 'estilosMenusinjs') hoja.disabled = true
		if(hoja.title == 'estilosMenujs') hoja.disabled = false

	}
	/*if(document.styleSheets && document.styleSheets.length > 0){*/
	for(s in document.styleSheets){
		hoja = document.styleSheets[s]
		if(hoja.title == 'estilosMenusinjs') hoja.disabled = true
		if(hoja.title == 'estilosMenujs') hoja.disabled = false
	}
	Menu = document.getElementById('menu_desp')
	as = document.getElementById('menu_desp').getElementsByTagName('a')
	for(var m in as){
		as[m].className = 'menuSecundario'
	}
	li = document.getElementById('menu_desp').getElementsByTagName('li')
	for(m in li){
		if(li[m].parentNode == Menu){
			li[m].firstChild.className = 'menuSecundario'
			li[m].style.cssFloat  = 'left'
			li[m].style.styleFloat  = 'left'
			}
	}
	uls = document.getElementById('menu_desp').getElementsByTagName('ul').length
	for(m=0; m < uls ; m++){
		ul = document.getElementById('menu_desp').getElementsByTagName('ul')[m]
		if(ul.parentNode.parentNode == Menu){
			ul.parentNode.style.cssFloat  = 'left'
			ul.parentNode.style.styleFloat  = 'left'
			ul.parentNode.firstChild.className = 'menuPrincipal'
			ul.parentNode.firstChild.style.cursor = 'pointer'
			if(ul.parentNode.firstChild.nodeName.toLowerCase() != 'a'){
				txt = ul.parentNode.firstChild.firstChild.nodeValue
				lin = document.createElement('a')
				lin.href = '#menu'
				lin.className = 'menuPrincipal'
				lin.title ="Haga click para abrir más opciones";
				lin.appendChild(document.createTextNode(txt))
				ul.parentNode.replaceChild(lin,ul.parentNode.firstChild)
			}
		}
		else{
			if(ul.parentNode.firstChild.nodeName.toLowerCase() != 'a'){
				txt = ul.parentNode.firstChild.firstChild.nodeValue
				lin = document.createElement('a')
				lin.href = '#menu'
				lin.className = 'menuSecundario'
				lin.title ="Haga click para abrir más opciones";
				lin.appendChild(document.createTextNode(txt))
				ul.parentNode.replaceChild(lin,ul.parentNode.firstChild)
			}
		}
		ul.style.visibility = 'hidden'
		ul.style.position = 'absolute'
		ul.className = 'cajaMenuDesp'
		ul.parentNode.firstChild.style.display = 'block'
		ul.parentNode.style.zIndex = zindex--
		ul.parentNode.firstChild.title ="Haga click para abrir más opciones"
		ul.parentNode.firstChild.setAttribute('visible','off') //atributo en A
		if(ul.offsetParent && ul.parentNode.parentNode != Menu){
			posIzq = 0
			derecha = 0
			ul2 = ul
			while(ul2 = ul2.offsetParent){
				posIzq += ul2.offsetLeft
			}
			anchoDoc = document.body.offsetWidth
			derecha = anchoDoc - (posIzq + ul.offsetWidth)
			if(derecha < 0) ul.style.marginLeft = derecha + "px"
		}
		ul.parentNode.firstChild.onclick = function(){ //click en el A
			hijo = nodoHijo(this.parentNode) //devuelve el UL del LI
			if(this.parentNode.parentNode == Menu){
				actMenus(this, hijo)
			}
			else{
				actSubMenus(this, hijo)
			}
			return false
		}
	}
	
	
	Menu.onmouseover = function(){
		menuActivo = true
	}
	
	
	Menu.onmouseout = function(){
		menuActivo = false
		clearTimeout(tiempoCierre)
		tiempoCierre = setTimeout('cierraMenu()',retardoEnCerrar)
	}
	
	
	$('.primermenu p p').css({
		'padding-left':'25px'
	});

	/*************** INICIO - MENUS PERSIANA**************/
	$('.primermenu').find('center:has(u)').each(function(){
		$(this).find('div:first').click(
			function(event){
				if (this == event.target) {
					if ($(this).parent().children().not('div').is(':hidden')) {
						/*Para evitar el parpadeo de la menu al inicial la animacion*/
						$(this).css('background-position','-1px -22px').parent().children().not('div').css('display','none');
						$(this).css('background-position','-1px -22px').parent().children().not('div').slideDown();
						$(this).css('background-position','-1px -22px').parent().children().not('div').css('display','block');
					}else{
						$(this).css('background-position','-1px 2px').parent().children().not('div').slideUp();
					}
				}
				return false;
			}
		)
		.css('cursor','pointer')
		.css('color','#FFFFFF')
		.parent().children().not('div').hide().find('a:last').css('border-bottom','none');
	});
	/***************** FIN - MENUS PERSIANA**************/
	
	/*}*/
}


function CerrarMenuCompleto(){
	var uls = document.getElementById('menu_desp').getElementsByTagName('ul').length
	for(var m=0; m < uls ; m++){
		var ul = document.getElementById('menu_desp').getElementsByTagName('ul')[m]
		ul.style.visibility = 'hidden'
		ul.parentNode.firstChild.className = 'menuPrincipal'
		ul.parentNode.firstChild.title="Haga click para mostrar más opciones"
		if(ul.parentNode.firstChild == obj) continue
		ul.parentNode.firstChild.setAttribute('visible','off')
	}
}


function actMenus(obj,hijo){
	var uls = document.getElementById('menu_desp').getElementsByTagName('ul').length
	for(var m=0; m < uls ; m++){
		var ul = document.getElementById('menu_desp').getElementsByTagName('ul')[m]
		ul.style.visibility = 'hidden'
		ul.parentNode.firstChild.className = 'menuPrincipal'
		ul.parentNode.firstChild.title="Haga click para mostrar más opciones"
		if(ul.parentNode.firstChild == obj) continue
		ul.parentNode.firstChild.setAttribute('visible','off')
	}
	if(obj.getAttribute('visible') == 'off'){
				hijo.style.visibility = 'visible'
				obj.setAttribute('visible','on')
				obj.className = 'menuSeleccionado'
				obj.title = "Haga click para ocultar las opciones"
			}
	else{
		hijo.style.visibility = 'hidden'
		obj.setAttribute('visible','off')
		obj.className = 'menuPrincipal'
		obj.title = "Haga click para mostrar más opciones"
	}
	obj.focus()
}



function actSubMenus(obj, hijo){
	ulspadre = obj.parentNode.parentNode
	var uls = ulspadre.getElementsByTagName('ul').length
	for(var m=0; m < uls ; m++){
		var ul = ulspadre.getElementsByTagName('ul')[m]
		ul.style.visibility = 'hidden'
		ul.parentNode.firstChild.className = 'menuPrincipal'
		ul.parentNode.firstChild.title="Haga click para mostrar más opciones"
		if(ul.parentNode.firstChild == obj) continue
		ul.parentNode.firstChild.setAttribute('visible','off')
	}
	if(obj.getAttribute('visible') == 'off'){
				hijo.style.visibility = 'visible'
				obj.setAttribute('visible','on')
				obj.className = 'menuSeleccionado'
				obj.title = "Haga click para ocultar las opciones"
			}
	else{
		hijo.style.visibility = 'hidden'
		obj.setAttribute('visible','off')
		obj.className = 'menuPrincipal'
		obj.title = "Haga click para mostrar más opciones"
	}
	obj.focus()
}



function cierraMenu(){
	if(!menuActivo){
		var uls = document.getElementById('menu_desp').getElementsByTagName('ul').length
		for(var m=0; m < uls ; m++){
			var ul = document.getElementById('menu_desp').getElementsByTagName('ul')[m]
			ul.style.visibility = 'hidden'
			ul.parentNode.firstChild.className = 'menuPrincipal'
			ul.parentNode.firstChild.title="Haga click para mostrar más opciones"
			ul.parentNode.firstChild.setAttribute('visible','off')
		}
	}
}



var javascriptActivado = document.getElementById && document.getElementsByTagName;
if(javascriptActivado){document.documentElement.className="js";}


