var gmaps = null;
var geocoder = null;
var marker = null;
var gmaps_map_div_id = 'map';
var gmaps_map_location_id = 'map_location';
var gmaps_map_latitude_id = 'map_latitude';
var gmaps_map_longitude_id = 'map_longitude';
var gmaps_map_location_error_id = 'location_error';

Event.observe(window, 'load', function() {
	getEventFromMapLocation();
});

function setIdsForGmaps(div, loc, lat, longi, err) {
	gmaps_map_div_id = div;
	gmaps_map_location_id = loc;
	gmaps_map_latitude_id = lat;
	gmaps_map_longitude_id = longi;
	gmaps_map_location_error_id = err;
}

function reinitGmaps() {
	gmaps = null;
	geocoder = null;
	marker = null;	
}

function getEventFromMapLocation() {
	if($(gmaps_map_location_id) != null) {
		$(gmaps_map_location_id).observe('keypress', function (ev) {
			if(ev.keyCode == Event.KEY_RETURN) {
				showLocation();
				ev.stop();
			}
		});
	}	
}

function initMultipleGoogleMaps(mapClass) {
	if (!mapClass) {
		mapClass='.googleMap';
	}
	var googleMapDivs = $$(mapClass);
	
	if (googleMapDivs.length > 0 ) {
			googleMapDivs.each(function(elm, index){

					if (!elm.id || elm.id=='undefined') {
						elm.id='gmap-'+index;
					}
					var tmp_lat = 49.282963;
					var tmp_long = -123.086874;
					var tmp_zoom = 15;
					var useOverlay = true;
					tmp_lat = elm.down(".mapLatitude").value;
					tmp_long = elm.down(".mapLongitude").value;
					tmp_zoom = parseInt(elm.down(".mapZoom").value);
					userOverlay = elm.down(".mapUseTaggaOverlay").value;
					if(GBrowserIsCompatible()) {
							var latLng = new GLatLng(tmp_lat, tmp_long);
							var gmaps = new GMap2(document.getElementById(elm.id));
							gmaps.setCenter(latLng, tmp_zoom);
							gmaps.checkResize();
							if (useOverlay && userOverlay=='true') {
								var taggaIcon = new GIcon(G_DEFAULT_ICON);
								taggaIcon.image = "/img/GMapIcon-tagga.png";
								taggaIcon.shadow = "/img/GMapIcon-tagga.png";
								taggaIcon.iconSize = new GSize(68, 47);
								taggaIcon.shadowSize = new GSize(68, 47);
								taggaIcon.iconAnchor = new GPoint(40, 45);
								var point = new GLatLng(tmp_lat, tmp_long); 
								var marker = new GMarker(point, taggaIcon); 
								gmaps.addOverlay(marker);
							}
							//gmaps.setZoom(parseInt(tmp_zoom));

					}	
			});
		
		
	}
}

function initGoogleMaps() {

	if(GBrowserIsCompatible()) {
	
		if (gmaps == null) {
			
			// init the map
			var latLng = new GLatLng($(gmaps_map_latitude_id).value, $(gmaps_map_longitude_id).value);
			
			gmaps = new GMap2(document.getElementById(gmaps_map_div_id));
			gmaps.setCenter(latLng, 10);
			mapControlZoom = new GSmallMapControl();
			mapControlType = new GHierarchicalMapTypeControl();
			gmaps.addControl(mapControlZoom);
			gmaps.addControl(mapControlType);
			
			// init the geocoder
			if(geocoder == null) {
				
				geocoder = new GClientGeocoder();
			}
			
			// show the marker if there is already a location specified
			if($(gmaps_map_location_id).value != null && $(gmaps_map_location_id).value.length > 0) {
			
				showLocation();
			}
		} else {
		
			gmaps.checkResize();
		}
	}
}

function showLocation() {
		
	var address = $(gmaps_map_location_id).value;
	if(address != null && address.length > 0 && geocoder != null) {
	
		$(gmaps_map_location_error_id).style.display = 'none';
		geocoder.getLatLng(address, function(point) {
			
				if(point != null) {
				
					$(gmaps_map_longitude_id).value = point.lng();
				 	$(gmaps_map_latitude_id).value = point.lat();
				 	gmaps.setCenter(point, 14);
				 	showMarker(point, address);
				} else {
				
					$(gmaps_map_location_error_id).style.display = 'block';
				}
			}
		);
	}
}

function showMarker(point, address) {

	if(marker == null) {
	
		marker = new GMarker(point);
		gmaps.addOverlay(marker);
	} else {
	
		marker.setLatLng(point);
	}
}

//Event.observe(window, 'unload', GUnload());