function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;

var MapData;
var map;
var marker;
//var locale = 'nl';

var appWidth = '410';

var formLabel = 'Toon route vanaf:';
var formButton = 'Toon routebeschrijving';

var address = 'Stationsplein 63, 1012 AB Amsterdam, The Netherlands';

$(window).unload(function(){ GBrowserIsCompatible() && GUnload(); });

/* 	Jquery voor de googlemap */
$(document).ready(function(){ 
	
	formLabel = $('span#translations span.sRoutefrom').html();
	formButton = $('span#translations span.sShowRoute').html();

	// insert de map container
	$('#mapcontainer')
	.append(
		$('<div/>')
		.attr('id','googlemap')
	);
	
	$('#routecontainer')
	.prepend(
		// prep directions container
		$('<div/>')
		.attr('id','directions')
		.css({
			'width':appWidth+'px'
		})
	);
	
	// addressdata
	MapData = new Array();
				
	MapData['address'] = address;
	
	// ====== Create a Client Geocoder ======
	var geo = new GClientGeocoder(); 
	
	geo.getLocations(address,function(result){
					
		var p = result.Placemark[0].Point.coordinates;
		
		MapData['latitude'] = p[1];
		MapData['longitude'] = p[0];
		
		processLocation(MapData);
		
	});

});

function processLocation(MapData) {
	
	// formulier elementen
	var labelfor = $('<label/>')
					.attr('for','toInput')
					.html(formLabel);
	
	var inputfrom = $('<input/>')
					.attr('id','toInput')
					.attr('size','40')
					.attr('name','from')
					.attr('type','text')
					.addClass('text');
					
	var submitbutton = $('<input/>')
					.val(formButton)
					.addClass('submit')
					.attr('name','submit')
					.attr('type','button')
					.mousedown(function(){
						
						// bij submit doe functie
						if(inputfrom.val() != ''){
							$('#directions').html('');
							setDirections(inputfrom.val());
						}
						return false;
						
					});
	
	var theform = 	$('<div/>')
					.css({
						'display':'inline-block'
					})
					.addClass('addressdata')
					.append(
						// "directions" - formuliertje
						$('<form/>')
						.attr('action','#')
						.attr('onsubmit',"return false;")
						.html(
							labelfor
						)
						.append(
							inputfrom
						)
						.append(
							submitbutton
						)
						.attr('id','directionform')
						.submit(function(){
							return false;
						})
					);
	
	$('#routecontainer')
	.prepend(
		$(theform)
		.submit(function(){
			return false;
		})
	);
	
	// bouw DOM binnen map container
	$('#googlemap')
	.append(
		$('<div/>')
		.attr('id','map')
		.addClass('googlemap')
		.css({
			'width':appWidth+'px',
			'height':appWidth/2+'px'
		})
	);

		
	
		// de googlemap voor deze address-data
		
	
	// initialize API  voor deze address-data
	initialize();
	
}

// directions voor één van de locaties
function setDirections(fromAddress) 
{	
	if (GBrowserIsCompatible()) 
	{	
		//showFullMap();
		
		// unload API
		map.clearOverlays();
		
		// maar nu met directions
		gdir	 = new GDirections(map, document.getElementById("directions"));
		
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		
		//gdir.load("from: " + fromAddress + " to: " + MapData.address,{"locale":locale});
		gdir.load("from: " + fromAddress + " to: " + MapData.address);
		//gdir.addControl(new GLargeMapControl());
		
		return false;
	}
}

function showFullMap() 
{		
	if (GBrowserIsCompatible()) 
	{	
		// unload API
		GUnload();
		
		// prep map div container
		$('#googlemap')
		.css({
			'width':appWidth+'px',
			'height':appWidth+'px',
			//'margin':'5px',
			'padding':'0',
			'display':'inline-block',
			'clear':'both'
		})
		//.after(addressForm);
	
		// opnieuw kaart
		lanlng	 = new GLatLng(MapData.latitude,MapData.longitude);
		map		 = new GMap2(document.getElementById("googlemap"));
		marker	 = new GMarker(lanlng,2);
		
		// center
		map.setCenter(lanlng, 12);
		//map.addControl(new GLargeMapControl());
		map.addControl(new GSmallMapControl());
		map.addOverlay(marker);
		
		// infowindow open
		//marker.openInfoWindowHtml(MapData.html);
		
	}
}

function initialize() {	
	if (GBrowserIsCompatible()) 
	{
		lanlng	 = new GLatLng(MapData.latitude,MapData.longitude);
		map		 = new GMap2(document.getElementById("map"));
		marker	 = new GMarker(lanlng,2);
		
		map.setCenter(lanlng, 11);
		map.addOverlay(marker);
		map.addControl(new GLargeMapControl());
		
		//GEvent.addListener(marker,'click',function() {
		//	showFullMap();  
		//});
		
	}

}

function handleErrors()
{
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		alert("No corresponding geographic location could be found for the specified address. Please be more specific.");
		//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: " + gdir.getStatus().code);
		return false;
	}
	else if (gdir.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: " + gdir.getStatus().code);
	else if (gdir.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: " + gdir.getStatus().code);
	else if (gdir.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: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	else alert("An unknown error occurred.");
}

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

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}