
var map			= null;
var geocoder	= null;
var marker		= null;

$(document).ready(function(){
	
	initialize();
	showAddress(start_address);
});

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    geocoder = new GClientGeocoder();
	
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
  }
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				
				if (!point) {
					//alert(address + " not found");
					
					// Ajax cache coordinates
					var target = "/events/cache_coords/" + event_id + "/e_e";
					$.post(target, null, function(data){
						// Don't need to do anything
					});
				
				} else {
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(full_address + "<br /><a href='" + directions + "'>Get Direction To Here</a>");

					GEvent.addListener(marker, "click", function() {
						showAddress(start_address);
					});

					document.getElementById('coords').innerHTML = point;

					// Ajax cache coordinates
					var coords = String(point);
					coords = str_replace("(", "", coords);
					coords = str_replace(")", "", coords);										   
					coords = str_replace(", ", "_", coords);											
					var target = "/events/cache_coords/" + event_id + "/" + coords;
					
					$.post(target, null, function(data){
						// Don't need to do anything
					});
				}
			}
		);
	}
}

