
var VERO_BEACH_LNG = -80.3972736;
var VERO_BEACH_LAT =  27.6386434;

function marker_click(event)
{
	var listing, ml;
	
	listing = g_listings[this.listing_id];
	ml = '\
		<div style="float:left;height:150px;">\
			<div style="float:left;width:150px;">\
				<img src="'+listing.photo_url+'" style="width:150px;" />\
			</div>\
			<div style="float:left;margin-left:8px;margin-top:20px;width:200px;">\
				<p>'+listing.addr+'</p>\
				<p>'+listing.city+', '+listing.state+' '+listing.zip+'</p>\
				<p>'+listing.bedrooms+' beds, '+listing.baths+' baths</p>\
				<p>'+format_dollars(listing.sale_price, false)+'</p>\
				<p><a href="'+g_www_root+'listing/?lid='+this.listing_id+'" target="_blank">Details</a></p>\
			</div>\
		</div>\
		<div class="clr"></div>\
	';
	this.openInfoWindowHtml(ml);
}

function add_listings()
{
	var bounds, i, id, listing, point;
	
	
	bounds = new g_mapi.LatLngBounds();
	i = 0;
	for (id in g_listings)
	{
		listing = g_listings[id];
		if (listing.lat == "14.0128638") continue;
		/*
		icon = new ns.Icon(G_DEFAULT_ICON);
		icon.image = "/img/gmap_marker/"+((location.is_advantage == 1) ? "blue" : "red")+"_"+(i+1)+".png";
		icon.shadow = "/img/gmap_marker/shadow.png";
		*/
		//dbg(listing.city+", "+listing.state+'::'+listing.lat+','+listing.lng);
		point = new g_mapi.LatLng(listing.lat, listing.lng);
		marker = new g_mapi.Marker(point, {"title":listing.addr});
		marker.listing_id = id;
		
		g_mapi.Event.addListener(marker, "click", marker_click);
		g_map.addOverlay(marker);
		
		bounds.extend(point);
		//if (i > 3) break;
		++i;
	}
	g_map.setZoom(g_map.getBoundsZoomLevel(bounds));
	g_map.setCenter(bounds.getCenter());
}

function google_callback()
{
	g_mapi = google.maps;
	g_map = new g_mapi.Map2($dom('#map_container'));
	g_map.setCenter(new g_mapi.LatLng(VERO_BEACH_LAT, VERO_BEACH_LNG), 13);
	g_map.addControl(new g_mapi.LargeMapControl3D());
	g_map.addControl(new g_mapi.MapTypeControl());
	
	add_listings();
	
	$('input[value="Update Map"]').click(map_update_submit);
}

var g_mapi, g_map;

function map_update_submit()
{
	var empty_ignore_fields, i, field;
	
	empty_ignore_fields = ['min_price', 'max_price', 'min_beds', 'min_baths'];
	for (i = 0; i < empty_ignore_fields.length; ++i)
	{
		field = empty_ignore_fields[i];
		if (empty($f(field))) $('#f [name='+field+']').remove();
	}
}

