$(document).ready(function()
{
	if ( ! GBrowserIsCompatible())
	{
		// Get the map
		var map = $('#map').css('text-align', 'center');

		// Create the upgrade warning
		$('<h4></h4>').html('Your browser is not compatible with Google Maps!')
			.css('font-size', '200%').css('padding-top', '15%').appendTo(map);

		// Add the upgrade link
		$('<p></p>').html('Please consider <a id="upgrade_browser">upgrading your browser</a> for better compatibility.')
			.css('font-size', '120%').appendTo(map);

		// Get the upgrade link
		var link = $('a#upgrade_browser', map).attr('target', '_upgrade');

		// Choose the appropriate download link
		switch ($.browser)
		{
			case 'msie':   link.attr('href', 'http://www.microsoft.com/windows/downloads/ie/getitnow.mspx'); break;
			case 'opera':  link.attr('href', 'http://www.opera.com/download/');                              break;
			case 'safari': link.attr('href', 'http://www.apple.com/safari/download/');                       break;
			default:       link.attr('href', 'http://www.mozilla.com/en-US/firefox/');                       break;
		}

		return false;
	}

	// Unload the map when the window is closed
	$(document.body).unload(GUnload);

	// Create a new custom map
	var map = new AZMap(document.getElementById('map'));

	// Map search form
	var search  = $('#map_search form');
	var results = $('#map_search_results');

	$('#map_legend a').click(function()
	{
		if (map.search_results == true)
		{
			// Reset search results and map
			results.html('');
			map.mgr.clearMarkers();
			map.goToCenter();

			$.getJSON(base_url +'points', function(collection)
			{
				// Load the map points
				map.loadCollection(collection, false);
			});
		}
		else
		{
			// Close all open windows
			map.openMarker(null);

			// Go back to the center and zoom out
			map.goToCenter();
		}

		return false;
	}).click();

	// Bind searches
	search.submit(function()
	{
		// Update the results with a searching status
		results.html('<p><em>Searching...</em></p>');

		// Close all markers
		map.openMarker(null);

		$.getJSON(base_url +'search', search.serialize(), function(collection)
		{
			if (collection == false)
			{
				results.html('<dl><dt>No results found.</dt><dd>Try using a country name instead of a city, or vice-versa.</dd></dl>');
				return false;
			}

			// Load all the map points
			map.loadCollection(collection, true, true);

			// Create the new list of results
			results.html('<dl></dl>');

			// Document body
			var body = $('body');

			// Open the list
			var list = results.find('dl:first');
			$.each(collection.places, function(name, place)
			{
				// Create the popup
				var popup = $('<div></div>').html(place.html);

				// Get the title, description, and address from the popup
				var title = $('p.title', popup).text();
				var description = $('p.description', popup).html();
				var address = $('p.address', popup).html();

				if (description != '')
				{
					// Add the decription to the address
					address = '<strong>'+ description +'</strong><br/>'+ address;
				}

				// Add the search result
				list.append('<dt><a href="#map">'+ title +'</a></dt>').append('<dd>'+ address +'</dd>');

				list.find('dt:last a[href="#map"]').click(function()
				{
					// Get the marker from the manager
					var marker = map.mgr.getMarker(place.lat, place.lon, 16);

					// Simulate a click to the marker
					GEvent.trigger(marker, 'click');

					return false;
				});
			});
		});

		return false;
	});
});
