
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é
}

// initialise les points du centre
emajineMap.prototype.setPoint = function(latitude,longitude) {
	this.point = new GLatLng(latitude, longitude);
}
// initialise le zoom 
emajineMap.prototype.setZoom = function(zoom){
	this.zoom = zoom;
}
// creation du point centrale 
emajineMap.prototype.setCenter=function(){
	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);
// 	this.tinyIcon.iconSize = new GSize(20, 32);//largeur,hauteur (en pixel) ; les images proposées font 32x32
// 	this.tinyIcon.iconAnchor = new GPoint(10, 32);//x,y
// 	this.tinyIcon.infoWindowAnchor = new GPoint(10, 2);
// 	this.tinyIcon.iconSize = new GSize(22, 22);//largeur,hauteur (en pixel) ; les images proposées font 32x32
// 	this.tinyIcon.iconAnchor = new GPoint(11, 22);//x,y
// 	this.tinyIcon.infoWindowAnchor = new GPoint(11, 2);

}

// Creation d'un point
emajineMap.prototype.getMarker = function (latitude,longitude) {
	var point = new GLatLng(latitude, longitude);
	marker = new GMarker(point,{icon:this.tinyIcon});
	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 });
}


 