var map;
var geocoder;
var marker=new Array();
var firstpoint;
function showAddress(evento,pos) {
    var icon = new GIcon();
    icon.iconSize = new GSize(31, 26);
    icon.shadowSize = new GSize(45, 16);
    icon.iconAnchor = new GPoint(14, 24);
    icon.infoWindowAnchor = new GPoint(20, 1);
    icon.shadow = "images/marker/icon_ombra.png";

    if(evento.getAttribute("sponsoriz"))
        icon.image = "images/marker/icon_sponsor.png";
    else icon.image = "images/marker/icon_eventi.png";
		
    var point = new GLatLng(parseFloat(evento.getAttribute("lat")),
        parseFloat(evento.getAttribute("lng")));
    //firstpoint= new GLatLng(45.439205,10.993037);
    firstpoint= new GLatLng(45.474, 9.187);    

    if(pos==0){
        firstpoint=point;
    }
    for(k=0;k<pos;k++){
        if((marker[k].getLatLng().lat()==point.lat()) && (marker[k].getLatLng().lng()==point.lng())){
            point= new GLatLng(point.lat()+0.0001,point.lng()+0.0001);
        }
    }

    marker[pos] = new GMarker(point,icon);
    map.setCenter(firstpoint, 11);

    GEvent.addListener(marker[pos], "click", function() {
			
		
			var lista_eventi = evento.childNodes;
			listone = "";
			for(j=0; j<lista_eventi.length; j++) {
				if(lista_eventi[j].tagName == "evento") {
					//listone += lista_eventi[j].attributes[1].nodeValue + '<br>';
					listone += '<li><a href="' + lista_eventi[j].getAttribute("ext_link") + '">' + lista_eventi[j].getAttribute("nome") + '</a></li>';
				}
			}
			
      marker[pos].openExtInfoWindow(map,'evento_marker',"<div class='mappa_hompage'><b>"+evento.getAttribute("nome")+"</b><br><ul>"+ listone +"</ul></div>");
			//marker[pos].openExtInfoWindow(map,'evento_marker',listone);
      /*  if(viaggio.getAttribute("foto")!="")
            marker[pos].openExtInfoWindow(map,'viaggio_marker',"<div><img src=_files/viaggi/immagini/t1_"+viaggio.getAttribute("foto")+" /><b><a class=cat"+viaggio.getAttribute("macro")+" href=viaggio-mip-travel.php?cat="+macrotitolo+"&viaggio="+viaggio.getAttribute("id")+">"+viaggio.getAttribute("titolo")+"</a></b><br>"+viaggio.getAttribute("motivazionale")+"</div>");
        else marker[pos].openExtInfoWindow(map,'viaggio_marker',"<div><b><a class=cat"+viaggio.getAttribute("macro")+" href=viaggio-mip-travel.php?cat="+macrotitolo+"&viaggio="+viaggio.getAttribute("id")+">"+viaggio.getAttribute("titolo")+"</a></b><br>"+viaggio.getAttribute("motivazionale")+"</div>");
    */});
    map.addOverlay(marker[pos]);
    return pos+1;
}


function loadHome() {

    if (GBrowserIsCompatible()) {
        //map=new google.maps.Map(document.getElementById("map_canvas"));
        map = new GMap2(document.getElementById("map_canvas"));
        filexml="data.php";
        GDownloadUrl(filexml, function(data, responseCode) {
            // To ensure against HTTP errors that result in null or bad data,
            // always check status code is equal to 200 before processing the data

            if(responseCode == 200) {
                var xml = GXml.parse(data);
                var eventi = xml.getElementsByTagName("location");

                for (var i = 0; i < eventi.length; i++) {
                        showAddress(eventi[i],i);
                }
            }
            else if(responseCode == -1) {
                alert("Data request timed out. Please try later.");
            } else {
                alert("Request resulted in error. Check XML file is retrievable.");
            }
        });
    }

}

function setZoom(zoom){
    var actual_zoom = map.getZoom();

    switch (zoom){
        case '+':
            map.setZoom(actual_zoom+1);
            break;

        case '-':
            map.setZoom(actual_zoom-1);
            break;
        case '1':
            map.setZoom(1);
            break;
        default:
            break;
    }
}
function setMapType(type){
    map.setMapType(type);
}
