var map = null;
	var geocode = null;
	var points = new Array();
	 
	function initialize() {}
	 
	function check(postcode) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallMapControl());
		geocoder = new GClientGeocoder();
		gdir = new GDirections();
		
		var icon = new GIcon();
		icon.image = "../images/house.png";
		icon.iconSize = new GSize(24, 24);
		icon.iconAnchor = new GPoint(0, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
	
		if (geocoder) {
			geocoder.getLatLng(
				postcode + ' Nederland',
				function(point) {
					if (!point) {
						alert(postcode + " niet gevonden!");
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point,icon) ;

						GEvent.addListener(marker, 'click', function() {
							marker.openInfoWindowHtml('Jouw postcode!');
						});
						map.addOverlay(marker);
						showActiviteiten();
					}
				}
			);
        }
		
   }
	
	function showActiviteiten() {	
		jQuery.ajax({
            type: "GET",
            url: "../act_get.php",
            dataType: "html",
            error: function(){
              alert('Error loading XML document');
			},
			success: function(data){
               	var array = data.split(";;");
				var html = null;
				for(var i in array){
					var act_info = array[i].split("||");
					html = new Array(act_info[3],act_info[2],act_info[4],act_info[7]);
					
					//html = '<b><a href=../activiteiten/' + act_info[3] + '>' + act_info[2] + '</a></b><br><i>' + act_info[4] +'</i><br><br><div style="width:200px">' + act_info[7] + '</div>';
					
					if(act_info[0] == ''){
						geoMark(act_info[8], act_info[6], act_info[9], act_info[3], html);						
					}else{
						var point = new GLatLng(act_info[0], act_info[1]);
						points.push(new Array(point,html));						
					}
					
				}
				createPoints();
			}
		});
	}
	
	function createPoints(){
		var latlong = new Array();
		var show = null;
			
		var icon = new GIcon();
		icon.image = "../images/marker.png";
		icon.iconSize = new GSize(24, 24);
		icon.iconAnchor = new GPoint(0, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		
		var icon1 = new GIcon();
		icon1.image = "../images/marker1.png";
		icon1.iconSize = new GSize(24, 24);
		icon1.iconAnchor = new GPoint(0, 20);
		icon1.infoWindowAnchor = new GPoint(5, 1);
		
		var index_str = "";
		for(var i in points){
			
			index_str = points[i][0].lat() + "-" + points[i][0].lng();
			
			if(latlong[index_str] == null){
				latlong[index_str] = new Array();
			}
			
			latlong[index_str].push(new Array(points[i][0],points[i][1]));
		
		}

		for(var j in latlong){
			if(j != "NaN-NaN"){
				if(latlong[j] != null && latlong[j].length > 1){
					show = '<b>' + latlong[j][1][1][2] + '</b><br><br>'
					if(latlong[j].length < 4){
						for(var k in latlong[j]){
							show = show + '<a href=../kids/activiteiten/' + latlong[j][k][1][0] + '>' + latlong[j][k][1][1] + '</a><br><div style="width:200px">' + latlong[j][k][1][3] + '</div><br>';
						}
					}else{
						for(var k in latlong[j]){
							show = show + '<a href=../kids/activiteiten/' + latlong[j][k][1][0] + '>' + latlong[j][k][1][1] + '</a><br><br>';
						}
					}
					map.addOverlay(createMarker(latlong[j][1][0],icon1,show));
				}else{
					
					show = '<b>' + latlong[j][0][1][2] + '</b><br><br><a href=../kids/activiteiten/' + latlong[j][0][1][0] + '>' + latlong[j][0][1][1] + '</a><br><div style="width:200px">' + latlong[j][0][1][3] + '</div>';
					map.addOverlay(createMarker(latlong[j][0][0],icon,show));
				}
			}
		}
	}
	
	function createMarker(point,icon,html) {
		var marker = new GMarker(point,icon);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	}
	
	function geoMark($adres, $postcode, $plaats, $id, html){
		geocoder.getLatLng(
			$adres + ', ' + $postcode + ', ' + $plaats,
			function(point) {
				if (point) {
					jQuery.post("../act_post.php", { id: $id, lat: point.lat(), lng: point.lng()} );
					points.push(new Array(point,html));	
				}
			}
		);
	}

