  sidebar_html = "";
       gmarkers = [];
       htmls = [];
       i = 0;
      	
	
function setFeedback(mese){
	document.getElementById('titolo-eventi').innerHTML="Eventi per il mese di "+mese;
	document.getElementById('clisting-eventi').innerHTML="<li id=\"noevents\">Attenzione: non esistono eventi per il mese di "+mese+".</li>";

}
//svuoto tutti gli array tra una richiesta e l'altra. Altrimenti placemark e label si duplicano	
function emptyInfos(){
	document.getElementById('clisting-eventi').innerHTML='';
	sidebar_html='';
	gmarkers=new Array();
	htmls=new Array();
	i=0;
	GUnload();
	}	
function chiudiMappa(){
			document.getElementById('cmappe').style.display='none';
			document.getElementById('clisting-eventi').innerHTML='';
			emptyInfos();

}
function initMaps(){
if (document.getElementById('close-map')){
	document.getElementById('close-map').onclick=chiudiMappa;
		}
	if (document.getElementById('sel-mappe-mese')){
	document.getElementById('sel-mappe-mese').onchange=setNewUrl;
		}

}

function setNewUrl(){
	if (this.selectedIndex==0){
		return;
		}
	emptyInfos();
	document.getElementById('titolo-eventi').innerHTML="<p style=\"font-weight:bold;color:red;\">Caricamento dati in corso. Si prega di attendere qualche istante...</p>";
	var urlAction="/tc/gmapXmlAction.do";
	var idCity=document.getElementById('x-idcitta').value;
	var idOp=document.getElementById('x-op').value;
	var xAnno=document.getElementById('x-anno').value;
	var xMese=this.options[this.selectedIndex].value;
	var nomeMese=this.options[this.selectedIndex].text;
	var paramAction="?idcitta="+idCity+"&op="+idOp+"&mese="+xMese+"&anno="+xAnno;
	var action=urlAction+paramAction;
	mgnInfoMap(action,nomeMese);
	
}
      function createMarker(point,name,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the sidebar
        gmarkers[i] = marker;
        htmls[i] = html;
        // add a line to the sidebar html
        sidebar_html += '<li><a href="#" onclick="myclick(' + i + ');return false;">' + name + '</a></li>';
		i++;
        return marker;
      }


  // This function picks up the click and opens the corresponding info window
      function myclick(i) {
	map.setZoom(6)
    gmarkers[i].openInfoWindowHtml(htmls[i],{maxWidth: 240});
      }

function mgnInfoMap(xmlGpath,nomeMese){
	document.getElementById('cmappe').style.display="block";
	emptyInfos();
	document.getElementById('titolo-eventi').innerHTML="<p style=\"font-weight:bold;color:red;\">Caricamento dati in corso. Si prega di attendere qualche istante...</p>";
	if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("gmappa"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(41.934976500546604, 11.7333984375), 5);

      var request = GXmlHttp.create();
	  request.open("GET",xmlGpath,true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = request.responseXML;
          var markers = xmlDoc.documentElement.getElementsByTagName("maker");
        if (nomeMese&&markers.length==0){
				setFeedback(nomeMese);
				return;
			}
		else if(nomeMese&&markers.length>0){
			document.getElementById('titolo-eventi').innerHTML="Eventi per il mese di "+nomeMese;
			}
		else{
			document.getElementById('titolo-eventi').innerHTML="Eventi collegati";
			}
          for (var i = 0; i < markers.length; i++) {
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
     		var titolo = unescape(markers[i].getElementsByTagName("label")[0].firstChild.nodeValue);
			var data = unescape(markers[i].getAttribute("data")); 
			var categoria = unescape(markers[i].getAttribute("macrocategoria"));
			var linkGoTo = unescape(markers[i].getAttribute("link"));
            if (markers[i].getElementsByTagName("html")[0].firstChild){
				var html = markers[i].getElementsByTagName("html")[0].firstChild.nodeValue;
				}
			else{
				var html='';
				}
			var htmlBaloon = '<p id="g-data">'+data+'</p><p id="g-categoria">'+categoria+'</p><p id="g-titolo">'+titolo+'</p><div id="g-html">'+html+'</div><p id="g-goto"><a href="'+linkGoTo+'">Vai alla scheda dell\'evento</a></p>';
            var label = titolo;
            var marker = createMarker(point,label,htmlBaloon);
            map.addOverlay(marker);
          }
          // put the assembled sidebar_html contents into the sidebar div
		  if (markers.length>0){
         	 document.getElementById("clisting-eventi").innerHTML = sidebar_html;
		  	}
			else{
				document.getElementById("clisting-eventi").innerHTML = '<li>Non sono presenti eventi collegati all\'evento corrente.</li>';
				}
		  myclick(0);
        }
      }
      request.send(null);
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
