/* ======================================== util ajax per GGMaps ================================================== */
/* ======================================== util ajax per GGMaps ================================================== */
/* ======================================== util ajax per GGMaps ================================================== */


function changelink(x, tipo){
	var p = x.parentNode.parentNode;
	if(p.className=="off"){
		p.className="on";
		x.innerHTML = "[nascondi]";
	}
	else{
		p.className="off";
		x.innerHTML = "[mostra]";
	}
}


	// oggetti nulli al primo caricamento di pagina
	var map_opere = null;
	var map_cantieri = null;
	
	var controllo_opere = -1;
	var controllo_cantieri = -1;
	var lista_mrk_opere = [];
	var lista_mrk_cantieri = [];

	
		
	// funzione per gestire l'onclick nei vari link per il controllo dei dati da visualizzare nella mappa
		function popola(id){
			
			if(id == 'opere'){
				if(map_opere){
					if(controllo_opere==1){
						for(i=0;i<lista_mrk_opere.length;i++){
							map.removeOverlay(lista_mrk_opere[i]);
						}
						controllo_opere = 0;
					}else{
						for(i=0;i<lista_mrk_opere.length;i++){
							map.addOverlay(lista_mrk_opere[i]);
						}
						controllo_opere = 1;
					}
				}
				else{
					map_opere = new mappa('opere');
					map_opere.S();
					controllo_opere = 1;
				}
			}else if(id == 'cantieri'){
				if(map_cantieri){
					if(controllo_cantieri==1){
						for(i=0;i<lista_mrk_cantieri.length;i++){
							map.removeOverlay(lista_mrk_cantieri[i]);
						}
						//$('#area-mappa').find('img[src="images/map_icons/cantieri_on.gif"]').parent().hide();
						controllo_cantieri = 0;
					}
					else{
						for(i=0;i<lista_mrk_cantieri.length;i++){
							map.addOverlay(lista_mrk_cantieri[i]);
						}
						controllo_cantieri = 1;
					}
				}
				else{
					map_cantieri = new mappa('cantieri');
					map_cantieri.S();
					controllo_cantieri = 1;
				}
			}
		} // fine della funzione popola()
	
		function mappa(fase){

			this.S = function(){
				if(fase=='opere'){
					creaMappa('listaOpere.jsp', fase, 'all');
				}
				else
					creaMappa('listaOpere.jsp', fase, 'all');
			}
		}
		
		
		
		function creaMappa(nomeFile, type, cat){
			$("#dialog_map").html('<img src="images/load.gif" alt="loading">');
			window.setTimeout("$('#dialog_map').html('')", 3000);
			var lista_mrk = [];
		
			var nomeFileCompleto = nomeFile + "?cat="+cat;
			if(type=='cantieri'){
				nomeFileCompleto += "&cantieri=true";
			}
			$.post(nomeFileCompleto,function(data,responseText){
				
				if(responseText=='success'){
					$(data).find("marker").each(function() {
						var marker = $(this);
						var dati = [
								marker.attr("idz"),
								marker.attr("idCat"),
								marker.find("name").text().replace(/\\\(/g,"(").replace(/\\\)/g,")"),
								marker.find("address").text().replace(/\\\(/g,"(").replace(/\\\)/g,")"),
								marker.find("immagine").text()
						];
						var luogo = new GLatLng(marker.attr("lat"), marker.attr("lng"));
						var immagine = "";
						immagine = "<div id=\"schedaMappa\"><b>"+dati[2]+"</b><br /><i>"+dati[3]+"</i><br /><br />";
						/*if(dati[4]!=null){ 
							immagine += "<img src=\""+dati[4]+"\" alt=\"\" /><br />";
						}*/
						immagine += "<a style=\"position:absolute;text-decoration:none;bottom:0px;right:0px;\" href=\"page.jsp?idProgetto="+dati[0]+"&amp;idCat="+dati[1]+"";
						if(type=='cantieri') {
							immagine+= "&amp;cantieri=true";
						}
						immagine += "\">&#187;&#160;Scheda</a></div>";//alert(dati);
						var iconColor = new GIcon();
						iconColor.shadow = "";
						iconColor.iconSize = new GSize(16, 16);
						iconColor.shadowSize = new GSize(16, 16);
						iconColor.iconAnchor = new GPoint(6, 20);
						iconColor.infoWindowAnchor = new GPoint(5, 1);
	
						function createMarkerColored(map, point, type,cat, stringa){
							if(type=='cantieri'){
								iconColor.image = "images/map_icons/cantieri_on.gif";
							}
							else{
								iconColor.image = "images/map_icons/opere_on.gif";
							}
							var marker = new GMarker(luogo, iconColor);
							GEvent.addListener(marker, "click", function() {
								document.getElementById('dialog_map').innerHTML = stringa;
							});   //map.addOverlay(marker);
							// popola l'array globale
							if(type=='cantieri'){
								lista_mrk_cantieri.push(marker);
							}else{
								lista_mrk_opere.push(marker);
							}
							return marker;
						}
						
						map.addOverlay(createMarkerColored(map, new GLatLng(45.741342, 11.86362), type,cat, immagine));
						
					});
					return lista_mrk;
				}
			});
		
			return null;
		}

	
	
		

// ajax cantina =====================================================================================================
// ==================================================================================================================
// ==================================================================================================================
	
	// funzione per assegnare un oggetto XMLHttpRequest
		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};



/** OGGETTI / ARRAY */

	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

	// array descrittivo dei codici restituiti dal server
	// [la scelta dell' array ? per evitare problemi con vecchi browsers]
		var statusText = new Array();
		statusText[100] = "Continue";
		statusText[101] = "Switching Protocols";
		statusText[200] = "OK";
		statusText[201] = "Created";
		statusText[202] = "Accepted";
		statusText[203] = "Non-Authoritative Information";
		statusText[204] = "No Content";
		statusText[205] = "Reset Content";
		statusText[206] = "Partial Content";
		statusText[300] = "Multiple Choices";
		statusText[301] = "Moved Permanently";
		statusText[302] = "Found";
		statusText[303] = "See Other";
		statusText[304] = "Not Modified";
		statusText[305] = "Use Proxy";
		statusText[306] = "(unused, but reserved)";
		statusText[307] = "Temporary Redirect";
		statusText[400] = "Bad Request";
		statusText[401] = "Unauthorized";
		statusText[402] = "Payment Required";
		statusText[403] = "Forbidden";
		statusText[404] = "Not Found";
		statusText[405] = "Method Not Allowed";
		statusText[406] = "Not Acceptable";
		statusText[407] = "Proxy Authentication Required";
		statusText[408] = "Request Timeout";
		statusText[409] = "Conflict";
		statusText[410] = "Gone";
		statusText[411] = "Length Required";
		statusText[412] = "Precondition Failed";
		statusText[413] = "Request Entity Too Large";
		statusText[414] = "Request-URI Too Long";
		statusText[415] = "Unsupported Media Type";
		statusText[416] = "Requested Range Not Satisfiable";
		statusText[417] = "Expectation Failed";
		statusText[500] = "Internal Server Error";
		statusText[501] = "Not Implemented";
		statusText[502] = "Bad Gateway";
		statusText[503] = "Service Unavailable";
		statusText[504] = "Gateway Timeout";
		statusText[505] = "HTTP Version Not Supported";
		statusText[509] = "Bandwidth Limit Exceeded";
