//initialize variables
var map;
var container;
var zoom = 9;
var centerPoint = new GLatLng(35.0,-90.0);
var dirObj = new GDirections();
var centerMarker;
var circleMarkers = Array();
var circlePoints = Array();
var drivePolyPoints = Array();
var searchPolygon,drivePolygon;
var distToDrive = 30; // miles
var pointInterval = 30;

//initialize map and add towers with coverage overlay
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(39.842937, -84.147802), 13);
    //map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    
    //add towers
    var towerOne = new GLatLng(39.842937, -84.147802);
    map.addOverlay(new GMarker(towerOne));
    getCirclePoints(towerOne, 75);
  }
}

//get position from user entered address
function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}

//get all points along a circle
function getCirclePoints(center,radius){
	var circlePoints = Array();
	var searchPoints = Array();
 
	with (Math) {
		var rLat = (radius/3963.189) * (180/PI); // miles
		var rLng = rLat/cos(center.lat() * (PI/180));
 
		for (var a = 0 ; a < 361 ; a++ ) {
			var aRad = a*(PI/180);
			var x = center.lng() + (rLng * cos(aRad));
			var y = center.lat() + (rLat * sin(aRad));
			var point = new GLatLng(parseFloat(y),parseFloat(x),true);
			circlePoints.push(point);
			if (a % pointInterval == 0) {
				searchPoints.push(point);
			}
		}
	}
 
	searchPolygon = new GPolygon(circlePoints, '#0056ff', 2, 1, '#0056ff', 0.2);	
	map.addOverlay(searchPolygon);
	map.setCenter(searchPolygon.getBounds().getCenter(),map.getBoundsZoomLevel(searchPolygon.getBounds()));
 
	return searchPoints;
 
}
