
/*****************/
/**** EWindow ****/

function EStyle(stemImage, stemSize, boxClass, boxOffset) {
	this.stemImage = stemImage;
	this.stemSize = stemSize;
	this.boxClass = boxClass;
	this.boxOffset = boxOffset;

	var agent = navigator.userAgent.toLowerCase();

	var fudge = 0;
	this.fudge = fudge;
}

function EWindow(map,estyle) {
	// parameters
	this.map=map;
	this.estyle=estyle;
	// internal variables
	this.visible = false;
	// browser - specific variables
	this.ie = false;
	var agent = navigator.userAgent.toLowerCase();
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
} 

EWindow.prototype = new GOverlay();

EWindow.prototype.initialize = function(map) {
	var div1 = document.createElement("div");
	div1.style.position = "absolute";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
	var div2 = document.createElement("div");
	div2.style.position = "absolute";
	div2.style.width = this.estyle.stemSize.width+"px";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
	this.div1 = div1;
	this.div2 = div2;
}

EWindow.prototype.openOnMap = function(point, html, offset) {
	this.offset = offset||new GPoint(0,0);
	this.point = point;
	this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '">' + html + '</div>';
	if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
		var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
		this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
	} else {
		this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'">';
	}
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z;
	this.div2.style.zIndex = z+1;
	this.visible = true;
	this.show();
	this.redraw(true);
}

EWindow.prototype.openOnMarker = function(marker,html) {
	var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
	var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
	this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}


EWindow.prototype.redraw = function(force) {
	if (!this.visible) {return;}
	var p = this.map.fromLatLngToDivPixel(this.point);
	this.div2.style.left   = (p.x + this.offset.x) + "px";
	this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
	this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
	this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
}

EWindow.prototype.remove = function() {
	this.div1.parentNode.removeChild(this.div1);
	this.div2.parentNode.removeChild(this.div2);
	this.visible = false;
}

EWindow.prototype.copy = function() {
	return new EWindow(this.map, this.estyle);
}

EWindow.prototype.show = function() {
	this.div1.style.display="";
	this.div2.style.display="";
	this.visible = true;
}

EWindow.prototype.hide = function() {
	this.div1.style.display="none";
	this.div2.style.display="none";
	this.visible = false;
}

EWindow.prototype.isHidden = function() {
	return !this.visible;
}

EWindow.prototype.supportsHide = function() {
	return true;
}

EWindow.prototype.zindex = function(zin) {
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z+zin;
	this.div2.style.zIndex = z+1+zin;
}


EWindow.prototype.getSize = function() {
	if (!this.visible) {
		return null;
	}
	else {
		return new GSize(this.div1.offsetWidth, this.div1.offsetHeight + this.div2.offsetHeight);
	}
}
/**** EWindow ****/
/*****************/


var ICON_HOUSE;
var ICON_HOUSE_DISCOUNT;
var ICON_HOUSE_MEMO;
var ICON_HOUSE_DISCOUNT_MEMO;
var ICON_MULTIPLE_HOUSES;


var TYPE_HOUSE = 0;
var TYPE_MULTIPLE_HOUSES = 1;
var TYPE_HOUSE_MEMO = 2;
var TYPE_HOUSE_DISCOUNT = 3;
var TYPE_HOUSE_DISCOUNT_MEMO = 4;

var E_STYLE_HOUSE;


var ew = null;
var ewm = null;


if (GBrowserIsCompatible()) {
	ICON_HOUSE = new GIcon();
	ICON_HOUSE.image = "/site/images/ICON_HOUSE.png";
	ICON_HOUSE.iconSize = new GSize(16, 16);
	ICON_HOUSE.iconAnchor = new GPoint(8, 16);
	ICON_HOUSE.infoWindowAnchor = new GPoint(8, 8);

	ICON_HOUSE_MEMO = new GIcon();
	ICON_HOUSE_MEMO.image = "/site/images/ICON_HOUSE_MEMO.png";
	ICON_HOUSE_MEMO.iconSize = new GSize(16, 16);
	ICON_HOUSE_MEMO.iconAnchor = new GPoint(8, 16);
	ICON_HOUSE_MEMO.infoWindowAnchor = new GPoint(8, 8);

	ICON_HOUSE_DISCOUNT = new GIcon();
	ICON_HOUSE_DISCOUNT.image = "/site/images/ICON_HOUSE_DISCOUNT.png";
	ICON_HOUSE_DISCOUNT.iconSize = new GSize(16, 16);
	ICON_HOUSE_DISCOUNT.iconAnchor = new GPoint(8, 16);
	ICON_HOUSE_DISCOUNT.infoWindowAnchor = new GPoint(8, 8)

	ICON_HOUSE_DISCOUNT_MEMO = new GIcon();
	ICON_HOUSE_DISCOUNT_MEMO.image = "/site/images/ICON_HOUSE_DISCOUNT_MEMO.png";
	ICON_HOUSE_DISCOUNT_MEMO.iconSize = new GSize(16, 16);
	ICON_HOUSE_DISCOUNT_MEMO.iconAnchor = new GPoint(8, 16);
	ICON_HOUSE_DISCOUNT_MEMO.infoWindowAnchor = new GPoint(8, 8)

	ICON_MULTIPLE_HOUSES = new GIcon();
	ICON_MULTIPLE_HOUSES.image = "/site/images/ICON_MULTIPLE_HOUSES.png";
	ICON_MULTIPLE_HOUSES.iconSize = new GSize(16, 16);
	ICON_MULTIPLE_HOUSES.iconAnchor = new GPoint(8, 16);
	ICON_MULTIPLE_HOUSES.infoWindowAnchor = new GPoint(8, 8);

	E_STYLE_HOUSE = new EStyle("/site/images/STEM_HOUSES.png", new GSize(24, 24), "e_style_house", new GPoint(-10, 23));
	E_STYLE_HOUSE_DISCOUNT = new EStyle("/site/images/STEM_HOUSES.png", new GSize(24, 24), "e_style_house_discount", new GPoint(-10, 23));; //new EStyle("/site/images/STEM_DISCOUNT.png", new GSize(24, 24), "e_style_house_discount", new GPoint(-10, 23));
	E_STYLE_MULTIPLE_HOUSES = new EStyle("/site/images/STEM_MULTIPLE_HOUSES.png", new GSize(24, 24), "e_style_multiple_houses", new GPoint(-10, 23));

	GMarker.prototype.type = -1;
}


/* Google Maps wrapper */
function googleMapsObject() {
	var _this = this;

	this.toggleLoading = function() {
		if (_this.settings.loading) {
			var b = _this.settings.loading.style.display;
			var bLoading = false;

			if (arguments.length > 0) {
				if (arguments[0]) {
					bLoading = true;					
				}
			}
			else {
				bLoading = (b == "" ? true : (b == "block" ? false : true));			
			}

			b = (bLoading ? "block" : "none");

			_this.settings.loading.style.display = b;
			if (b == "block") {
				if (_this.map) {
					var x = Math.round((_this.map.getSize().width - _this.settings.loading.offsetWidth)/ 2);
					var y = Math.round((_this.map.getSize().height - _this.settings.loading.offsetHeight)/ 2);
					_this.settings.loading.style.top = y +"px";
					_this.settings.loading.style.left = x +"px";
				}
			}

		}
	}



	this.mapPoints = []; //Array();
	this.addMapPoint = function(gmPoint) {
		_this.mapPoints[_this.mapPoints.length] = gmPoint;
		_this.initMapPoint(_this.mapPoints[_this.mapPoints.length - 1]);
		return _this.mapPoints.length - 1;
		
	}



	this.initMapPoint = function(gmPoint) {
		if (gmPoint != null) {
			var waitObj = new waitForIt();

			waitObj.waitFor = function() {
				return gmPoint.completed();
			}
			waitObj.whenDone = function() {
				if (gmPoint.point) {
					_this.createMarker(gmPoint);
				}
				else {
					//could not find supplied Point
				}
			}

			waitObj.run();
		}
	}



	this.getBounds = function() {
		if (_this.mapPoints.length > 0) {
			var minLat = null;
			var minLng = null;
			var maxLat = null;
			var maxLng = null;

			for (var i = 0; i < _this.mapPoints.length; i++) {
				var p = _this.mapPoints[i].point;
				if (p) {
					minLat = minLat ? (minLat <= p.lat() ? minLat : p.lat()) : p.lat();
					minLng = minLng ? (minLng <= p.lng ? minLng : p.lng) : p.lng();
					maxLat = maxLat ? (maxLat >= p.lat() ? maxLat : p.lat()) : p.lat();
					maxLng = maxLng ? (maxLng >= p.lng() ? maxLng : p.lng()) : p.lng();
				}
			}

			return new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng));
		}	
		else {
			return null;
		}
	}




	this.map = null;
	this.geocoder = null;
	this.point = null;




	this.settings = {
		enableGeocodeLookup : false,
		zoom : 13,
		center : (GBrowserIsCompatible() ? new GLatLng(56.180211, 10.136023) : null),
		container : null,
		loading : null,
		scrollWheelZoom : true
	};


	this.controls = {
		mapTypeControl : true,
		smallMapControl : false,
		largeMapControl : false,
		overviewMapControl : false
	};

	this.setZoom = function(zoom) {
		if (GBrowserIsCompatible) {
			_this.map.setZoom(zoom);
		}
	};

	this.zoomToAll = function() {
		var _this = this;
		if (_this.mapPoints.length > 0) {
			var oBounds = new GLatLngBounds();

			for (var i = 0; i < _this.mapPoints.length; i++) {
				oMP = _this.mapPoints[i];
				if (oMP.point != null) {
					if ((oMP.point.lat() != 0) && (oMP.point.lng != 0)) {
						oBounds.extend(oMP.point);					
					}
				}
			}
			_this.map.setCenter(oBounds.getCenter(), _this.map.getBoundsZoomLevel(oBounds));

//			_this.map.setZoom(_this.map.getBoundsZoomLevel(oBounds));
//			_this.map.panTo(oBounds.getCenter());

		}
	}


	this.wheelevent = function(e) {
		if (!e) {
			e = window.event;
		}
		if (e.preventDefault) {
			e.preventDefault();
		}
		e.returnValue = false;
	}


	this.init = function() {
		if (GBrowserIsCompatible()) {
			_this.map = new GMap2(_this.settings.container);

			GEvent.addDomListener(_this.map.getContainer(), "DOMMouseScroll", _this.wheelevent);
			_this.map.getContainer().onmousewheel = _this.wheelevent;

			_this.map.setCenter(_this.settings.center, _this.settings.zoom); 

			if (_this.settings.scrollWheelZoom) {
				_this.map.enableScrollWheelZoom();
			}

			if (_this.controls.smallMapControl) {
				_this.map.addControl(new GSmallMapControl());
			}

			if (_this.controls.largeMapControl) {
				_this.map.addControl(new GLargeMapControl());
			}

			if (_this.controls.overviewMapControl) {
				_this.map.addControl(new GOverviewMapControl());
			}
			
			if (_this.controls.mapTypeControl) {
				_this.map.addControl(new GMapTypeControl());
			}

			ew = new EWindow(_this.map, E_STYLE_HOUSE);
			_this.map.addOverlay(ew);
		
			/* house "cluster" info window */
			ewm = new EWindow(_this.map, E_STYLE_MULTIPLE_HOUSES);
			_this.map.addOverlay(ewm);

		}
	}




	this.markers = Array();

	this.markerExistsAtPoint = function(point) {
		var _this = this;
		var exists = false;
		for (var i = 0; i < _this.markers.length; i++) {
			mp = _this.markers[i].getLatLng();

			if ((mp.lat() == point.lat()) && (mp.lng() == point.lng())) {
				exists = true;
				break;
			}
		}
		return exists;
	}

	this.getMarkerAtPoint = function(point) {
		var _this = this;
		var res = null;
		for (var i = 0; i < _this.markers.length; i++) {
			latlng = _this.markers[i].getLatLng();
			if ((latlng.lat() == point.lat()) && (latlng.lng() == point.lng())) {
				res = _this.markers[i];
			}
		}

		return res;
	}

	this.removeMarker = function(oMarker) {
		var _this = this;

		var res = Array();
		for (var i = 0; i < _this.markers.length; i++) {
			if (_this.markers[i] != oMarker) {
				res[res.length] = _this.markers[i];
			}
		}
		_this.markers = res;
		_this.map.removeOverlay(oMarker);
	}

	this.addMarker = function(oMarker) {
		var _this = this;
		_this.markers[_this.markers.length] = oMarker;
		_this.map.addOverlay(oMarker); 
	}


	this.pointsEqual = function(p1, p2) {
		if ((p1 != null) && (p2 != null)) {
			if ((p1.lat() == p2.lat()) && (p1.lng() == p2.lng())) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}

	this.getHousesAtPoint = function(point) {
		var _this = this;
		var res = Array();
		var mp = null;
		for (var i = 0; i < _this.mapPoints.length; i++) {
			if (_this.pointsEqual(_this.mapPoints[i].point, point)) {
				res[res.length] = _this.mapPoints[i];
			}
		}
		return res;
	}


	this.createSimpleHouseMarker = function(oLatLng) {
		var _this = this;
		var oMarker = new GMarker(oLatLng, {icon : ICON_HOUSE, clickable : false});
		_this.map.addOverlay(oMarker);
	}


	this.clearMarkers = function() {
		var _this = this;
		for (var i = 0; i < _this.markers.length; i++) {
			_this.map.removeOverlay(_this.markers[i]);
		}
		_this.markers = Array();
	}

	this.clearMapPoints = function() {
		var _this = this;
		for (var i = 0; i < _this.mapPoints.length; i++) {
			_this.mapPoints[i] = null;
		}
		_this.mapPoints = Array();

	}

	this.clearHouses = function() {
		var _this = this;

		if (ew) {
			_this.map.removeOverlay(ew);
		}
		if (ewm) {
			_this.map.removeOverlay(ewm);
		}
		_this.clearMarkers();
		_this.clearMapPoints();
		return false;
	}
		

		
	this.closeHouse = function() {
		if (ew) {
			ew.hide();
		}
		return false;
	}

	this.closeMultipleHouses = function() {
		if (ew) {
			ew.hide();
		}
		if (ewm) {
			ewm.hide();
		}
		return false;
	}




	this.showHouseInfo = function(obj, lat, lng, locId) {
		var _this = this;

		var objPos = findPos(obj);
		var mapPos = findPos(_this.settings.container);

		var x = objPos[0] - mapPos[0];
		var y = objPos[1] - mapPos[1];

		
		var objectPoint = new GPoint(x, y);
		var objectLatLng = _this.map.fromContainerPixelToLatLng(objectPoint);
		objectPoint = _this.map.fromLatLngToDivPixel(objectLatLng);

		var parentMarkerLatLng = new GLatLng(lat, lng);
		var oHouse = _this.getHouse(locId);

		_this.fetchHouseInfo(oHouse, null, objectPoint, parentMarkerLatLng);
	}



	//Given internal id - return house from our list
	this.getHouse = function(locId) {
		for (var i = 0; i < _this.mapPoints.length; i++) {
			if (_this.mapPoints[i].data.locId == locId) {
				return _this.mapPoints[i];
			}
		}	
		return null;
	}



	this.updateHouseIcon = function(oHouse) {
		var newIcon = null;

		if (oHouse.data.memoState) {
			newIcon = (oHouse.data.hasDiscount ? ICON_HOUSE_DISCOUNT_MEMO : ICON_HOUSE_MEMO);
		}	
		else {
			newIcon = (oHouse.data.hasDiscount ? ICON_HOUSE_DISCOUNT : ICON_HOUSE);
		}

		if (newIcon != null) {
			oHouse.icon = newIcon;
			if (oHouse.marker != null) {
				oHouse.marker.setImage(oHouse.icon.image);
			}
			else {
				//Refresh image in parent infowindow:
				var oImg = document.getElementById("house_icon_"+ oHouse.data.locId);
				if (oImg != null) {
					oImg.src = oHouse.icon.image;
				}
			}
		}
	}

	this.toggleMemo = function(bAdd, obj, id, startdate, duration) {
		var _this = this;
		var reqKey = "memo";
		myRequests.remove(reqKey);
		var oXML = new XMLObject();
		var oXMLHttp = new XMLHttpRequestObject();
		oXMLHttp.async = true;
		oXMLHttp.src = "/custom/site/booking/memoxml.asp";

		var params = "";
		params += "act="+ (bAdd ? "add" : "del");
		params += "&id="+ encodeURIComponent(id);
		params += "&startdate="+ encodeURIComponent(startdate);
		params += "&duration="+ encodeURIComponent(duration);

		oXMLHttp.args = params;

		var waitObj = new waitForIt();

		waitObj.waitFor = function() {
			return oXMLHttp.completed();
		}

		waitObj.whenDone = function() {
			if (oXMLHttp.xml != null) {
				if (oXML.loadXML(oXMLHttp.xml)) {
					oError = oXML.selectSingleNode("//error");
					if (oError == null) {
						updateMemoCount(oXML.getText(oXML.selectSingleNode("memoxml/@count")));
						obj.innerHTML = (bAdd ? "Fjern fra huskeliste" : "Føj til huskeliste");

						if (bAdd) {
							removeClassName(obj, "add");
							addClassName(obj, "remove");
						}
						else {
							removeClassName(obj, "remove");
							addClassName(obj, "add");
						}

						obj.onclick = function() {
							return oGM.toggleMemo(!bAdd, this, id, startdate, duration);
						}

						var objHouse = _this.getHouse(id);
						if (objHouse != null) {
							objHouse.data.memoState = bAdd;
							_this.updateHouseIcon(objHouse);
						}

					}
					else {
						//some error occured
					}
				}
				else {
					//error in xml
				}	
			}
			else {
				//error in http request
			}
			toggleLoading(false, reqKey);			
		}

		toggleLoading(true, reqKey);
		oXMLHttp.load();
		waitObj.run();
		myRequests.add(reqKey, oXMLHttp, waitObj);
		
		return false;		
	}



	this.fetchHouseInfo = function(objHouse, objMarker, objPoint, parentMarkerLatLng) {
		var _this = this;

		if (ew != null) {
			_this.map.removeOverlay(ew);
		}

		var targetPoint = (objPoint != null ? objPoint : _this.map.fromLatLngToDivPixel(objMarker.getLatLng()));
		var targetLatLng = null;
		

		var parentMarkerPoint = (parentMarkerLatLng != null ? _this.map.fromLatLngToDivPixel(parentMarkerLatLng) : null);
		var offsetPoint = (parentMarkerPoint != null ? new GPoint(objPoint.x - parentMarkerPoint.x + 8, parentMarkerPoint.y - objPoint.y - 8) : null)
		

		var ewSize = null;


		var objForm = document.forms["ferieringSearch"];

		var oXML = new XMLObject();
		
		if (cXMLHttp != null) {
			cXMLHttp.abort();
		}
		else {
			cXMLHttp = new XMLHttpRequestObject();
		}
		cXMLHttp.async = true;
		cXMLHttp.src = "/custom/site/booking/maphousexml.asp?language_id=1";

		var params = "";
		params += "houseid="+ encodeURIComponent(objHouse.data.id);
		params += "&duration="+ encodeURIComponent(objForm.duration.value);
		params += "&visitors="+ encodeURIComponent(objForm.visitors.value);
		params += "&startdate="+ encodeURIComponent(objForm.yearmonth.value +"-"+ objForm.day.value);
	
		cXMLHttp.args = params;


		var waitObj = new waitForIt();
		waitObj.waitFor = function() {
			return cXMLHttp.completed();
		}
		waitObj.whenDone = function() {
			if (cXMLHttp.xml != null) {
				if (oXML.loadXML(cXMLHttp.xml)) {
					var objErr = oXML.selectSingleNode("//error");
					if (objErr == null) {

						var houseNode = oXML.selectSingleNode("maphousexml");
						var houseUrl = generateHouseUrl(houseNode);
						var lId = parseInt(oXML.getText(houseNode.selectSingleNode("@lid")), 10);
						var bMemo = (oXML.getText(houseNode.selectSingleNode("@memo")) == "true");
						var oImg = houseNode.selectSingleNode("image");
						var curr = oXML.getText(houseNode.selectSingleNode("period/@curr"));
						var price = oXML.getText(houseNode.selectSingleNode("period/@price"));
						var total= oXML.getText(houseNode.selectSingleNode("period/@total"));
						var discount = parseFloat(oXML.getText(houseNode.selectSingleNode("period/@discount")));
						var dtArr = oXML.getText(houseNode.selectSingleNode("period/@arrival"));
						var dur = oXML.getText(houseNode.selectSingleNode("period/@duration"));

						html = "";
						html += "<div class=\"content\">";
						html += "<img class=\"close\" src=\"/site/images/close.png\" width=\"10\" height=\"9\" alt=\"\" onclick=\"return oGM.closeHouse();\" />";


						if (oImg != null) {
							html += "<div class=\"img\"><img src=\""+ oXML.getText(oImg.selectSingleNode("@src")) +"\" width=\"165\" alt=\"\" /></div>";
						}
					
						html += "<div class=\"area\"><a href=\""+ houseUrl +"\">"+ oXML.getText(houseNode.selectSingleNode("address1")) +"<br />"+ oXML.getText(houseNode.selectSingleNode("area")) +"</a></div>";
						html += "<div class=\"number\"><a href=\""+ houseUrl +"\">" + "Feriehus" +" "+ oXML.getText(houseNode.selectSingleNode("@id")) +"</a></div>";

						if (str2date(dtArr) > DT_OPTION) {
							html += "<div class=\"priceBefore\">&nbsp;</div>";
							html += "<div class=\"price\">OPTION</div>";
						}
						else {
							if (discount > 0) {
								html += "<div class=\"priceBefore\"><del>"+ formatPrice(price, 2, curr) +"</del></div>";
							}
							html += "<div class=\"price\">"+ formatPrice(total, 2, curr) + "</div>";
						}

						html += "<div class=\"capacity\">Max. "+ oXML.getText(houseNode.selectSingleNode("@maxvisitors")) +" "+ "personer" +"</div>";
						html += "<div class=\"m2\">"+ oXML.getText(houseNode.selectSingleNode("@m2")) +" m&#xb2;</div>";

						if (lId > 0) {
							html += "<div class=\"memo\">";
							html += "<div><a class=\"memo "+ (bMemo ? "remove" : "add") +"\" href=\"#\" onclick=\"return oGM.toggleMemo("+ !bMemo +", this, "+ lId +",'"+ dtArr +"', "+ dur +");\">" + (bMemo ? " Fjern fra huskeliste" : "Føj til huskeliste") +"</a></div>";
							html += "</div>";
						}
						html += "</div>";


						if (ew != null) {
							_this.map.removeOverlay(ew);
						}

						ew = new EWindow(_this.map, (discount <= 0 ? E_STYLE_HOUSE : E_STYLE_HOUSE_DISCOUNT));
						_this.map.addOverlay(ew);
		
						if (objPoint != null) {
							ew.openOnMap(parentMarkerLatLng, html, offsetPoint);
						}
						else {
							ew.openOnMarker(objMarker, html);
						}


						ewSize = ew.getSize();

						if (ewSize) {
							targetPoint = new GPoint(targetPoint.x + Math.round(ewSize.width / 2), targetPoint.y - Math.round(ewSize.height / 2));
							targetLatLng = _this.map.fromDivPixelToLatLng(targetPoint);

						}
						else {
							targetLatLng = parentMarkerLatLng;
						}

						_this.map.panTo(targetLatLng);

					}
					else {
						//error: no such house
					}
				}
				else {
					//error in xml
				}
			}
			else {
				//error in http request
			}
			toggleLoading(false);
		}
	
		toggleLoading(true);
		cXMLHttp.load();
		waitObj.run();

	}


	//Handles clicks on normal house icons
	this.showMarkerInfo = function(oMarker) {
		var _this = this;

		var myHouses = _this.getHousesAtPoint(oMarker.getLatLng());

		switch (oMarker.type) {
			case TYPE_HOUSE :

				if (ew != null) {
					_this.map.removeOverlay(ew);
				}

				if (ewm != null) {
					_this.map.removeOverlay(ewm);
				}

				myHouse = myHouses[0];

				_this.fetchHouseInfo(myHouse, oMarker, null, null);
				break;

			case TYPE_MULTIPLE_HOUSES :
				if (ew != null) {
					_this.map.removeOverlay(ew);
				}

				if (ewm != null) {
					_this.map.removeOverlay(ewm);
				}

				var str = "";
				for (var i = 0; i < myHouses.length; i++) {
					myHouse = myHouses[i];
					str += "<img id=\"house_icon_"+ myHouse.data.locId +"\" class=\"house\" onclick=\"oGM.showHouseInfo(this, "+ oMarker.getLatLng().lat() +", "+ oMarker.getLatLng().lng() +", "+ myHouse.data.locId +");\" src=\""+ myHouse.icon.image +"\" width=\""+ myHouse.icon.iconSize.width +"\" height=\""+ myHouse.icon.iconSize.height +"\" />";
				}
				
				
				html = ""
				html += "<div class=\"content\">";
				html += "<img class=\"close\" onclick=\"return oGM.closeMultipleHouses();\" src=\"/site/images/close.png\" width=\"10\" height=\"9\" alt=\"\" />";
				html += myHouses.length +" feriehuse fundet<br />"+ str;
				html += "</div>";


				ewm = new EWindow(_this.map, E_STYLE_MULTIPLE_HOUSES);
				_this.map.addOverlay(ewm);

				ewm.openOnMarker(oMarker, html);

				var targetPoint = _this.map.fromLatLngToDivPixel(oMarker.getLatLng());
				var targetLatLng = null;
				var ewmSize = ewm.getSize();

				if (ewmSize) {
					targetPoint = new GPoint(targetPoint.x + Math.round(ewmSize.width / 2), targetPoint.y - Math.round(ewmSize.height / 2));
					targetLatLng = _this.map.fromDivPixelToLatLng(targetPoint);
				}
				else {
					targetLatLng = oMarker.getLatLng;
				}

				_this.map.panTo(targetLatLng);
				break;
		}
	}


	this.createMarker = function(gmPoint) {
		var _this = this;

		var addNewMarker = true;

		var oMarker = _this.getMarkerAtPoint(gmPoint.point);

		//marker found at this coordinate:
		if (oMarker != null) {
			addNewMarker = false;

			//Replace marker object
			_this.removeMarker(oMarker);


			//Remove reference to marker objects:
			var myHouses = _this.getHousesAtPoint(gmPoint.point);
			for (var i = 0; i < myHouses.length; i++) {
				myHouses[i].marker = null;
			}

			oMarker = new GMarker(gmPoint.point, ICON_MULTIPLE_HOUSES);
			oMarker.type = TYPE_MULTIPLE_HOUSES;

			GEvent.addListener(oMarker, "click", function() {
				_this.showMarkerInfo(this);
			});

			_this.addMarker(oMarker);

		}

		//no marker found at specified coordinate, create one:
		if (addNewMarker) {
			oMarker = new GMarker(gmPoint.point, gmPoint.icon);
			oMarker.type = TYPE_HOUSE;

			GEvent.addListener(oMarker, "click", function() {
				_this.showMarkerInfo(this);
			});
			
			_this.addMarker(oMarker);

			gmPoint.marker = oMarker;

		}
		

	}


	this.createMapPoint = function() {
		var gmPoint  = new googleMapPoint();
		gmPoint.map = _this;
		return gmPoint;
	}




}


GClientGeocoder.prototype.statusDescription = function(id) {
	switch (id) {
		case G_GEO_SUCCESS :
			return "200: No errors occurred; the address was successfully parsed and its geocode has been returned.";
			break;
		case G_GEO_BAD_REQUEST :
			return "400: A directions request could not be successfully parsed.";
			break;
		case G_GEO_SERVER_ERROR :
			return "500: A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.";
			break;
		case G_GEO_MISSING_QUERY :
			return "601: The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.";
			break;
		case G_GEO_MISSING_ADDRESS :
			return "601: Synonym for G_GEO_MISSING_QUERY.";
			break;
		case G_GEO_UNKNOWN_ADDRESS :
			return "602: No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.";
			break;
		case G_GEO_UNAVAILABLE_ADDRESS :
			return "603: The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.";
			break;
		case G_GEO_UNKNOWN_DIRECTIONS :
			return "604: The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.";
			break;
		case G_GEO_BAD_KEY :
			return "610: The given key is either invalid or does not match the domain for which it was given.";
			break;
		case G_GEO_TOO_MANY_QUERIES :
			return "620: The given key has gone over the requests limit in the 24 hour period.";
			break;
		default :
			return "unknown status";
			break;
	}
}

var oGeocoder = (GBrowserIsCompatible() ? new GClientGeocoder() : null);



function googleMapPoint() {
	var _this = this;

	this.map = null;
	this.icon = null;
	this.type = "";

	this.data = null;

	this.point = null;
	this.marker = null;

	this.accuracy = 0;
	this.getPoint = function() {
		return _this.point;
	}
	
	this.html = "";

	this.toggleLoading = function() {
		if (_this.map) {
			_this.map.toggleLoading();
		}
	}

	this.address = {
		street : "",
		street2 : "",
		zipcode : "",
		city : "",
		area : "",
		region : "",
		state : "",
		country : "",
		longitude : "",
		latitude : ""
	}

	this.addressStr = "";

	this.stripBadChars = function(s) {
		var rx = /[\*]/gi;
		return _this.trim(s.replace(rx, ""));
	}

	this.trim = function(s) {
		return s.replace(/^\s+|\s+$/g, "");
	}

	this.pointLoading = true;
}

googleMapPoint.prototype.geocodeEquals = function(lat, lng) {
	var _this = this;
	return ((_this.address.lat == lat) && (_this.address.lng == lng));
}

googleMapPoint.prototype.getAddress = function() {
	var _this = this;
	var str = "";

	if (_this.addressStr != "") {
		str = _this.addressStr;
	}
	else {
		str += _this.address.street;
		str += (str != "" ? (_this.address.street2 != "" ? ", "+ _this.address.street2 : "") : "");
		str += (str != "" ? (_this.address.zipcode != "" ? ", "+ _this.address.zipcode : "") : "");
		str += (str != "" ? (_this.address.city != "" ? ", "+ _this.address.city : "") : "");
		str += (str != "" ? (_this.address.area != "" ? ", "+ _this.address.area : "") : "");
		str += (str != "" ? (_this.address.region != "" ? ", "+ _this.address.region : "") : "");
		str += (str != "" ? (_this.address.state != "" ? ", "+ _this.address.state : "") : "");
		str += (str != "" ? (_this.address.country != "" ? ", "+ _this.address.country : "") : "");
	}

	return str;
}

googleMapPoint.prototype.getSearchAddress = function() {
	var _this = this;
	var str = "";

	if (_this.addressStr != "") {
		str = _this.addressStr;
	}
	else {
		str += _this.address.street;
		str += (str != "" ? (_this.address.street2 != "" ? ", "+ _this.address.street2 : "") : "");
		str += (str != "" ? (_this.address.zipcode != "" ? ", "+ _this.address.zipcode : "") : "");
		str += (str != "" ? (_this.address.city != "" ? ", "+ _this.address.city : "") : "");
	}
	return str;
}


googleMapPoint.prototype.completed = function() {
	var _this = this;
	return !_this.pointLoading;
}

googleMapPoint.prototype.setPoint = function(oPoint) {
	var _this = this;
	_this.pointLoading = false;
	_this.point = oPoint;
}

googleMapPoint.prototype.setAddress = function(strAddress) {
	var _this = this;
	_this.addressStr = strAddress;
}

googleMapPoint.prototype.findAddress = function() {
	var _this = this;

	if (oGeocoder != null) {
		_this.pointLoading = true;

		//Load by geographical coordinates:
		if ((_this.address.lat != "") && (_this.address.lng != "")) {
			_this.setPoint(new GLatLng(_this.address.lat, _this.address.lng, true));
		}

		//Attempt to find geographical coordinates by address string:
		else {
			//_this.pointLoading = false;

			if (_this.map != null) {
				if (_this.map.settings.enableGeocodeLookup) {
					oGeocoder.getLatLng(_this.getSearchAddress(), function(point) {
						if (!point) {
							//address not found
							_this.pointLoading = false;
						}
						else {
							_this.setPoint(point);
							_this.cachePoint();
						}
					});
				}
				else {
					_this.pointLoading = false;
				}
			}
			else {
				_this.pointLoading = false;
			}
		}
	}		
}
googleMapPoint.prototype.cachePoint = function() {
	//Default: no caching - depends on current implementation
}

window.onunload = GUnload;





function addHouse(id, locId, lat, lng, bMemo, bDiscount) {
	var mp = oGM.createMapPoint();
	mp.data = {
		id : id,
		locId : locId,
		memo : bMemo,
		hasDiscount : bDiscount
	};


	if (mp.data.memo) {
		mp.icon = (mp.data.hasDiscount ? ICON_HOUSE_DISCOUNT_MEMO : ICON_HOUSE_MEMO);
	}
	else {
		mp.icon = (mp.data.hasDiscount ? ICON_HOUSE_DISCOUNT : ICON_HOUSE);
	}

	if ((lat != 0) && (lng != 0)) {
		mp.address.lat = lat;
		mp.address.lng = lng;
		mp.findAddress();
		oGM.addMapPoint(mp);
	}
}









function refreshMapHouses() {
	if (GBrowserIsCompatible()) {
		if (cXMLHttp != null) {
			cXMLHttp.abort();
			oGM.toggleLoading(0);
		}
		
		oGM.clearHouses();
		oGM.toggleLoading(1);
		loadMapHouses();
	}
}

var cXMLHttp = null;



