var gdir;
var geocoder = null;	
var MaCarte = null;
				
function afficheDirection(longitude,latitude,adress){
	if(gdir){gdir.clear();}
	//CarteItineraire = new GMap2(document.getElementById("EmplacementCarteItineraire"));
	gdir = new GDirections(MaCarte, document.getElementById("EmplacementTexteItineraire"));
	gdir.load("from: "+longitude+","+latitude+ " to: " + adress,	{ "locale": 'FR' });
	return false;
}
			
//afficheDirection('47.394921','0.680401',"METZ");
function loadcarte() {
	if (GBrowserIsCompatible()) {
		MaCarte = new GMap2(document.getElementById("lacarte"));
		geocoder = new GClientGeocoder();
		
		MaCarte.addControl(new GMapTypeControl());MaCarte.addControl(new GSmallMapControl());
		//MaCarte.setCenter(new GLatLng(37.4419, -122.1419), 13,G_NORMAL_MAP);
	}
	else
		{
		//alert('Votre navigateur n\'est pas compatible avec GoogleMap');
	}
}
					
function createMarker(point,html) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		});

	return marker;
}


function geocode(address,html,type_truc) {
	geocoder.getLatLng(
	address,
	function(point) {
		if (!point) {
			//alert(address + " non trouvï¿½ sur la carte");
			} 
		else {
			MaCarte.setCenter(point, 16);
			var marker = createMarker(point, html);
			
			var center = marker.getPoint();
			var gpscenter = center.toString();

			var TableauPositionMaxiCarte = gpscenter.split(',');


			var latitude = TableauPositionMaxiCarte[0].replace('(', '');
			var longitude= TableauPositionMaxiCarte[1].replace(')', '');

			

			MaCarte.addOverlay(marker);
			if(type_truc=='simple'){
				marker.openInfoWindowHtml(html);
			} else {
				//afficheDirection(LongitudeCarteClick,LatitudeCarteClick,this.from.value);
				//alert(LongitudeCarteClick);
				//alert(LatitudeCarteClick);
				var formulaire_itineraire ='<div style="width:350px; height:200px; border: 1px solid #ccc; background:#efffef; font: 1em \'trebuchet ms\',verdana, helvetica, sans-serif;">' +
				'<form action="#" onsubmit="alert(this.latitude.value);alert(this.longitude.value);afficheDirection(this.longitude.value,this.latitude.value,this.from.value);return false;">' +
				'	<table align="center">' +
				'		<tr>' +
				'			<td colspan="2" align="center"><b>Indiquez votre adresse de d&eacute;part ci-dessous</b></td>' +
				'		</tr>' +
				'		<tr>' +
				'			<td colspan="2" align="center"><input type="text" size="25" id="fromAddress" name="from" value="" 		style="width:300px; height:20px; border:1px solid grey;" /></td>' +
				'		</tr>' +
				'		<tr>' +
				'			<td colspan="2" align="center"><b>Adresse d\'arriv&eacute;e</b></td>' +
				'		</tr>' +
				'		<tr>' +
				'			<td colspan="2" align="center">societe<br />adresse<br />code postal ville</td>' +
				'		</tr>' +
				'		<tr>' +
				'			<td align="right" colspan="2" align="center"><input type="hidden" id="longitude" name="longitude" value="'+longitude+'" /><input type="hidden" id="latitude" name="latitude" value="'+latitude+'" /></td>' +
				'		</tr>' +
				'		<tr>'  +
				'			<td colpsan="2">'  +
				'				<input name="submit" type="submit" value="Afficher l\'itin&eacute;raire" />' +
				'			</td>' +
				'		</tr>' +
				'	</table>' +
				'</form>' +
				'</div>';
				
				marker.openInfoWindowHtml(formulaire_itineraire);
				}
			}
		}
	);
return false;
}

