var ot_marker  = null;
var ot_point_x = 0;
var ot_point_y = 0;
var ot_latid   = "";
var ot_lonid   = "";
var geocoder   = new GClientGeocoder();
var map        = null;

function setup_gmaps(mapid, latid, lonid, lat, lon, zoom, arealocate, editable) {
	ot_latid = latid;
	ot_lonid = lonid;

	if (GBrowserIsCompatible()) {
		w_lat = lat;
		w_lon = lon;
		w_zom = zoom;

		if ( lat == null || lon == null ) {
			w_lat = 38.7;
			w_lon = 137.8;
			w_zom = 5;
		}

		map = new GMap2(document.getElementById(mapid));
		map.setCenter(new GLatLng(w_lat, w_lon), w_zom);
		map.addControl(new GSmallMapControl());

		if ( arealocate == 0 && lat != null && lon != null ) {
			ot_marker = new GMarker(new GLatLng(lat, lon));
			map.addOverlay(ot_marker);
		}

		if ( editable != 0 ) {
			document.getElementById(ot_lonid).value     = lon;
			document.getElementById(ot_lonid).innerText = lon;
			document.getElementById(ot_latid).value     = lat;
			document.getElementById(ot_latid).innerText = lat;

			GEvent.addListener(map, 'click',
				function(overlay, point) {
					map.clearOverlays();
					document.getElementById(ot_lonid).value     = null;
					document.getElementById(ot_lonid).innerText = null;
					document.getElementById(ot_latid).value     = null;
					document.getElementById(ot_latid).innerText = null;

					if ( point != null && point.x != null && point.y != null ) {
						point_x = point.x;
						point_y = point.y;

						document.getElementById(ot_lonid).value     = point_x;
						document.getElementById(ot_lonid).innerText = point_x;
						document.getElementById(ot_latid).value     = point_y;
						document.getElementById(ot_latid).innerText = point_y;

						ot_marker = new GMarker(new GPoint(point_x, point_y));
						map.addOverlay(ot_marker);
					}
				}
			);
		}
	}
}

function setAddress(mapid, latid, lonid, zom, address) {
	if ( geocoder ) {
		geocoder.getLatLng(
			address,
			function(point) {
				if ( point != null && point.x != null && point.y != null ) {
					point_x = point.x;
					point_y = point.y;

					document.getElementById(lonid).value     = point_x;
					document.getElementById(lonid).innerText = point_x;
					document.getElementById(latid).value     = point_y;
					document.getElementById(latid).innerText = point_y;

					map.setCenter(new GLatLng(point_y, point_x), zom);

					ot_marker = new GMarker(new GPoint(point_x, point_y));
					map.addOverlay(ot_marker);
				} else {
					document.getElementById(lonid).value     = "";
					document.getElementById(lonid).innerText = "";
					document.getElementById(latid).value     = "";
					document.getElementById(latid).innerText = "";

					ot_marker = null;
				}
			}
		);
	}
}

