function displayFileContent(url,divID){
	var request = GXmlHttp.create();
	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			document.getElementById(divID).innerHTML=request.responseText;
		}
	}
	request.send(null);
}

var emajineMap=function(divmap){
	this.map = new GMap2(document.getElementById(divmap));
	this.map.addMapType(G_PHYSICAL_MAP);//permet d'ajouter le bouton "relief" quand map_type_control est activé

	this.latlngbounds = new GLatLngBounds( );
}

// initialise les points du centre
emajineMap.prototype.setPoint = function(latitude,longitude) {
	this.point = new GLatLng(latitude, longitude);
	this.latlngbounds.extend( this.point );
}
// initialise le zoom 
emajineMap.prototype.setZoom = function(zoom){
	this.zoom = zoom;
}
// creation du point centrale 
emajineMap.prototype.setCenter=function(){
	if (typeof this.point === 'undefined') {
		this.map.setCenter( this.latlngbounds.getCenter( ), this.map.getBoundsZoomLevel( this.latlngbounds ) );
	} else {
		this.map.setCenter(this.point, this.zoom);
	}
}

emajineMap.prototype.setType = function(type) {
	this.map.setMapType(type);
}
// Fixe les controles 
emajineMap.prototype.setControl = function(control) {
	//active le control passé en parametre
	if(control) this.map.addControl(new control());
}

emajineMap.prototype.setGoogleBar = function(bool) {
	//active googlebar
	if(bool) this.map.enableGoogleBar();
}

emajineMap.prototype.setDefaultControl = function() {
	// active le zoom de la souris 
	this.map.enableScrollWheelZoom();
	//active les controles clavier
	new GKeyboardHandler(this.map);
}

//initialisation de l'image du marker 
emajineMap.prototype.setIcon = function(icon) {
	if(!icon) icon = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	// construit un objet GIcon
	this.tinyIcon = new GIcon();
	//associe l'image
	this.tinyIcon.image = icon;
	//configure la taille
	this.tinyIcon.iconSize = new GSize(32, 32);//largeur,hauteur (en pixel) ; les images proposées font 32x32
	this.tinyIcon.iconAnchor = new GPoint(16, 32);//x,y
	this.tinyIcon.infoWindowAnchor = new GPoint(16, 2);
}

// Creation d'un point
emajineMap.prototype.getMarker = function (latitude,longitude) {
	var point = new GLatLng(latitude, longitude);
	this.latlngbounds.extend( point );
	marker = new GMarker(point,{icon:this.tinyIcon});
	return marker;
}

//Creation d'un point avec label
emajineMap.prototype.getLabeledMarker = function (latitude,longitude,label) {
	var point = new GLatLng(latitude, longitude);
	this.latlngbounds.extend( point );
	var offset=new GSize(-3.3*(label.length), -30);
	var opts={"icon": this.tinyIcon,"clickable": true,"labelText": label,"labelClass": "labelMarker","labelOffset": offset};
	var marker = new LabeledMarker(point,opts);
	return marker;
}

//Ajout du layer sur le marker (infobulle)
emajineMap.prototype.setMarker = function (marker,url) {
	if(url) {
		GEvent.addListener(marker, "click", function (){
			var coord = marker.getLatLng();
			var coord1 = coord.lat();
			var coord2 = coord.lng();
			var info = '<div id="gMapWindowContent" class="googleMapBulle">Veuillez patienter...</div>';
			marker.openInfoWindowHtml(info);
		});
		GEvent.addListener(marker, "infowindowopen", function (){
			displayFileContent(url,'gMapWindowContent')
		});
	}
	this.map.addOverlay(marker);
}

// layer pour afficher les coordonnées.
emajineMap.prototype.afficheCoord = function (marker){
	var coord = marker.getLatLng();
	var coord1 = coord.lat();
	var coord2 = coord.lng();
	var info = '<span style="font-size:small;"><strong>Coordonnées :</strong> <br />Latitude : '+coord1+'<br />Longitude : '+coord2+'</span>';
	marker.openInfoWindowHtml(info);
}

//Itineraire :
emajineMap.prototype.getDirection = function (divdirection) {
	document.getElementById(divdirection).innerHTML = '';
	
	this.gdir = new GDirections(this.map, document.getElementById(divdirection));
	var gdir = this.gdir;
 	GEvent.addListener(gdir, "load", function() {
 	// Use this function to access information about the latest load()
 // results.

 // e.g.
 // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
 // and yada yada yada...
 	});
	GEvent.addListener(gdir, "error", function() {
			document.getElementById('divMap').style.display = 'none';
			if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("La localisation est incorrecte.\nError code: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert("Recherche inactive.\n Error code: " + gdir.getStatus().code);
		
		else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
		
		// else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
		// alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
		
		else if (gdir.getStatus().code == G_GEO_BAD_KEY)
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
		
		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		
		else alert("An unknown error occurred.");
		
		
	});
}

emajineMap.prototype.setDirections = function (fromAddress, toAddress, locale) {
		this.gdir.load("from: " + fromAddress + " to: " + toAddress,{ "locale": locale });
}

emajineMap.prototype.loadJsonSpots=function(url)
{
	var latlngbounds=this.latlngbounds;
	var map=this.map;
	var centerPoint=this.point;

	GDownloadUrl(url,function(datas,state){
		datas=eval(datas);//to json
		var len=datas.length;
		if(len>0){
			for(var i=0;i<len;i++){
				if (typeof datas[i] != 'undefined') {
					this.map.setIcon(datas[i].icon);
					var m=this.map.getLabeledMarker(datas[i].latitude,datas[i].longitude,datas[i].label);
					this.map.setMarker(m,datas[i].uri);
				}
			}
		}
		if (typeof centerPoint === 'undefined') {
			map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );
		}
		return;
	});
}


	
//
function loadFieldMap(fieldID, mode)
{
	var fieldId = fieldID;
	var mod = mode;//adress | map,  si vide, la sélection se fera avec les 2 modes
	var fieldHidden = document.getElementById(fieldId+'idhidden');
	var fieldHtml = document.getElementById(fieldId+'span');
	var defCoord = document.getElementById(fieldId+'idhidden').value;
	var _coord='';// coordonnées sous la forme "latitude|longitude"
	var _info='';//  coordonnées sous la forme "Latitude : latitude Longitude : longitude" 
	var mapOptions=new Array();
	var marker;
	
	if(GBrowserIsCompatible())
	{
		if(mod=='address'){
			//geocoder
			var geo = document.getElementById("search"+fieldId);
			geo.onclick= function(){
				var geocoder = new GClientGeocoder();
				var address = document.getElementById("adresse"+fieldId).value;
				if(address!=''){
					geocoder.getLatLng(address, function(point) {
						if (!point) {
							alert(address + " _not_found_");
						} else {
							var lng = point.x, lat = point.y;
							_coord=lat+'|'+lng;
							_info = "Latitude"+' : '+lat+" Longitude"+' : '+lng;
							updateFieldsMap(_coord,_info,fieldHidden,fieldHtml)
						}
					});
				}else{
					alert("_vous_devez_renseigner_le_champ_adresse_");
					document.getElementById("adresse"+fieldId).focus();
				}
			}
		}else if(mod=='map')
		{

			var map = new GMap2(document.getElementById("map"+fieldId));//init map
			if(defCoord){
				defCoord = defCoord.split('|');
				var defPt = new GLatLng(defCoord[0], defCoord[1]);
				_coord=defCoord[0]+'|'+defCoord[1];
				_info = "Latitude"+' : '+defCoord[0]+" Longitude"+' : '+defCoord[1];
				
				updateFieldsMap(_coord,_info,fieldHidden,fieldHtml)
				
				map.setCenter(defPt, 5);
				var defMkr = new GMarker(defPt, {draggable: true});
				GEvent.addListener(defMkr, "dragend", function() {//met à jour le champ coord à la fin d'un deplacement de marker
					var latLng = defMkr.getLatLng();
					coord1 = latLng.lat();
					coord2 = latLng.lng();
					_coord=coord1+'|'+coord2;
					_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
					
					updateFieldsMap(_coord,_info,fieldHidden,fieldHtml)
				});
				map.addOverlay(defMkr);
			}else{
				map.setCenter(new GLatLng(46.61926103617151, 2.8125), 5);//fixe le centre de la carte
			}

							map.setZoom(5);
						//affiche la barre latérale de navigation
							mapOptions["zoomControl"] = new GLargeMapControl();
				map.addControl(mapOptions['zoomControl']);
			
							map.setMapType(G_NORMAL_MAP);
						//affiche le button [plan|satellite|mixte]
						//Affiche l'éechelle de la map
						
			//Affiche la mini map de navigation
			
// 			map.addControl(new GLargeMapControl());//affiche la barre latérale de navigation
// 			map.addControl(new GMapTypeControl());//affiche le button [plan|satellite|mixte]
// 			map.enableScrollWheelZoom();//active le zoom avec la roulette de la souris
			new GKeyboardHandler(map);//active les controles clavier 
			
			//affiche un nouveau marker "draggable" à chaque clic sur la map
			GEvent.addListener(map,"click", function(overlay,point) {
				map.clearOverlays();//efface tous les markers
				var marker = new GMarker(point, {draggable: true});//creation d'un nouveau marker "draggable"
				var latLng = marker.getLatLng();
				coord1 = latLng.lat();
				coord2 = latLng.lng();
				_coord=coord1+'|'+coord2;
				_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
				
				updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				
				GEvent.addListener(marker, "dragend", function() {//met à jour le champ coord à la fin d'un deplacement de marker
					var latLng = marker.getLatLng();
					coord1 = latLng.lat();
					coord2 = latLng.lng();
					_coord=coord1+'|'+coord2;
					_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
					updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				});
	
				//affichage du marker sur la map
				map.addOverlay(marker);
			});
		}
		else
		{
			var map = new GMap2(document.getElementById("map"+fieldId));//init map
			if(defCoord){
				defCoord = defCoord.split('|');
				var defPt = new GLatLng(defCoord[0], defCoord[1]);
				_coord=defCoord[0]+'|'+defCoord[1];
				_info = "Latitude"+' : '+defCoord[0]+" Longitude"+' : '+defCoord[1];
				updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				map.setCenter(defPt, 5);
				var defMkr = new GMarker(defPt, {draggable: true});
				GEvent.addListener(defMkr, "dragend", function() {//met à jour le champ coord à la fin d'un deplacement de marker
					var latLng = defMkr.getLatLng();
					coord1 = latLng.lat();
					coord2 = latLng.lng();
					_coord=coord1+'|'+coord2;
					_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
					updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				});
				map.addOverlay(defMkr);
				mapOptions["marker"]= defMkr; 
			}else{
				map.setCenter(new GLatLng(46.61926103617151, 2.8125), 5);//fixe le centre de la carte sur Paris
			}
							map.setZoom(5);
			<!--			affiche la barre latérale de navigation-->
							mapOptions["zoomControl"] = new GLargeMapControl();
				map.addControl(mapOptions['zoomControl']);
			
							map.setMapType(G_NORMAL_MAP);
						
<!--			affiche le button [plan|satellite|mixte]-->
						
<!--			Affiche l'échelle de la map-->
				
			
			//Affiche la mini map de navigation
			

// 			map.addControl(new GLargeMapControl());//affiche la barre latérale de navigation
//			map.addControl(new GMapTypeControl()); //affiche le button [plan|satellite|mixte]
// 			map.enableScrollWheelZoom();//active le zoom avec la roulette de la souris
			new GKeyboardHandler(map);//active les controles clavier

			
			/*****************************************************************/
			//geocoder
			var geo = document.getElementById("search"+fieldId);
			
			geo.onclick= function(){
				var geocoder = new GClientGeocoder();
				var address = document.getElementById("adresse"+fieldId).value;
				if(address!=''){
					geocoder.getLatLng(address, function(point) {
						if (!point) {
							alert(address + " _not_found_");
						} else {
							map.clearOverlays();//efface tous les markers
							map.panTo(point);//deplace la carte de sorte que "point" soit le centre
							var lng = point.x;
							var lat = point.y;
							_coord=lat+'|'+lng;
							_info = "Latitude"+' : '+lat+" Longitude"+' : '+lng;
							updateFieldsMap(_coord,_info,fieldHidden,fieldHtml)
							var marker = new GMarker(point, {draggable: true});
							GEvent.addListener(marker, "dragend", function() {//met à jour le champ coord à la fin d'un deplacement de marker
								var latLng = marker.getLatLng();
								coord1 = latLng.lat();
								coord2 = latLng.lng();
								_coord=coord1+'|'+coord2;
								_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
								updateFieldsMap(_coord,_info,fieldHidden,fieldHtml)
							});
							map.addOverlay(marker);//affiche le marker sur la map
						}
					});
				}else{
					alert("_vous_devez_renseigner_le_champ_adresse_");
					document.getElementById("adresse"+fieldId).focus();
				}
			}
			/****************************************************************/
			//affiche un nouveau marker "draggable" à chaque clic sur la map
			GEvent.addListener(map,"click", function(overlay,point) {
				map.clearOverlays();//efface tous les markers
				var marker = new GMarker(point, {draggable: true});//creation d'un nouveau marker "draggable"
				var latLng = marker.getLatLng();
				coord1 = latLng.lat();
				coord2 = latLng.lng();
				_coord=coord1+'|'+coord2;
				_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
				updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				
				GEvent.addListener(marker, "dragend", function() {//met à jour le champ coord à la fin d'un deplacement de marker
					var latLng = marker.getLatLng();
					coord1 = latLng.lat();
					coord2 = latLng.lng();
					_coord=coord1+'|'+coord2;
					_info = "Latitude"+' : '+coord1+" Longitude"+' : '+coord2;
					updateFieldsMap(_coord,_info,fieldHidden,fieldHtml);
				});
	
				//affichage du marker sur la map
				map.addOverlay(marker);
				
			});	
		}
		
	}else{
		alert("_navigateur_pas_compatible_");
	}
	
	mapOptions["map"]= map;
	
	
	return mapOptions;
}

function updateFieldsMap(coordhidden,coordhtml,fieldhidden,fieldhtml)
{
	if(fieldhidden) fieldhidden.value=coordhidden;
	if(fieldhtml) fieldhtml.innerHTML=coordhtml;
}
