var map;
var directionDisplay;
var directionsService;
var geocoder;

function initialize(latitude, longitude) {
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsService = new google.maps.DirectionsService();
	geocoder = new google.maps.Geocoder();
	
	var latlng = new google.maps.LatLng(latitude, longitude);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("map_directions"));
	
	var marker1 = new google.maps.Marker({
		position: latlng, 
		map: map, 
		title: 'Itabom'
	});
}

function setDirections(latitude,longitude) {	
	var from = document.getElementById('from').value;
	var to = new google.maps.LatLng(latitude, longitude);
	
	if (geocoder) {
		geocoder.geocode({'address': from}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var request = {
					origin: results[0].geometry.location, 
					destination:to,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
				};
				directionsService.route(request, function(result, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(result);
					}
				});
			} else {
				alert(from + " não encontrado");
			}
		});
	}
}
