var map = null;
var geocoder = null;
var loc = new Array();
var bound = null;

var directionsPanel = null;
var directions = null;

// Initialize Google maps
function initialize()
{
	if (GBrowserIsCompatible())
	{
		// Initialize map object
		map = new GMap2(document.getElementById("googlemap"));
		bound = new GLatLngBounds();
        map.setCenter(new GLatLng(52.473833, 5.506582), 7);
        
        // Set locations
        loc[0] = {
        	name: 'de Warandeloop',
        	address: 'Oude Warande 1<br/> 5036 NE Tilburg',
        	coord: new GLatLng(51.563186,5.035721)
        };

        //Set controls
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		map.enableDoubleClickZoom();
		
		// all locations parsing
		for(var index = 0; index < loc.length; index++)
		{
			// bound extention
			bound.extend(loc[index]['coord']);
			
			// marker placement
			createMarker(index);
		}
		
		// zoom to viewport with locations
		//var zoom = map.getBoundsZoomLevel(bound);
		map.setCenter(bound.getCenter(), 11);
		
		// setup directions
		directionsPanel = document.getElementById("routeDesc");
		directions = new GDirections(map, directionsPanel);
		GEvent.addListener(directions,"error",
		function() {
			if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		     alert("De locatie van het opgegeven vertrekpunt kon niet worden bepaald.\nGebruik uw postcode en/of plaats.\nError code: " + directions.getStatus().code);
		   else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
		     alert("Er is iets misgegaan. Maar het is onduidelijk wat!\n Error code: " + directions.getStatus().code);
		   
		   else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
		     alert("Een parameter ontbreekt. Misschien was er geen vertrekpunt ingevoerd.\n Error code: " + directions.getStatus().code);
		
		//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
		//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
		     
		   else if (directions.getStatus().code == G_GEO_BAD_KEY)
		     alert("De Google Maps sleutel is ongeldig of de gebruikte sleutel is niet geldig voor dit domein. \n Error code: " + directions.getStatus().code);
		
		   else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
		     alert("Er is iets misgegaan. Maar het is volstrekt onduidelijk wat de oorzaak is!\n Error code: " + directions.getStatus().code);
		    
		   else alert("An unknown error occurred.");
		});
	}
}
window.setTimeout(initialize, 0);

// Create Marker
function createMarker(index)
{
	var marker = new GMarker(loc[index]['coord']);
	GEvent.addListener(marker, "click",
	function()
	{
		map.openInfoWindowHtml(loc[index]['coord'], '<p><strong>'+loc[index]['name']+'</strong><br>'+loc[index]['address']+'</p>');
	});
	map.addOverlay(marker);
}


// Calculate Route
function calcRoute()
{
	// clear traces
	document.getElementById('routeDesc').innerHTML = '';
	directions.clear();
	
	// get data
	var from = document.getElementById('address').value;
	var toId = 0;
	
	// new directions
	directions.load('from: ' + from + ' to: ' + loc[toId]['coord'].lat() + ', ' + loc[toId]['coord'].lng(), {locale:"nl_nl"});
}
