var map = null;
var geocoder = null;
var bounds = null;
var directionsPanel;
var directions;

function initialize_map(zoom, dirs) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));	
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(0, 0), zoom);
		geocoder = new GClientGeocoder();
		bounds = new GLatLngBounds();

		if(dirs && dirs != "") {
		  directionsPanel = document.getElementById("directions");
		  directions = new GDirections(map, directionsPanel);
		// Load directions (if any) and set our locale
		  directions.load(dirs, {"locale": "nl_NL"});
		// Add event listeners. These don't do anything yet, may change.
		  GEvent.addListener(dirs, "load", onGDirectionsLoad);
		  GEvent.addListener(dirs, "error", handleErrors);
		}
	}
}

function showAddress(zoom, address, name, id, index) {
	if (geocoder) {
		geocoder.getLatLng(address,
			function(point) {
				if (!point) {
					// do nothing yet.
				}
				else {
					bounds.extend(point);

					var baseIcon = new GIcon(G_DEFAULT_ICON);
					baseIcon.shadow = "";
					// We need to specify icon size, otherwise default size will be used which isn't same size as our own custom icons. 
					baseIcon.iconSize = new GSize(26, 32);
					baseIcon.iconAnchor = new GPoint(9, 34);
					baseIcon.infoWindowAnchor = new GPoint(9, 2);
					var numberIcon = new GIcon(baseIcon);
					if(index) {
						// Set number icon
						numberIcon.image = "/images/mapfiles/" + index + ".png";
					}
					else {
						numberIcon.image = "http://www.google.com/mapfiles/marker.png";
					}
					
					markerOptions = { icon:numberIcon }
					// Create our customer new marker
					var marker = new GMarker(point, markerOptions);
										
					map.addOverlay(marker);

					map.setCenter(bounds.getCenter());	
					map.setZoom(map.getBoundsZoomLevel(bounds));

					GEvent.addListener(marker, "click", function() {
						addr = address.split(", ")
						marker.openInfoWindowHtml(
							"<h4>"+name+"</h4>"+addr[0]+"<br />"+addr[1]+"<p style='text-align: center;'><a href='/bedrijf/"+id+"'>Toon bedrijf</a></p>"
						);
					});
				}
			}
		);
	}
}

function handleErrors(){
	// Error messages need to be translated.
   // 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_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.");
	   
}

function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.
}
	

function showCoordinate(latitude, longitude, address, id) {
	var point = new GLatLng(latitude, longitude);

	bounds.extend(point);

	var marker = new GMarker(point);
	map.addOverlay(marker);

	map.setCenter(bounds.getCenter());	
	map.setZoom(map.getBoundsZoomLevel(bounds));

	GEvent.addListener(marker, "click", function() {
		addr = address.split(", ")
		marker.openInfoWindowHtml(address);
	});
}