var map;
var geocoder;
var icon = new GIcon();
var tab_addr = new Array();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

// 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 address and the country code.
function addAddressToMap(response) {

	if (!response || response.Status.code != 200) {
		alert("L'adresse n'est pas correcte. Exemple : 22 rue de bâle strasbourg, france");
    } 
	else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
    }

}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
	var address = document.forms[0].q.value;
    geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	document.forms[0].q.value = address;
    showLocation();
}

function createMarker(point, index) {
  // Create a lettered icon for this point using our icon class

  var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var iconMarker = new GIcon(icon);
  icon.image = "http://www.google.com/mapfiles/marker.png";
  var marker = new GMarker(point, iconMarker);

  GEvent.addListener(marker, "click", function() {
	  location.href = tab_addr[index];
  });

  return marker;
}

function getCo(address, index) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		map.addOverlay(new createMarker(point, index));
      }
    }
  );
}