
    var map = null;
    var geocoder = null;
    var point = null;
    var map_address = "";
    var map_defaultZoom = 16;
    var map_htmlInfo = "";
    var map_id_attivita = 0;
    var nome_attivita = "";
    
    function loadMap() {
    
    
    
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        //map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.enableDoubleClickZoom();
        
        mapLoaded();
      }
    }

    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the map_address and the country code.
    function addAddressToMap(response) {
 
        if (!map_id_attivita)
            alert("map_id_attivita: 0");

      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Spiacenti, non siamo riusciti a mappare questo indirizzo");
        document.getElementById("map").style.display = "none";
        document.getElementById("helpmap").style.display = "none";
        
        // update db cache coordinate 
        if (response.Status.code == 602) {           
            newSrc = "/engine/updateCoords.php?ida="+map_id_attivita+"&lat=-1&lng=-1";
            document.getElementById("updateCoords").src = newSrc;
        }

      } else {
              
        // facciamo scegliere tra le varie possibilità
        if (response.Placemark.length>1) {
        
            layerChoices = document.getElementById("layerMultiChoice");
                
            layerChoices.innerHTML = "L'indirizzo '"+map_address+"' è ambiguo, scegliere quello ritenuto corretto:<br><br>";
                
           for (i=0; i<response.Placemark.length; i++)   {
                nuovoHtml = "<b>"+nome_attivita+"</b><br>"+response.Placemark[i].address;
               dst = "showMap('"+nuovoHtml+"', '', -1, '"+response.Placemark[i].address+"', "+response.Placemark[i].Point.coordinates[1]+", "+response.Placemark[i].Point.coordinates[0]+")";
               layerChoices.innerHTML += "<a onClick=\""+dst+"; document.getElementById('layerMultiChoice').style.display = 'none'\" style=\"cursor: pointer;\" class=\"testo_centro_eventi_link\">"+response.Placemark[i].address+"</a><br>";
               
           }
           layerChoices.style.display = "block";
                 
            return false;
            
        }
        
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
                            
        map.setCenter(point, map_defaultZoom);
        marker = new GMarker(point);
        map.addOverlay(marker);
 
        marker.openInfoWindowHtml(map_htmlInfo);
        
        // place.map_address è l'indirizzo formattato da GMap

/*        
0 Unknown location. (Since 2.59) 
1 Country level accuracy. (Since 2.59) 
2 Region (state, province, prefecture, etc.) level accuracy. (Since 2.59) 
3 Sub-region (county, municipality, etc.) level accuracy. (Since 2.59) 
4 Town (city, village) level accuracy. (Since 2.59) 
5 Post code (zip code) level accuracy. (Since 2.59) 
6 Street level accuracy. (Since 2.59) 
7 Intersection level accuracy. (Since 2.59) 
8 map_address level accuracy. (Since 2.59) 
*/
        // se il geocoding è accurato salviamo le info per non richiederle dopo
        if (place.AddressDetails.Accuracy>=6 && response.Placemark.length==1) {
            lat = place.Point.coordinates[1];
            lng = place.Point.coordinates[0];

            // update db cache coordinate            
            newSrc = "/engine/updateCoords.php?ida="+map_id_attivita+"&lat="+lat+"&lng="+lng;
            document.getElementById("updateCoords").src = newSrc;
        } else {
            alert('Attenzione, la posizione di questa specifica struttura sulla mappa potrebbe non essere accurata.\nL\'immagine è posizionata all\'indirizzo: \"'+place.address+'\"');
        }
       
      }
    }
    
    function showMap(_map_htmlInfo, _nome_attivita, _map_id_attivita, _map_address, lat, lng) {
        
        if (_map_htmlInfo != "")
            map_htmlInfo = _map_htmlInfo;
        
        if (_map_id_attivita != -1)
            map_id_attivita = _map_id_attivita;
            
        if (_nome_attivita != "")
            nome_attivita = _nome_attivita;
            
        map_address = _map_address;

        //lat=lng=0;        
       // map_address = "via garibaldi, milano, italia";
               
        if (lat!=0 && lng!=0) {
           
            point = new GLatLng(lat, lng);    
                      
            map.setCenter(point, map_defaultZoom);
                        
            marker = new GMarker(point);
            map.addOverlay(marker);

            marker.openInfoWindowHtml(map_htmlInfo);

       } else {   
 
            geocoder = new GClientGeocoder();  
            geocoder.getLocations(map_address, addAddressToMap);
        }
        
        
        GEvent.clearInstanceListeners(map);
        GEvent.addListener(map, "click", function(marker, pointClicked) {
          if (marker) {
            marker.openInfoWindowHtml(map_htmlInfo);
          } else {
            dist = Math.round(point.distanceFrom(pointClicked));
            alert("Distanza da \""+nome_attivita+"\": "+dist+" metri");
          }
        });
            
    }
    

