var map;
var geocoder;


function createMarker(point,html) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
  });
  return marker;
}

function geocode(rue, cp_ville, nom, coords) {
  if(coords != '') {
    address = coords;
  } else {
    //address = rue + ' ' + cp_ville;
    address = cp_ville;
  }
  geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
                alert(address + " est introuvable");
          } else { 
                /*point="48.702327, 2.427489";*/
                //test = 48.702327, 2.427489;
                //alert(point);
                //map.setCenter(new GLatLng(48.702327, 2.427489), 13);
        if (nom != '') {
            address = nom + '<br />' + rue + '<br />' + cp_ville;
        } else {
            address = rue + '<br />' + cp_ville;
        }
        address += '<br /><a href="#"';
        address += ' onclick="redirige_agence(document.getElementById(\'search_agency\').value);return false;"';
        address += '>Fiche complète de l\'agent Proxéo</a>';
                map.setCenter(point, 8);
                var marker = createMarker(point, address);
                map.addOverlay(marker);

                marker.openInfoWindowHtml(address);
          }
        }
  );
  
  return false;
}

function load_map(code_postal) {
  if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_adress"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(46.85, 1.75), 6);
        geocoder = new GClientGeocoder();
    if (code_postal!='') {
		return recherche_agence(code_postal);		
    }
  } else {
        alert("Desole, l'API Google Maps n'est pas compatible avec votre navigateur.");
  }
}

