// JavaScript Document

function comprovar_thickbox(){
	//$(document).ready(function(){   
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;
	//});
}

// mostra la capa de Loading mentre carrega l'AJAX
function mostraCapaLoading(){
	document.getElementById("capa_loading").style.display = 'inline';
}

// amaga la capa Loading
function amagaCapaLoading(){
	document.getElementById("capa_loading").style.display = 'none';
}


function generaNoticies(pagina, noticia){
	var crida="genera_noticies.php?target=taula_resultats";
	if(typeof pagina != "undefined") crida+="&pagina="+pagina;
	if(typeof noticia != "undefined") crida+="&noticia="+noticia;
	cridaAJAX(crida);
}

function mostraProjecte(projecte){
	var crida="mostra_projecte.php?target=taula_resultats";
	if(typeof projecte != "undefined") crida+="&projecte="+projecte;
	cridaAJAX(crida);
}

function generaProjectes(pagina, numprojectes){
	var crida="genera_projectes.php?target=caixa_projectes";
	if(typeof pagina != "undefined") crida+="&pagina="+pagina;
	if(typeof numprojectes != "undefined") crida+="&numprojectes="+numprojectes;
	cridaAJAX(crida);
}

function cridaAJAX(url){
	var httpRequest;
			
		var params="";

		try{
			params=url.split('?')[1];
			url=url.split('?')[0];
		}catch(e){}
		try{
			target=params.split('target=')[1].split('&')[0];
		}catch(e){
			target="";	
		}
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!httpRequest) {
            alert("Error, no s'ha pogut crear una instància d'AJAX");
            return false;
        }
        httpRequest.onreadystatechange = function() { 
			//posicionaCapaLoading();
			//mostraCapaLoading();
			mostraResultat(httpRequest); 
		};
        httpRequest.open('GET', url+"?"+params, true);
        httpRequest.send(null);
}

function mostraResultat(httpRequest) {
	document.location.href="#top";
	if(httpRequest.readyState < 4){//loading
		//alert("estat igual a 1");
		//	document.getElementById("load").innerHTML="<img src=\"imatges/cargando.gif\" id=\"cargando\" name=\"cargando\" width=\"25\"/>";
	}
    if (httpRequest.readyState == 4) {//la resposta un cop s'ha carregat
		//setTimeout(amagaCapaLoading,1000); 
        if (httpRequest.status == 200) {
            // Mostrem la taula resultant
            resposta=httpRequest.responseText;
			document.getElementById(target).innerHTML=resposta;
			comprovar_thickbox();
        } else {
            alert('Error amb la petició');
        }
    }

}

