var map;
var directionsDisplay;
var directionsService;

function initialize(position, zoome) {
	// Instantiate a directions service.
	directionsService = new google.maps.DirectionsService();

	// Options 
	var mapOptions = {
		zoom: zoome,
		center: position,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false
	};

	// Map
	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

	var rendererOptions = {
		map: map
	}

	directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	directionsDisplay.setPanel(document.getElementById("panel"));
	
}


function itineraire(id) {
	
	$("#lien29").after('<p>Vous venez de :<br/><input type="text" class="map" id="input' + id + '" /><button onclick="calculItineraire(' + id + ')">Ok</button></p>');
	$("#form_itineraire input[type=text]").focus();
}
			
function calculItineraire(id) {

	var adresseFrom = $("#input" + id).val();
	var adresseTo = new google.maps.LatLng($("#lien" + id).attr('rel'), $("#span" + id).html());
	
	setDirections(adresseFrom, adresseTo);
}


function setDirections(fromAddress, toAddress) {

	if(fromAddress == "" || toAddress == "") {
		alert("Les champs doivent être renseignés.");
		return false;
	}
	
	var request = {
		origin: fromAddress,
		destination: toAddress,
		travelMode: google.maps.TravelMode.DRIVING // WALLING ou BICYCLING aussi...
	};

	// Route the directions and pass the response to a
	// function to create markers for each step.
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			/*var warnings = document.getElementById("warning");
			warnings.innerHTML = "" + response.routes[0].warnings + "";*/
			directionsDisplay.setDirections(response);
			//showSteps(response);
			//alert("ok");
		}
		else {
			alert(status + " : " + fromAddress + "\n\n" + toAddress);
		}
	});

	$("#form_itineraire").html(" ");
}

/*
function showSteps(directionResult) {
  // For each step, place a marker, and add the text to the marker's
  // info window. Also attach the marker to an array so we
  // can keep track of it and remove it when calculating new
  // routes.
  var myRoute = directionResult.routes[0].legs[0];

	for (var i = 0; i < myRoute.steps.length; i++) {
		var marker = new google.maps.Marker({
			position: myRoute.steps[i].start_point,
			map: map
		});
		attachInstructionText(marker, myRoute.steps[i].instructions);
		markerArray[i] = marker;
	}
}

function attachInstructionText(marker, text) {
	google.maps.event.addListener(marker, 'click', function() {
		stepDisplay.setContent(text);
		stepDisplay.open(map, marker);
	});
}
*/
