function getElement(aID){
	return (document.getElementById) ? document.getElementById(aID) : document.all[aID];
}

/*
 * Crea un objeto XMLHttpRequest
 */
function crear_objeto_ajax() {
	var peticion; 
	try {
		peticion = new XMLHttpRequest();
	} catch (trymicrosoft) {
	
		try {
			peticion = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				peticion = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				peticion = false;
			}	 
		}
	}

	return peticion;
}

/*
 * Realiza una petición al servidor
 */
function peticion(par){
	
	oxml = crear_objeto_ajax();
	rfunc=mostrar_seccion;
	oxml.onreadystatechange=rfunc;
	oxml.open('POST','phpengine.php',true);
	oxml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	oxml.send("par="+par);
	
}


/*
 * Pide mostrar una sección
 */

function mostrar_seccion(){
	
	contenedor=getElement('contents');
	
	if (oxml.readyState==4){
		datos = oxml.responseText;
		contenedor.innerHTML = datos;
	}
	else{
		contenedor.innerHTML='<img class="loading" src="img/loading.gif" alt="Cargando..." />';
	}
	
}

/*
 * Pide mostrar una categoría
 */
function mostrar_categoria(){
	
	contenedor=getElement('productos');	
	
	if (oxml.readyState==4){
		datos = oxml.responseText;
		contenedor.innerHTML = datos;
	}
	else{
		contenedor.innerHTML='<img class="loading" src="img/loading.gif" alt="Cargando..." />';
	}
	
}

/*
 * Precarga de imágenes
 */

var i;
var imagenes = new Array("img/lista_hover.png","img/loading.gif");
var lista_imagenes = new Array();

for(i in imagenes){
	lista_imagenes[i] = new Image();
	lista_imagenes[i].src = imagenes[i];
}

/* Hace un polling sobre el hash continuamente */

hashAnterior="";
titulo=document.title;

function paginaCargada(){
	hashCambiado();
	tituloCambiado();
}

function hashCambiado(){
	if(window.location.hash.length==0) window.location.hash="inicio";
	if(window.location.hash!=hashAnterior){
		if(hashAnterior.indexOf("faq")<1 || (hashAnterior.indexOf("faq")>1 && window.location.hash.indexOf("faq")<1) ) peticion(window.location.hash);
		hashAnterior=window.location.hash;
		document.title=titulo;
	}
	setTimeout("hashCambiado()",200);
}

function tituloCambiado(){
	if(document.title!=titulo) document.title = titulo;
	setTimeout("tituloCambiado()",350);
}

window.onload = paginaCargada;
