var locatieMarkers = [];
var locatieHtml = [];
var locatieStatus = [];

function setupMap() { 
	
	var adresMarker;
	var bounds = new GLatLngBounds();
	
	if (GBrowserIsCompatible()) {
	
		var mapHolder = document.getElementById("map_canvas");
		
		mapHolder.style.display = "block";
		
		var map = new GMap2(mapHolder);

		var locationIcon = new GIcon();
		var homeIcon = new GIcon();
		
		map.setUIToDefault();
		map.setMapType(G_NORMAL_MAP);
		map.disableScrollWheelZoom();
		
		locationIcon.image = "/static/gfx/google_maps_icon.png";
		locationIcon.iconSize = new GSize(32, 32);
		locationIcon.iconAnchor = new GPoint(16, 31);
		locationIcon.infoWindowAnchor = new GPoint(28, 3);
		
		homeIcon.image = "/static/gfx/google_maps_adres.png";
		homeIcon.iconSize = new GSize(32, 32);
		homeIcon.iconAnchor = new GPoint(16, 31);
		homeIcon.infoWindowAnchor = new GPoint(28, 3);
		
		adresMarker = createMarker(locations.adres[0].lat, locations.adres[0].lon, locations.adres[0].label, locations.adres[0].beschrijving, homeIcon);
		map.addOverlay(adresMarker);
		
		for (var i = 0; i < locations.locaties.length; i++) {
			
			locatieMarkers[i] = createMarker(locations.locaties[i].lat, locations.locaties[i].lon, locations.locaties[i].label, locations.locaties[i].beschrijving, locationIcon);
			locatieHtml[i] = locations.locaties[i].beschrijving;
			
			map.addOverlay(locatieMarkers[i]);
			
			locatieStatus[i] = false;
		}
		
		locatieStatus[0] = true;
		locatieMarkers[0].openInfoWindowHtml(locatieHtml[0]);

		map.setCenter(bounds.getCenter( ), map.getBoundsZoomLevel(bounds));
	}
	
	function createMarker(lat, lon, label, beschrijving, icon) {
		
		var latLon = new GLatLng(lat, lon);
		
		if (typeof(icon) != "undefined") {
		
			var marker = new GMarker(latLon, icon);
		}
		else {
		
			var marker = new GMarker(latLon);
		}
		
		bounds.extend(latLon);
		
		if (beschrijving != "") {
		
			GEvent.addListener(marker, "click", function() {
			
				marker.openInfoWindowHtml(beschrijving);
			});
		}
		
		return marker;
	}
}

function openBeschrijving(index) {

	if (index < locatieHtml.length) {
		
		locatieStatus[index] = !locatieStatus[index];
		
		if (locatieStatus[index]) {	
		
			locatieMarkers[index].openInfoWindowHtml(locatieHtml[index]);
		}
		else {
		
			locatieMarkers[index].closeInfoWindow();
		}
	}
}

function geocode() {
	
	var geocoder = new GClientGeocoder();
	var adres = $("#adres").val();
	
	geocoder.setBaseCountryCode("NL");
	
	if (adres != '') {
	
		geocoder.getLocations(adres, function(response) {
			
			if (response != null) {
				
				$("#httpStatusCode").val(response.Status.code)
				
				if (response.Status.code == 200) {
				
					$("#latitude").val(response.Placemark[0].Point.coordinates[1]);
					$("#longitude").val(response.Placemark[0].Point.coordinates[0]);
					$("#accuracy").val(response.Placemark[0].AddressDetails.Accuracy);
				}
			}
			else {
			
				$("#latitude").val("0");
				$("#longitude").val("0");
			}
			
			$("#zoekForm").submit();
		});
	}
	
	return false;
}