var map;
var point;
var gdir;
var geocoder = null;
var addressMarker;
var toAddress;
var locale;
var marker;
var directionsPanel;
var directions;

function initGoogleMap(markerText)
{
	locale = "nl_BE";
	
	marker = markerText;
	
	map = new GMap2(document.getElementById("map_canvas"));
	gdir = new GDirections(map, document.getElementById("route"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);

	map.setCenter(new GLatLng(50.883117, 4.067266), 13);
	point = new GLatLng(50.883117, 4.067266);
	map.addOverlay(new GMarker(point));
	map.addControl(new GLargeMapControl());
	
	map.openInfoWindowHtml(point, markerText);

	map.addMapType(G_NORMAL_MAP);
	map.addMapType(G_SATELLITE_MAP);
	map.addMapType(G_HYBRID_MAP);
	map.addMapType(G_PHYSICAL_MAP);

	map.addControl(new GMapTypeControl());	

	gdir = new GDirections(map, $('#google_direction').val());
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);

	map.addControl(new GLargeMapControl());

	map.addMapType(G_NORMAL_MAP);
	map.addMapType(G_SATELLITE_MAP);
	map.addMapType(G_HYBRID_MAP);
	map.addMapType(G_PHYSICAL_MAP);
}

function planRoute()
{
	var fromAddress = $('#google_direction').val();
	
	if(fromAddress != '')
	{
		//clear all the overlays
		map.clearOverlays();
		
		var addresses = "from: " + fromAddress + " to: " + toAddress;
		
		//load the route on the map by showing the line
		gdir.load(addresses, { "locale": locale });
		
		//load the directions to view a textual description
		directionsPanel = document.getElementById("textual_div");
		directions = new GDirections(map, directionsPanel);
		directions.load(addresses, { "locale": locale });
	}
}

function handleErrors()
{
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	{
		alert('Meerdere mogelijkheden gevonden, verfijn de zoekopdracht.');
	}
	
	resetRouteCalculator();
}

function onGDirectionsLoad()
{
	//plaats hier acties tijdens het laden van de route
	resetRouteCalculator();
}

function resetRouteCalculator()
{
	map.openInfoWindowHtml(point, marker);
}