	function initOverlayCollections(){
		//Initialize Variables and Event Handlers
		overlays.addHandler("showhide",overlays.showPlacemarkListings,true);
		overlays.addHandler("openinfobubble",infoWindowOpen,true);
		overlays.catIcons = false;

		pinned.getInfoHTML = itineraryOverlays.getInfoHTML = overlays.getInfoHTML = function(placemark){
			return parsePlacemarkData(placemarkHtml,placemark);
		};

		m.addCollection(overlays, true);
		m.addCollection(pinned, false);
		m.addCollection(directionOverlays, false);
		m.addCollection(custom, false);
		
		//Initialize Variables and Event Handlers	
		//pinned.addHandler("showhide",pinned.showPinnedListings,true);

		//Initialize Variables and Event Handlers	
		itineraryOverlays.addHandler("showhide", itineraryOverlays.markPlacemarkListings, true);
		itineraryOverlays.addHandler("gotoplacemark", m.goToPlacemark, true);
		
		//Create Set for Pinned Listings

			custom.getInfoHTML = function(placemark){
				var addr = new Address();
				try{
					addr.loadFromObject(placemark);
				}
				catch(ex){
				}
				var str = new String();
				str = "<div class=\"infoWindowTitle\">placemark:name</div>";
				str += "<div class=\"infoWindowAddress\">" + addr.toHTMLString() + "</div>";
				if(placemark.distance){
						str += "<div class=\"infoWindowSection\">Distance: " + placemark.distance.toFixed(1) + " miles</div>";
				}
			
				return parsePlacemarkData(str,placemark);
			}
			directionOverlays.getInfoHTML = custom.getInfoHTML;

	}
	
	/****************************************************
	End Extension functions for listings preview
	*****************************************************/
	
	//Go to Marker and Open InfoWindow
	function goToPlacemark(prikey){
		$.scrollTo("#map_" + mapName,400,{axis:"y",offset:-50});
		var p = findPlacemark(mapName,prikey);
		p.marker.openInfoWindowHtml(p.marker.myInfoHTML);
		return false;
	}
	
	function transferFromOverlays(prikey,dstCollection,cancelHandlers){
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = false;
		m.removePlacemark(placemark,overlays,cancelHandlers);
		m.addPlacemark(placemark,dstCollection,cancelHandlers);
		m.showPlacemarks(dstCollection,1,cancelHandlers);
		overlays.showPlacemarkListings();
	}

	//Pin an overlay to the map (make permanent)
	function transferToOverlays(prikey,srcCollection,cancelHandlers){
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = true;
		m.removePlacemark(placemark,srcCollection,cancelHandlers);
		m.addPlacemark(placemark,overlays,cancelHandlers);
		overlays.sortBy("name");
		m.showPlacemarks(overlays,overlays.currPage,cancelHandlers);
		overlays.showPlacemarkListings();
	}

	
	//Pin an overlay to the map (make permanent)
	function pinMe(prikey,cancelHandlers){
		if(!cancelHandlers)
			cancelHandlers = false;
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = false;
		if(placemark.collection != pinned){
			placemark.pinned = true;
			placemark.pinnedsource = placemark.collection;
			m.removePlacemark(placemark,placemark.collection,cancelHandlers);
			m.addPlacemark(placemark,pinned,cancelHandlers);
			delete placemark.marker;
			m.showPlacemarks(pinned,1,cancelHandlers);
		}
	}
	
	//Pin an overlay to the map (make permanent)
	function unpinMe(prikey,cancelHandlers){
		if(!cancelHandlers)
			cancelHandlers = false;
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = true;
		placemark.pinned = false;
		m.removePlacemark(placemark,pinned,cancelHandlers);
		m.addPlacemark(placemark,placemark.pinnedsource,cancelHandlers);
		placemark.pinnedsource = null;
		delete placemark.marker;
		m.showPlacemarks(placemark.collection,placemark.collection.currPage,cancelHandlers);
	}
	
	//This function sets up the filter fields on each placemark
	function preparePlacemarks(){
		for (var i in placemarks){
			for (var j = 0; j < placemarks[i].length; j++){
				placemarks[i][j].filteredBy = new Object();
				placemarks[i][j].filtered = false;
				placemarks[i][j].active = true;
				placemarks[i][j].pinned = false;
				placemarks[i][j].point = new GLatLng(placemarks[i][j].latitude,placemarks[i][j].longitude);
				placemarks[i][j].itinerary = jQuery.inArray(placemarks[i][j].prikey, itinLookup) >= 0;
			}
		}
	}
	
	//Run all filters
	function runFilters(){
		for (var i in maps[mapName].allFilters){
			maps[mapName].allFilters[i].run();
		}
	}
	
	var directions;
	
	//Initialize Map
	function initMap(){
		m = new Map(mapName,"map_" + mapName,defaultLat, defaultLng, defaultZoom);
		initOverlayCollections();
		m.create();
		m.gmap.addControl(new SVZoomControl());
		m.gmap.addControl(new GMapTypeControl());
		m.gmap.setMapType(G_HYBRID_MAP);
	
		//Make sure they are initialized with filter flags
		preparePlacemarks();
	
		registerFilters();
		runFilters();
		
		//Intialize Directions Object
		directions = new DrivingDirections(mapName,m);
		directions.init();
		loadItinerary();
		loadNearby();
	}
	
	function registerFilters(){
		var catFilter = new CategoryFilter();
		maps[mapName].registerFilter("catFilter",catFilter);
	
		//var regionFilter = new RegionFilter();
		//maps[mapName].registerFilter("regionFilter",regionFilter);
	
		var distanceFilter = new DistanceFilter();
		maps[mapName].registerFilter("distanceFilter",distanceFilter);
	}
	
	//Runs whenever the filters are updated
	function updatedFilters(unfiltered){
		maps[mapName].removePlacemarks(maps[mapName].defaultCollection,true);
		maps[mapName].addPlacemarks(unfiltered,maps[mapName].defaultCollection,true);
		m.cancelCentering = true;
		maps[mapName].showPlacemarks(maps[mapName].defaultCollection,1,false);
		m.cancelCentering = false;
	}
	
	//Find a placemark in the placemarks collection
	function findPlacemark(mapname,placemarkID){
		for (var i = 0; i < placemarks[mapname].length; i++){
			if(placemarks[mapname][i].prikey == placemarkID)
				return placemarks[mapname][i];
		}
		return null;
	}
	
	function swapInfoTab(prikey, selTab, updateHeaders){
		var ptabs = "Description,Directions,SendToEmail,CustomAddress,Street".split(",");
		if(updateHeaders)
		{
			//Mark all tab headers as unselected
			$(".infoTabNames .infoTabName").each(function(e){e.removeClassName("selected");});
			//Mark clicked header as selected
			$(".infoTab" + selTab).each(function(e){$(this).addClass("selected");});
		}
				
		for (var i = 0; i < ptabs.length; i++){
			$("#infoTab_" + ptabs[i] + "_" + prikey).hide();
		}
		$("#infoTab_" + selTab + "_" + prikey).show();
		return false;
	}

	/*Begin Driving Directions Code */

		//Loads the list of currently visible markers to choose as a destination when getting driving directions
	function loadMarkerList(prikey){
		var markerFrom = $("#markerFrom").get(0);
		if(markerFrom)
		{
			markerFrom.options.length = 0;
			var placemarks = maps[mapName].getVisiblePlacemarks();
			for (var i = 0; i < placemarks.length; i++){
				var o = new Option();
				o.value = placemarks[i].prikey;
				o.text = placemarks[i].name;
				if (o.text.length > 30)
					o.text = o.text.substring(0,30) + "...";
				markerFrom.options[markerFrom.options.length] = o;
			}
		}
	}
	
	//Temporarily pin an overlay to the map (make permanent) for directions will be removed when directions are cleared
	function addToDirectionOverlays(prikey){
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = false;
		if(placemark.collection != directionOverlays){
			placemark.directionsource = placemark.collection;
			m.removePlacemark(placemark,placemark.collection);
			m.addPlacemark(placemark,directionOverlays);
			m.showPlacemarks(directionOverlays,1);
		}
	}
	
	//Temporarily unpin an overlay from the map
	function removeFromDirectionOverlays(prikey){
		var placemark = findPlacemark(mapName,prikey);
		placemark.active = true;
		
		m.removePlacemark(placemark,directionOverlays);
		
		if(String(placemark.prikey).indexOf("custom") < 0) {
			m.addPlacemark(placemark, placemark.directionsource);
		} else {
			m.removePlacemark(placemark, placemark.collection);
			placemark.collection = null;
		}
		placemark.directionsource = null;
		if (placemark.collection) {
			placemark.collection.sortBy("name");
			m.showPlacemarks(placemark.collection,placemark.collection.currPage);
		}
	}
	
	function clearDirections(delMarks, rDir) {
		if (!rDir || rDir != true) {
			if (m.circle) {
				m.gmap.addOverlay(m.circle);
			}
		}
		
		if(directions.dst)
		{
			removeFromDirectionOverlays(directions.dst.prikey);
			directions.placemarkToField.val("");
		}
		
		if(directions.src)
		{
			removeFromDirectionOverlays(directions.src.prikey);
			directions.placemarkFromField.val("");
		}
		directions.clearDirections();
		
		if (!rDir || rDir != true) {
			if($("#leftCont .bodyCont").innerHeight() > $("#rightCont .bodyRight").innerHeight()) {
				$("#rightCont .bodyRight").css({height: $("#leftCont .bodyCont").innerHeight()});
			} else {
				$("#rightCont .bodyRight").css({height: ""});
			}
		}
		if (delMarks && delMarks == true) {
			for (var i = 0; i < placemarks[mapName].length; i++) {
				if (String(placemarks[mapName][i].prikey).search('custom') != -1) {
					placemarks[mapName].splice(i);
				}
			}
		}
		//pinMe('#URL.listingID#');
	}

	function reverseDirections(){
		getDirections(directions.src.prikey,directions.dst.prikey);
	}
	
	function getMarkerDirections(prikeyFrom, prikeyTo){
		if($(".infoTab_Directions input[type=radio][name=direction]:checked").val() == "to")
			getDirections(prikeyTo,prikeyFrom);
		else
			getDirections(prikeyFrom,prikeyTo);
	}

	function getDirections(prikeyFrom, prikeyTo, rDir) {
		var pFrom, pTo;
		
		clearDirections(false, rDir);
		if(prikeyFrom){
			pFrom = findPlacemark(mapName,prikeyFrom);
			directions.directionsFrom(prikeyFrom);
		}
		else
			pFrom = directions.src;
		
		if(prikeyTo){
			pTo = findPlacemark(mapName,prikeyTo);
			directions.directionsTo(prikeyTo);
		}
		else
			pTo = directions.dst;
		
		pFrom.isDst = 1;
		pTo.isDst = 0;
		
		if(pFrom.marker)
			pFrom.marker.closeInfoWindow();
		if(pTo.marker)
			pTo.marker.closeInfoWindow();
		
		if(!maps[mapName].collections.directionOverlays.check(prikeyTo))
			addToDirectionOverlays(prikeyTo);
		
		if(!maps[mapName].collections.directionOverlays.check(prikeyFrom))
			addToDirectionOverlays(prikeyFrom);
		
		directions.getDirectionsTo(pFrom, pTo);
		return false;
	}

	function addAddress(prikey){
		var name = $("#custom").val();
		
		var g = new GClientGeocoder();
		g.getLocations(name, addCustomFromGeocoder);
		
		if($(".infoTab_CustomAddress input[type=radio][name=directionCustom]:checked").val() == "to")
		{
			directions.placemarkFromField.val(prikey);
		}
		else
		{
			directions.placemarkToField.val(prikey);
		}
		return false;
	}

	function addCustomFromGeocoder(results){
		var name = $("#custom").val();
		var o = defaultPlacemark();
		
		o = parseGeocodingResults(results, o);
			
		placemarks[mapName].push(o);
		
		maps[mapName].addPlacemark(o,custom);
		maps[mapName].showPlacemarks(custom,1);
		
		if ($(".infoTab_CustomAddress input[type=radio][name=directionCustom]:checked").val() == "to") {
			directions.placemarkToField.val(o.prikey);
		} else {
			directions.placemarkFromField.val(o.prikey);
		}
		getDirections(directions.placemarkFromField.val(), directions.placemarkToField.val());
	 }

	function parseGeocodingResults(results, o){
		if(results.Status.code != 200){
			alert("We're sorry, but we couldn't find that address.");
			return null;
		}
		else{
			var p = results.Placemark[0];
			o.latitude = p.Point.coordinates[1];
			o.longitude = p.Point.coordinates[0];
			o.point = new GLatLng(o.latitude,o.longitude);
			o.iconstyle = "default";
			o.description = "";
			o.weburl = "";
			if(p.address)
				o.name = p.address;
			if(p.AddressDetails.Country){
				var currLevel = p.AddressDetails.Country;
				o.country = currLevel.CountryNameCode;
				if(currLevel.AdministrativeArea){
					var prevLevel = currLevel;
					currLevel = currLevel.AdministrativeArea;
					o.state = currLevel.AdministrativeAreaName;
					if(p.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea)
					{
						prevLevel = currLevel;
						currLevel = p.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea;
					}
					if(currLevel.Locality){
						prevLevel = currLevel;
						currLevel = currLevel.Locality;
						o.city = currLevel.LocalityName;
						if(currLevel.PostalCode)
							o.zip = currLevel.PostalCode.PostalCodeNumber;
						if(currLevel.Thoroughfare){
							currLevel = currLevel.Thoroughfare;
							o.addr1 = currLevel.ThoroughfareName;
							o.name = o.addr1;
						}
					}
				}
			}
		}
		return o;
	}

	//Create a blank custom placemark and add it to the "custom" collection
	function defaultPlacemark(name){
		var o = new Object();
		o.name = "";
		o.prikey = "custom_" + (++customprikey);
		o.addr1 = "";
		o.addr2 = "";
		o.addr3 = "";
		o.city = "";
		o.state = "";
		o.zip = "";
		o.country = "";
		o.description = "";
		o.weburl = "";
		o.active = false;
		o.filteredBy = new Object();
		o.filtered = false;
		o.phone = "";
		o.iconstyle = "default";
		return o;
	}

	/*End Driving Directions Code*/
	
	function findNearBy(prikey){
		if($("#distanceFromID").val().length > 0)
			clearNearBy();
		$("#" + mapName + "_DistanceFilter .bgEventsAlt").show();
		pinMe(prikey,true);
		$("#distanceFromID").val(prikey);
		maps[mapName].allFilters.distanceFilter.run();
		return false;
	}

	function clearNearBy(){
		$("#" + mapName + "_DistanceFilter .bgEventsAlt").hide();
		unpinMe($("#distanceFromID").val(),true);
		$("#distanceFromID").val("");
		runFilters();
		return false;
	}

	function email(prikey){
		$.ajax(
		{
			url: emailUrl,
			type: "POST",
			data: {listingID:prikey,friendemail:$("input[name='mapEmailTo']").val(),friendname:$("input[name='mapNameTo']").val(),youremail:$("input[name='mapEmailFrom']").val(),yourname:$("input[name='mapNameFrom']").val(),comments:$("textarea[name='mapEmailNote']").val()},
			success:function(data, textStatus){$("#infoTab_SendToEmail_" + prikey).html("E-mail sent successfully.");},
			error:function(xmlhr, textStatus, errorThrown){$("#infoTab_SendToEmail_" + prikey).html("The email could not be sent. Please try again later.");}
		});
		$("#infoTab_SendToEmail_" + prikey).html("<div style=\"margin:100px auto;text-align:center\"><img src=\"" + imgroot + "/includes/images/loading.gif\" alt=\"Loading\" /></div>");
		return false;
	}
	
	function infoWindowOpen(placemark)
	{
		client = new GStreetviewClient();
		client.getNearestPanorama(placemark.point, function(streetViewData){if(streetViewData.code != GStreetviewClient.ReturnValues.SUCCESS) setTimeout("$(\".infoTabStreet\").hide();", 500);});
	}

	function loadStreet(prikey){
			var placemark = findPlacemark(mapName,prikey);
			var point = placemark.point;

			var mypano = new GStreetviewPanorama($("#street")[0]);
			mypano.setLocationAndPOV(point);
			var handlePErrors = function(errorCode){
				if (errorCode == 603){
					$("#street").html("Error: Flash doesn't appear to be supported by your browser");
				}
				if(errorCode == 600){
					$("#street").html("No data found for this location");
				}
				return;
			}

			GEvent.addListener(mypano, "error", handlePErrors);
			mypano.checkResize();
	}
