var circle = null;
var map = null;

function map_center()
{
  var address = document.getElementById('zip').innerHTML;
  var geo = new google.maps.Geocoder();
  if(geo)
  {
    geo.geocode( { 'address': address}, function(results, status)
    {
      if(status == google.maps.GeocoderStatus.OK)
      {
        map_load(results[0].geometry.location.lat(), results[0].geometry.location.lng());
        return;
      }
    });
  }
  map_load();
}

function map_load(lat, lng)
{
  if(!document.getElementById("map"))
    return;
  var latitude = (lat != undefined) ? lat : 34.038529;
  var longitude = (lng != undefined) ? lng : -84.343111;

  var gmaplatlng = new google.maps.LatLng(latitude, longitude);
  var gmapopts = { zoom: 9, center: gmaplatlng, mapTypeId: google.maps.MapTypeId.ROADMAP };

  map = new google.maps.Map(document.getElementById("map"), gmapopts);
  map.setCenter(new google.maps.LatLng(latitude, longitude), 6);
  circleLatLngs = new Array();
  var circleLat = 15 * 0.014483;
  var circleLng = circleLat / Math.cos(latitude * (Math.PI / 180));
  var numPoints = 50;
	
  for(var i = 0; i < numPoints + 1; i++)
  { 
    var theta = Math.PI * (i / (numPoints / 2)); 
    var vertexLat = latitude + (circleLat * Math.sin(theta)); 
    var vertexLng = longitude + (circleLng * Math.cos(theta));
    var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
    circleLatLngs.push(vertextLatLng); 
  }
  polygon = new google.maps.Polygon({
    paths: circleLatLngs,
    strokeColor: "#ffffff",
    strokeOpacity: 1.0,
    strokeWeight: 1,
    fillColor: "#0099ff",
    fillOpacity: 0.3
  });

  polygon.setMap(map);
}
window.onload = map_center;

