
var geocoder = null;
var map = [];

function majCarte(id, adresse, ville) {
	if (ville != '')
		ville = ville + " france";
	geocoder.getLatLng(adresse + " " + ville, 
		function(point) {
			if (!point) {
				chargerVille(id, ville);
			} else {
				chargerCarte(id, point.y, point.x, 15, true);
			}
		}
	);
}

function chargerVille(id, ville) {
	geocoder.getLatLng(ville, 
		function(point) {
			if (!point) {
				chargerCarte(id, "46.800059446787316", "1.669921875", 5, true);
			} else {
				chargerCarte(id, point.y, point.x, 13, true);
			}
	});
}

function initMap(id) {
	map[id] = null;	
}

function chargerCarte(id, x, y, zoom, move) {
	if (GBrowserIsCompatible()) {
		map[id] = new GMap2(document.getElementById(id));
		
		map[id].setCenter(new GLatLng(x, y), zoom);
		map[id].setMapType(G_NORMAL_MAP);
		map[id].addControl(new  GLargeMapControl3D());
		map[id].addControl( new GOverviewMapControl());
		map[id].enableScrollWheelZoom();
		map[id].enablePinchToZoom();
		map[id].enableContinuousZoom();
		map[id].removeControl( new GMapTypeControl() );
		
		var center = new GLatLng(x, y);
		map[id].setCenter(center, zoom);
		/*
		if (move)
			var marker = new GMarker(center, {draggable: true});
		else
			var marker = new GMarker(center, {draggable: false});
		GEvent.addListener(marker, "dragstart", function() {
			map[id].closeInfoWindow();
        });
		map[id].addOverlay(marker);
		map[id].marker = marker;
		*/
	}
}

function chargerCarteMarker(id, x, y, zoom, x_marker, y_marker, move) {	
	if (GBrowserIsCompatible()) {
		map[id] = new GMap2(document.getElementById(id));
		
		map[id].setCenter(new GLatLng(x, y), zoom);
		map[id].setMapType(G_NORMAL_MAP);
		map[id].addControl(new  GLargeMapControl3D());
		map[id].addControl( new GOverviewMapControl());
		map[id].enableScrollWheelZoom();
		map[id].enablePinchToZoom();
		map[id].enableContinuousZoom();
		map[id].removeControl( new GMapTypeControl() );
				
		var center = new GLatLng(x, y);
		map[id].setCenter(center, zoom);

		var centerMarker = new GLatLng(x_marker, y_marker);
		if (move)
			var marker = new GMarker(centerMarker, {draggable: true});
		else
			var marker = new GMarker(centerMarker, {draggable: false});
		GEvent.addListener(marker, "dragstart", function() {
			map[id].closeInfoWindow();
        });
		map[id].addOverlay(marker);
		map[id].marker = marker;
	}
}

$(document).ready(function() {
	window.onUnLoad = GUnload();
	geocoder = new GClientGeocoder();
});

function getLatitude(id) {
	if (map[id] && map[id] != null) {
		var center = map[id].getCenter();
		return center.lat();
	}
	return "";
}

function getLongitude(id) {
	if (map[id] && map[id] != null) {
		var center = map[id].getCenter();
		return center.lng();
	}
	return "";
}

function getLatitudeMarker(id) {
	if (map[id] && map[id] != null)
		return map[id].marker.getPoint().lat();
	return "";
}

function getLongitudeMarker(id) {
	if (map[id] && map[id] != null)
		return map[id].marker.getPoint().lng();
	return "";
}

function getZoom(id) {
	if (map[id] && map[id] != null)
		return map[id].getZoom();
	return "";
}

