var geocoder;
var map;

var latitude;
var longitude;
var marker = null;
var newMarker = null;

function initialize()
{
	var gmapNomVille = document.getElementById("gmapNomVille").value;
	var gmapAdresse = document.getElementById("gmapAdresse").value;
	var gmapNomSalle = document.getElementById("gmapNomSalle").value;

	geocoder = new google.maps.Geocoder();
	
	var myOptions =
	{
		zoom: 12,
		disableDefaultUI: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
	var address = "FRANCE " + gmapNomVille + " " + gmapAdresse + " " + gmapNomSalle;
	geocoder.geocode({'address': address},
	function(results, status)
	{
		if (status == google.maps.GeocoderStatus.OK)
		{
			map.setCenter(results[0].geometry.location);
			
			latitude = results[0].geometry.location.lat();
			longitude = results[0].geometry.location.lng();
			myLatlng = new google.maps.LatLng(latitude, longitude);
			
			marker = new google.maps.Marker({
						position: myLatlng,
						map: map
			});
		}
		else
		{
			// Si on ne trouve pas , on reteste sans le nom de la salle
			var address = "FRANCE " + gmapNomVille + " " + gmapAdresse;
			geocoder.geocode({'address': address},
			function(results, status)
			{
				if (status == google.maps.GeocoderStatus.OK)
				{
					map.setCenter(results[0].geometry.location);
									
					latitude = results[0].geometry.location.lat();
					longitude = results[0].geometry.location.lng();
					myLatlng = new google.maps.LatLng(latitude, longitude);
					
					marker = new google.maps.Marker({
								position: myLatlng,
								map: map
					});
				}
				else
				{
					alert("Adresse non trouvée");
				}
			});
		}
	});
	
}




function loadScript()
{
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	document.body.appendChild(script);
}
