// Creates a marker at the given point with the given number label
function createMarker(point, name, withInfoWindow) {
  var marker = new GMarker(point);
  if(withInfoWindow)
  {
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(name);
	  });
  }
  return marker;
}

function showPoint(lat, lng, html_infos)
{
	point = new GLatLng(lat, lng);
    map.setCenter(point, 13);
    createMarkerAndAdd(point, html_infos, true);
}

function createMarkerAndAdd(point, infoWindow, withInfoWindow)
{
	var marker = createMarker(point, infoWindow, withInfoWindow);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(infoWindow);
}

// show a geocoded address
    function showAddress(address) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            alert(address + ' not found');
          } else {
            map.setCenter(point, 14);
            var marker = new GMarker(point);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(address);
          }
        }
      );
    }

function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
  	 displayFeedback($('feedback'), 'Sorry we were unable to found this address', 'error');
  	 $('save_btn').disable();
  	 $('search_form').reset();
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    marker = new GMarker(point,{draggable: true});
    
    var htmlInfos = place.address + '<br>' + 
      '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode;
    
    GEvent.addListener(marker, "dragstart", function() {
	    map.closeInfoWindow();
	    });

	GEvent.addListener(marker, "dragend", function() {
		marker.openInfoWindowHtml(htmlInfos);
		updateLatLng(point);
		$('save_btn').enable();
		});
    
    map.setCenter(point, 15);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(htmlInfos);
    
    
    country = place.AddressDetails.Country;
    
    // display the save button
    $('save_btn').value = "Yeah save it !"
    $('save_btn').enable();
    new Effect.Highlight($('save_btn'));
    // display feedback
    displayFeedback($('feedback'), place.address + ' found', 'ok');
    
    // display form conainting informations we found
    $('form_informations').show();
    // TODO: deplacer a une meilleures place
    $('geo_dat_country').value = country.CountryNameCode;
    $('geo_dat_lat').value = place.Point.coordinates[1];
    $('geo_dat_lng').value = place.Point.coordinates[0];
    locality = country.AdministrativeArea.SubAdministrativeArea.Locality;
    $('geo_dat_address').value = locality.Thoroughfare.ThoroughfareName;
    $('geo_dat_zip').value = locality.PostalCode.PostalCodeNumber;
    $('geo_dat_city').value = locality.LocalityName;
  }
}

function displayFeedback(div, message, type)
{
	if(type == 'error')
	{
		if(div.hasClassName('ok')){
	  		div.removeClassName('ok');
	  	}
	  	div.addClassName('error');
	}
	else
	{
		if(div.hasClassName('error')){
	  		div.removeClassName('error');
	  	}
	  	div.addClassName('ok');
	}
    div.update(message);
}

function updateLatLng(point)
{
	$('geo_dat_lat').value = point.lat();
    $('geo_dat_lng').value = point.lng();
}

// 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(address) {
  geocoder.getLocations(address, addAddressToMap);
}