/*
<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" />
		<history 
			author="Kyle Perkins" comments="Fixed for Google Maps 2" 
			date="12/07/2006" 
			email="kyle@globalreach.com" 
			role="Updater" type="Update" />
	</properties>
</fusedoc>
*/

// GENERAL FUNCTIONS

function createMarker(point,html,icon) {
	// We'll use this function to make adding markers on the map easier.
	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),
						new google.maps.Point(14,35)
			),
			map:map
		});
	}
	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
			),
			map:map
		});
	}

	google.maps.event.addListener(marker, 'click', (function(markerArg) {
		return function() {
			objInfoWindow.setContent(html);
			objInfoWindow.setOptions({pixelOffset: new google.maps.Size(-5,11)});
			objInfoWindow.open(map, markerArg);
		};
	})(marker));
	return marker;
}

// PROPERTY LISTINGS

function getListings() {
	DWREngine._execute(_cfscriptLocation,null,'getListings',sortby,bedrooms,baths,areaid,schoolDistrictID,propertyTypeID,propertyStyleID,postalCode,priceHigh,priceLow,squareFeet,garage,lotSize,page,MLSNumber,agentID,address1,openHouses,propertySubTypeID,city,zoomlevel,showListings);
}

function showListings(result) {
	if (result=="NOLISTINGS") {
		//No listings found
		var mapObject = document.getElementById("map");
		var sidebarObject = document.getElementById("map_sidebar");
		var mapInstructions = document.getElementById("map_instructions");
		mapObject.style.border="none";
		mapObject.style.backgroundColor="#FFFFFF";
		mapObject.innerHTML="<h3>No Properties Found</h3><p>Sorry, there no properties matching the search criteria you specified. If you like, you may <a href=\"javascript:history.go(-1);\">modify your search criteria</a>.</p>";
		mapInstructions.style.display = 'none';
		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: 290px;\"><a href=\"/properties/index.cfm/" + currentListing[13] + "\"><img src=\"";
			currentHTML += mediaRoot;
			currentHTML += "/images/properties/";
			currentHTML += currentListing[8];
			currentHTML += "\" border=\"0\" hspace=\"5\" align=\"left\"></a><strong>";
			currentHTML += currentListing[1];
			currentHTML += "</strong><br /><strong>";
			currentHTML += currentListing[2];
			currentHTML += " ";
			currentHTML += currentListing[3];
			currentHTML += "</strong><br />";
			currentHTML += currentListing[4];
			currentHTML += ", ";
			currentHTML += currentListing[5];
			currentHTML += "<br />";
			if(currentListing[6] != '') {
				currentHTML += currentListing[6];
				currentHTML += " &bull; ";
			}
			currentHTML += currentListing[7];
			currentHTML += "</div><div style=\"clear: both;\" align=\"right\"><strong><a href=\"/properties/index.cfm/" + currentListing[13] + "\">Details&nbsp;&raquo;</a></strong></div>";
			// Plot marker
			var point = new google.maps.LatLng(currentListing[9], currentListing[10]);
			var marker = createMarker(point, currentHTML, iconListing);
		}
	}
}

// SOLD PROPERTY LISTINGS

// 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;\"><h3 class=\"no_padding\">";
		currentHTML += currentListing[0];
		currentHTML += "</h3>";
		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;
	}
}

function deleteMapPoints(mappointcategoryid) {
	var iconList = eval("iconList" + mappointcategoryid);
	for (var rr=0; rr < iconList.length; rr++) {
		iconList[rr].setMap(null);
	}
	iconList.length = 0;
}
