// ############################################################
// ATTENZIONE SE INSIEME ALLA PAGINAZIONE USI LIGHTBOX
// (O CLONE) BISOGNA INSERIRE L'INIT DEL LIGHTBOX NELLO SCRIPT
// JS CHE GESTISCE LA PAGINAZIONE!!!!
// ############################################################

//Global vars to hold connection to web pages
var xmlHttp
var xmlHttp2
loading_inner_html_code = '<p><img src="'+js_loading_icon+'" alt="loading" width="'+js_loading_icon_w+'" height="'+js_loading_icon_h+'" /></p>'+'<h3>'+js_loading_samples_title+'</h3>'; // jose 07.10.2009 il codice da iniettare nel DIV durante il processo di paging


function showPage(str) { 
	//Function that gets called
	//Currently we only call one other sub, but this could change
	document.getElementById(js_pagination_contents_div).innerHTML=loading_inner_html_code // jose 07.10.2009 inietto il codice loading
	showStates(str)
}

function showStates(str) { 
	//This sub will populate a table with all the states and get the 
	//pagination built
	
	//Make the AJAX connection for both the navigation and content
	xmlHttp=GetXmlHttpObject()
	xmlHttp2=GetXmlHttpObject()
	
	//If we cant do the request error out
	if (xmlHttp==null || xmlHttp2==null ) {
	 	alert ("Browser does not support HTTP Request")
	 	return
	}
		
		
        // ****************************************************************************
        // parte di codice per mostra tutti i record
        // ****************************************************************************
        show_all_trigger = ''
        if (typeof str === 'undefined') // verifica che la var str sia definita altrimenti sulla prima pagina lo script si blocca
	{
	}
	else
        {
            if (str == 'showall')
            {
              show_all_trigger = '&showrec=all' // definisce la variabile da passare via url
              str = 1 // ridefinisce str per passare alla pagina 1
            }
        }
        


        //  *URL VARS 01* - se aggiungi qualcosa qui fallo anche su *URL VARS 02*    
	//First build the navigation panel // se occorre passare delle variabili alla pagina di process bisogna inserirle qui, tipo *lang_trigger* e *page_trigger*
	var url=js_paging_class // la pagina di appoggio che crera i contenuti da iniettare nella pagina di origine (jose)
	url=url+"?p="+str
	url=url+"&t=nav"
	url=url+"&lt="+js_lang_trigger // passaggio var lang trigger, la var viene passata da php a js nella pag page.inc.php (jose)
	url=url+"&pt="+js_page_trigger // passaggio var page trigger, la var viene passata da php a js nella pag page.inc.php (jose)
	url=url+'&table='+js_tablename // aggiunge la var relativa alla tab del DB da usare (jose)
	url=url+'&catfield='+js_category_field_name // aggiunge la var relativa alla nome del campo categoria usato nella query (jose)
	url=url+'&catid='+js_category_id // aggiunge la var relativa alla categoria da usare (jose)
	url=url+show_all_trigger // aggiunge la var per mostrare tutti i record (jose)
	url=url+"&sid="+Math.random()
        

	//Once the page finished loading put it into the div
	xmlHttp2.onreadystatechange=navDone 

	//Get the php page
	xmlHttp2.open("GET",url,true)
	xmlHttp2.send(null)
	
	//Build the url to call
	//Pass variables through the url
        
        //  *URL VARS 02* - se aggiungi qualcosa qui fallo anche su *URL VARS 01* 
	var url=js_paging_class // la pagina di appoggio che crera i contenuti da iniettare nella pagina di origine (jose)
	url=url+"?p="+str
	url=url+"&t=con"
	url=url+"&lt="+js_lang_trigger // passaggio lang trigger (jose)
	url=url+"&pt="+js_page_trigger // passaggio page trigger (jose)
	url=url+'&table='+js_tablename // aggiunge la var relativa alla tab del DB da usare (jose)
	url=url+'&catfield='+js_category_field_name // aggiunge la var relativa alla nome del campo categoria usato nella query (jose)
	url=url+'&catid='+js_category_id // aggiunge la var relativa alla categoria da usare (jose)
	url=url+show_all_trigger // aggiunge la var per mostrare tutti i record (jose)
	url=url+"&sid="+Math.random()
        
                        
	//Once the page finished loading put it into the div
	xmlHttp.onreadystatechange=stateChanged
        	
	//Get the php page
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function navDone() { 
	//IF this is getting called when the page is done loading then fill the pagination div
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") { 
	 	//Update the Div tag with the outputted text // inserire l'ID del div che deve contenere la barra di navigazione (jose)
	 	document.getElementById(js_pagination_navbar_div).innerHTML=xmlHttp2.responseText
	} 
}

function stateChanged() { 
	//IF this is getting called when the page is done loading the states then output the div
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	 	//Update the Div tag with the outputted text // inserire l'ID del div dei contenuti (jose)
	 	document.getElementById(js_pagination_contents_div).innerHTML=xmlHttp.responseText
		// ####################################################################################### INIT DEL LIGHTBOX
		Shadowbox.clearCache(); // questo fa s che shadowBox con si incasini con il conteggio degli elementi
					// nella gallery, da verificare che non dia problemi con IE7 e IE8
		Shadowbox.setup(); // questo  il lancio vero e proprio
		// #########################################################################################################
	} 
}

function GetXmlHttpObject() {
	//Determine what browser we are on and make a httprequest connection for ajax
	var xmlHttp=null;

	try {
	 	// Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	 	//Internet Explorer
	 	try {
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}
	 	catch (e) {
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
	}
	
	return xmlHttp;
}


//Onload start the user off on page one
//window.onload = showPage("1");
window.onload = function() {showPage("1");};
