	var geocoder;
	var map;
	var lat;
	var lng;
	var id;
	var html;
	var typ_id;
	var url;
	var mapControl = null;
	var baseIcon;


	var bounds = new GLatLngBounds();

	function loadMap()
	{
		baseIcon = new GIcon();
		baseIcon.shadow = "http://www.vagabund.biz/img/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);


		if(map == null) map = new GMap2(document.getElementById("map"));
		if(geocoder == null) geocoder = new GClientGeocoder();

		map.clearOverlays();

		var start = new GLatLng(0, 0);
		map.setCenter(start, 2);
	}


	function addPlace(size, data, lid, html, typ_id, url)
	{
		if(mapControl == null) mapControl = addMapControl(size);

		id = lid;
		control = size;
		html = html;
		typ_id = typ_id;
		url = url;
     		geocoder.getLocations(data, addToMap);
	}


	function addPlaceLatLng(size, str_lat, str_lng, html, typ_id, url, new_marker)
	{
		if(mapControl == null) mapControl = addMapControl(size);

		if(new_marker == 'orange')
		{
       			var icon = new GIcon(baseIcon);
			icon.image = "http://www.vagabund.biz/_images/marker_orange_"+typ_id+".png";
		}
		else
		{
       			var icon = new GIcon(baseIcon);
			icon.image = "http://www.vagabund.biz/_images/marker_"+typ_id+".png";
		}



		point = new GLatLng(str_lat, str_lng);
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(html);
		});
		GEvent.addListener(marker, "click", function() {
			window.location = url;
		});

		/*
		GEvent.addListener(marker, "mouseout", function() {
			map.closeInfoWindow();
		});
		*/

		map.addOverlay(marker);

		bounds.extend(marker.getPoint());
	}


	function addMapControl(control)
	{
		if(control == 'small')
		{
			map.addControl(new GSmallMapControl());
			map.setMapType(G_NORMAL_MAP);
		}
		else
		{
			map.addControl(new GSmallMapControl());
  			map.addControl(new GMapTypeControl());
			map.setMapType(G_HYBRID_MAP);
		}
	}


	function addToMap(response)
	{
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);

		lat = place.Point.coordinates[1];
		lng = place.Point.coordinates[0];


		if(id != undefined && id != '' && lat != undefined && lng != undefined){
			img = document.createElement('img');
			img.alt = '';
			img.style.width = '1';
			img.style.height = '1';
			img.src = 'http://www.vagabund.biz/geo/save_geo.php?id='+id+'&lat='+lat+'&lng='+lng;
			document.getElementById('img_geo').parentNode.appendChild(img);
		}

       		var icon = new GIcon(baseIcon);
		icon.image = "http://www.vagabund.biz/_images/marker_"+typ_id+".png";

		point = new GLatLng(lat, lng);
		var marker = new GMarker(point, icon);

		//map.setCenter(point, 14);
		//marker = new GMarker(point, icon);
		map.addOverlay(marker);
		bounds.extend(marker.getPoint());
	}


	function loadMapAgain()
	{
		var center = bounds.getCenter();
		map.setCenter(center);

		var zoom = Math.min(map.getBoundsZoomLevel(bounds), 15);
		map.setZoom(zoom);
	}



	function editMap(latitude, longitude, zoom, view_mode, preview)
	{
		//if (!latitude)  latitude  = g_latitude;
		//if (!longitude) longitude = g_longitude;
		if (!zoom)      zoom      = g_zoom;
		//if (!view_mode) view_mode = g_view_mode;

		zoom = parseInt(g_zoom);


		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());


		type = G_NORMAL_MAP;
		map.setCenter(new GLatLng(g_latitude,g_longitude), zoom, type);


		document.getElementById("latitude").value   = latitude;
		document.getElementById("longitude").value  = longitude;


		var pointee = null;
		var marker = new GMarker(g_point);
		map.addOverlay(marker);

		GEvent.addListener(map, 'click', function(overlay, point){
			if (!point) return;

			kokot = new GMarker(point);
			map.removeOverlay(marker); // Remove the first marker.
			map.addOverlay(kokot);     // Remove the last pin before placing new one.

			// Center the map where the pin is.
			map.panTo(new GLatLng(point.y, point.x));

			// Write the form values.
			document.getElementById("longitude").value = point.x;
			document.getElementById("latitude").value  = point.y;

			if (pointee) map.removeOverlay(pointee);
			pointee = kokot;
		});

		// Capture zoom level.
		GEvent.addListener(map, "moveend", function(){
			document.getElementById("zoom").value = map.getZoom();
		});

		// Capture current map view mode.
		GEvent.addListener(map, "maptypechanged", function() {
			var myMapType = map.getCurrentMapType();
			if (myMapType == G_SATELLITE_MAP){
				document.getElementById("view_mode").value = 'G_SATELLITE_MAP';
			}
			if (myMapType == G_NORMAL_MAP){
				document.getElementById("view_mode").value = 'G_NORMAL_MAP';
			}
			if (myMapType == G_HYBRID_MAP){
				document.getElementById("view_mode").value = 'G_HYBRID_MAP';
			} 
		});
	}

	