
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(51.581118, 5.146675), 7);
        
        // Set locations
        loc[0] = {
            name: 'Mixed Hockey Club Berkel-Enschot',
            address: 'Generaal Eisenhowerweg 11<br /> 5056 CR, Berkel-Enschot',
            coord: new GLatLng(51.581118, 5.146675)
        };


        //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("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	      else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
	        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
	      
	      else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
	        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
	
	   //   else if (directions.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: " + directions.getStatus().code);
	        
	      else if (directions.getStatus().code == G_GEO_BAD_KEY)
	        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
	
	      else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	        alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	       
	      else alert("An unknown error occurred.");

		});
	}
}

window.onload = initialize;

// 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
// Calculate Route
function calcRoute()
{
   // clear traces
   directions.clear();
   document.getElementById('routeDesc').innerHTML = '';
   
   // Set width of left/right
   document.getElementById('printRoute').style.display='inline';
   document.getElementById('right-content').style.height='400px';
         
   // 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());
}

// Function to print the route
function printRoute()
{
   var disp_setting="toolbar=no,location=no,directories=no,menubar=no,";
   disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
   var content_vlue = document.getElementById("routeDesc").innerHTML;
   var docprint=window.open("","",disp_setting);

   docprint.document.open();
   docprint.document.write('<html><head><title>Mixed Hockey Club Berkel-Enschot</title>');

   docprint.document.write('</head><body onLoad="window.print();"><center><h1>Routebeschrijving naar Mixed Hockey Club Berkel-Enschot</h1>');
   docprint.document.write(content_vlue);
   docprint.document.write('</center></body></html>');
   docprint.document.close();
   docprint.focus();
} 
