/*****************************************************************
**		Aplicacion javascript de formateo de informacion		**
**																**
**		Autor: Carlos Sánchez Montero	Fecha: 21/09/2006		**
**																**
**					- www.apoyo3d.com -							**
******************************************************************/

////////////////////////////////////////////////////
//  Funciones traductoras del formato XML a HTML  //
////////////////////////////////////////////////////

/* Funcion que lee un XML de tipo menu y le da formato */
function generaMenu(){
	var salida = '';
	var defecto = new Array();

	var indice = 0;
	if(xmlDoc[2]){ // Comprobamos si es una llamada de generarMenuPortada
		indice = 2;
	}


	if ( xmlDoc[indice].getElementsByTagName("menu").length != 0){
		var docmenu		= xmlDoc[indice].getElementsByTagName("menu");
		var titulo		= docmenu[0].getElementsByTagName("titulo");
		var elementos	= docmenu[0].getElementsByTagName("enlace");
		
//		portada		= docmenu[0].getElementsByTagName("portada")[0].firstChild.nodeValue;
		if (titulo[0].firstChild!=null)
			cambiaTituloMenu(titulo[0].firstChild.nodeValue);
		for ( var i = 0; i < elementos.length ; i++ ){
			var nombre		= elementos[i].getElementsByTagName("seccion")[0].firstChild.nodeValue;
			var contenido	= elementos[i].getElementsByTagName("contenido")[0].firstChild.nodeValue;			
			var id			= elementos[i].getAttribute('id');

			// Si tiene un menu diferente al actual
			if(elementos[i].getElementsByTagName("menu").length != 0){
				menu = elementos[i].getElementsByTagName("menu")[0].firstChild.nodeValue;
			}



			// Presentamos el menu de una de las formas indicadas por
			if ( elementos[i].getAttribute('oculto') == null ){
				salida += '<div id="menu_'+id+'"> <a class="Menu" href="javascript:cambiaContenido(\'' +
							contenido+ '\',\''+ nombre +'\')">' + nombre + '</a></div>'; //<br/>';
			}


			// Comprobamos si es la seccion por defecto
			if ( elementos[i].getAttribute('defecto') != null ){
				defecto.nombre = nombre;
				defecto.contenido = contenido;
			}

		}

		if (document.getElementById("menu") != null){
			document.getElementById("menu").innerHTML = salida;
		}
	}

	return defecto;
}

/* Funcion que genera el menu y muestra el contenido por defecto del mismo */
function generaMenuPortada(){
	xmlDoc[2]=false;
	var defecto = generaMenu();	
	cambiaContenido(defecto.contenido,defecto.nombre);

	ListaNoticias();
}

/* Funcion que lee un XML de tipo contenido y le da formato */
function generaContenido(){
	var salida = '';
	if ( xmlDoc[1].getElementsByTagName("contenido").length != 0 ){
	
		var ordenafecha		= false;
		var doccontenido	= xmlDoc[1].getElementsByTagName("contenido")[0];
		var elementos		= doccontenido.getElementsByTagName("elemento");
		var formato		    = doccontenido.getElementsByTagName("formato");
		
		if ( formato.length != 0){
			formato = formato[0].firstChild.nodeValue;
		}else{
			formato = "";
		}

		var arrayinfo		= new Array();

		for(var i=0; i < elementos.length ; i++){
			arrayinfo[i] = new Array();
			if (elementos[i].getAttribute('fecha')!=null){
				arrayinfo[i]["fecha"]  = elementos[i].getAttribute('fecha').split('/');			
				for (var j=0; j < arrayinfo[i]["fecha"].length; j++){
					for (var k=0; arrayinfo[i]["fecha"][j].charAt(k)=='0' && 
								   k < arrayinfo[i]["fecha"][j].length ; k++ ){}
						
					arrayinfo[i]["fecha"][j] = arrayinfo[i]["fecha"][j].substring(k,
					  								arrayinfo[i]["fecha"][j].length)
					arrayinfo[i]["fecha"][j] = parseInt(arrayinfo[i]["fecha"][j]);
					
				}
				ordenafecha = true;
			}else{
				arrayinfo[i]["fecha"] = null;
			}
				
			arrayinfo[i]["id"]     = elementos[i].getAttribute('id');			
			if (elementos[i].getElementsByTagName('titulo')[0].firstChild!=null)
				arrayinfo[i]["titulo"] = elementos[i].getElementsByTagName('titulo')[0].firstChild.nodeValue;	
			else
				arrayinfo[i]["titulo"] = "";
			arrayinfo[i]["texto"]  = elementos[i].getElementsByTagName('texto')[0];	
		}

		// Ordenamos las salidas segun el atributo fecha de los elementos
		if (ordenafecha){
			for (var j=0; j<elementos.length; j++){
				var masnuevo = j;
				for (var i=j; i < elementos.length; i++){
					if(	(arrayinfo[i]["fecha"][2] > arrayinfo[masnuevo]["fecha"][2]) ||
						(arrayinfo[i]["fecha"][2] == arrayinfo[masnuevo]["fecha"][2] &&
						 arrayinfo[i]["fecha"][1] > arrayinfo[masnuevo]["fecha"][1]) ||
						(arrayinfo[i]["fecha"][2] == arrayinfo[masnuevo]["fecha"][2] &&
						 arrayinfo[i]["fecha"][1] == arrayinfo[masnuevo]["fecha"][1] &&
						 arrayinfo[i]["fecha"][0] > arrayinfo[masnuevo]["fecha"][0])){
						masnuevo = i;
					}
				}
				var copia = arrayinfo[j];
				arrayinfo[j] = arrayinfo[masnuevo];
				arrayinfo[masnuevo] = copia;
			}
		}

		// Damos el formato correcto a la salida de contenidos
		salida +=	formatea_contenido (formato,arrayinfo);

		if (document.getElementById("contenido") != null)
			document.getElementById("contenido").innerHTML = salida;

	}
}


/* Funcion que lee y da formato a uno de los elementos de contenido */
function generaContenidoSeleccionado(){
	var salida = '';
	var arrayinfo = new Array();

	if ( xmlDoc[3].getElementsByTagName("contenido").length != 0 ){
		var doccontenido = xmlDoc[3].getElementsByTagName("contenido")[0];
		var formato		 = doccontenido.getElementsByTagName("formato");
		if ( formato.length != 0){
			formato = formato[0].firstChild.nodeValue;
		}else{
			formato = "";
		}

		var elementos    = doccontenido.getElementsByTagName("elemento");
	
		var sigue = true;
		for (var i=0 ; sigue && i < elementos.length ; i++ ){
			id = elementos[i].getAttribute("id");
			if ( id == ID_seleccionado )
				sigue = false;
		}
		i--;

		if (sigue){
			salida += 'Informacion no encontrada';
		}else{
			arrayinfo["titulo"] = elementos[i].getElementsByTagName('titulo')[0].firstChild.nodeValue;
			if (elementos[i].getAttribute('fecha')!=null)
				arrayinfo["fecha"] = elementos[i].getAttribute('fecha').split('/');
			arrayinfo["texto"]  = elementos[i].getElementsByTagName('texto')[0];	
			

			// Damos el formato correcto a la salida del contenido seleccionado
			salida += formatea_contenido_seleccionado (formato,arrayinfo);
		}

		document.getElementById("contenido").innerHTML = salida;
	}
}


/* Error producido por generador mal configurado */
function generaError(){
	alert('Error: Manejador desconocido');
}


function generaListaNoticias(){
	var salida = '';
	if ( xmlDoc[4].getElementsByTagName("contenido").length != 0 ){
		var ordenafecha		= false;
		var doccontenido	= xmlDoc[4].getElementsByTagName("contenido")[0];
		var elementos		= doccontenido.getElementsByTagName("elemento");
		var arrayinfo		= new Array();

		for(var i=0; i < elementos.length ; i++){
			arrayinfo[i] = new Array();
			if (elementos[i].getAttribute('fecha')!=null){
				arrayinfo[i]["fecha"]  = elementos[i].getAttribute('fecha').split('/');			
				for (var j=0; j < arrayinfo[i]["fecha"].length; j++){
					for (var k=0; arrayinfo[i]["fecha"][j].charAt(k)=='0' && 
								   k < arrayinfo[i]["fecha"][j].length ; k++ ){}
						
					arrayinfo[i]["fecha"][j] = arrayinfo[i]["fecha"][j].substring(k,
					  								arrayinfo[i]["fecha"][j].length)
					arrayinfo[i]["fecha"][j] = parseInt(arrayinfo[i]["fecha"][j]);
					
				}
				ordenafecha = true;
			}else{
				arrayinfo[i]["fecha"] = null;
			}
				
			arrayinfo[i]["id"]     = elementos[i].getAttribute('id');			
			if (elementos[i].getElementsByTagName('titulo')[0].firstChild!=null)
				arrayinfo[i]["titulo"] = elementos[i].getElementsByTagName('titulo')[0].firstChild.nodeValue;	
			else
				arrayinfo[i]["titulo"] = "";
			arrayinfo[i]["texto"]  = elementos[i].getElementsByTagName('texto')[0];	
		}

		// Ordenamos las salidas segun el atributo fecha de los elementos
		if (ordenafecha){
			for (var j=0; j<elementos.length; j++){
				var masnuevo = j;
				for (var i=j; i < elementos.length; i++){
					if(	(arrayinfo[i]["fecha"][2] > arrayinfo[masnuevo]["fecha"][2]) ||
						(arrayinfo[i]["fecha"][2] == arrayinfo[masnuevo]["fecha"][2] &&
						 arrayinfo[i]["fecha"][1] > arrayinfo[masnuevo]["fecha"][1]) ||
						(arrayinfo[i]["fecha"][2] == arrayinfo[masnuevo]["fecha"][2] &&
						 arrayinfo[i]["fecha"][1] == arrayinfo[masnuevo]["fecha"][1] &&
						 arrayinfo[i]["fecha"][0] > arrayinfo[masnuevo]["fecha"][0])){
						masnuevo = i;
					}
				}
				var copia = arrayinfo[j];
				arrayinfo[j] = arrayinfo[masnuevo];
				arrayinfo[masnuevo] = copia;
			}
		}

		// Damos el formato correcto a la salida de contenidos
		salida += formatea_contenido ('listanoticias',arrayinfo);

		if( document.getElementById("ListaNoticias") != null)
			document.getElementById("ListaNoticias").innerHTML = salida;
	}
}


function cambiaTituloMenu(titulo){
	if (document.getElementById("titulo_menu")!=null){
		document.getElementById("titulo_menu").innerHTML = titulo; 
	}
}

function cambiaTituloContenido(titulo){
	if (document.getElementById("titulo_seccion")!=null){
		document.getElementById("titulo_seccion").innerHTML = titulo;
	}
}



