var _map = null;
var _location = null;

function loadMap(lat, lng) {
	var mapNode = document.getElementById("map");
	setMapSize(mapNode);

	if (GBrowserIsCompatible()) {
		_map = new GMap2(mapNode);
		_location = new GLatLng(lat, lng);
		_map.addControl(new GSmallMapControl());
		_map.setCenter(_location, 15);

		var icon = new GIcon();
		icon.image = "http://onthe.net/common/surgery_grey/images/map_marker.png";
		icon.shadow = "http://onthe.net/common/surgery_grey/images/map_marker_shadow.png";
		icon.iconSize = new GSize(35, 36);
		icon.shadowSize = new GSize(32, 36);
		icon.iconAnchor = new GPoint(9, 36);

		var marker = new GMarker(_location, icon);

		GEvent.addListener(marker, "click", function() {
			_map.setCenter(_location, 15);
		});

		GEvent.addListener(_map, "dblclick", function() {
			_map.setCenter(_location, 15);
		});

		_map.addOverlay(marker);
	}
}

function resizeMap() {
	var mapNode = document.getElementById("map");
	setMapSize(mapNode);
	if (_map != null)
		_map.setCenter(_location, 15);
}

function setMapSize(mapNode) {
	mapNode.style.height = Math.max(200, Math.min(mapNode.offsetWidth / 4 * 3, document.body.clientHeight - getElementPosition(mapNode).y - (mapNode.offsetHeight - mapNode.clientHeight) - 5)) + "px";
}

