function URLEncode(str) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";

	for (var i = 0; i < str.length; i++ ) 
	{
		var ch = str.charAt(i);
		if (ch == " ") {
			encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '" 
						+ ch 
						+ "' cannot be encoded using standard URL encoding.\n" +
						  "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

var map = null;
var geocoder = null;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		geocoder = new GClientGeocoder();
		showAddress(daddr);
	}
}

function initialize2() {
		if (GBrowserIsCompatible()) {
			if (GBrowserIsCompatible()) {
				map = new GMap2(document.getElementById("map"));
				//map.addControl(new GSmallMapControl());
				map.setCenter(new GLatLng(51.0784885, 5.3960431), 9);
				map.setUIToDefault();
				}
		}
		geocoder = new GClientGeocoder();
}

function showAddress(address) {
	address = URLEncode(address);
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function (point) {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				
				GEvent.addListener(marker, "click", function() {
				  marker.openInfoWindowHtml(html);
				});
				map.addOverlay(marker);
				marker.openInfoWindowHtml(html);
			}
		);
	}
}

function getRoute() {
	var saddr = URLEncode(document.getElementById('start').value);
	window.open('http://maps.google.com/maps?saddr='+saddr+'&daddr='+daddr, '_blank');
}

function loadMarkers(filter) {
	// get zip code
	var zip = (filter) ? $F('zip') : null;

	// show map
	if (GBrowserIsCompatible()) {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			//map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(51.0784885, 5.3960431), 9);
			map.setUIToDefault();
		}
	}
	
	// fetch projects
	new Ajax.Request('/nl/ajax/project/markers',
	{
	method:'get',
	parameters: {zip:zip},
	onLoading: function() {
		clearList();
	},
	onSuccess: function(transport) {
	},
	onFailure: function() {
		alert('Sorry, de projecten konden niet worden geladen.');
	},
	onComplete: function(transport) {
		var response = transport.responseText || "no response text";
		var data = response.evalJSON();
		var projects = data.projects;
		if (projects.length > 0) {
			geocoder = new GClientGeocoder();
			for (var id = 0; id < projects.length; id++) {
				var name = projects[id].name;
				var address = projects[id].address + ', ' + projects[id].zip + ' ' + projects[id].city;
				var image = projects[id].image;
				var status = projects[id].status;
				var url = projects[id].url;
				var description = '<div class="marker" style="height:150px"><h2>' + name + '</h2>' + 
				'<p><img src="/default/images/thumb/' + image + '" alt="" width="100" height="100" />' + address + '</p>' + 
				'<p><a href="' + url + '">Meer over dit project &raquo;</a></p></div>';
				addMarker(name, address, status, url, description);
			}
		}	
	}});
}

function addMarker(name, address, status, url, description) {
	var icon = new GIcon();
    icon.image = "http://www.dubolimburg.be/default/images/marker_" + status + ".png";
    icon.iconSize = new GSize(24, 23);
    icon.iconAnchor = new GPoint(0, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
	geocoder.getLatLng(
		    address,
		    function(point) {
		      if (point != false) {
		        var marker = new GMarker(point, icon);
		        var listItem = document.createElement('li');
		        var listItemLink = listItem.appendChild(document.createElement('a'));
		        listItemLink.href = url;
		        listItemLink.innerHTML = '<strong>' + name + ' </strong>, <span>' + address + '</span>';
		        var focusPoint = function() {
		        	marker.openInfoWindowHtml(description);
		        	map.panTo(point);
		        	return false;
		        }
		        GEvent.addListener(marker, 'click', focusPoint);
		       /* listItemLink.onclick = focusPoint;*/
		        /*GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(description); });*/
		        $('project-list').appendChild(listItem);
		        map.addOverlay(marker);
				
		      }
		    }
		  );
}

function addMarker2(name, address, status, description) {
	var icon = new GIcon();
    icon.image = "http://www.dubolimburg.be/default/images/marker_" + status + ".png";
    icon.iconSize = new GSize(24, 23);
    icon.iconAnchor = new GPoint(0, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
	geocoder.getLatLng(
		    address,
		    function(point) {
		      if (point != false) {
		        var marker = new GMarker(point, icon);
		       
		        var focusPoint = function() {
		        	marker.openInfoWindowHtml(description);
		        	map.panTo(point);
		        	return false;
		        }
		        GEvent.addListener(marker, 'click', focusPoint);
		       /* listItemLink.onclick = focusPoint;*/
		        /*GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(description); });*/
		        map.addOverlay(marker);
				map.setCenter(point, 9);
		      }
		    }
		  );
}

function clearList() {
	var list = $('project-list');
	while( list.hasChildNodes() ) {
		list.removeChild( list.lastChild );
	}
}

function showList() {
	$('listwrapper').show();
}

