// JavaScript Document
function GRoute(address,lat,lng,fromempty,icon,iconw,iconh,anchorw,anchorh,windoww,windowh) {
	this.address = address;
	this.lat=lat;
	this.lng=lng;
	this.fromempty=fromempty;
	this.icon = icon;
	this.iconw = iconw;
	this.iconh = iconh;
	this.anchorw = anchorw;
	this.anchorh = anchorh;
	this.windoww = windoww;
	this.windowh = windowh;	
	this.geocoder = null;
	this.map='groutemap';
	this.directions='groutedirections';
	this.from='';
	
	this.directionDisplay= null;
	this.directionsService = new google.maps.DirectionsService();
	
	this.loadGmap();
}

GRoute.prototype.load_marker = function(nummer,naam,coord,icon,htmlsrc1,latitude, longitude)
	{
		  // Add markers to the map
		  
		  
		var image = new google.maps.MarkerImage(icon,
			// This marker size
			new google.maps.Size(50, 50),
			// The origin
			new google.maps.Point(0,0),
			// The anchor
			new google.maps.Point(25, 45)
		);
		/*var shape = 
		{
			coord: [1, 1, 1, 20, 18, 20, 18 , 1],
			type: 'poly'
		};*/
		//for (var i = 0; i < vestigingen.length; i++) 
		{
			
			//var vestiging = vestigingen[i];


			var myLatLng = new google.maps.LatLng(latitude, longitude); 
			var cont = htmlsrc1;
			infowindow = new google.maps.InfoWindow({
				content:  cont
			});			
			//alert(i + ' ' + myLatLng);
					
			var marker = new google.maps.Marker({
				clickable: true,
				position: myLatLng,
				map: map,
				icon: image,
				title: naam,
				zIndex: 100+nummer
			});					
			marker.cont = cont;
			marker.nummer = nummer;
			this.attachInfowindow(map,marker);

					
		}
	}
	
GRoute.prototype.attachInfowindow=function(map, marker)
{

	///alert(1);
	google.maps.event.addListener(marker, 'click', function() 
	{ 
		//alert(marker.cont);
		//alert(marker.nummer);
		infowindow.setContent(marker.cont);
		infowindow.open(map,marker); 
	}); 		
}	

GRoute.prototype.loadGmap=function() 
{
	//alert('Gmaploaded');
			this.directionsDisplay = new google.maps.DirectionsRenderer();
			this.geocoder = new google.maps.Geocoder();
			var myLatlng = new google.maps.LatLng(this.lat,this.lng);
			var myOptions = {
				zoom: 11,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			map = new google.maps.Map(document.getElementById(this.map), myOptions);
			//setMarkers(map, vestigingen);
			this.directionsDisplay.setMap(map);  
			this.directionsDisplay.setPanel(document.getElementById(this.directions));	

			var htmlsrc = '<div ><b>Tergooiziekenhuizen Blaricum</b><br>Rijksstraatweg 1<br/>1261AN Blaricum<br/>Nederland<br><br/>(035) 539 1111<br/>informatiecentrum@tergooiziekenhuizen.nl<br/><a href="http://www.tergooiziekenhuizen.nl" target="_blank">www.tergooiziekenhuizen.nl</a><br/><br/></div>';
			
			var marker = this.load_marker(19,'Tergooiziekenhuizen Blaricum','52.2663709, 5.2071056','http://www.tergooiziekenhuizen.nl/upload/Gmaps/icons/buitenpoli.png',htmlsrc,52.2663709, 5.2071056);
			
			//map.addOverlay(marker);			
			
			var htmlsrc = '<div><b>Tergooiziekenhuizen Hilversum</b><br>Van Riebeeckweg 212<br/>1213XZ Hilversum<br/>Nederland<br><br/>(035) 688 7777<br/>informatiecentrum@tergooiziekenhuizen.nl<br/><a href="http://www.tergooiziekenhuizen.nl" target="_blank">www.tergooiziekenhuizen.nl</a><br/><br/></div>';

			marker = this.load_marker(18,'Tergooiziekenhuizen Hilversum','52.2172496, 5.2044505','http://www.tergooiziekenhuizen.nl/upload/Gmaps/icons/zieknhuis.png',htmlsrc,52.2172496, 5.2044505);
			//map.addOverlay(marker);
			//geocoder = new google.maps.Geocoder(); 

			//this.codeAddress("Hugo de Grootlaan 30b, Weesp");

	
};

GRoute.prototype.loaddirections=function() {
	
	
	if(document.getElementById('to1') && document.getElementById('to1').checked)
		var to = document.getElementById('to1').value;
	else if(document.getElementById('to2') && document.getElementById('to2').checked)
		var to = document.getElementById('to2').value;
	else if(document.getElementById('to3') && document.getElementById('to3').checked)
		var to = document.getElementById('to3').value;		
	else if(document.getElementById('to4') && document.getElementById('to4').checked)
		var to = document.getElementById('to4').value;		
	else if(document.getElementById('to5') && document.getElementById('to5').checked)
		var to = document.getElementById('to5').value;
	else if(document.getElementById('to6') && document.getElementById('to6').checked)
		var to = document.getElementById('to6').value;						
	else
	{
		alert('Kies een locatie.');
		return;
	}	
	//alert(to);
	if(to)
	{
		if(to==1)
		{
			this.lat = '52.2172496';
			this.lng = '5.2044505';
			this.naam = 'Tergooiziekenhuizen Hilversum - Van Riebeeckweg 212, 1213 XZ Hilversum, Nederland';			
			
		}
		else if(to==2)
		{
			this.lat = '52.2663709';
			this.lng = '5.2071056';			
			this.naam = 'Tergooiziekenhuizen Blaricum - Rijksstraatweg 1, 1261 AN Blaricum, Nederland';
			
		}
		else if(to==3)
		{
			this.lat = '52.3676609';
			this.lng = '5.1886505';
			//52.367582,5.188637
			//52.367319,5.188283
			this.naam = 'Gezondheidscentrum Medi-Mere, Bartok kliniek - Bartokweg 161, 1311 ZX Almere-stad, Nederland';	
			//alert(this.codeAddress('Bartokweg 161 almere'));
		}		
		else if(to==4)
		{
			this.lat = '52.304593';//'52.304753'; 
			this.lng = '5.03846';//'5.039206'; ,
			this.naam = 'Buitenpoli Weesp - C.J. van Houtenlaan 1b, 1381 CN Weesp, Nederland';//'Buitenpoli Weesp - Hugo de Grootlaan 30b, 1381 DE Weesp, Nederland';			
		}	
		else if(to==5)
		{
			this.lat = '52.2172496';
			this.lng = '5.2044505';
			this.naam = 'CBSL Hilversum - Van Riebeeckweg 212, 1213 XZ Hilversum, Nederland';			
		}
		else if(to==6)
		{
			this.lat = '52.200943'; 
			this.lng = '5.153221';
			this.naam = 'Landgoed Zonnestraal - Loosdrechtse Bos 7, 1213 RH Hilversum , Nederland';			
		}		
			
	}
	
		var dest = this.naam + '@' + this.lat + ',' + this.lng;	
		var dest = new google.maps.LatLng(this.lat,this.lng);
		var from = document.getElementById('groute_from').value;  

		var request = {
			origin:from, 
			destination:dest,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		this.directionsService.route(request, function(response, status) {
		  if (status == google.maps.DirectionsStatus.OK) {
			groute.directionsDisplay.setDirections(response);
		  }
		});
}



GRoute.prototype.codeAddress=function(adres) {
			if (geocoder) {
			  geocoder.geocode( { 'address': adres}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
				alert(results[0].geometry.location); 
				  return results[0].geometry.location; 
				} else {
				  alert("Geocode was not successful for the following reason: " + status);
				  return false;
				}
			  });
			}
		  }
		  
GRoute.prototype.clearadres=function(obj) 
{
	if(obj.value == this.fromempty) 
	{
		obj.value = '';
		//obj.style.color = '000000';
	}
	else if(obj.value=='')
	{
		obj.value = this.fromempty;
		//obj.style.color = 'FF0000';
	}
}		  
		  
var groute = null;
function loadGRoute(address,lat,lng,fromempty,icon,iconw,iconh,anchorw,anchorh,windoww,windowh)
{
	groute=new GRoute(address,lat,lng,fromempty,icon,iconw,iconh,anchorw,anchorh,windoww,windowh);	
}		  
