//window.onload=function(){pintaXML();}
window.onload=function(){descargaArchivo();}

var READY_STATE_UNINITIALIZED=0; 
var READY_STATE_LOADING=1; 
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3; 
var READY_STATE_COMPLETE=4;
 
var peticion_http;
 
function cargaContenido(url, metodo, funcion) {
  peticion_http = inicializa_xhr();
 
  if(peticion_http) {
    peticion_http.onreadystatechange = funcion;
    peticion_http.open(metodo, url, true);
    peticion_http.send(null);
  }
}
 
function inicializa_xhr() {
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
}
 
function muestraContenido() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
      xmlDoc=peticion_http.responseXML;
	  pintamenuBotones();
    }
  }
}
 
function descargaArchivo() {
  cargaContenido("./XML/menuDom.xml", "GET", muestraContenido);
}

function pintamenuBotones(){
  var botones = xmlDoc.getElementsByTagName('boton');
  var botonesLVL2 = xmlDoc.getElementsByTagName('nodo');
  var listaMenu = document.createElement('ul');
  for (i=0; i < botones.length; i++){
	  var li = document.createElement('li');
	  li.id = "nodo" + i;
	  var encabezado = document.createElement('h1');
	  var enlace = document.createElement('a');
	  enlace.href = botones[i].getAttribute('ruta');
	  listaMenu.appendChild(li); 
	  li.appendChild(encabezado);
	  encabezado.appendChild(enlace);
	  enlace.appendChild(document.createTextNode(botones[i].getAttribute('texto')));
	  if(botones[i].getElementsByTagName('nodo').length>0){
		  
		var ulLVL2 = document.createElement('ul');
		ulLVL2.id = 'ulLVL2';
		li.appendChild(ulLVL2);
		
		for (var j=0; j < botones[i].getElementsByTagName('nodo').length; j++){
			var rutaLVL2 = botones[i].getElementsByTagName('nodo')[j].getAttribute('rutaLVL2');
			var nd=botones[i].getElementsByTagName('nodo')[j];
			//alert(nd.childNodes[0].firstChild.nodeValue);
			if(document.all){
				var textoBoton = nd.childNodes[0].firstChild.nodeValue;
			}else{
				var textoBoton = nd.childNodes[1].firstChild.nodeValue;
			}
			var liLVL2 = document.createElement('li');
			liLVL2.id="marca"+i+"_"+j;
			var enlaceLVL2 = document.createElement('a');
			enlaceLVL2.href = rutaLVL2;
			enlaceLVL2.id="enlace"+i+"_"+j;
			ulLVL2.appendChild(liLVL2);
			liLVL2.appendChild(enlaceLVL2);
			enlaceLVL2.appendChild(document.createTextNode(textoBoton));		  
			if(document.all){
				if(nd.childNodes[1]!= null){
					var liSub=liLVL2;
					var icoLVL2=document.createElement("img");
					icoLVL2.src="img/mas.gif";
					icoLVL2.border="0px";
					liSub.appendChild(icoLVL2);
					//liSub.className="liMas";
					var ulLVL3 = document.createElement('ul');
					liSub.appendChild(ulLVL3);
					for(var k = 0; k <nd.childNodes[1].childNodes.length; k++){
						var liLvl3=document.createElement("li");
						ulLVL3.appendChild(liLvl3);
						var aLVL3=document.createElement("a");
						aLVL3.href=nd.childNodes[1].childNodes[k].getAttribute("rutaLVL3");
						liLvl3.appendChild(aLVL3);
						aLVL3.appendChild(document.createTextNode(nd.childNodes[1].childNodes[k].firstChild.nodeValue));
						//alert(nd.childNodes[1].childNodes[k].firstChild.nodeValue)
					}
				}
			}else{
				//alert(nd.childNodes[3])
				if(nd.childNodes[3]!= null){
					var liSub=liLVL2;
					var icoLVL2=document.createElement("img");
					icoLVL2.src="img/mas.gif";
					icoLVL2.border="0px";
					liSub.appendChild(icoLVL2);
					//liSub.className="liMas";
					var ulLVL3 = document.createElement('ul');
					liSub.appendChild(ulLVL3);
					for(var k = 0; k <nd.childNodes[3].getElementsByTagName("subBoton").length; k++){
						var liLvl3=document.createElement("li");
						ulLVL3.appendChild(liLvl3);
						var aLVL3=document.createElement("a");
						aLVL3.href=nd.childNodes[3].getElementsByTagName("subBoton")[k].getAttribute("rutaLVL3");
						liLvl3.appendChild(aLVL3);
						var texto3=document.createTextNode(nd.childNodes[3].getElementsByTagName("subBoton")[k].firstChild.nodeValue);
						aLVL3.appendChild(texto3);
					}
				}
			}			
		} 
	  }
  }
  document.getElementById('menuCont').appendChild(listaMenu);
  //alert(botones[i].getElementsByTagName('nodo')[j].childNodes[2].length);
  //calcula();
  destaca(nodo,subNodo);
  var blog=document.getElementById('enlace8_4');
  blog.target = "_blank";
}

function calcula(){				
	//var alturaCuerpo = document.getElementById('fixed').scrollHeight;
	if(document.all){
		document.body.style.height="100%";
		var alturaCuerpo= document.body.offsetHeight;
		document.body.style.height="";
	}else{
		var alturaCuerpo= window.innerHeight;
	}
	//alert(document.getElementById('tablaRef').scrollHeight+"   "+alturaCuerpo)
	if(document.getElementById('tablaRef').scrollHeight<alturaCuerpo){
		document.getElementById('pieWeb').style.top = (alturaCuerpo-document.getElementById('tablaRef').scrollHeight+2) +"px";
		//document.getElementById("fixed").style.height=alturaCuerpo+"px";
		document.getElementById('tablaRef').style.height = alturaCuerpo + "px";
	}
	
	//alert(document.getElementById('pieWeb').scrollTop);
}

function destaca(nodo,subNodo){
	if(nodo!=null){
		var objetoLVL0 = document.getElementById(nodo);
		objetoLVL0.className = "liAct";
	}
	if(subNodo!=null){
		//document.getElementById('tablaRef').style.backgroundImage="none";
		if(document.getElementById('btFlashBanner_1')){document.getElementById('tablaRef').style.backgroundImage='url(img/tile/tileHome.jpg)';
			}else{
				document.getElementById('tablaRef').style.backgroundImage='url(img/tile/tileHome_lvl3.jpg)';
		}
		var objetoLVL1 = document.getElementById(subNodo);
		objetoLVL1.className = "liAct";
	}
	
}