var map;
var geocoder;
var marker

function load(latitude, longitude) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    if (apartman_latitude!='' && apartman_longitude!='' && apartman_latitude!='0.000000' && apartman_longitude!='0.000000')
    	var center = new GLatLng(apartman_latitude, apartman_longitude);
    else
			var center = new GLatLng(47.498403, 19.040759);
		map.setCenter(center, 14);
		map.addControl(new GSmallMapControl());
		
		marker = new GMarker(center, {draggable: true});
		if (apartman_latitude=='0.000000' && apartman_longitude=='0.000000') {
			var point = marker.getLatLng();
			document.getElementById("latitude").value = point.y;
			document.getElementById("longitude").value = point.x;
		}
		GEvent.addListener(marker, "dragend", function() {
			var point = marker.getLatLng();
			document.getElementById("latitude").value = point.y;
			document.getElementById("longitude").value = point.x;
		});
		
		map.addOverlay(marker);
		
		geocoder = new GClientGeocoder();
  }
}

function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		alert(address_error);
	} else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1],
		                  place.Point.coordinates[0]);
		marker.setPoint(point);
		map.addOverlay(marker);
		map.setCenter(point, 14);
		document.getElementById("latitude").value = point.y;
		document.getElementById("longitude").value = point.x;
	}
}

function showLocation() {
	var address = document.getElementById('search_address').value;
	geocoder.getLocations(address, addAddressToMap);
} 