  function drawCircle(center, radius, color, thickness, opacity) {
	//Function created by Chris Haas
	var circleQuality = 5;			//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)) * circleSquish
			);
		points.push(P);
	}
	points.push(points[0]);	// close the circle
	var p = new GPolyline(points, color, thickness, opacity)
	map.addOverlay(p);
  }

  function add_marker(lat, lng, text, options)
  {
      options = typeof(options) != 'undefined' ? options : {};
      var point = new GLatLng(lat, lng);
      y = point;
      var marker = new GMarker(point, options);
      map.addOverlay(marker);
      GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(text);
      });
      return marker;
  }

  function show_marker(marker, text)
  {
      map.centerAndZoom(marker.getPoint(), 4);
      marker.openInfoWindowHtml(text);
  } 
  
  function add_and_show_marker(lat, lng, text, options)
  {
      marker = add_marker(lat,lng,text, options);
      x = marker;
      show_marker(marker, text);
  } 

            
function ml_to_img(ml)
{
    return webroot + "images/" + ml.substring(0,5) + "/" + ml + ".jpg";
}

function show_properties(data)
{
  for(x in data)
  {
    var prop = data[x];
    if(prop.pics > 0)
    {
        var img =  ml_to_img(prop.ML_Number);
    }
    else
        var img = webroot + 'media/siteimages/house.gif';

    var ml = '<a style="float:left;margin:3px;color:#000;text-decoration:none;" href="' + webroot + 'property/' + prop.ML_Number + '">' +
        '<img src="' + img + '" width="100"><br>' + prop.ML_Number + "</a>";
    var price = prop.price;
    var address = prop.address.split(",");
    address = address.join("<br>");
    add_marker(prop.latitude, prop.longitude, ml + "<br>$" + price + "<br>" + address + "<br>Beds: " + prop.beds + "<br>Baths: " + prop.baths);
  }
}

function show_school(id)
{
  s = schools[id];
  map.clearOverlays();
  add_and_show_marker(s.lat, s.lng, s.address, {icon: iconBlue});
  center = new GLatLng(s.lat, s.lng);
  drawCircle(center, 1/70 * radius , '#0000FF', 1, .5);
  $.getJSON(webroot + "ajax/radius?lat=" + s.lat + "&lng=" + s.lng + "&radius=" + radius, show_properties);
}

function show_point(s)
{
  map.clearOverlays();
  //add_and_show_marker(s.lat(), s.lng(), '', {icon: iconBlue});
  center = new GLatLng(s.lat(), s.lng());
  drawCircle(center, 1/70 * radius, '#0000FF', 1, .5);
  $.getJSON(webroot + "ajax/radius?lat=" + s.lat() + "&lng=" + s.lng() + "&radius=" + radius, show_properties);
}

function show_address(s)
{
  ilat = parseInt(s.lat);
  ilng = parseInt(s.lng);                        
  if((ilat < 37 || ilat > 38) || (ilng > -120 || ilng < -121))
  {
    $("#address-error").text("Address not found in the Modesto Area. Addresses should look like 123 Smith St, Modesto CA");
    return false;
  } 
  else $("#address-error").text("");
  map.clearOverlays();
  add_and_show_marker(s.lat, s.lng, $("#address").val(), {icon: iconBlue});
  center = new GLatLng(s.lat, s.lng);
  drawCircle(center, 1/70 * radius, '#0000FF', 1, .5);
  $.getJSON(webroot + "ajax/radius?lat=" + s.lat + "&lng=" + s.lng + "&radius=" + radius, show_properties);
}