var map = null;
var geocoder = null;
var marker = null;
var ajout_marker = false;
var infosBulle = "<span style=\"font:normal 12px Arial,sans-serif;text-align:center;\">Déplacez ce marqueur à l'endroit<br>où se trouve le radar mobile</span>";

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map_canvas"));
	var center = new GLatLng(46.227638, 2.213749);
	map.setCenter(center, 6);
	map.addControl(new GLargeMapControl());
	geocoder = new GClientGeocoder();
	
	marker = new GMarker(center, {draggable: true});
	GEvent.addListener(marker, "dragstart", function() {
		map.closeInfoWindow();
	});
	GEvent.addListener(marker, "dragend", function() {
		var pos = marker.getLatLng();
		marker.openInfoWindowHtml("<span style=\"font:normal 12px Arial,sans-serif;text-align:center;\">Position du radar que vous déclarez :<br>Lat : "+pos.lat()+"<br>Lng : "+pos.lng()+"</span>");
		document.getElementById("inputLat").value = pos.lat();
		document.getElementById("inputLng").value = pos.lng();
	});
	GEvent.addListener(marker, "click", function() {
		var pos = marker.getLatLng();
		marker.openInfoWindowHtml("<span style=\"font:normal 12px Arial,sans-serif;text-align:center;\">Position du radar que vous déclarez :<br>Lat : "+pos.lat()+"<br>Lng : "+pos.lng()+"</span>");
	});
	map.addOverlay(marker);
	marker.hide();
	
	GEvent.addListener(map, "zoomend", function(oldLevel,newLevel) {
		map.closeInfoWindow();
		var pos = marker.getLatLng();
		var recupCenter = map.getCenter();
		map.setCenter(recupCenter, newLevel);
		if (ajout_marker == true) {
			marker.openInfoWindowHtml("<span style=\"font:normal 12px Arial,sans-serif;text-align:center;\">Position du radar que vous déclarez :<br>Lat : "+pos.lat()+"<br>Lng : "+pos.lng()+"</span>");
		}
		if (newLevel < 14) marker.disableDragging();
		else marker.enableDragging();
	});
  }
}

function ajouterMarqueur() {
	if (ajout_marker == false) {
		var nivZoom = map.getZoom();
		var recupCenter = map.getCenter();
		if (nivZoom > 13) {
			ajout_marker = true;
			marker.setLatLng(recupCenter);
			marker.show();
			marker.openInfoWindowHtml(infosBulle);
		}
		else {
			alert("Le niveau de zoom n'est pas assez précis");
		}
	}
	else {
		alert("Veuillez utiliser le marqueur situé sur la carte");
	}
}

function supprimerMarqueur() {
	if (ajout_marker == true) {
		ajout_marker = false;
		marker.hide();
		marker.closeInfoWindow();
		document.getElementById("inputLat").value = "";
		document.getElementById("inputLng").value = "";
	}
	else {
		alert("Aucun marqueur n'a été ajouté");
	}
}

function showAddress(id,valZoom) {
  var address = document.getElementById(id).value;
  if (id == 'selectDept') address = address+' france';
  if (id == 'ville') address = address+' '+document.getElementById('selectDept').value+' france';
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " est introuvable");
		} else {
		  map.setCenter(point, valZoom);
		}
	  }
	);
  }
}
