/*cont_inicio.js practicamente es el motor que carga los contenidos XML a traves de AJAX y los almacena en variable para que puedas sectorizar la informacion.
NOTAS:
1. En lo que a noticias se refiere levantarewmos un php para extraer la informacion necesaria crearla en un nuevo xml y mandarlo a la pagina, asi las consultas que el motor AJAX realize sera mas ligero en lo que a noticias se refiere, aligerando el peso de los XML a llamar
2. En vista de la manipulacion que debemos darle a noticias independizaremos las noticas en un archivo aparte haciendo que el modulo php los llame ambos por mientras haremos un truco que lo pueda aguantar*/


// Bloque ajax para llamar dinamicamente a las diferentes partes de la pagina, estas estan almacenadas en un archivo php el cual declara a traves de echo un esquema xml. El atributo que pongas en la funcion que cada boton flash tenga, el archivo php lo procesara y devolvera el bloque xml. Es la respuesta la que desmenuza el xml y lo filtra de acuerdo al ID pasado
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}

var http = getXMLHTTPRequest();
var docXML;
var titXML = new Array();
var ccontXML = new Array();
var fot2XML = new Array();
var title1XML = new Array();
var cont1XML = new Array();
var foto2_1XML = new Array();
var largFot2 = new Array();
var anchFot2 = new Array();
var enlaceXML = new Array();
var url_XML = new Array();
var atr_dir = new Array();
var galeriaXML = new Array();
var txtGal_XML = new Array();
var atrGal_dir = new Array();
var atrGal_ancho = new Array();
var atrGal_alto = new Array();
var atrGal_espArriba = new Array();
var atrGal_titulo = new Array();
var navego;
var media_XML;
var hattrick;

function mainBanner_ID(valor) {
		var myRand = parseInt(Math.random()*999999999999999);
		var modurl ="XMLemb/inicio2.php?valID=" + valor;
		http.open("GET", modurl + "&rand=" + myRand, true);
		http.onreadystatechange = useHttpResponse;
		http.send(null);
}

function mainBanner2_ID(valor) {
		var myRand = parseInt(Math.random()*999999999999999);
		var modurl ="XMLemb/inicio2.php?valID=" + valor;
		http.open("GET", modurl + "&rand=" + myRand, true);
		http.onreadystatechange = useHttpResponse2;
		http.send(null);
}

function useHttpResponse() {
	if (http.readyState == 4) {
		if(http.status == 200) {
			docXML = http.responseXML;
			noticias();
			navego = getNaveg();
			window_main();
  		}
	}
}
function useHttpResponse2() {
	if (http.readyState == 4) {
		if(http.status == 200) {
			docXML = http.responseXML;
			secciones(hattrick);
			navego = getNaveg();
  		}
	}
}

//Ahora que ya tenemos el objeto XML en la variable docXML podemos utilizarlo para sectorizar la informacion a nuestro gusto.
function noticias() {
	var notiXML = docXML.getElementsByTagName("noticias")[0];
	var ccontCx = notiXML.getElementsByTagName("contexto").length;
	var contBase = ccontCx - 2;
	for(var h=contBase; h<ccontCx; h++) {//cuento los titulos y lo uso de referencia para soltar dos noticias en la principal
		var notiAcop = "";
		var contCx = notiXML.getElementsByTagName("contexto")[h];
		titXML[h] = contCx.getElementsByTagName("titulo")[0];//titulo
		ccontXML[h] = contCx.getElementsByTagName("subtexto").length;//contenido
		title1XML[h] = titXML[h].childNodes[0].nodeValue;//valor de titulo
		if (ccontXML[h] > 1){
			var conteosin = ccontXML[h];
			for (var o=0; o<conteosin; o++){
				notiAcop += contCx.getElementsByTagName("subtexto")[o].childNodes[0].nodeValue+"<br /><br />";
			}
		}else{
			notiAcop = contCx.getElementsByTagName("subtexto")[0].childNodes[0].nodeValue;
		}

		//verifica si hay enlace o no, si lo hay pone el enlace a vista.
		if (contCx.getElementsByTagName("enlace").length > 0) {
			var contEnl = contCx.getElementsByTagName("enlace").length;
			for (var p=0; p<contEnl; p++) {
				enlaceXML = contCx.getElementsByTagName("enlace")[p];//direccion
				url_XML = enlaceXML.childNodes[0].nodeValue;//texto de la direccion
				atr_dir = enlaceXML.attributes.item(0).nodeValue;//URL de la direccion
				notiAcop += '<span style="cursor:pointer" onclick="abrir_ventana(\''+atr_dir+'\','+null+', \'\')">'+url_XML+'</span><br />';
			}
		}
		/*}else{
			enlaceXML = contCx.getElementsByTagName("enlace")[0];//direccion
			url_XML = enlaceXML.childNodes[0].nodeValue;//texto de la direccion
			atr_dir = enlaceXML.attributes.item(0).nodeValue;//URL de la direccion
			notiAcop += '<span style="cursor:pointer" onclick="abrir_ventana(\''+atr_dir+'\','+null+', \'\')">'+url_XML+'</span><br />';
		}*/
		
		//verifica si hay galeria o no, si la hay pone el enlace a vista.
		if (contCx.getElementsByTagName("galeria")[0] != null) {
			galeriaXML = contCx.getElementsByTagName("galeria")[0];//direccion
			txtGal_XML = galeriaXML.childNodes[0].nodeValue;//texto de la direccion
			atrGal_dir = galeriaXML.attributes.item(0).nodeValue;//URL de la direccion
			atrGal_ancho = galeriaXML.attributes.item(1).nodeValue;
			atrGal_alto = galeriaXML.attributes.item(2).nodeValue;
			atrGal_espArriba = galeriaXML.attributes.item(3).nodeValue;
			atrGal_titulo = galeriaXML.attributes.item(4).nodeValue;
			notiAcop += '<span style="cursor:pointer" onclick="ventana_pop2(\''+atrGal_dir+'\','+atrGal_ancho+', '+atrGal_alto+', '+atrGal_espArriba+', \''+atrGal_titulo+'\')">'+txtGal_XML+'</span>';
		}
		
		//verifica si hay imagen o flash, y lo pone a vista
		if (contCx.getElementsByTagName("foto")[0] != null) {
			fot2XML = contCx.getElementsByTagName("foto")[0];//ref de foto
		//cont1XML[h] = contXML[h].childNodes[0].nodeValue;//valor de contenido
			foto2_1XML = fot2XML.childNodes[0].nodeValue;//valor de ref de la foto
			largFot2 = fot2XML.attributes.item(0).nodeValue;//valor del atributo "largo" del tag foto2
			anchFot2 = fot2XML.attributes.item(1).nodeValue;//valor del atributo "ancho" del tag foto2
			media_XML = '<center><img src="'+foto2_1XML+'" alt="" width="'+largFot2+'" height="'+anchFot2+'" /></center><h5>'+notiAcop+'</h5>';
		} else if (contCx.getElementsByTagName("flash")[0] != null) {
			flashXML = contCx.getElementsByTagName("flash")[0];//direccion
			atrFlash_url = flashXML.attributes.item(0).nodeValue;//texto de la dirección
			atrFlash_ancho = flashXML.attributes.item(1).nodeValue;//texto de la dirección
			atrFlash_alto = flashXML.attributes.item(2).nodeValue;
			atrFlash_transparencia = flashXML.attributes.item(3).nodeValue;
			flash_XML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+atrFlash_ancho+'" height="'+atrFlash_alto+'"><param name="movie" value="'+atrFlash_url+'" /><param name="quality" value="high" /><param name="wmode" value="'+atrFlash_transparencia+'" /><embed src="'+atrFlash_url+'" width="'+atrFlash_ancho+'" height="'+atrFlash_alto+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="'+atrFlash_transparencia+'"></embed></object>';
			//alert(flash_XML);
			media_XML = '<center>'+flash_XML+'</center><h5>'+notiAcop+'</h5>';
		}
		var a = h-(contBase-1);
		document.getElementById('heading0'+a+'').innerHTML = '<CENTER><h4>'+title1XML[h]+'</CENTER></h4>';
		if ((h % 2) == 0) {
			//alert(contCx.getElementsByTagName("enlace").length);
			document.getElementById('text0'+a+'').innerHTML = media_XML;
		}else{
			document.getElementById('text0'+a+'').innerHTML = media_XML;
		}
	}
}
//Las funciones presentadas a partir de aqui obedecen a la barra de navegacion izquierda
function secciones(id_barra) {
	var secXML = docXML.getElementsByTagName("estatico")[0];//titulo
	//buscar la seccion pedida en la barra main_sep
	var numCont = secXML.getElementsByTagName("contexto").length + 1;
	
	//busco en el arbol el tag contexto cuyo atributo id sea igual al entregado por la barra de navegación
	for (var i=0; i<numCont; i++){
		if (secXML.getElementsByTagName("contexto")[i].attributes.item(0).nodeValue == id_barra){
			/*una vez encontrado creamos las tablas y sus respectivos contenidos, contamos cuantas filas habran.
			cada parte esta compuesta de titulo el contenido y una linea que divide un contenido de otro*/
			var selectedCont = secXML.getElementsByTagName("contexto")[i];//<contexto> seleccionado
			var contRow = selectedCont.getElementsByTagName("titulo").length;//lista de <titulo> del <contexto>
			var tabla = document.createElement("table");//creando la tabla
			if (navego == "Microsoft Internet Explorer") {
				var nodoViejo = document.getElementById("pantalla").childNodes[0];//nodo a reemplazar en IE
			}else{
				var nodoViejo = document.getElementById("pantalla").childNodes[1];//nodo a reemplazar en FF
			}
			//Crear las filas de titulo contenido y la linea separadora. cada tres lineas se repite el patron. El contenido de celda2 es llamado dependiendo si hay uno o mas <subtexto> y se van adjuntando como un parrafo, si solo hay uno entonces se agrega el <subtexto> encontrado.
			for (var j=0; j<contRow; j++){
				var textoAcop = "";
				var apartElem = selectedCont.getElementsByTagName("apartado")[j];//<apartado> de <contexto> seleccionado
				var imgExist = apartElem.getElementsByTagName("fotop").length
				var subtCont = apartElem.getElementsByTagName("subtexto").length;//lista de <subtexto> de <apartado>
				var fila = tabla.insertRow(j*3);
				var celda = fila.insertCell(0);
				var fila2 = tabla.insertRow(j*3+1);
				var celda2 = fila2.insertCell(0);
				var fila3 = tabla.insertRow(j*3+2);
				var celda3 = fila3.insertCell(0);
				celda.innerHTML='<h4>'+selectedCont.getElementsByTagName("titulo")[j].childNodes[0].nodeValue+'</h4>';
				celda3.innerHTML="<br /><hr width=454 align=center size=1 noshade=noshade color=#E7EDF7 /><br />"
				if (subtCont > 1){
					for (var k=0; k<subtCont; k++){
						textoAcop += apartElem.getElementsByTagName("subtexto")[k].childNodes[0].nodeValue+"<br /><br />";
					}
				}else if (subtCont == 0){
					textoAcop = "";
				}else{
					textoAcop = apartElem.getElementsByTagName("subtexto")[0].childNodes[0].nodeValue;
				}
				if (imgExist > 0){
					var imgAcom = apartElem.getElementsByTagName("fotop")[0];
					var imgRef = imgAcom.childNodes[0].nodeValue;
					var imgLAttr = imgAcom.attributes.item(0).nodeValue;
					var imgAAttr = imgAcom.attributes.item(1).nodeValue;
					if (imgAcom.attributes.item(2).nodeValue == null) {
						celda2.innerHTML = '<h5>'+textoAcop+'</h5><img src="'+imgRef+'" alt="" width="'+imgLAttr+'" height="'+imgAAttr+'" align="left" />';
					}else{
						var urlFoto = '"'+imgAcom.attributes.item(2).nodeValue+'"';
						var hImgLAttr = imgAcom.attributes.item(3).nodeValue;
						var hImgAAttr =	imgAcom.attributes.item(4).nodeValue;
						celda2.innerHTML = '<h5>'+textoAcop+'</h5><img src="'+imgRef+'" alt="" width="'+imgLAttr+'" height="'+imgAAttr+'" align="left" style="cursor:pointer" onclick=\'ventana_pop2('+urlFoto+', '+hImgLAttr+', '+hImgAAttr+')\' />';
					}
				}else{
					celda2.innerHTML = '<h5>'+textoAcop+'</h5>';
				}
			}
			//adjuntando la tabla creada al documento
		document.getElementById("pantalla").replaceChild(tabla, nodoViejo);
		//control_capa(i);
		break;//rompe el 1er for para no dejar undefined a valores vacios que puedan presentarse
		}
	}
}
//esta funcion controla el tamaño de la capa "pantalla" la capa para poder mostrar el contenido sin deformar el diseño "crece" sola ya que el atributo correspondiente para esa accion es "auto" pero si el contenido es muy pequeño la capa deformara el estilo asi que el condicional se aplica para los contenidos que son muy pequeños y deformarian el diseño dandole un "height minimo" cuando estos sean cargados
function control_capa(contextoList) {
	if (contextoList == 1) {
		if (navego == "Microsoft Internet Explorer"){
			document.styleSheets[0].rules[24].style.height = "500px";
		}else{
			document.styleSheets[0].cssRules[24].style.height = "500px";
		}
	}else{
		if (navego == "Microsoft Internet Explorer"){
			document.styleSheets[0].rules[24].style.height = "auto";
		}else{
			document.styleSheets[0].cssRules[24].style.height = "auto";
		}
	}
}
function detect_come(valor) {
	window.location = ("index.php?T="+valor);
}