function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }
  return xmlhttp;

}


function initPagina(){
	var queryString = window.top.location.search.substring(1);
	var pagina=getCookie('pagina');
	var menu=getCookie('menu');
	var fotoMenu=getCookie('fotoMenu');

	//ocultar flash
	var aux='flash';
	var flash= MM_findObj(aux);
	flash.style.height=0;


	obtenerMenuPrincipal();
}
var urlBasePaginas='pages/';

/**
* quita la intro de flash
*/
function quitarIntro(){
	var capaFlash= MM_findObj('capaFlashIntro');	
	capaFlash.style.left=-10000;
}

/**
 * Obtiene el xml con la información de la página a cargar
 */
function obtenerPagina(nombrePagina,nombreContenido){
	var aux='flash';
	var flash= MM_findObj(aux);
	flash.style.height=450;

	var httpRequest= getHTTPObject();

	httpRequest.open("GET",urlBasePaginas+nombrePagina+'.xml',false);
	httpRequest.onreadystatechange=function() { 

		//dentro de la funcion comprobamos el estado del proceso 
        //si es 4 (terminado) pedimos lo que nos han mandado 
        if (httpRequest.readyState==4) 
        { 
				cargarPagina(httpRequest,nombreContenido);
				document.cookie='pagina='+nombrePagina;
        } 
    } 

	httpRequest.send(null);

//	alert(httpRequest.responseText);
}

/**
 * recoge la página (xml) y carga la página (DHTML)
 */
function cargarPagina (httpRequest,nombreContenido){

//	alert(httpRequest.responseText);
	var paginaXML=httpRequest.responseXML;
//	contenidoXML=document;


//	alert(f.firstChild.getAttribute('value'));

	generarMenu(paginaXML,nombreContenido);
	
}

function limpiarEstiloOpcionMenu(){
		if(document.opcionMenu){
			var aux= MM_findObj(document.opcionMenu);
			//alert(document.opcionMenu);
			if(aux){
				aux.style.color='#7d7d7d';
			}
		}
}
function recuperarEstiloOpcionMenu(obj){
/*	alert(document.opcionMenu);
	alert(obj.id);
		alert(obj.style.color);
	*/
	if(document.opcionMenu){
		if(document.opcionMenu==obj.id){
			obj.style.color='#FFa100';
		}else{
			obj.style.color='#7d7d7d';
		}
	}else{
		obj.style.color='#7d7d7d';
	}
}
function generarOpcionMenu(nodoXML){
	
	var texto='';
	var nombreXML=nodoXML.getElementsByTagName('nombreXML')[0].firstChild.data;
	var srcFoto=nodoXML.getElementsByTagName('foto')[0].getAttribute('src');
	var descripcion=nodoXML.getElementsByTagName('descripcion')[0].firstChild.data;
	texto+='<tr>';
	texto+='<td>';
	texto+='<a id="'+nombreXML+'_opcion" href="#" ';
	texto+='	onclick="obtenerContenido(\''+nombreXML+'\');'+
				'document.fotoMenu=\''+srcFoto+'\';'+
				'document.cookie=\'fotoMenu='+srcFoto+'\';"';
	texto+='	onmouseover="this.style.color=\'#FFa100\';cargarFotoMenu(\''+srcFoto+'\')"';
	texto+='	onmouseout="recuperarEstiloOpcionMenu(this);recuperarFotoMenu();">';
	texto+=descripcion;
	texto+='</a>';
	texto+='</td>';
	texto+='</tr>';
	//alert(texto);
	return texto;
}

function cargarFlash (paginaXML){
	var flash=paginaXML.getElementsByTagName('flash');
	if(flash){
		var flashSrc=flash[0].getAttribute('src');
		
		var f =MM_findObj('flash');
		f.movie=flashSrc;
	}
	
	
	
}

function generarMenu(paginaXML,nombreContenido){
	var menu= MM_findObj('menu');

	var texto="<table id=\"tablaMenu\">";

	var listaMenus=paginaXML.getElementsByTagName('menu');
	var contenidoPrincipal='';
	var srcFotoPrincipal='';
	for (i = 0; i < listaMenus.length; i++){ 
		texto+=generarOpcionMenu(listaMenus[i]);
		
		if(i==0){
			contenidoPrincipal=listaMenus[0].getElementsByTagName('nombreXML')[0].firstChild.data;
			srcFotoPrincipal=listaMenus[0].getElementsByTagName('foto')[0].getAttribute('src');
		}
	}
	texto+="</table>";
	menu.innerHTML=texto;
	if(nombreContenido){
		obtenerContenido(nombreContenido);
	}else{
		obtenerContenido(contenidoPrincipal);
	}
	
	document.fotoMenu=srcFotoPrincipal;
	cargarFotoMenu(srcFotoPrincipal);
	cargarFlash(paginaXML);

}

/* ************************************************************************
 * funciones para contenidos
 * ************************************************************************/
var urlBaseContenidos='pages/contenidos/';


/**
 * obtiene el xml con el contenido a mostrar
*/
function obtenerContenido (nombreContenido){
	var httpRequest= getHTTPObject();
//	httpRequest.setRequestHeader('content-type', 'text/xml; charset=ISO-8859-1');
	httpRequest.open("GET",urlBaseContenidos+nombreContenido+'.xml',false);

	httpRequest.onreadystatechange=function() { 

		//dentro de la funcion comprobamos el estado del proceso 
        //si es 4 (terminado) pedimos lo que nos han mandado 
        if (httpRequest.readyState==4) 
        { 
				cargarContenido(httpRequest);
				document.cookie='menu='+nombreContenido;
			
				var aux=MM_findObj(nombreContenido+'_opcion');
				limpiarEstiloOpcionMenu();
//				alert(nombreContenido);
				document.opcionMenu=nombreContenido+'_opcion';
				recuperarEstiloOpcionMenu(aux);
				aux.style.color='FFa100';
        } 
    } 

	httpRequest.send(null);

//	alert(httpRequest.responseText);
}


function obtenerHTMLContacto(){
		var httpRequest= getHTTPObject();
//	httpRequest.setRequestHeader('content-type', 'text/xml; charset=ISO-8859-1');
	httpRequest.open("GET",'contacto.htm',false);
	httpRequest.onreadystatechange=function() { 

		//dentro de la funcion comprobamos el estado del proceso 
        //si es 4 (terminado) pedimos lo que nos han mandado 
        if (httpRequest.readyState==4) 
        { 
				cargarContacto(httpRequest.responseText);
        } 
    } 

	httpRequest.send(null);

}
function cargarContacto(html){
//	alert(html);
	var capaContenido= MM_findObj('capaTextoContenido');
	
	capaContenido.innerHTML+=html;
}
/**
 * recoge el contenido (xml) y carga la página (DHTML)
 */
function cargarContenido (httpRequest){

//	alert(httpRequest.responseText);
	var contenidoXML=httpRequest.responseXML;
//	contenidoXML=document;

	generarTexto(contenidoXML);
	cargarFotos(contenidoXML);
	
	if(contenidoXML.getElementsByTagName('contacto').length>0){
		obtenerHTMLContacto();	
	}
	if(contenidoXML.getElementsByTagName('contacto2').length>0){
		obtenerHTMLContacto2();		
  }
}

function obtenerHTMLContacto2(){
		var httpRequest= getHTTPObject();
//	httpRequest.setRequestHeader('content-type', 'text/xml; charset=ISO-8859-1');
	httpRequest.open("GET",'contacto2.htm',false);
	httpRequest.onreadystatechange=function() { 

		//dentro de la funcion comprobamos el estado del proceso 
        //si es 4 (terminado) pedimos lo que nos han mandado 
        if (httpRequest.readyState==4) 
        { 
				cargarContacto2(httpRequest.responseText);
        } 
    } 

	httpRequest.send(null);

}
function cargarContacto2(html){
//	alert(html);
	var capaContenido= MM_findObj('capaTextoContenido');
	
	capaContenido.innerHTML+=html;
}
/**
 * recoge el contenido (xml) y carga la página (DHTML)
 */


function obtenerAtributos(nodo){

	var atts=nodo.attributes;
	var cadena="";
	var j=atts.length;
	var i=0;
	while (i<j){
		cadena+=' '+atts[i].name+'="'+atts[i].value+'"';
		i++;	
	}
	return cadena;
}

function tratarTexto(nodo){
	var texto='';
	var hijo=nodo.firstChild;
	while(hijo){
		if(hijo.nodeType==3){
			texto+=hijo.data;	
		}else{
			texto+='<'+hijo.nodeName+obtenerAtributos(hijo)+'>'+tratarTexto(hijo)+'</'+hijo.nodeName+'>';
		}
		//alert(texto);
		hijo=hijo.nextSibling;	
	}
	return texto;
}

function generarTexto(contenidoXML){
	var capaContenido= MM_findObj('capaTextoContenido');

	var texto="";

	var listaTextos=contenidoXML.getElementsByTagName('parrafo');
	var t;
	var estilo;
	tratarTexto(listaTextos[1]);
	for (i = 0; i < listaTextos.length; i++){ 
		estilo=listaTextos[i].getAttribute('style');
		if(listaTextos[i].firstChild){
			t=tratarTexto(listaTextos[i]);
//			t=listaTextos[i].firstChild.data;
			t=t.replace('#2','&sup2;');
		}else{
			t="";
		}
			
		
		texto+='<div class="'+estilo+'">'+t+'</div>';
	}
//	alert(texto);
	var imgFlotanteHTML='<p style="float:right;padding-right:3px;"><img src="images/fotos_constr/1.jpg" name="foto" border="0" id="foto"/></p>'
	capaContenido.innerHTML=imgFlotanteHTML+texto;
	
}

function cargarFotoMenu(src){
//	alert(document.fotoMenu);
	var fotoMenu= MM_findObj('imagenMenu');	
	fotoMenu.src=src;
}

function recuperarFotoMenu(){
//	alert(document.fotoMenu);
	if(document.fotoMenu==null){
		MM_swapImgRestore();
	}else{
		var fotoMenu= MM_findObj('imagenMenu');	
		fotoMenu.src=document.fotoMenu;
	}
}

function cargarFotos(contenidoXML){

	//cargamos la foto del menú
	var fotoPrincipal= MM_findObj('imagenMenu');
	
	//cargamos la tira de fotos
	var capaTiraFotos= MM_findObj('tiraFotos');
	var banner=contenidoXML.getElementsByTagName('banner');
	var listaFotos=contenidoXML.getElementsByTagName('foto');
	var texto='';
	var srcFoto;
	var fotoGrande=MM_findObj('foto');
	fotoGrande.src='images/px_trans.gif';
  
	if(banner[0]){
		texto+='<img src="'+banner[0].getAttribute('src')+'"/>';
		if(listaFotos[0]){
		  srcFoto=listaFotos[0].getAttribute('src');
		  fotoGrande.src=srcFoto;
    }
	}else{
		for (i = 0; i < listaFotos.length; i++){ 
			srcFoto=listaFotos[i].getAttribute('src');
	
			if(i==0){
				//cargamos la foto grande
				fotoGrande.src=srcFoto;
			}
	
			texto+='<a href="#" onclick="MM_swapImage(\'foto\',\'\',\''+srcFoto+'\',1)">';
			texto+='<img src="'+srcFoto+'"/>';
			texto+='</a>';
		}
	}

	capaTiraFotos.innerHTML=texto;

}



/*****************************************************************************************/
/** menu principal***************************************************************************************/
/*****************************************************************************************/
/**
 * Obtiene el xml con la información de la página a cargar
 */
function obtenerMenuPrincipal(){
	var httpRequest= getHTTPObject();

	httpRequest.open("GET",'menu.xml',false);
	httpRequest.onreadystatechange=function() { 

		//dentro de la funcion comprobamos el estado del proceso 
        //si es 4 (terminado) pedimos lo que nos han mandado 
        if (httpRequest.readyState==4) 
        { 
				cargarMenuPrincipal(httpRequest);
        } 
    } 

	httpRequest.send(null);

//	alert(httpRequest.responseText);
}

/**
 * recoge la página (xml) y carga la página (DHTML)
 */
function cargarMenuPrincipal (httpRequest){
//	alert(httpRequest.responseText);
	var menuXML=httpRequest.responseXML;

	generarMenuPrincipal(menuXML);
	
}

function generarOpcionMenuPrincipal(nombreXML,descripcion){
	var texto="";

	texto+='<td align="left" valign="bottom">';
		texto+='<a class="opcionMenuPrincipal" href="#" onclick="obtenerPagina(\''+nombreXML+'\');quitarIntro();"';
		texto+='onMouseOver="MM_swapImage(\'img'+nombreXML+'\',\'\',\'images/px_naranja.gif\',1)" ';
			texto+='onMouseOut="MM_swapImgRestore()">'+descripcion+'</a>';
		texto+='</td>';
		texto+='<td width="29" align="left" valign="bottom">&nbsp;</td>';
	
return texto;

}
function generarMenuPrincipal(menuXML){
	var menu= MM_findObj('menuPrincipal');

	var texto="<table>";

	var listaMenus=menuXML.getElementsByTagName('menu');
	var nombreXML;
	var descripcion;

	var texto="<tr>";
	for (i = 0; i < listaMenus.length; i++){ 
		nombreXML=listaMenus[i].getElementsByTagName('nombreXML')[0].firstChild.data;
		descripcion=listaMenus[i].getElementsByTagName('descripcion')[0].firstChild.data;
		texto+=generarOpcionMenuPrincipal(nombreXML,descripcion);
		
		if(i<listaMenus.length-1){
			texto+='<td align="left" valign="bottom">&nbsp;&nbsp;<img src="images/menus/punto.gif" width="6" height="6"></td><td width="31" align="left" valign="bottom">&nbsp;&nbsp;&nbsp;</td>';
		}
	}
	texto+="</tr>";
	
	
	texto+="</table>";

	menu.innerHTML=texto;
	

}
