function MapCollection() {

	function setListingData(listingthumbnail_resTemp, listingthumbnail_res_previewTemp) {
		listingthumbnail_res = listingthumbnail_resTemp;
		listingthumbnail_res_preview = listingthumbnail_res_previewTemp;
	}
	
	function mapListing(mls) {
		for(var i = 0; i < currentMLS.length; i++) {
			if(currentMLS[i] == mls) {		
				showPopup(currentMarkers[i], mapListingThumbnails[i]);
			}
		}
	}
	
	function setMapData(mapTemp, icon1Temp, icon2Temp, icon3Temp, icon4Temp, icon5Temp, icon6Temp, icon7Temp, icon8Temp, icon9Temp) {
		map = mapTemp;
		icon1 = icon1Temp;
		icon2 = icon2Temp;
		icon3 = icon3Temp;
		icon4 = icon4Temp;
		icon5 = icon5Temp;
		icon6 = icon6Temp;
		icon7 = icon7Temp;
		icon8 = icon8Temp;
		icon9 = icon9Temp;
	}
	
	function parseStatistics(data, tag1, tag2, tag3) {
		var groupsText = getValue(tag1,tag2,data);
		var groups = groupsText.split(tag3);

		for(var i = 1; i < groups.length; i++) {
			lattitude = getValue("<lat>", "</lat>",groups[i]);
			longitude = getValue("<lon>", "</lon>",groups[i]);
			price = getValue("<price>", "</price>",groups[i]);
			avgprice = getValue("<avgprice>", "</avgprice>",groups[i]);
			name = getValue("<name>", "</name>",groups[i]);
			count = getValue("<count>", "</count>",groups[i]);
			low = getValue("<low>", "</low>",groups[i]);
			high = getValue("<high>", "</high>",groups[i]);
			
			var icon = new GIcon();
			icon.image = "index.php?fuseaction=mapsearch.city&noheader=true&name=" + myescape(name) + "&price=" + myescape(price) + "&count=" + myescape(count);
			//document.write("index.php?fuseaction=mapsearch.city&noheader=true&name=" + myescape(name) + "&price=" + myescape(price) + "&count=" + myescape(count));
            icon.iconSize = new GSize(101, 15);
            icon.iconAnchor = new GPoint(10, 7);				
			
			if(price > 1000000) {
				price = "" + (price / 1000000 * 1000000);
			}
			
			if(avgprice > 1000000) {
				avgprice = "" + (avgprice / 1000000 * 1000000);
			}
			
			if(low > 1000000) {
				low = "" + (low / 1000000 * 1000000);
			}
			
			if(high > 1000000) {
				high = "" + (high / 1000000 * 1000000);
			}

			var text = '<table><div style="border: 1px solid #000; background-color: #fff; width: 101px; text-align: center; font: 10px Arial, Helvetica;">Avg: ' + formatPrice(avgprice) + '<br />Median: ' + formatPrice(price) + '<br />' + formatPrice(low) + ' - ' + formatPrice(high) + '<br />' + count + ' Listings<br /><div class="zoomlink" onclick="zoomIn(' + lattitude + ',' + longitude + ');">zoom in</div></div></table>';
			createMarkerArea(lattitude, longitude, icon, text);
		}
	}

	function parseListings(data) {
	
		var legend = "";
		for(var i = 0; i < document.legendForm.legendGroup.length; i++) {
			if(document.legendForm.legendGroup[i].checked) {
				legend = document.legendForm.legendGroup[i].value;
			}
		}
	
		var listingsText = getValue("<listings>","</listings>",data);
		var listings = listingsText.split("<l>");

		for(var i = 1; i < listings.length; i++) {
			mls = getValue("<m>", "</mls>",listings[i]);
			lattitude = getValue("<la>", "</la>",listings[i]);
			longitude = getValue("<lo>", "</lo>",listings[i]);
			price = getValue("<p>", "</p>",listings[i]);
			var address = getValue("<a>", "</a>",listings[i]);
			var city = getValue("<c>", "</c>",listings[i]);
			var state = getValue("<s>", "</s>",listings[i]);
			var zip = getValue("<z>", "</z>",listings[i]);
			var br = getValue("<b>", "</b>",listings[i]);
			var ba = getValue("<ba>", "</ba>",listings[i]);
			var hb = getValue("<hb>", "</hb>",listings[i]);
			var sqft = getValue("<sq>", "</sq>",listings[i]);
			var acres = getValue("<ac>", "</ac>",listings[i]);
			var county = getValue("<co>", "</co>",listings[i]);
			var subdivision = getValue("<su>", "</su>",listings[i]);
			var description = getValue("<d>", "</d>",listings[i]);
			var image = getValue("<i>", "</i>",listings[i]);

			// See which legend we are looking at
			var icon = lookupIconFromListing(legend, price, sqft, acres);
			
			// Fill out the link and the photo
			link = '<a href="http://wncdesktop.com/residential/details/' + mls + '_" target="_details">';
			if(image == "1") {
				photo = '<img border=0 src="http://dev.mlskit.com/wncr/jpg1/' + mls + '.jpg" width=145px height=109px />';
			}
			else {
				// Fille image
				photo = '<img border=0 src="http://dev.mlskit.com/demo/templates/images/missingPhoto.jpg" width=145px height=109px />';
			}

			var mydata = new Array(address, city + ' ' + state, zip, photo, link, '</a>', 
				mls, formatPrice(price), br, ba, hb, formatNumber(sqft), acres, 
				county, city, subdivision, description);

			var mylabels = new Array('%property_address%', '%property_city%', '%property_zip%', '%photo%', '%view_link_start%', '%view_link_end%',
				'%mls%', '%list_price%', '%br%', '%ba%', '%hb%', '%sqft%', '%acres%', 
				'%county%', '%city%', '%subdivision%', '%description%');

			text = replaceEngine(listingthumbnail_res, mylabels, mydata);
			text2 = replaceEngine(listingthumbnail_res_preview, mylabels, mydata);

			createMarker(lattitude, longitude, icon, mls, text, text2, price, sqft, acres);
		}
	}
	
	function getListingThumbnails() {
		return listingThumbnails;
	}

	function cleanupMap(latmin, latmax, lonmin, lonmax) {
		// Remove everything not witihin this lattitude and longitude
		var i = 0;

		while(i < currentMarkers.length) {
			if(lattitudes[i] < latmin || lattitudes[i] > latmax || longitudes[i] < lonmin || longitudes[i] > lonmax) {
				// Remove this listing from the map
				map.removeOverlay(currentMarkers[i]);
				
				// Remove from our cached data
				lattitudes.splice(i,1);
				longitudes.splice(i,1);
				currentMarkers.splice(i,1);
				mapListingThumbnails.splice(i,1);
				listingThumbnails.splice(i,1);
				currentMLS.splice(i,1);
				acres.splice(i,1);
				sqfts.splice(i,1);
				prices.splice(i,1);
				handles.splice(i,1);
			}
			else {
				// If we didn't splice, we need to increment
				i++;
			}
		}
	}
	
	function getCurrentCount() {
		return currentMarkers.length;
	}
	
	function changeLegend(legend) {
		for(i = 0; i < currentMarkers.length; i++) {
			map.removeOverlay(currentMarkers[i]);
			var icon = lookupIcon(i, legend);
			var marker = new GMarker(new GLatLng(lattitudes[i],longitudes[i]), icon);
			var temp = mapListingThumbnails[i];
			GEvent.removeListener(handles[i]);
			handles[i] = GEvent.addListener(marker, "click", function() {
				showPopup(marker, temp);
			});
			
			map.addOverlay(marker);
			currentMarkers[i] = marker;
		}
	}
	
	function lookupIcon(i, legend) {
		if(legend == "price") {
			var price = prices[i];
			if(price <= 100000) {
				return icon1;
			}
			else if(price <= 150000) {
				return icon2;
			}
			else if(price <= 200000) {
				return icon3;
			}
			else if(price <= 250000) {
				return icon4;
			}
			else if(price <= 300000) {
				return icon5;
			}
			else if(price <= 350000) {
				return icon6;
			}
			else if(price <= 400000) {
				return icon7;
			}
			else if(price <= 500000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "sqft") {
			var sqft = sqfts[i];
			if(sqft <= 500) {
				return icon1;
			}
			else if(sqft <= 750) {
				return icon2;
			}
			else if(sqft <= 1000) {
				return icon3;
			}
			else if(sqft <= 1250) {
				return icon4;
			}
			else if(sqft <= 1500) {
				return icon5;
			}
			else if(sqft <= 2000) {
				return icon6;
			}
			else if(sqft <= 2500) {
				return icon7;
			}
			else if(sqft <= 3000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "$PerSqft") {
			var pricesqft = (prices[i] / sqfts[i]);
			if(pricesqft <= 50) {
				return icon1;
			}
			else if(pricesqft <= 75) {
				return icon2;
			}
			else if(pricesqft <= 100) {
				return icon3;
			}
			else if(pricesqft <= 125) {
				return icon4;
			}
			else if(pricesqft <= 150) {
				return icon5;
			}
			else if(pricesqft <= 200) {
				return icon6;
			}
			else if(pricesqft <= 250) {
				return icon7;
			}
			else if(pricesqft <= 300) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "$PerAcre") {
			var priceacre = (prices[i] / acres[i]);
			if(priceacre <= 25000) {
				return icon1;
			}
			else if(priceacre <= 50000) {
				return icon2;
			}
			else if(priceacre <= 100000) {
				return icon3;
			}
			else if(priceacre <= 125000) {
				return icon4;
			}
			else if(priceacre <= 300000) {
				return icon5;
			}
			else if(priceacre <= 600000) {
				return icon6;
			}
			else if(priceacre <= 750000) {
				return icon7;
			}
			else if(priceacre <= 1000000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else {
			var acre = acres[i];
			if(acre <= 0.2) {
				return icon1;
			}
			else if(acre <= 0.3) {
				return icon2;
			}
			else if(acre <= 0.5) {
				return icon3;
			}
			else if(acre <= 0.75) {
				return icon4;
			}
			else if(acre <= 1) {
				return icon5;
			}
			else if(acre <= 2) {
				return icon6;
			}
			else if(acre <= 5) {
				return icon7;
			}
			else if(acre <= 10) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
	}
	
	function lookupIconFromListing(legend, price, sqft, acre) {
		if(legend == "price") {
			if(price <= 100000) {
				return icon1;
			}
			else if(price <= 150000) {
				return icon2;
			}
			else if(price <= 200000) {
				return icon3;
			}
			else if(price <= 250000) {
				return icon4;
			}
			else if(price <= 300000) {
				return icon5;
			}
			else if(price <= 350000) {
				return icon6;
			}
			else if(price <= 400000) {
				return icon7;
			}
			else if(price <= 500000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "sqft") {
			if(sqft <= 500) {
				return icon1;
			}
			else if(sqft <= 750) {
				return icon2;
			}
			else if(sqft <= 1000) {
				return icon3;
			}
			else if(sqft <= 1250) {
				return icon4;
			}
			else if(sqft <= 1500) {
				return icon5;
			}
			else if(sqft <= 2000) {
				return icon6;
			}
			else if(sqft <= 2500) {
				return icon7;
			}
			else if(sqft <= 3000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "$PerSqft") {
			var pricesqft = (price / sqft);
			if(pricesqft <= 50) {
				return icon1;
			}
			else if(pricesqft <= 75) {
				return icon2;
			}
			else if(pricesqft <= 100) {
				return icon3;
			}
			else if(pricesqft <= 125) {
				return icon4;
			}
			else if(pricesqft <= 150) {
				return icon5;
			}
			else if(pricesqft <= 200) {
				return icon6;
			}
			else if(pricesqft <= 250) {
				return icon7;
			}
			else if(pricesqft <= 300) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else if(legend == "$PerAcre") {
			var priceacre = (price / acre);
			if(priceacre <= 25000) {
				return icon1;
			}
			else if(priceacre <= 50000) {
				return icon2;
			}
			else if(priceacre <= 100000) {
				return icon3;
			}
			else if(priceacre <= 125000) {
				return icon4;
			}
			else if(priceacre <= 300000) {
				return icon5;
			}
			else if(priceacre <= 600000) {
				return icon6;
			}
			else if(priceacre <= 750000) {
				return icon7;
			}
			else if(priceacre <= 1000000) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
		else {
			if(acre <= 0.2) {
				return icon1;
			}
			else if(acre <= 0.3) {
				return icon2;
			}
			else if(acre <= 0.5) {
				return icon3;
			}
			else if(acre <= 0.75) {
				return icon4;
			}
			else if(acre <= 1) {
				return icon5;
			}
			else if(acre <= 2) {
				return icon6;
			}
			else if(acre <= 5) {
				return icon7;
			}
			else if(acre <= 10) {
				return icon8;
			}
			else {
				return icon9;
			}				
		}
	}
	
	function clearMap() {
		
		for(i = 0; i < currentMarkers.length; i++) {
			map.removeOverlay(currentMarkers[i]);
		}
		
		listingThumbnails = new Array();
		mapListingThumbnails = new Array();
		currentMarkers = new Array();
		currentMLS = new Array();
		lattitudes = new Array();
		longitudes = new Array();
		prices = new Array();
		sqfts = new Array();
		acres = new Array();
	}
	
	function createMarker(lattitude, longitude, icon, mls, mapText, thumbnailText, price, sqft, acre) {
		var marker = new GMarker(new GLatLng(lattitude,longitude), icon);
		map.addOverlay(marker);
		var handle = GEvent.addListener(marker, "click", function() {
			showPopup(marker, mapText);
		});
	
		currentMarkers.push(marker);
		currentMLS.push(mls);
		
		lattitudes.push(lattitude);
		longitudes.push(longitude);
		
		mapListingThumbnails.push(mapText);
		listingThumbnails.push(thumbnailText);
		
		prices.push(price);
		sqfts.push(sqft);
		acres.push(acre);
		handles.push(handle);
	}

	function createMarkerArea(lattitude, longitude, icon, mapText) {
		var marker = new GMarker(new GLatLng(lattitude,longitude), icon);
		map.addOverlay(marker);
		var handle = GEvent.addListener(marker, "click", function() {
			showPopupArea(marker, mapText);
		});
	
		/*GEvent.addListener(marker, "mouseout", function() {
			removePopup();
		});*/
		
		currentMarkers.push(marker);
		currentMLS.push("");
		
		lattitudes.push(lattitude);
		longitudes.push(longitude);
		
		mapListingThumbnails.push(mapText);
		listingThumbnails.push("");
		
		prices.push(0);
		sqfts.push(0);
		acres.push(0);
		handles.push(handle);
	}
	
	// The methods
	this.getListingThumbnails = getListingThumbnails;
	this.cleanupMap = cleanupMap;
	this.clearMap = clearMap;
	this.changeLegend = changeLegend;
	this.parseListings = parseListings;
	this.parseStatistics = parseStatistics;
	this.createMarker = createMarker;
	this.createMarkerArea = createMarkerArea;
	this.setMapData = setMapData;
	this.setListingData = setListingData;	
	this.mapListing = mapListing;
	this.getCurrentCount = getCurrentCount;
	
	// The data
	var listingThumbnails = new Array();
	var mapListingThumbnails = new Array();
	var currentMarkers = new Array();
	var currentMLS = new Array();
	var lattitudes = new Array();
	var longitudes = new Array();
	var prices = new Array();
	var sqfts = new Array();
	var acres = new Array();
	var handles = new Array();
	var map;
	var icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, icon9;
}