 var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
  var origin = null;
  var destination = null;
  var waypoints = [];
  var markers = [];
  var directionsVisible = false;

  function initialize(zoomlevel,type) {
			var kaarttype = google.maps.MapTypeId.ROADMAP; 
			if (type == '3')
			   kaarttype = google.maps.MapTypeId.HYBRID
			else if(type == '1')
			   kaarttype = google.maps.MapTypeId.ROADMAP;
			else if (type == '2')
				   kaarttype = google.maps.MapTypeId.SATELLITE;
			
			
	   directionsDisplay = new google.maps.DirectionsRenderer();
    var endpoint = new google.maps.LatLng(_lat, _lon);
    var myOptions = {
      zoom:zoomlevel,
      mapTypeId: kaarttype,
      center: endpoint
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
				if (document.getElementById('directionsPanel'))
				{
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
 			destination = endpoint;
    google.maps.event.addListener(map, 'click', function(event) {
      if (origin == null) {
        origin = event.latLng;
        addMarker(origin);
      } else if (destination == null) {
        destination = event.latLng;
        addMarker(destination);
								
								
      } else {
							clearMarkers();
	      addMarker(destination);
	      origin = event.latLng;
       addMarker(origin);
        /*if (waypoints.length < 1) {
          waypoints.push({ location: destination, stopover: true });
          destination = event.latLng;
          addMarker(destination);
        } else {
          alert("Maximum number of waypoints reached");
        }*/
	      }

							calcRoute();
    });
				}
  }
		
		function addAddress(address)
		{
		   //Als adres gevuld is
					//address = 'voorsterstraat 91, nuth, the netherlands';
    	var geocoder;
   	 geocoder = new google.maps.Geocoder();
     geocoder.geocode( { 'address': address}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
								
							clearMarkers();
	      addMarker(destination);
 						origin = results[0].geometry.location;
	      addMarker(origin);
								
								
								calcRoute();
/*        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });*/;
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

		}
		

  function addMarker(latlng) {
    markers.push(new google.maps.Marker({
      position: latlng, 
      map: map,
      icon: "/images/pushpin.png"
    }));    
  }

  function calcRoute() {
    if (origin == null) {
      alert("Click on the map to add a start point");
      return;
    }
    
    if (destination == null) {
      alert("Click on the map to add an end point");
      return;
    }
    
    var mode = google.maps.DirectionsTravelMode.DRIVING;
    
    var request = {
        origin: origin,
        destination: destination,
        waypoints: waypoints,
        travelMode: mode,
        optimizeWaypoints: true,
        avoidHighways: false,
        avoidTolls: true
    };
    
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
    clearMarkers();
		
    directionsVisible = true;
				
	 }
  
  function updateMode() {
    if (directionsVisible) {
      calcRoute();
    }
  }
  
  function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
      markers[i].setMap(null);
    }
  }
  
  function clearWaypoints() {
    markers = [];
    origin = null;
    destination = null;
    waypoints = [];
    directionsVisible = false;
  }
  
  function reset() {
    clearMarkers();
    clearWaypoints();
    directionsDisplay.setMap(null);
    directionsDisplay.setPanel(null);
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
				if (document.getElementById("directionsPanel"))
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));    
  }

