var directions;
var dir_map;

function getDirections() {
	$("<div id=\"directions_address\" title=\"Get Driving Directions to "+$("#selected_location_title").text()+"\">Starting Address:<br />(street, city, state, zip):<br /><input type=\"text\" name=\"origin_address\" size=\"30\" /></div>").dialog({
		modal: true,
		resizable: false,
		draggable: false,
		buttons: {
			"Get Directions": function() {
				$("#directions").dialog('open');
				$(this).dialog('close');
			}
		}
	});
}

$(document).ready(function() {
	$("body").append("<div id=\"directions\" title=\"Driving Directions to "+$("#selected_location_title").text()+"\"><div id=\"directions_map\"></div><div id=\"directions_route\"></div></div>");
	$("#directions").dialog({
		width: 760,
		height: 476,
		autoOpen: false,
		modal: false,
		resizable: false,
		draggable: true,
		open: function(event, ui) {
			if(GBrowserIsCompatible()) {
				$("#map_sidebar").empty();
				dir_map = new GMap2(document.getElementById("directions_map"));
				dir_map.addControl(new GSmallMapControl());
				dir_map.enableScrollWheelZoom();
				point = new GLatLng(latitude, longitude);
				zoom  = 12;
				dir_map.setCenter(point, zoom);
				directions = new GDirections(dir_map, document.getElementById("directions_route"));
				var waypoints = [];
				waypoints.push($("input[name=origin_address]").val());
				waypoints.push(point);
				directions.loadFromWaypoints(waypoints, {travelMode:G_TRAVEL_MODE_DRIVING});
			}
		}
	});
});