    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

function initialize() {
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));

		map.setCenter(new GLatLng(51.81825887922792, 4.699353575706482),14);
		//var gr=new GMapType(G_PHYSICAL_MAP.getTileLayers(),G_PHYSICAL_MAP.getProjection(),"Relief",{shortName:"Rel"});
		//map.addMapType(gr);
		
        gdir = new GDirections(map, document.getElementById("route"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        //setDirections("3313cs", "", "en_US");
		map.enableScrollWheelZoom();
		

		//map.addControl(new GLargeMapControl());										// Dit laat Zoom zien MET slidebar (linksboven)
		//map.addControl(new GSmallMapControl());										// Dit laat Zoom zien zonder slidebar (linksboven)
		//map.addControl(new GMapTypeControl());										// Dit laat Kaart, Sateliet en Beide zien (rechtsboven)
		//map.addControl(new GScaleControl());											// Dit laat de schaal zien (linksonder)
		//map.addControl(new GOverviewMapControl()); 									// Dit is het kleine landkaartje (rechtsonder)
        //map.addControl(new GSmallMapControl());
        //map.addControl(new GMapTypeControl());
		//map.setMapType(G_NORMAL_MAP);													// Dit zet de kaart op beide - G_NORMAL_MAP  -G_HYBRID_MAP
        		
		
		
		
		/*directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);
		directions.load("from: 2951TC to: 3313CS");
		*/
		
		// Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
		function createMarker2(point,index,sText,sRouteDir) {
		 	var sLabelText = sText;
			var letteredIcon = new GIcon();
				letteredIcon.image = "" + baseUrl + "/googlemaps/images/point.png";
				letteredIcon.iconSize = new GSize(20, 20);
				letteredIcon.shadowSize = new GSize(20, 20);
				letteredIcon.iconAnchor = new GPoint(10, 10);
				letteredIcon.infoWindowAnchor = new GPoint(10, 10);
			
			var letteredIcon2 = new GIcon(letteredIcon);
				letteredIcon2.image = "" + baseUrl + "/googlemaps/images/point3.png";

			markerOptions = { icon:letteredIcon };
			var marker = new GMarker(point,markerOptions);
			
			//if(sPostcode != 'null'){
				//alert(sPostcode);
			//}
			GEvent.addListener(marker, 'click', function(){
				//alert(sText);
				
			});
		
			
			GEvent.addListener(marker, 'mouseover', function(){ 
			});


			GEvent.addListener(marker, "mouseout", function() {
				//map.closeExtInfoWindow()
			});
			
			return marker;
		}
		
		
		GEvent.addListener(map, "moveend", function(){
			var center = map.getCenter();					// Zet het center van kaart in variable
			document.getElementById("centerPosition").innerHTML = center.toString();		// Schrijf variable naar div message
		});
		
		var mts = map.getMapTypes();
		for(var i = 0; i < mts.length; i++) {
			//alert(mts[i].getName())
			//var text = "   " + mts[i].getName();
			//selMap.options[selMap.options.length] = new Option(text, i);
		}
		
		
		
		
        // Download the data in data.xml and load it on the map. The format we
        // expect is:
        // <markers>
        //   <marker lat="37.441" lng="-122.141"/>
        //   <marker lat="37.322" lng="-121.213"/>
        // </markers>
		
        GDownloadUrl("" + baseUrl + "/googlemaps/data.xml", function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			var alertLine = '';
			var prevLatlng
			
			for (var i = 0; i < markers.length; i++) {
				var latlng = new GLatLng(
					parseFloat(markers[i].getAttribute("lat")),
					parseFloat(markers[i].getAttribute("lng"))
				);
				var sName = markers[i].getAttribute("name");
				var sPostcode = markers[i].getAttribute("postcode");
				var sPlaats = markers[i].getAttribute("plaats");
				var sStraat = markers[i].getAttribute("straat");
				var sRouteDir = sPlaats + "," + sStraat + "," + sPostcode;
				
				map.addOverlay(createMarker2(latlng, i,sName,sRouteDir));
				//map.addOverlay(new GMarker(latlng));
				
				prevLatlng = prevLatlng == undefined ? latlng : prevLatlng;
				alertLine += i + ":" + latlng + "\n" + prevLatlng + "\n\n"
				
				var polyline = new GPolyline([  
							prevLatlng,  
							latlng], "#ff0000", 2);
					
				
				map.addOverlay(polyline);
				
				var value = polyline.getLength();
				value = value / 1000;
				var message = "Length: " + value + " kilometers.";
				//alert(message)
				
				prevLatlng = latlng;
				
				


				
				
				//alert(alertLine)

			}
			

        });
	
	
		function MDControl(){} MDControl.prototype=new GControl();
	
		MDControl.prototype.initialize=function(map) {
			var container = document.createElement("div");
			var selMap = document.createElement("select");
				selMap.style.display = 'none'
			
			// Create options list for map types
			var mts = map.getMapTypes();
			var mapTypeCssNames = ['Map','Satellite','Hybrid','Relief'] 
			
			for(var i = 0; i < mts.length; i++) {
				var text = mts[i].getName();
				selMap.options[selMap.options.length] = new Option(text, i);
				
				
				var name = mts[i].getName();
				var mapTypeCssName = mapTypeCssNames[i];
				var sClassName = 'btn btnBig btn_mapType' + mapTypeCssName;
				var btn = document.createElement("div");
					
					btn.className = sClassName;
					btn.sClassName = sClassName;
					btn.title = mapTypeCssName;
					btn.i = i;
					btn.bActive = false;
					btn.id = 'mapType' + i;
					//btn.appendChild(document.createTextNode(name));
					btn.className += i == 0 ? 'Active' : '';
					btn.bActive = i == 0 ? true : false;
					
					
				GEvent.addDomListener(btn, "click", function() {
					var choose = mts[selMap.options[this.i].value];
					//var choose = 'G_' + aTest[this.i].toUpperCase() + '_MAP';
					map.setMapType(choose);
					
					for(var i = 0; i < mts.length; i++) {
						var sObjectBtnName = document.getElementById('mapType' + i);
						sObjectBtnName.className = sObjectBtnName.sClassName;
						sObjectBtnName.bActive = false;
					}
					this.className += 'Active'
					this.bActive = true;
					
				});
				
				GEvent.addDomListener(btn, "mouseover", function() {
					if(this.bActive != true){
						this.className += 'Hover';
					}
				});
				GEvent.addDomListener(btn, "mouseout", function() {
					if(this.bActive != true){
						this.className = this.sClassName;
					}
		
				});
				
				container.appendChild(btn);
			}
			
	
			container.appendChild(selMap);
			
			map.getContainer().appendChild(container);
			
			return container;
		}
	
	
		MDControl.prototype.getDefaultPosition=function() {
			return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(5,5));
		}


		function btnControl() {}btnControl.prototype = new GControl();
		btnControl.prototype.initialize = function(map) {
		
			var container = document.createElement("div");
				container.className = 'btnHolder'			
			//var aControlNames = ['zoomIn','zoomOut','navUp','navDown','navLeft','navRight'];
			var aControlNames = ['zoomIn','zoomOut'];
			
			var containerCenter = document.createElement("div");
				containerCenter.className = 'btnHolder btnHolder_center';
				container.appendChild(containerCenter);
			

			
			
			for(var i = 0; i < aControlNames.length; i++) {
				var btn = document.createElement("div");
				var aControlName = aControlNames[i];
				btn.className = 'btn btn_' + aControlName;
				btn.title = aControlName;
				btn.i = i;
				
				GEvent.addDomListener(btn, "click", function() {
					//alert(aControlNames[this.i]);
					var functionName = aControlNames[this.i]
					if(functionName == "zoomIn"){ 
							map.zoomIn();
					}else if(functionName == "zoomIn"){ 
							map.zoomIn();
					}else if(functionName == "zoomOut"){ 
							map.zoomOut();
					}else if(functionName == "navUp"){ 
							map.panDirection(0, 1)
					}else if(functionName == "navDown"){ 
							map.panDirection(0, -1)
					}else if(functionName == "navLeft"){ 
							map.panDirection(1, 0)
					}else if(functionName == "navRight"){ 
							map.panDirection(-1, 0)
					} else alert(aControlNames[this.i]);
				});

				containerCenter.appendChild(btn);
			}
			
			/*var testBtn = document.createElement("div");
				//this.setButtonStyle_(testBtn);
				testBtn.className = 'btn btn_Info'

				
				containerCenter.appendChild(testBtn);
				//testBtn.appendChild(document.createTextNode("xx"));
				GEvent.addDomListener(testBtn, "click", function() {
					//map.setZoom(15);
					//map.panTo(new GLatLng(51.86377197456952, 4.670090675354004)); 
					//map.setCenter(new GLatLng(51.86377197456952, 4.67009067535400), 13); 
					//map.panBy(20);
					//map.panDirection(-1, 0)
					//alert(container.innerHTML)
					if(document.getElementById('simple_example_window')){
						alert(document.getElementById('simple_example_window').innerHTML);
					} else {
						//map.panTo(new GLatLng(51.86377197456952, 4.670090675354004))	
						//alert(document.getElementById('map_canvas').innerHTML);
					}
				});
			*/
			map.getContainer().appendChild(container);
			return container;
		}
		
		btnControl.prototype.getDefaultPosition = function() {
			return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
		}
		

		
		
		map.addControl(new MDControl());
		map.addControl(new btnControl());
		
		GEvent.addDomListener(map.getContainer(),(window.addEventListener?"DOMMouseScroll":"mousewheel"), prev);
	}
}

function prev(e) {

	// Prevent from scrolling the page when zooming the map
	if(window.event) { event.returnValue = false; } // IE
	if(e.cancelable) { e.preventDefault(); } // DOM-Standard
}


    
	function toggleMyKml() {
		if (toggleState == 1) {
			map.removeOverlay(marker);
			
			toggleState = 0;
		} else {
			map.addOverlay(geoXml);
			toggleState = 1;
		}
	}

    function setDirections(fromAddress, toAddress, locale) {
		//alert('ss')
      gdir.load("from: " + fromAddress + " to: " + toAddress + ", Netherlands",
                { "locale": locale });
    }

    function handleErrors(){
	   if (gdir.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: " + gdir.getStatus().code);
	   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_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: " + 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...
	}
