		var map;
		var lastMapAction;
		var resetMapAction = 0;
		var centerLatitude = 41.77131167976406;
		var centerLongitude = -100.8984375;
		var startZoom = 3; //start with a low level zoom of the entire USA
		var keepInfoWindowsOpen = 0;
		var pointsLength = 0;
		
		var existingMarkers = {};
		
		//Create an icon for the clusters
		var iconCluster = new GIcon();
		iconCluster.image = "images/cluster.png";
		iconCluster.shadow = "images/cluster_shadow.png";
		iconCluster.iconSize = new GSize(26, 25);
		iconCluster.shadowSize = new GSize(22, 20);
		iconCluster.iconAnchor = new GPoint(13, 25);
		iconCluster.infoWindowAnchor = new GPoint(13, 1);
		iconCluster.infoShadowAnchor = new GPoint(26, 13);
		
		//create an icon for the sale pins
		var iconSingle = new GIcon();
		iconSingle.image = "images/single.png";
		iconSingle.shadow = "images/single_shadow.png";
		iconSingle.iconSize = new GSize(12, 20);
		iconSingle.shadowSize = new GSize(22, 20);
		iconSingle.iconAnchor = new GPoint(6, 20);
		iconSingle.infoWindowAnchor = new GPoint(6, 1);
		iconSingle.infoShadowAnchor = new GPoint(13, 13);
		
		//create an icon for the auction pins
		var iconSingleAuc = new GIcon();
		iconSingleAuc.image = "images/single_auction.png";
		iconSingleAuc.shadow = "images/single_shadow.png";
		iconSingleAuc.iconSize = new GSize(12, 20);
		iconSingleAuc.shadowSize = new GSize(22, 20);
		iconSingleAuc.iconAnchor = new GPoint(6, 20);
		iconSingleAuc.infoWindowAnchor = new GPoint(6, 1);
		iconSingleAuc.infoShadowAnchor = new GPoint(13, 13);
		
		//create an icon for the featured sale pins
		/* var iconSingleFtr = new GIcon();
		iconSingleFtr.image = "images/single_featured.gif";
		iconSingleFtr.shadow = "images/single_shadow.png";
		iconSingleFtr.iconSize = new GSize(30, 36);
		iconSingleFtr.shadowSize = new GSize(22, 20);
		iconSingleFtr.iconAnchor = new GPoint(6, 20);
		iconSingleFtr.infoWindowAnchor = new GPoint(6, 1);
		iconSingleFtr.infoShadowAnchor = new GPoint(13, 13); */
		
		var iconFeatured = new GIcon();
		iconFeatured.image = "images/single_ftr.gif";
		iconFeatured.shadow = "images/single_ftr_shadow.png";
		iconFeatured.iconSize = new GSize(20.0, 34.0);
		iconFeatured.shadowSize = new GSize(38.0, 34.0);
		iconFeatured.iconAnchor = new GPoint(10.0, 17.0);
		iconFeatured.infoWindowAnchor = new GPoint(10.0, 17.0);
    
    function init() {
    
    	if (document.getElementById("findForm"))
    		document.getElementById("findForm").address.focus();
    
		map = new GMap2(document.getElementById("map"));
    	map.addControl(new GSmallMapControl());
    	map.enableDoubleClickZoom();
    	map.enableContinuousZoom();
    	
    	showNowLoading();
    	
    	//process any pre-defined location as passed in the URL as query parameters
    	if (document.location.href.indexOf("?") > -1) {
    	
    		var lat = centerLatitude;
			var lng = centerLongitude;
			var zoom = startZoom;
		
    		var queryString = document.location.href.split("?")[1];
    		var args = queryString.split("&");
    		for (var i = 0;i < args.length;i++) {
    			var keyValue = args[i].split("=");
    			if (keyValue[0] == "lat") {
    				lat = keyValue[1];
    				zoom = 15
    			} else if (keyValue[0] == "lng") {
    				lng = keyValue[1];
    			} else if (keyValue[0] == "address") {
    				var value = unescape(keyValue[1]);
    				value = value.replace(/\+/g," ");
    				eval(keyValue[0] + " = '" + value + "'");
    			} else if (keyValue[0] == "zoom") {
    				eval("zoom = " + keyValue[1]);
    			} 
    		}
    		
    		if (typeof(address) != "undefined" && typeof(address) != "object") { //safari returns undefined variables as "object"
				//address found in url query params, lets go to it
				var geocoder = new GClientGeocoder();
				geocoder.getLocations(address, function(response) {
					if (!response || response.Status.code != 200) {
						alert("Address not found. Ensure the address is complete and accurate.");
						map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
						updateMarkers();
					} else {
						document.forms["findForm"].elements["address"].value = address;
						//document.getElementById("location").innerHTML = " "+address+" ";
						lastMapAction = "search";
						var place = response.Placemark[0];
						var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
						var accuracy = place.AddressDetails.Accuracy;
						//alert("accuracy: " + accuracy);
						var zoomLevel = accuracy + 8;
						//alert("zoomLevel: " + zoomLevel);
						map.setCenter(point, zoomLevel);
						updateMarkers();
					}
				});
    		} else {
    			map.setCenter(new GLatLng(lat, lng), zoom);
    			updateMarkers();
    		}
    	} else { //no preset location, use default
    		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    		updateMarkers();
    	}

		GEvent.addListener(map,'zoomend',function() {
    	
    		if (resetMapAction) {
    			lastMapAction = "zoom";
    		}
    		
    		if (lastMapAction == "search") {
    			resetMapAction = 1;
    		}
    		
    		map.closeInfoWindow();
    		showNowLoading();
    	});
    	
    	GEvent.addListener(map,'moveend',function() {
    		showNowLoading();
    		updateMarkers();
 			//updateWeather();
    	});
    	
    	GEvent.addListener(map,'dragend',function() {
    		lastMapAction = "drag";
    	});

    }

    function updateMarkers() { //this function saves overlays in the viewable area so they arent redrawn
    
    	//Don't remove all the overlays!

		//map.clearOverlays();
		var mapBounds = map.getBounds();
		var southWest = mapBounds.getSouthWest();
    	var northEast = mapBounds.getNorthEast();
    	var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue();
	
		//loop though each of the points in memory and remove those that
		//aren't going to be shown
		for(k in existingMarkers) {
			if(!mapBounds.contains(existingMarkers[k].getPoint())) {
				map.removeOverlay(existingMarkers[k]);
				delete existingMarkers[k];
			}
		}

    	//log the URL for testing
    	//GLog.writeUrl('geoDataCluster.php?'+getVars);
    	//GLog.writeUrl(map.getCenter());
    	
    	//     
    	//
    	//http://A4543464446.api.wxbug.net/getForecastRSS.aspx?ACode=A4543464446&lat=36.972101&long=-121.988055&unittype=
    
    	//retrieve the points
    	var request = GXmlHttp.create();
    	request.open('GET', 'geoDataCluster.php?'+getVars, true);
    	request.onreadystatechange = function() {
    		if (request.readyState == 4) {
    			var jscript = request.responseText;
    			eval(jscript);
    			if (document.getElementById("listView"))
    				var listElem = document.getElementById("listView");
    			if (!points.p1) { //fix for not repeatedly showing the noresults window after user pans the map
    				//display custom overlay with message here
    				//no points within map bounds
    				if (lastMapAction == "search") {
    					var myAjax = new Ajax("/noResults.php", {
							method: 'get',
							onComplete: updateNoResults
						}).request();
    				}
    				if (typeof(listElem) != "undefined")
    					listElem.innerHTML = "";
    			} else { 
    				if (map.getZoom() > 8) { //show list view of results
						var htmlOut = "";
						var includesForeignResults = false;
						//var displayEventType = 0;
						for (k in points) {
							if (points[k].source == "Weekend Treasure" && points[k].markertype == "x")
								continue;
							
							//if (points[k].eventtype == "auction" && displayEventType == 0) {	
								//var htmlOut = "<ul id='eventselectlist'><li id='caption'>Show:</li><li id='active'><a href='#'>Garage Sales</a></li><li><a href='#'>Auctions</a></li><li><a href='#'>Garage Sales &amp; Auctions</a></li></ul>";
								//displayEventType = 1;
							//}
								
							if (points[k].markertype == "m" || points[k].markertype == "a" || points[k].markertype == "f") {
								var when = getDateString(points[k]);
								var style = (points[k].markertype == "f") ? "saleItemFeatured" : "saleItem";
								var style = (points[k].eventtype == "auction") ? "auctionItem" : style;
								if (points[k].source == "Weekend Treasure") {
									htmlOut += "<div class='"+style+" clearfix' id='marker"+points[k].id+"'>";
									htmlOut += "<span class=\"title\">"+points[k].title+"</span><br/><b>When:</b> "+when+"<br/><b>Where:</b> "+points[k].address;
									htmlOut += "<div class='description'>"+points[k].description+"</div>";
								} else if (points[k].source == "Global Auction Guide") {
									htmlOut += "<div class='"+style+" clearfix' id='marker"+points[k].id+"'>";
									htmlOut += "<a href=\""+points[k].url+"\" class=\"title\" target=\"_blank\">"+points[k].title+"</a><br/><b>When:</b> "+when+"<br/><b>Where:</b> "+points[k].address;
									htmlOut += "<div class='description'>"+points[k].description+"</div>";
								} else {
									htmlOut += "<div class='"+style+" clearfix' id='marker"+points[k].id+"'>";
									htmlOut += "<a href=\""+points[k].url+"\" class=\"title\" target=\"_blank\">"+points[k].title+"</a><br/><b>When:</b> "+when+"<br/><b>Where:</b> "+points[k].address;
									htmlOut += "<div class='description'>* View the <a href=\""+points[k].url+"\" target=\"_blank\" title=\"Weekend Treasure searched the web to locate this "+points[k].eventtype+". Although every attempt has been made to provide an accurate date and location, you should confirm the information on the original source.\">original source</a> for sale details.</div>";
									//htmlOut += "</div><div class='buttons'><a href=\""+points[k].url+"\" target=\"_blank\"><img src='images/btn_viewDetails.gif' alt='View Details' width='90' height='24' border='0' /></a></div>";
									var includesForeignResults = true;
								}
								htmlOut += "</div>";
							}
						}
						if (includesForeignResults) {
							htmlOut += "<p>* Weekend Treasure searched the web to locate this "+points[k].eventtype+". Although every attempt has been made to provide an accurate date and location, you should confirm the information on the original source.</p>";
						}
						listElem.innerHTML = htmlOut;
					} else {
						listElem.innerHTML = "<center><p><em>Tip: Zoom in closer to view sales in list format!</em></p></center>";
					}

			    }
			   
    			
    			//create each point from the list
    			for (k in points) {
    				//if (points[k].source == "Weekend Treasure" && points[k].markertype == "x")
								//continue;
    				var point = new GLatLng(points[k].lat,points[k].lng);
    				//var marker = createMarker(point, points[k].markertype, points[k].html);
    				//map.addOverlay(marker);	
    				
    				//skip it if the marker already exists
					//or is not in the viewable area
					if(!existingMarkers[k] && mapBounds.contains(point)) { //if not in existing markers and map bounds doesnt contain the point
						
						var when = getDateString(points[k]);
						
						var html = "<div class=\"infoWindow\"><p><a href=\""+points[k].url+"\" target=\"_blank\" class=\"title\">"+points[k].title+"<\/a><br/><span class=\"metaInfoTitle\">When:</span> <span class=\"metaInfo\">"+when+"</span><br/><span class=\"metaInfoTitle\">Where: <span class=\"metaInfo\">"+points[k].address+"</span></p><p>"+points[k].description+"</p>";
						existingMarkers[k] = createMarker(point, points[k].id, points[k].markertype, html);
						map.addOverlay(existingMarkers[k]);
					}
    			}
    			
    			hideNowLoading();
    		}
    	}
    	request.send(null);
    }
    
    function updateWeather() {
    	
    	if (map.getZoom() < 11) {
    		hideWeather(); //only show weather on closer zoom levels
    		
    	} else {
    		//get local weather from weatherbug
    		
			var mapCenter = String(map.getCenter());
			mapCenterLatLon = mapCenter.split(",");
			newCenterLatitude = mapCenterLatLon[0].substr(1,mapCenterLatLon[0].length);
			newCenterLongitude = mapCenterLatLon[1].substr(1,mapCenterLatLon[1].length - 2);
			
			var myAjax = new Ajax("/weatherBug.php?lat="+newCenterLatitude+"&lon="+newCenterLongitude, {
				method: 'get',
				onComplete: showWeather
			}).request();
			
			//alert("lat: "+newCenterLatitude);
			//alert("lng: "+newCenterLongitude);
			
			//centerLatitude = 
			//centerLongitude = 
			
		}
    	
    	
    }
    
    function showWeather(responseHTML) {
    	document.getElementById("weatherBug").innerHTML = responseHTML;
    	document.getElementById("weatherBug").style.display = "block";
    }
    
    function hideWeather() {
    	document.getElementById("weatherBug").style.display = "none";
    }

    
    //zooms the map out until it contains at least one sale point
    function zoomToClosestSale() {
    	closeNoResults(1);
		map.setZoom(endZoom);
    }
    
    //closes the "no results" window or populates it with affiliate info
    function closeNoResults(finish) {
    	if (finish) {
    		document.getElementById("noresultContainer").style.display = "none";
    		document.getElementById("noresult").innerHTML = "";
    	} else {
    		var myAjax = new Ajax("/affiliate.php?affid=1", {
				method: 'get',
				onComplete: updateNoResults
			}).request();
    	}
    }
    
    function updateNoResults(responseHTML) { //updates no results content with reposneText from AJAX operation
    	document.getElementById("noresult").innerHTML = responseHTML;
    	if (document.getElementById("location")) {
			document.getElementById("location").innerHTML = " "+document.getElementById("findForm").address.value+" ";
    		showNoResults();
    	}
    }
    
    //display no results window and zoom out until sales are found
    function showNoResults(responseText) {
    	
    	//make sure now loading bar is not visible
    	hideNowLoading();
    	
    	if (responseText) {
    		//we should have some response from our geolocator
    		//alert(responseText);
    		eval(responseText);
    		
    		if (points.p1) {
				//points found, get the zoom level and return
				endZoom = map2.getZoom();
				//display distance
				switch (endZoom) {
					case 0: var distance = "way too far"; break;
					case 1: var distance = "a very very long way"; break;
					case 2: var distance = "a very long way"; break;
					case 3: var distance = "a long way"; break;
					case 4: var distance = "between 320 and 640 miles"; break;
					case 5: var distance = "between 160 and 320 miles"; break;
					case 6: var distance = "between 80 and 160 miles"; break;
					case 7: var distance = "between 40 and 80 miles"; break;
					case 8: var distance = "between 20 and 40 miles"; break;
					case 9: var distance = "between 10 and 20 miles"; break;
					case 10: var distance = "between 5 and 10 miles"; break;
					case 11: var distance = "between 2 and 5 miles"; break;
					case 12: var distance = "a few miles"; break;
					default: var distance = "less than a mile"; break;
				}

				document.getElementById("distance").innerHTML = " "+distance+" ";
				document.getElementById("noresultMessage").style.display = "block";
				return;
			} //else {
				//if (!confirm ("no points found at zoom level " + map2.getZoom())) {
					//return;
				//}
			//}
    	} else {
			//display no results window
    		document.getElementById("noresultContainer").style.display = "block";
    		//init the invisible map
    		map2 = new GMap2(document.getElementById("mapNoResults"));
			map2.setCenter(map.getCenter(), map.getZoom());
			
    	}
		
		//alert("zooming out map2");
    	map2.zoomOut();
    	var mapBounds = map2.getBounds();
		var southWest = mapBounds.getSouthWest();
    	var northEast = mapBounds.getNorthEast();
    	var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue();
    	
    	//check address
    	//document.getElementById("noresultBody").innerHTML = document.getElementById("noresultBody").innerHTML + "<p>/geoDataCluster.php?"+getVars+"</p>";
		var myAjax = new Ajax("/geoDataCluster.php?"+getVars, {
			method: 'get',
			onComplete: showNoResults
		}).request();
		
    }
    
    function createMarker(point, markerId, type, markerHTML) {
    
    	//create the marker with the appropriate icon
    	if (type == 'c') {
    		//GLog.writeUrl('cluster icon found');
    		var marker = new GMarker(point,iconCluster);
    		
    		GEvent.addListener(marker, 'click', function() {		
				//marker.openInfoWindowHtml(markerHTML);
				map.setCenter(point);
				map.zoomIn();
				//keepInfoWindowsOpen = 1;
			});
			
			
    	} else if (type == 'f') {
    		
    		var marker = new GMarker(point,iconFeatured);
    		
    		GEvent.addListener(marker, 'click', function() {		
				marker.openInfoWindowHtml(markerHTML);
				//keepInfoWindowsOpen = 1;
			});
			
			
    	} else if (type == 'a') {
    		
    		var marker = new GMarker(point,iconSingleAuc);
    		
    		GEvent.addListener(marker, 'click', function() {		
				marker.openInfoWindowHtml(markerHTML);
				//keepInfoWindowsOpen = 1;
			});
			
			
    	}   else {

    		//GLog.writeUrl('single icon found');
    		//alert("var marker"+markerId+" = new GMarker(point,iconSingle);");
    		
    		var marker = new GMarker(point,iconSingle);
    		
    		GEvent.addListener(marker, 'click', function() { 
    			marker.openInfoWindowHtml(markerHTML); }
    		);
    		
    		//open info window when user clicks a list result
    		//if(document.getElementById("marker"+markerId)) {
				//addEvent(document.getElementById("marker"+markerId), 'click', function() { 
					//marker.openInfoWindowHtml(markerHTML); }
				//);
			//}
    		
    		
    		
    	}
    	
    	//GEvent.addListener(marker, 'mouseover', function() {		
			//marker.openInfoWindowHtml(markerHTML);
		//});
		
		//GEvent.addListener(marker, 'click', function() {		
			//marker.openInfoWindowHtml(markerHTML);
			//keepInfoWindowsOpen = 1;
		//});
		
		//GEvent.addListener(marker, 'infowindowclose', function() {		
			//keepInfoWindowsOpen = 0;
		//});
		
		//GEvent.addListener(marker, 'mouseout', function() {		
			//if (!keepInfoWindowsOpen) {
				//map.closeInfoWindow();
				//keepInfoWindowsOpen = 0;
			//}
		//});

    	return marker;
    }
	
	function hideNowLoading() {
		if (document.getElementById("loadingIcons"))
			document.getElementById("loadingIcons").style.display = "none";
	}
	
	function showNowLoading() {
		if (document.getElementById("loadingIcons"))
			document.getElementById("loadingIcons").style.display = "block";
	}
	
	function getDateString(p) {
		var when = "(see source)";
		if (p.source == "Weekend Treasure" || p.source == "Global Auction Guide") {
			if (p.start_date != p.end_date) {
				when = p.start_date + " - " + p.end_date;
			} else {
				when = p.start_date;
			}
		if (p.start_time != "" && p.end_time != "")
			when += ", " + p.start_time + " - " + p.end_time;
		}
		return when;
	}

    window.onload = init;