var defaultSearchText = "City or Zip";

$(document).ready(function(){

	// Search box autofill
	$("#q").focus(function() {
		if ($(this).val() == defaultSearchText) $(this).val("");
	});
	$("#q").blur(function(){
		if ($(this).val() == "") $(this).val(defaultSearchText);
	});

	$("#q").blur();

	// Start GMaps on #eventmap
	if (GBrowserIsCompatible()) {
		// Load map and add controls
		spotMap = new GMap2(document.getElementById("eventmap"));

		// Set the right zoom level and center if we need bounds
		if (typeof minLat != 'undefined' && typeof maxLat != 'undefined' && typeof minLon != 'undefined' && typeof maxLon != 'undefined') {
			var mapBounds = new GLatLngBounds(new GLatLng(minLat, minLon), new GLatLng(maxLat, maxLon));
			var mapCenter = new GLatLng((maxLat + minLat) / 2, (maxLon + minLon) / 2);
			spotMap.setCenter(mapCenter, spotMap.getBoundsZoomLevel(mapBounds));
		}
		else {
			spotMap.setCenter(new GLatLng(startLat,startLon), startZoom);
		}

		spotMap.setMapType(eval(spotMapType));
		spotMap.addControl(new GMapTypeControl());
		spotMap.addControl(new GSmallMapControl());

		currentDate = new Date();
		currentYear = parseInt(currentDate.getFullYear());

		// Add a zoom listener to refresh spots
		GEvent.addListener(spotMap, "zoomend", function() {
			showSpots({ sortBy: "date_added", year: currentYear, limit: "20" });
		});
		// Add a drag listener to refresh spots
		GEvent.addListener(spotMap, "dragend", function() {
			showSpots({ clearPrevious : false, sortBy: "date_added", year: currentYear, limit: "20" });
		});
		// Add a move listener to refresh spots
		GEvent.addListener(spotMap, "moveend", function() {
			showSpots({ clearPrevious : false, sortBy: "date_added", year: currentYear, limit: "20" });
		});

		// Restrict the range of zoom
		// Get the list of map types
		var mt = spotMap.getMapTypes();
		// Overwrite the getMinimumResolution() and getMaximumResolution() methods
		for (var i = 0; i < mt.length; i++) {
			mt[i].getMinimumResolution = function() {return 1;}
		}

		// Set loading box
		$("#eventmap").append("<div class=\"loadingstatus\" style=\"display: none\"><p>Loading...</p></div>");

		// Show latest spots
		showSpots({ sortBy: "date_added", year: currentYear, limit: "20" });

	}

});