  var map;
  var gdir;
  var geocoder = null;
  var addressMarker;
  var point;
  var locale;	
  var pinurl = "/gfx/pin.png";
  var office_prefix_url = "/files/google-maps/peak-it-kantoor-";
  var icon = new GIcon(G_DEFAULT_ICON,pinurl);
  icon.iconSize = new GSize(35, 40);  	
  var office = [
		
    {
      name: "Naarden",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "Gooimeer 6-19, 1411 DD Naarden",
          telephone: "035 699 6500",
		  image:  office_prefix_url + "naarden.jpg",
          Point: {
            coordinates: [5.1381382, 52.3074133, 0]
          },
          AddressDetails: {
            Country: {
              CountryNameCode: "NL",
              AdministrativeArea: {
                AdministrativeAreaName: "Noord-Holland",			  
              	Locality: {LocalityName: "Naarden"}
			  }
            }
          }
        }
      ]
    },
    {
      name: "Rijswijk",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "Veraartlaan 6, 2288 GM Rijswijk",
          telephone: "070 327 3548",
		  image:  office_prefix_url + "rijswijk.jpg",
          Point: {
            coordinates: [4.336998, 52.04071, 0]
          },
          AddressDetails: {
            Country: {
              CountryNameCode: "NL",
              AdministrativeArea: {
                AdministrativeAreaName: "Zuid-Holland",			  
              	Locality: {LocalityName: "Rijswijk"}
			  }
            }
          }
        }
      ]
    }
  ];
  
  function showLocation(coordinates, icon) {
	  var map_ctrl=new GLargeMapControl();
	  var map_type_ctrl=new GMapTypeControl();
	  var map_scale_ctrl=new GScaleControl();
//Adding a navigation control */
	  map.addControl(map_ctrl);
//Buttons for choosing map type */
	  map.addControl(map_type_ctrl);
//Adding a ruler */
	  map.addControl(map_scale_ctrl);
	  map.setCenter(coordinates, 16); 
	  marker = new GMarker(coordinates, icon);

  }
	
  function initialize() {
	  if (GBrowserIsCompatible()) 
		  {		  
			 locale = 'nl_NL';					
      		 map = new GMap2(document.getElementById("map_canvas"));
			   
	  var map_ctrl=new GLargeMapControl();
	  var map_type_ctrl=new GMapTypeControl();
	  var map_scale_ctrl=new GScaleControl();
	  map.addControl(map_ctrl);
	  map.addControl(map_type_ctrl);
	  map.addControl(map_scale_ctrl);

			 gdir = new GDirections(map, document.getElementById("route"));
			 GEvent.addListener(gdir, "load", onGDirectionsLoad);		
			 GEvent.addListener(gdir, "addoverlay", onGDirectionsPlaced); 
			 GEvent.addListener(gdir, "error", handleErrors); 
			 
			 map.setMapType(G_NORMAL_MAP);//G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP
		  	 map.setCenter(new GLatLng(52.25,4.715), 9);
			 			 
      		 geocoder = new GClientGeocoder();
			 geocoder.setCache(new CapitalOfficesCache());	  
  
			 for (i=0;i<=office.length;i++)
			 	placeMarker(i);

		  }	  
  }
	
//*********************************************************************

  // CapitalCitiesCache is a custom cache that extends the standard GeocodeCache.
  // We call apply(this) to invoke the parent's class constructor.
  function CapitalOfficesCache() {
    GGeocodeCache.apply(this);
  }

  // Assigns an instance of the parent class as a prototype of the
  // child class, to make sure that all methods defined on the parent
  // class can be directly invoked on the child class.
  CapitalOfficesCache.prototype = new GGeocodeCache();

  // Override the reset method to populate the empty cache with
  // information from our array of geocode responses for capitals.
  CapitalOfficesCache.prototype.reset = function() {
    GGeocodeCache.prototype.reset.call(this);
    for (var i in office) {
      this.put(office[i].name, office[i]);
    }
  }

  function showAddressOnMap(response) {
    map.clearOverlays();
    if (response && response.Status.code != 200) {
      alert("Unable to locate " + decodeURIComponent(response.name));
    } else {
	  
	  var icon2 = new GIcon(G_DEFAULT_ICON,pinurl);
	  icon2.iconSize = new GSize(27, 36);
	  icon2.shadowSize = new GSize(1, 1);
	  	
      var place = response.Placemark[0];
      var point = new GLatLng(place.Point.coordinates[1],
                              place.Point.coordinates[0]);
	  
      showLocation(point, icon2);
      marker = new GMarker(point, icon2); 
	  map.addOverlay(marker);
	  
			 for (i=0;i<=office.length-1;i++)
			 	{
					if (office[i].name == response.name) {which=i+1}
				}

	  var infoWindowContent = '<p class="address"><img src='+place.image+' class="google_map_popup_image" align="left" alt="PEAK-IT '+response.name+'"><b>PEAK-IT '+response.name+'</b><br>'+place.address.replace(", ", "<br />")+'<br />Tel.: '+place.telephone+'<br /><form id="Form1" action="#" onsubmit="planRoute(this.address.value,\''+place.address+'\','+which+'); return false" method="post"><span class="address">Vertrekpunt: </span><input id="address" type="text" onclick="this.select()" value="" name="address"/><input type="submit" value="go" class="submit" /></form>'+'</p>';	  
	  map.openInfoWindowHtml(point, infoWindowContent);
	  GEvent.addListener(marker, "click", function() {map.openInfoWindow(point,infoWindowContent);});
	  document.getElementById("extra_navigation").style.visibility = 'visible';
	  var officeName;
	  officeName = response.name;
	  officeName = officeName.toLowerCase();
	  officeName = officeName.replace(" ", "_");
    }
  }

  function findOffice(which) {
    if (which != 0) {
	  geocoder.getLocations(office[which - 1].name, showAddressOnMap);

document.getElementById("extra_navigation").innerHTML = '<select onchange="findOffice(this.selectedIndex);"><option>&nbsp;kies een andere vestiging </option><option>Naarden</option><option>Rijswijk</option></select>';	  
document.getElementById("extra_navigation").style.visibility = 'visible';
document.getElementById("route").innerHTML = '';

if (document.getElementById("print_route")) document.getElementById("print_route").style.visibility = 'hidden';
    }
  }
  
  function placeMarker(which) {
    if (which != 0) {     
	  var icon2 = new GIcon(G_DEFAULT_ICON,pinurl);
	  icon2.iconSize = new GSize(27, 36); 
	  icon2.shadowSize = new GSize(1, 1);	  

	  var place = office[which - 1].Placemark[0];
      var point = new GLatLng(place.Point.coordinates[1],
                              place.Point.coordinates[0]);	
      marker = new GMarker(point, icon2); 
	  var offset = new GSize(20,-20);
	  var infoWindowContent = '<p class="address"><img src='+place.image+' class="google_map_popup_image" align="left" alt="PEAK-IT '+office[which - 1].name+'"><b>PEAK-IT '+office[which - 1].name+'</b><br>'+place.address.replace(", ", "<br />")+'<br />T: '+place.telephone+'<br /><a href="javascript:void(0);" onclick="javascript: findOffice('+which+');">Klik hier voor een routebeschrijving</a></p>';
	  GEvent.addListener(marker, "click", function() { map.openInfoWindow(point,infoWindowContent, { pixelOffset: offset }) 
													   var officeName;
													   officeName = office[which - 1].name;
													   officeName = officeName.toLowerCase();
													   officeName = officeName.replace(" ", "_");
													//   document.getElementById("/extra_navigation_contactform").innerHTML = '<a href="peak-it-contactform.asp?kantoor='+officeName+'">contactformulier</a>';						  
																		  });    	   
	  map.addOverlay(marker);	
  
    }
  }
  
  function openMarkerInfoWindow(which) {
    if (which != 0) {     

var icon2 = new GIcon(G_DEFAULT_ICON,pinurl);
	  icon2.iconSize = new GSize(27, 36);
	  icon2.shadowSize = new GSize(1, 1);  

	  var place = office[which - 1].Placemark[0];
      var point = new GLatLng(place.Point.coordinates[1],
                              place.Point.coordinates[0]);	
      marker = new GMarker(point, icon2); 
	  map.addOverlay(marker);
	  var offset = new GSize(20,-20);
	  var infoWindowContent = '<p class="address"><img src='+place.image+' class="google_map_popup_image" align="left" alt="PEAK-IT '+office[which - 1].name+'"><b>PEAK-IT '+office[which - 1].name+'</b><br>'+place.address.replace(", ", "<br />")+'<br />Tel.: '+place.telephone+'<br /><form action="#" onsubmit="planRoute(this.address.value,\''+place.address+'\','+which+'); return false" method="post"><span class="address">Vertrekpunt: </span><input id="address" type="text" onclick="this.select()" value="" name="address"/><input type="submit" value="go" class="submit" /></form>'+'</p>';  
	  marker.openInfoWindow(infoWindowContent, { pixelOffset: offset });
	  GEvent.addListener(marker, "click", function() {marker.openInfoWindow(infoWindowContent); 
													  var officeName;
													  officeName = office[which - 1].name;
													  officeName = officeName.toLowerCase();
													  officeName = officeName.replace(" ", "_");
													//  document.getElementById("extra_navigation_contactform").innerHTML = '<a href="peak-it-contactform.asp?kantoor='+officeName+'">contactformulier</a>';																			
													  });
	  
    }
  }   
  
  function openWin3(fromAddress,toAddress,telNumber) {
  myWin= open("", "displayWindow","width=500,height=400,scrollbars=yes,status=yes,toolbar=yes,menubar=yes");
	var re = /\b[A-Z]{2}\b/;
	var m = re.exec(toAddress);
	
	if (m == null) strToWhere = ''
	else {
			var posFirst = m.index+3;
			var posLast = toAddress.length;
			var city = toAddress.substring(posFirst,posLast);
			strToWhere = " naar PEAK-IT kantoor "+city;
	}


  toAddress = toAddress.replace(', ','<br>')
  // open document object for further print 
  myWin.document.open();
  
  // create new document 
  myWin.document.write("<html><head><title>Routebeschrijving"+strToWhere+"");
  myWin.document.write("</title></head><body>");
  myWin.document.write("<br>Routebeschrijving"+strToWhere+":<br>");
  myWin.document.write(toAddress+"<br>");
  myWin.document.write("Tel.: "+telNumber+"<br>");
  myWin.document.write(document.getElementById("route").innerHTML);
  myWin.document.write("</body></html>");

  // close document - (but not the window)
  myWin.window.print();
  myWin.document.close(); 
  myWin.window.close(); 
}

	  
  function planRoute(fromAddress,toAddress,which) {
	if (which != 0) { 
	
		map.clearOverlays();
		var place = office[which - 1].Placemark[0];
    	var toAddress = place.address;
		var telNumber = place.telephone;
		gdir.load("from: " + fromAddress + " to: " + toAddress, { locale: locale });
document.getElementById("extra_navigation").innerHTML = '<select onchange="findOffice(this.selectedIndex)"><option>&nbsp;kies een andere vestiging </option><option>Amsterdam</option><option>Den Bosch</option><option>Den Haag</option><option>Naarden</option><option>Utrecht</option></select> | <a href="javascript:void(0);" onclick="javascript:openMarkerInfoWindow('+which+');">route vanuit een ander vertrekpunt berekenen</a>'+' | <a id="print_route" href="javascript:void(0);" onclick="openWin3(\''+fromAddress+'\',\''+toAddress+'\',\''+telNumber+'\');">routebeschrijving afdrukken</a>';
	}
  }

  function planRouteSimple(fromAddress,toAddress,which) {
	map.clearOverlays();
	gdir.load("from: " + fromAddress + " to: " + toAddress, { locale: locale });	
  }  


  function handleErrors() {
    if (which != 0) {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("Het opgegeven adres kon niet teruggevonden worden, dat kan te wijten zijn aan het feit dat het een recent of onjuist adres is");
		openMarkerInfoWindow(which); 
		document.getElementById("print_route").style.visibility = 'hidden';
	
}
	else if (document.getElementById("address").value=="")
		alert('Gelieve een adres op te geven...');
		 else 
			alert("Unknow error");
    } 
  }
			
  function onGDirectionsLoad() {
		//here the actions can be placed which will be performed during route loading

  }

  function onGDirectionsPlaced() {
  	if (gdir.getStatus().code == 200){
  		var route_height = document.getElementById("route").offsetHeight;
  		var old_height = document.getElementById("links").offsetHeight;
  		var new_height = old_height + route_height;
  		document.getElementById("links").style.height = new_height + 'px';
  		document.getElementById("content").style.height = new_height + 'px';
  		document.getElementById("footer").style.top = new_height - 25 + 'px';
  	}
  }  
