﻿var map = null;
var geocoder = null;


function initialize() {
    if(document.getElementById("map"))
    {
        if (GBrowserIsCompatible()) 
        {
        
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GLargeMapControl()); 
            map.enableScrollWheelZoom() 
            //map.setCenter(new GLatLng(36.772992477693414,11.99432373046875), 13);
            geocoder = new GClientGeocoder();
            
            callShowAddress();
        }
        else
            alert("Browser non compatibile");
    }
    
}

function showAddress(nome,address,coord) 
{
    if (geocoder) 
    {
        if(coord)
        {
            
            var marker = new GMarker(coord,{clickable :true});
              
            map.addOverlay(marker);
            marker.openInfoWindowHtml(nome+"<br>"+address);
            
            GEvent.addListener(marker, "click", function() {
            
                marker.openInfoWindowHtml(nome+"<br>"+address);
                
            });
            map.setCenter(coord, 13);
            /*var myLayer = new GLayer("com.panoramio.all");
            map.addOverlay(myLayer);*/
        }
        else
        {
            geocoder.getLatLng(
                address,
                function(point) 
                {
                    if (!point) 
                    {
                        //alert("mappa momentaneamente non disponibile ("+address + " indirizzo sconosciuto)");
                        showAddress(nome,address.split(',')[2])
                    } 
                    else 
                    {
                        
                        var marker = new GMarker(point,{clickable :true});
                          
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(nome+"<br>"+address);
                        
                        GEvent.addListener(marker, "click", function() {
                        
                            marker.openInfoWindowHtml(nome+"<br>"+address);
                            
                        });
                        map.setCenter(point, 13);
                    }
                }
            );
            
        }
    }
    else
        alert("geocoder null");
}