	var map;
	var geocoder;
	var title;
	var geoXml;
	var toggleState = 1;
	var latitude = 37.4419;
	var longitude= -100.1419;
	// Create our "tiny" marker icon

	var center;
	var marker;
	var counter = 0;

	function initialize()
	{
	  	if (GBrowserIsCompatible())
		{
			// If ClientLocation was filled in by the loader, use that info instead
			if (google.loader.ClientLocation) {
			  latitude = google.loader.ClientLocation.latitude;
			  longitude= google.loader.ClientLocation.longitude;

			}
			map = new GMap2(document.getElementById("map_canvas"));
			center = new GLatLng(latitude, longitude);

			map.setCenter(center, 4, G_HYBRID_MAP);
			map.addControl(new GLargeMapControl3D());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.enableScrollWheelZoom();
			map.enableContinuousZoom();

			marker = new GMarker(center, {draggable: true});
			map.openInfoWindowHtml("Start by dragging this marker to any location.");
			map.addOverlay(marker);
			GEvent.addListener(marker, "dragstart", function() {
				Effect.Fade('searchresults');
			});
															
			GEvent.addListener(marker, "dragend", function() {
				getBars(marker.getLatLng());
			});
			
			GEvent.addListener(map, "click", function(overlay, latlng) {
				getBars(latlng);
				marker.setLatLng(latlng);
			});

			}
      Element.setOpacity('ads', .7);
      Element.setOpacity('searchresults', .9);
	  Element.setOpacity('share', .8);
	}

  function queryValue(text)
  {
    if(text.value == "Find businesses, addresses and places of interest.") {
      text.value = '';
    }
    else if(text.value == "") {
      text.value = 'Find businesses, addresses and places of interest.';
    }
  }


	function findLocation()
	{
		var coords;
		var query = $('searchfield').value;
		
		if(query != "")
		{
			//assume input is an address and let Google deal with it
			var geocoder = new GClientGeocoder();
			if(geocoder)
			{
				geocoder.getLatLng(query, getBars);
			}
		}
	}


function drawCircle(center, radius, color, thickness, opacity, fillColor, fillOpacity) {
	//Function created by Chris Haas
	var circleQuality = 1;			//1 is best but more points, 5 looks pretty good, too
	var M = Math.PI / 180;			//Create Radian conversion constant
	var L = map.getBounds();		//Holds copy of map bounds for use below
	var sw = L.getSouthWest();
	var ne = L.getNorthEast();

	//The map is not completely square so this calculates the lat/lon ratio
	// this works because we create a square map
	var circleSquish = (ne.lng() - sw.lng()) / (ne.lat() - sw.lat());

	var points = [];							//Init Point Array
	//Loop through all degrees from 0 to 360
	for(var i=0; i<360; i+=circleQuality){
		var P = new GLatLng(
			center.lat() + (radius * Math.sin(i * M)),
			center.lng() + (radius * Math.cos(i * M)) * 1
			);
		points.push(P);
	}
	points.push(points[0]);	// close the circle
	var p = new GPolygon(points, color, thickness, opacity, fillColor, fillOpacity)
	map.addOverlay(p);
}
function showAddress(response) {
var place = response.Placemark[0];
title = place.address + " " + place.AddressDetails.Country.CountryName;
}

	function getBars(latlng)
	{
		if(latlng != null)
		{
			counter++;
			map.clearOverlays();
			drawCircle(latlng, .18, '#FF9527', 1, .9, '#000000', .5);
geocoder = new GClientGeocoder();
if(geocoder)
{
	geocoder = geocoder.getLocations(latlng, showAddress);
//alert (title);
}

			geoXML = GDownloadUrl("/nearby2.php?lat="+latlng.lat()+"&lon="+latlng.lng()+"&title="+escape(title)+"&ads=", function(doc) {
				var xmlDoc = GXml.parse(doc);
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	
				for (var i = 0; i < markers.length; i++)
				{
				// obtain the attribues of each marker
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var lng = parseFloat(markers[i].getAttribute("lng"));
					var point = new GLatLng(lat,lng);
					var html = markers[i].childNodes[0].nodeValue;
					var barmarker = createMarker(point,html);
					map.addOverlay(barmarker);
				}
				var dropped;
				if(markers.length == 1)
				{
					map.setCenter(latlng, 10);
					dropped = "One bar was found within 10 miles of this location.  Click the orange marker to view its details."
				}
				else if (markers.length == 0)
				{
				map.setCenter(latlng);
					dropped = "No bars were found within 10 miles of this location."
				}
				else
				{
					map.setCenter(latlng, 10);
					dropped = markers.length+" bars were found within 10 miles of this location.  Click any of the orange markers to view the details of a given bar."
				}
	
				if(counter % 10 == 0 || markers.length > 10)
				{
					$('ads').style.display = "block";
					$('ads').src = "/nearby2.php?lat="+latlng.lat()+"&lon="+latlng.lng()+"&ads=1";
				}
				$('searchresults').style.display="none";
				
				map.savePosition();
				marker.openInfoWindowHtml(dropped, { maxWidth: 200});
				map.addOverlay(marker, {zIndexProcess: function(marker, b) { return 999999;}});
	
			});
		}
	}

	// A function to create the marker and set up the event window
	function createMarker(point, html) {
		var kokopelli = new GIcon(G_DEFAULT_ICON);
		kokopelli.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/orange-dot.png";

		// Set up our GMarkerOptions object
		var markerOptions = { icon:kokopelli };

		var barmarker = new GMarker(point, markerOptions);
		GEvent.addListener(barmarker, "click", function() {
		barmarker.openInfoWindowHtml(html);
		counter++;
		if(counter % 10 == 0)
		{
			$('ads').style.display = "block";
			$('ads').src = "/nearby.php?lat="+point.lat()+"&lon="+point.lng()+"&ads=1";
		}
		});
		// save the info we need to use later for the side_bar
		return barmarker;
	}
