// base_ajax_url doit être définie
// phrase_cliquez_sur_carte doit être définie
// phrase_navig_incompatible_geo doit être définie
// phrase_non_localise doit être définie
// phrase_chargement doit être définie

var map = null;
var geocoder = null;

function load_map(lat, lng, zoom) {
  
  if ($("#infos-meteo-map").attr('id') == 'infos-meteo-map') {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("infos-meteo-map"));
	    map.addControl(new GLargeMapControl());
	    GEvent.addListener(map,"click", function(overlay, point) {     
        afficher(point);
      });
      var latlng = new GLatLng(lat, lng);
      map.setCenter(latlng, zoom);
    }
    else {
      alert(phrase_navig_incompatible_geo);
    }
  }
}


function afficher(point) {
  map.clearOverlays();
  var marker = new GMarker(point, {clickable : false});
  map.addOverlay(marker);
  $('#infos-meteo-resultats').ajaxStart(function(){
 	  $(this).html(phrase_chargement);
  });
  $('#infos-meteo-resultats').load(base_ajax_url + "meteo.php", {lat : point.lat(), lng : point.lng()});
}

function localiser() {
  if (GBrowserIsCompatible()) {
    if (geocoder == null) {
      geocoder = new GClientGeocoder();
    }
    var adresse = "";
    adresse += $('#infos-meteo-form-ville').attr('value') ? $('#infos-meteo-form-ville').attr('value') : "";
	  if (adresse != "") {
	    geocoder.getLatLng(
        adresse,
        function(point) {
	    	  map.setCenter(point);
		      if (!point) {
			      map.clearOverlays();
	          $('#infos-meteo-resultats').html(phrase_cliquez_sur_carte);
	          alert( adresse + " " + phrase_non_localise);
          } else {
          	afficher(point);
          }
        }
      );
    }
    else {
      map.clearOverlays();
      $('#infos-meteo-resultats').html(phrase_cliquez_sur_carte);
	  }
  }
  else {
    alert(phrase_navig_incompatible_geo);
  }
}

$(document).ready(function() {
  $('#infos-meteo-form-ville').keypress(function (e) {
    if (e.which == 13) {
      localiser();
	    return false;
    }
  });
});
