// An instance of GClientGeocoder
var g_geocoder;
var g_goldMemberIcon;
var g_silverMemberIcon;

var g_site_base_path = location.protocol + "//" + location.host + "/";
//g_site_base_path += "chn/"; // TODO: Remove once the site is live

function create_marker_with_html(map_in, point_in, membership, html_in)
{
	membership = parseInt(membership);
	
	// Create the marker
	var markerOptions = {
		/* zIndexProcess:calc_custom_zindex */
	};
	var marker = null;
	switch (membership) {
		case 2: // Silver
			markerOptions.icon = g_silverMemberIcon;
			//marker = new GMarker(point_in, {icon:g_silverMemberIcon});
			break;
			
		case 3: // Gold
			markerOptions.icon = g_goldMemberIcon;
			//marker = new GMarker(point_in, {icon:g_goldMemberIcon});
			break;
		
		default:
			//marker = new GMarker(point_in);
	}
	marker = new GMarker(point_in, markerOptions);
	if (marker) {
		// Specify the membership value of this marker
		marker.membership = membership;
			
		// Give the marker an on-click event
		if (html_in != undefined) {
			GEvent.addListener(marker, "click", onclick_handler_for_marker.bind(marker, html_in));
		}
		
		// Add the marker to the map
		map_in.addOverlay(marker);
		
		// Auto-open the info window
		marker.openInfoWindowHtml(html_in);
		
		// Return a reference to the created marker (just to be nice).
		return marker;
	} else {
		return null;
	}
}

function generate_html_for_listing(listing_in)
{
	var str = "";
	str += "<div style=\"width: 250px;\">";
	str += "<h2><span><a href=\"property?id=" + listing_in.id + "\">" + listing_in.name + "</a></span></h2>";
	str += "<div class=\"popup_content\">";
		str += "<span class=\"street_address\">";
			str += listing_in.address;
			if (listing_in.address2) {
				str += ", <span class=\"address2\">" + listing_in.address2 + "</span>";
			}
		str += "</span>";
		str += "<span class=\"location\">" + listing_in.city + ", " + listing_in.state + " " + listing_in.zip + "</span>";
		//str += "<a href=\"property?id=" + listing_in.id + "\">View this property</a>";
	str += "</div>";
	str += "</div>";
	
	return str;
}

function onclick_handler_for_marker(html_to_show)
{
	this.openInfoWindowHtml(html_to_show);
}

/* start_geocode_request2() exists to allow for closures. The two arguments
	(map_in, property_in) are needed inside the anonymous function
	but can't be passed into the source of the callback (getLatLng).  Prototype
	could have been used, but it's just doing the same thing behind the scenes.
*/
function start_geocode_request2(map_in, property_in)
{
	// Generate the merged address string
	var address = property_in.address;
	if (property_in.address2) { if (property_in.address2.length > 0) {
		address += ", " + property_in.address2;
	}}
	address += ", " + property_in.city;
	if (property_in.country == "United States of America") {
		address += ", " + property_in.state;
	}
	if (property_in.zip) { if (property_in.zip.length > 0) {
		address += " " + property_in.zip;
	}}
	address += ", " + property_in.country;

	//alert("Coordinates unavailable; using geocoding the address " + address); 
	
	g_geocoder.getLatLng(
		address,
		function(point) {
			// Inherited: map_in, property_in
			if (point) {
				// Set the map center
				map_in.setCenter(point, 7); // "State"

				// Create the marker
				var html = generate_html_for_listing(property_in);
				create_marker_with_html(map_in, point, property_in.membership, html);
				
				// TODO: Throw off an AJAX request to update the database with these lat/lon coordinates.
			} else {
				//alert("failed geocode: " + point);
			}
		}
	);
}

function property_init()
{
	//alert("Google map coming soon...");
	
	// Will Google Maps work?
	if (GBrowserIsCompatible()) {
		// Does the map container exist?
		//var objMap = document.getElementById("search_results_map");
		var objMap = $("property_map");
		
		if (!objMap) {
			//alert("Cannot find the map container.");
		} else {
			objMap.addClassName("scripted");
			
			// Create our "tiny" marker icon
			g_goldMemberIcon = new GIcon(G_DEFAULT_ICON);
			g_goldMemberIcon.image = g_site_base_path + "images/_google_maps/goldpin.png";
			g_goldMemberIcon.iconSize = new GSize(20, 34);

			g_silverMemberIcon = new GIcon(G_DEFAULT_ICON);
			g_silverMemberIcon.image = g_site_base_path + "images/_google_maps/silverpin.png";
			g_silverMemberIcon.iconSize = new GSize(20, 34);

			// Create the map
			var map = new GMap2(objMap);

			// Add a pan/zoom control
			map.addControl(new GSmallMapControl());
			//map.addControl(new GLargeMapControl());
			
			// Center the map on America, zoomed out
			map.setCenter(new GLatLng(37.0625, -95.677068), 4);

			// Get the location of the property
			if (g_property.lat || g_property.lon) {
				// Create a lat/lon object from the .lat and .lon properties
				var point = new GLatLng(g_property.lat, g_property.lon);
	
				//alert("Coordinates available, using (" + g_property.lat + ", " + g_property.lon + ")");

				// Set the new zoomed-in center
				map.setCenter(point, 7); // "State"
				
				// Create the marker
				var html = generate_html_for_listing(g_property);
				//alert("HTML for marker = " + html);
				create_marker_with_html(map, point, g_property.membership, html);
			} else {
				g_geocoder = new GClientGeocoder();
				
				// Start a request for lat/long coordinates
				start_geocode_request2(map, g_property);
			}
			
			// Create the marker 
				
		} // end if map
	} // end GBrowserIsCompatible
}

// Require that Prototype is available
if (typeof Prototype == "undefined") {
	//alert("The Google Maps on this page require that Javascript is enabled.");
} else {
	document.observe("dom:loaded", property_init);
}

