
	Function.prototype.calledOn = function(objInstance) {
		var functionObj = this;
		return function() {
			return functionObj.apply(objInstance, arguments);
		}
	};

	function Europeer(id, firstname, lastname, link) {
		this.id = id;
		this.firstname = firstname;
		this.lastname = lastname;
		this.link = link;
	}

	function EuropeerResidence(town, plz, townLat, townLng, europeer) {
		this.town = town;
		this.plz = plz;
		this.townLocation = new GLatLng(townLat, townLng);
		this.europeer = europeer;
	}

	EuropeerResidence.prototype = {
		getHtmlDescription: function() {
			return '<h3><a href="' + this.europeer.link + '">' + this.europeer.firstname + ' ' + this.europeer.lastname + '</a></h3>';
		}
	}

	function EuropeerResidenceMapMarker(location) {
		this.location = location;
		this.europeerResidences = new Array();
	}

	EuropeerResidenceMapMarker.prototype = {
		html: '',
		addEuropeerResidence: function(europeerResidence) {
			this.europeerResidences.push(europeerResidence);
		},
		putToMap: function(markerManager) {

			var icon = new GIcon();
			icon.iconSize = new GSize(16, 28);
			icon.iconAnchor = new GPoint(8, 28);
			icon.infoWindowAnchor = new GPoint(8, 28);
			icon.image = "/static/img/icon_orange.png";

			var marker = new GMarker(this.location, icon);

			marker.html = '<div class="infoWindow">';
			marker.html += '<h2>Europeers in ' + this.europeerResidences[0].town + '</h2>';
			marker.html += '<ul>';
			for (var n=0; n < this.europeerResidences.length; n++) {
				marker.html += '<li>' + this.europeerResidences[n].getHtmlDescription() + '</li>';
			}
			marker.html += '</ul>';
			marker.html += '</div>';

			GEvent.addListener(marker, "click", function() {
				this.openInfoWindowHtml(this.html, {maxWidth: 200});
			});

			markerManager.addMarker(marker, 0, null);
		}
	}

	function EuropeerResidenceMap(containerName) {
		var domContainer = document.getElementById(containerName);

		this.map = new GMap2(domContainer);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.enableContinuousZoom();
		this.map.enableDoubleClickZoom();

		this.europeerResidences = [];

		var winOnUnload = window.onunload;
		window.onunload = function() {
			if (typeof winOnUnload == "function") winOnUnload();
			GUnload();
		}
	}

	EuropeerResidenceMap.prototype = {

		show: function() {
			this.showGermany();
			this.markerManager = new GMarkerManager(this.map);
			this.putMarkers();
		},

		showGermany: function() {
			var germany = new GLatLngBounds();
			germany.extend(new GLatLng(55.39, 4.99));
			germany.extend(new GLatLng(47.19, 15.73));

			this.map.setCenter(germany.getCenter());
			this.map.setZoom(this.map.getBoundsZoomLevel(germany));
		},

		addEuropeerResidence: function(europeerResidence) {
			this.europeerResidences.push(europeerResidence);
		},

		putMarkers: function() {
			var markers = [];
			var townMarkers = [];
			var d2tourLocations = [];

			for(var n=0; n<this.europeerResidences.length; n++) {
				if (!townMarkers[this.europeerResidences[n].town]) {
					townMarkers[this.europeerResidences[n].town] = new EuropeerResidenceMapMarker(this.europeerResidences[n].townLocation, /* isTownMarker */ true);
				}
				townMarkers[this.europeerResidences[n].town].addEuropeerResidence(this.europeerResidences[n]);
			}
			for (var townLocation in townMarkers) townMarkers[townLocation].putToMap(this.markerManager);
			this.markerManager.refresh();
		}

	}
