var bounds;
var icon1;
var iconDefault;
var markerVpoint;
var markerVadres;
var markerV;
var directions;
var markerInfo = new Array;
var markerInfoTemp = new Array;
var markerTeller = 0;
var retryTeller = 0;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmMap"));
		geocoder = new GClientGeocoder();
		map.setCenter(new GLatLng(0,0), 0);
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMenuMapTypeControl());
		map.enableScrollWheelZoom();
		
		bounds = new GLatLngBounds();
		
		icon1 = new GIcon();
		icon1.image = "/template/images/icon3.png";
		icon1.iconSize = new GSize(30, 15);
		icon1.iconAnchor = new GPoint(0, 10);
		icon1.infoWindowAnchor = new GPoint(30, 1);
		
		iconDefault = new GIcon();
		iconDefault.image = "/template/images/icon-default.png";
		iconDefault.iconSize = new GSize(30, 15);
		iconDefault.iconAnchor = new GPoint(0, 10);
		iconDefault.infoWindowAnchor = new GPoint(30, 1);
		
		iconGold = new GIcon();
		iconGold.image = "/template/images/icon-gold.png";
		iconGold.iconSize = new GSize(30, 15);
		iconGold.iconAnchor = new GPoint(0, 10);
		iconGold.infoWindowAnchor = new GPoint(30, 1);
		
		iconSilver = new GIcon();
		iconSilver.image = "/template/images/icon-silver.png";
		iconSilver.iconSize = new GSize(30, 15);
		iconSilver.iconAnchor = new GPoint(0, 10);
		iconSilver.infoWindowAnchor = new GPoint(30, 1);
		
		iconBronze = new GIcon();
		iconBronze.image = "/template/images/icon-bronze.png";
		iconBronze.iconSize = new GSize(30, 15);
		iconBronze.iconAnchor = new GPoint(0, 10);
		iconBronze.infoWindowAnchor = new GPoint(30, 1);
		
		setAllMarkers();
		updateMap();
	}
}

function latenzien(veld) {
	var x = document.formulier.elements[veld].selectedIndex;
	return document.formulier.elements[veld].options[x].value;
}

function setMarkerLatLng(id,adres,lat,lng,inhoud,status) {
	point = new GLatLng(lat,lng);
	if (status == '1') {
		var oIcon = iconGold; // gold
	} else if (status == '2') {
		var oIcon = iconSilver; // silver
	} else if (status == '3') {
		var oIcon = iconBronze;// bronze
	} else {
		var oIcon = iconDefault;// default
	}
	var marker = new GMarker(point,oIcon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(inhoud);
	});
	bounds.extend(point);
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
	markerInfo[markerTeller] = new Array(id,adres);
	markerTeller++;
	map.addOverlay(marker);
}

function setMarkerAddress(adres,woonplaats, land) {
	var address = adres+','+woonplaats+','+land;
	geocoder.getLatLng(
	address,
	function(point) {
		if (!point) {
			alert(gmAlertAddressNotFound);
		} else {
			clearMap();
			if(markerV) {
				markerV.remove();
			}
			markerV = new GMarker(point,iconDefault);
			bounds = new GLatLngBounds;
			bounds.extend(point);
			map.setZoom(10);
			map.setCenter(bounds.getCenter());
			GEvent.addListener(markerV, "click", function() {
				markerV.openInfoWindowHtml("<strong>"+gmDeparturePoint+"</strong><br />"+adres+"<br />"+woonplaats);
			});
			map.addOverlay(markerV);
			
			markerVpoint = point;
			markerVadres = address;
			document.getElementById('gmRoute').style.display = 'none';
			markerInfoTemp = markerInfo.slice();
			berekenAfstanden();
		}
	});
}

function zoomNaar(lat,lng) {
	var point = new GLatLng(lat,lng);
	bounds = new GLatLngBounds;
	bounds.extend(point);
	map.setZoom(12);
	map.setCenter(bounds.getCenter());
}

function maakRoute(naarLat,naarLng,naam,adres,postcode,plaats,url) {
	if (markerVpoint) {
		var from = markerVpoint;
		var to = new GLatLng(naarLat,naarLng);
		clearMap();
		document.getElementById('gmRoute').style.display = 'block';
		document.getElementById('gmRouteNaar').innerHTML = markerVadres;
		document.getElementById('gmToHead').innerHTML = '<b>'+gmRoutePlannedTo+'</b>';
		document.getElementById('gmToNaam').innerHTML = naam;
		document.getElementById('gmToAdres').innerHTML = adres;
		document.getElementById('gmToPostcode').innerHTML = postcode;
		document.getElementById('gmToWoonplaats').innerHTML = plaats;
		if (url != 'http://' && url != '') {
			document.getElementById('gmToWebsite').innerHTML = '<a class="gmLink" href="'+url+'" target="_blank">'+gmVisitSite+'</a>';
		}
		
		directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);
		directions.load('from: '+from+' to: '+to);
		
		GEvent.addListener(directions,"load", function() {
			
		});
		GEvent.addListener(directions,"click", function() {
			to.openInfoWindowHtml("<strong>"+gmAlertDeparturePoint+"</strong><br />"+adres+"<br />");
		});
	} else {
		alert(gmAlertNoDeparturePoint);
	}
}

function clearMap() {
	if (directions) {
		directions.clear();
	}
	document.getElementById("route").innerHTML = '';
}

function berekenAfstanden() {
	if(markerInfoTemp.length>0) {
		
		dezeMarker=markerInfoTemp.pop();
		afstandBerekening(markerVadres,dezeMarker[1],dezeMarker[0]);
		setTimeout('berekenAfstanden()',200);
	} else {
		//klaar
	}
}

function afstandBerekening(from,to,id) {
	document.getElementById('gm_'+id).innerHTML = '&nbsp;&nbsp;';
	var directions = new GDirections();
	directions.load('from: '+from+' to: '+to);
	GEvent.addListener(directions,"load", function() {
		document.getElementById('gm_'+id).innerHTML = directions.getDuration().html;
		retryTeller = 0;
	});
	GEvent.addListener(directions,"error", function() {
		document.getElementById('gm_'+id).innerHTML = 'error';
		if (directions.getStatus().code == 620) {
			if (retryTeller == 15) {
				document.getElementById('gm_'+id).innerHTML = gmMaxLimit;
				retryTeller = 0;
			} else {
				retryTeller++;
				afstandBerekening(from,to,id);
			}
		} else if (directions.getStatus().code == 602) {
			document.getElementById('gm_'+id).innerHTML = gmWrongAddress;
		} else {
			document.getElementById('gm_'+id).innerHTML = directions.getStatus().code;
		}
	});
}
