	var map = null;
	var geocoder = null;
	var directions = null;
	var correctAddr = null;

	$(document).ready(function(){
		initGoogleMaps();
		
		$("#btn-route").click(function(e){
			e.preventDefault();
			handleRoute();
		});
		
		$("#postcode").keydown(function(e){
			keydown(e);
		});

		var waarde = $("#postcode_input_text").html();
		$("input[name='postcode'").attr('value', waarde);

		initWebcam();	

	});
	
	function initGoogleMaps(){
		map = new GMap2(document.getElementById("landkaart"));
		map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		geocoder = new GClientGeocoder();
		map.setUIToDefault();
		
		showAddress(vestRouteString, true);
	}
	
	function showAddress(address, save) {
		
		geocoder.getLocations(address, function(result) {
			if (result.Status.code == G_GEO_SUCCESS)
			{
				for ( var i = 0; i < result.Placemark.length; i++)
				{
					var vest_point = ""+result.Placemark[i].Point.coordinates;
					var vest_point_arr = vest_point.split(",");
					var vestiging_point = new GLatLng(vest_point_arr[1],vest_point_arr[0]);
					addMarker(result.Placemark[i].address, vestiging_point, "/images/maps-logo.png");
					map.savePosition();
				}
			}
		});
		
		/*
		if (geocoder) {
			geocoder.getLatLng(
			address,
				function(point) {
					if (!point) {
					alert(address + " is niet gevonden op de kaart");
					} else {
						addMarker(address, point, "");
					}
				}
			);
		}
		*/
    }
	
	function getAddress(address){
		geocoder.getLocations(address, function(result){
			if (result.Status.code == G_GEO_SUCCESS)
			{
				var nr_results = result.Placemark.length;
				if (nr_results == 1)
				{
					correctAddr = result.Placemark[0].address;
					showDirections(correctAddr, vestRouteString);
				}
				else
				{
					alert("Er zijn meerdere adressen gevonden met uw opgegeven adres, specificeer uw adres a.u.b");
				}
			}
			else
			{
				alert("Het ingevoerde adres is niet gevonden.");
			}
		});
	}
	
	function addMarker(adres, point, image){
		map.setCenter(point, 13);
		
		var icon = new GIcon();
		icon.image = image;
		icon.iconSize = new GSize(40, 40);
		icon.iconAnchor = new GPoint(0, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		
		
        var marker = new GMarker(point, icon);
        map.addOverlay(marker);
        //marker.openInfoWindowHtml(adres);
	}
	
	function showDirections(from, to) {
		var directionsPanel = document.getElementById("directions");
		directions = new GDirections(map, directionsPanel);
		map.clearOverlays();
		directionsPanel.innerHTML = "";
		
		directions.load("from:"+ from +" to:"+ to);
	}
	
	function handleRoute(){
		var address = $("#postcode").val();

		getAddress(address);
	}
	
function keydown(e) {
  if (e==null) return;
  try 
  {
    var keyCode = e.keyCode;
  }
  catch(e) 
  {
    alert(e.message);
  }
  if (keyCode==null) keyCode=e.which;
  if (keyCode==13) {
    try 
    {
	    if (e.cancelBubble!=null) e.cancelBubble=true;
	    if (e.returnValue!=null) e.returnValue=false;
	    if (e.keyCode!=null) e.keyCode=0;
	    if (e.which!=null) e.which=0;
    }
    catch(e)
    {
    }
    handleRoute();
    return false;
  };
}

function initWebcam(){
	$(".webcamurl").click(function(e){
		e.preventDefault();
		var webcamurl = $(this).attr("href");
		$("body").overlay({
			window_width: 480,
			window_height: 317,
			src: '<iframe width="480" height="317" scrolling="no" frameborder="none" src="'+webcamurl+'"></iframe>'
		});
	});
	
	var urlStr = ""+ window.location;
	urlStr = urlStr.split("#");
	if (urlStr.length > 1)
	{
		var param = urlStr[1];
		if (param == "cam")
		{
			var webcamurl = $(".webcamurl").attr("href");
			if (webcamurl != "")
			{
				$("body").overlay({
					window_width: 480,
					window_height: 317,
					src: '<iframe width="480" height="317" scrolling="no" frameborder="none" src="'+webcamurl+'"></iframe>'
				});
			}
		}
	}
}
