/*
<fusedoc fuse="fbx_Switch.cfm" language="ColdFusion" specification="3.0">
	<responsibilities>
		Google Maps Integration Ajax Framework: Client-Side Scripting
	</responsibilities>
	<properties>
		<history 
			author="Ryan J. Heldt" comments="" 
			date="12/05/2005" 
			email="rheldt@globalreach.com" 
			role="Architect" type="Create" />
	</properties>
</fusedoc>
*/

// GENERAL FUNCTIONS

function createMarker(point,html,icon) {
	// We'll use this function to make adding markers on the map eaiser.
	if (icon.shadow != ''){
		var marker = new google.maps.Marker({
			position: point,
			icon: new google.maps.MarkerImage(
							icon.image,
							icon.iconSize,
							new google.maps.Point(0,0),
							icon.iconAnchor
			),
			shadow: new google.maps.MarkerImage(
							icon.shadow,
							icon.shadowSize,
							new google.maps.Point(0,0),
							icon.iconAnchor
			),
		});
	}
	else{
		var marker = new google.maps.Marker({
			position: point,
			icon: new google.maps.MarkerImage(
							icon.image,
							icon.iconSize,
							new google.maps.Point(0,0),
							icon.iconAnchor
			)
		});
	}
	
	google.maps.event.addListener(marker, 'click', (function(markerArg) {
		return function() {
			objInfoWindow.setContent(html);
			objInfoWindow.setOptions({pixelOffset: new google.maps.Size(5,10)});
			objInfoWindow.open(map, markerArg);
		};
	})(marker));
	return marker;
}

// PROPERTY LISTINGS

function getListings() {
	DWREngine._execute(_cfscriptLocation,null,'getListings',streetname,bedrooms,locations,listingclassifications,styles,maxprice,minprice,squarefeet,listingid,zoomlevel,showListings);
}

function showListings(result) {
	if (result=="NOLISTINGS") {
		//No listings found
		var mapObject = document.getElementById("map")
		var sidebarObject = document.getElementById("map_sidebar")
		mapObject.style.border="none";
		mapObject.style.backgroundColor="#FFFFFF";
		mapObject.innerHTML="<p class=\"subtitle\">No Properties Found</p><p>Sorry, there no properties matching the search criteria you specified.<br />If you like, you may <a href=\"index.cfm?fuseaction=properties.search\">modify your search criteria</a></p>";
		sidebarObject.innerHTML="";
	} else {
		// Parse returned values into an array we can loop through
		var listings = result.split("^");
		
		// Center map on mid point of selected markers - listings[0] = Longitude, listings[1] = Latitude, listings[2] = Zoom Level
		map.setCenter(new google.maps.LatLng(listings[1],listings[0]));
		map.setZoom(17 - listings[2]);
		
		// Loop through the result
		for (var rr=3; rr<listings.length; rr++) {
			var currentListing = listings[rr].split("|");
			var currentHTML = "";
			// Generate HTML for Popup Window
			currentHTML += "<div id=\"popupWindow\" style=\"width: 300px;\"><a href=\"index.cfm?fuseaction=properties.detail&ListingID=";
			currentHTML += currentListing[0];
			//currentHTML += "\"><img src=\"http://www.nextgenerationrealty.com/images/listingphotos/";
			currentHTML += "\"><img src=\"";
			currentHTML += mediaRoot;
			currentHTML += "/listing_photos/";
			currentHTML += currentListing[8];
			currentHTML += "\" width=\"100\" border=\"0\" hspace=\"5\" align=\"left\"></a><strong>";
			currentHTML += currentListing[1];
			currentHTML += "</strong><br /><span class=\"subtitle\">";
			currentHTML += currentListing[2];
			currentHTML += " ";
			currentHTML += currentListing[3];
			currentHTML += "</span><br />";
			currentHTML += currentListing[4];
			currentHTML += ", ";
			currentHTML += currentListing[5];
			currentHTML += "<br />";
			currentHTML += currentListing[6];
			currentHTML += " &bull; ";
			currentHTML += currentListing[7];
			currentHTML += "</div><div style=\"clear: left;\" align=\"right\"><strong><a href=\"index.cfm?fuseaction=properties.detail&ListingID=";
			currentHTML += currentListing[0];
			currentHTML += "\">Details&nbsp;&raquo;</a></strong></div>";
			
			// Plot marker
			var point = new google.maps.LatLng(currentListing[9], currentListing[10]);
			var marker = createMarker(point, currentHTML, iconListing);
			marker.setMap(map);
		}
	}
}

// SOLD PROPERTY LISTINGS

function getSoldListings(obj) {
	if (obj.checked==true) {
		DWREngine._execute(_cfscriptLocation,null,'getSoldListings',streetname,bedrooms,locations,listingclassifications,styles,maxprice,minprice,squarefeet,showSoldListings);
	} else {
		deleteSoldListings();
	}
}

function showSoldListings(result) {
	// Parse returned values into an array we can loop through
	var listings = result.split("^");
	// Loop through the result
	for (var rr=0; rr<listings.length; rr++) {
		var currentListing = listings[rr].split("|");
		var currentHTML = "";
		// Generate HTML for Popup Window
		currentHTML += "<div id=\"popupWindow\" style=\"width: 300px;\"><a href=\"index.cfm?fuseaction=analysis.\">"
		currentHTML += "<img src=\"";
		currentHTML += mediaRoot;
		currentHTML += "/listing_photos/";
		currentHTML += currentListing[8];
		currentHTML += "\" width=\"100\" border=\"0\" hspace=\"5\" align=\"left\"></a><strong class=\"redtext\">SOLD! ";
		currentHTML += currentListing[1];
		currentHTML += "</strong><br /><span class=\"subtitle\">";
		currentHTML += currentListing[2];
		currentHTML += " ";
		currentHTML += currentListing[3];
		currentHTML += "</span><br />";
		currentHTML += currentListing[4];
		currentHTML += ", ";
		currentHTML += currentListing[5];
		currentHTML += "<br />";
		currentHTML += currentListing[6];
		currentHTML += " &bull; ";
		currentHTML += currentListing[7];
		currentHTML += "</div><div style=\"clear: left; padding: 2px; font-size: 10px; color: #656565; \">Learn more about this sold property, and many others using<br />NextAnalysis, our free market analysis tool.</div>";
		currentHTML += "<div style=\"clear: left;\" align=\"right\"><strong><a href=\"index.cfm?fuseaction=analysis.";
		currentHTML += "\">Learn More&nbsp;&raquo;</a></strong></div>";
		// Plot marker
		var point = new google.maps.LatLng(currentListing[9], currentListing[10]);
		var marker = createMarker(point, currentHTML, iconListingSold);
		marker.setMap(map);
	}
}

function deleteSoldListings() {
	for (var rr=0; rr < iconListSold.length; rr++) {
		iconListSold[rr].setMap(null);
	}
	iconListSold.length = 0;
}

// MAP POINTS

function getMapPoints(obj,mappointcategoryid) {
	if (obj.checked==true) {
		DWREngine._execute(_cfscriptLocation,null,'getMapPoints',mappointcategoryid,showMapPoints);
	} else {
		deleteMapPoints(mappointcategoryid);
	}
}

function showMapPoints(result) {
	// Parse returned values into an array we can loop through
	var listings = result.split("^");

	// Loop through the result
	for (var rr=0; rr<listings.length; rr++) {
		var currentListing = listings[rr].split("|");
		var currentHTML = "";
		var iconType = "";
		var iconList = "";
		// Generate HTML for Popup Window
		currentHTML = "<div id=\"popupWindow\" style=\"width: 300px;\"><span class=\"subtitle\">";
		currentHTML += currentListing[0];
		currentHTML += "</span><br />";
		currentHTML += currentListing[1];
		currentHTML += "<br />";
		currentHTML += currentListing[2];
		currentHTML += ", ";
		currentHTML += currentListing[3];
		currentHTML += " ";
		currentHTML += currentListing[4];
		currentHTML += currentListing[5];
		currentHTML += currentListing[6];
		currentHTML += "</div>";
		iconType = eval("icon" + currentListing[9]);
		iconList = eval("iconList" + currentListing[9]);
		// Plot marker
		var point = new google.maps.LatLng( currentListing[7], currentListing[8] );
		var marker = createMarker(point, currentHTML, iconType);
		iconList[iconList.length] = marker;
		marker.setMap(map);
	}
}

function deleteMapPoints(mappointcategoryid) {
	var iconList = eval("iconList" + mappointcategoryid);
	for (var rr=0; rr < iconList.length; rr++) {
		iconList[rr].setMap(null);
	}
	iconList.length = 0;
}
