$(document).ready(function() {
    var myOptions = {
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var bounds = new google.maps.LatLngBounds();
	var currentInfoWindow = null;
	for (var i = 0; i < jsProps.length; i++) {
		(function(i){
			var latLng = new google.maps.LatLng(jsProps[i].decLat, jsProps[i].decLng);
			bounds.extend(latLng);
			var marker = new google.maps.Marker({
				position: latLng,
				map: map,
				icon: "/images/icons/pin-point-" + (i + 1) + ".gif"
			});
			var $listing = $(".listing");
			var $bubble = $("#bubble");
			var $bubbleDetails = $("#bubble-details");
			var $bubblePhoto = $("#bubble-photo");
			var $bubbleDetailLink = $(".bubble-detail-link");
			google.maps.event.addListener(marker, "mouseover", function() {
				var $currentListing = $listing.eq(i);
				$bubbleDetails.html($currentListing.find("p.details").html());
				$bubblePhoto.attr("src", $currentListing.find(".photobox img").attr("src"));
				$bubbleDetailLink.attr("href", $currentListing.find(".photobox a").attr("href"));
				
				var infoWindow = new google.maps.InfoWindow({
					content: $bubble.html(),
					position: latLng
				});
				if (infoWindow !== currentInfoWindow) {
					if (currentInfoWindow != null) {
						currentInfoWindow.close();
					}
					infoWindow.open(map);
					currentInfoWindow = infoWindow;
				}
			});
		})(i);
	}
	var oneTimeListener = google.maps.event.addListener(map, "bounds_changed", function() {
		google.maps.event.removeListener(oneTimeListener);
		map.setZoom(map.getZoom() - 0);
	});
	map.fitBounds(bounds);
	
	$(".save-listing-link").click(function (e) {
		var guiCustomerId = $(this).siblings("input[name='guiCustomerId']").val();
		if (guiCustomerId == "") {
			return;
		}					
		e.preventDefault();
		var guiPropertySummaryId = $(this).siblings("input[name='guiPropertySummaryId']").val();
		$link = $(this);
		$.get("/search/save-property-async.cfm", {
			"guiPropertySummaryId": guiPropertySummaryId,
			"guiCustomerId": guiCustomerId
		}, function () {
			$link.fadeOut("slow", function() {
				$link.siblings(".listing-saved").fadeIn("slow");
			});
		});
		
	});
});
