function Map() {
	this.markerManager = '';
	
	this.map = '';
	
	this.bounds = '';
	
	this.options = {}; // Options til intializeMap
	
	this.initializeMap = function(options) {
		if (GBrowserIsCompatible()) {
			if (options == undefined || typeof options !== 'object') {
				var options = {};
			}
			
			this.options.zoom = options.zoom != undefined ? parseInt(options.zoom) : 9;
			this.options.center = options.center != undefined ? options.center : {latitude: 57.48483265961265, longitude: 10.24200439453125};
			
			this.map = new GMap2(document.getElementById("map_canvas"));
			this.map.setCenter(new GLatLng(this.options.center.latitude, this.options.center.longitude), this.options.zoom);
			this.map.enableDoubleClickZoom();
			this.map.addControl(new GSmallZoomControl3D());
			this.map.enableScrollWheelZoom();
			
			var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: false };
			this.markerManager = new MarkerManager(this.map, mgrOptions);
			
			this.bounds = new GLatLngBounds();
			
			// Create our "tiny" marker icon
			this.houseIcon = new GIcon(G_DEFAULT_ICON);
			//houseIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
			this.houseIcon.image = "/images/hus_ikon_v2.png";
			this.houseIcon.shadow = "";
			this.houseIcon.iconSize = new GSize(21, 15);
			this.houseIcon.shadowSize = new GSize(21, 15);
			this.houseIcon.iconAnchor = new GPoint(0, 0);
			this.houseIcon.infoWindowAnchor = new GPoint(9, 2);
		}
	}
	
	this.addMarkers = function(houses, iconType) {
		if (houses != undefined
			&& houses.length > 0) {
			// Set up our GMarkerOptions object
			var iconType = iconType == undefined || iconType == '' ? 'house' : iconType;
			var markers = new Array();
			
			for (var i = 0; i < houses.length; i++) {
				var point = new GLatLng(houses[i].lat, houses[i].lng);
				if(this.bounds)
					this.bounds.extend(point);
				var icon = this.getIcon(iconType, houses[i].image_id)
				if(this.createMarker) {
					var marker = this.createMarker(point, 'Feriehus ' + houses[i].external_house_id, houses[i].external_house_id, icon)
					marker.data = {id: houses[i].id}
					markers.push(marker);
				}
			}
			if(this.markerManager)
				this.markerManager.addMarkers(markers, 6);
		}
		
		this.refresh();
	}
	
	this.refresh = function() {
		if(this.markerManager)
			this.markerManager.refresh();
	}
	
	this.createMarker = function(point, title, external_house_id, icon) {
		var marker = new GMarker(point, {title: title, icon: icon});
		GEvent.addListener(marker, "click", function() {
			document.location = '/' + external_house_id;
			return false;
        });
        return marker
	}
	
	this.getIcon = function (iconType, iterator) {
		switch (iconType) {
			case 'numbered':
				var icon = new GIcon(this.houseIcon);
				iterator = parseInt(iterator);
				icon.image = '/images/map_numbers/'+iterator+'.png'
				icon.shadow = '';
				icon.iconSize = new GSize(18, 18)
				return icon;
			break;
			case 'house':
			default:
				return this.houseIcon;
			break;
		}
		
	}
	
	this.updateSearch = function(callbackFunc) {
		var bounds = this.map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		$("#south_bound").val(southWest.lat());
		$("#west_bound").val(southWest.lng());
		$("#east_bound").val(northEast.lng());
		$("#north_bound").val(northEast.lat());
		
		callbackFunc();
	}
	
	this.addSearchListeners = function(callbackFunc) {
		GEvent.addListener(this.map, 'moveend', function () {
			Map.updateSearch(callbackFunc);
		});
		
		GEvent.addListener(this.map, 'zoomend', function () {
			Map.updateSearch(callbackFunc);
		});
	}
	
	this.adjustZoomLevel = function() {
		 // ===== determine the zoom level from the bounds =====
//		if(this.map)
//          this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
//
//          // ===== determine the centre from the bounds ======
//		if(this.map)
//          this.map.setCenter(this.bounds.getCenter());
	}
}
