var all_markers = Array();

function geocode_and_show (address) {

  geocoder = new google.maps.ClientGeocoder();
  geocoder.setBaseCountryCode('CA');  
  geocoder.getLocations(address, set_location);
  
}
function set_location(response)
{
  if(response.Status.code != 200)
  {
    alert('Oops... address not recognized by Google!');
    return false;
  }
  
  var zoom=15;
  switch(response.Placemark[0].AddressDetails.Accuracy)
  {
    case 0:
      zoom=1;
      break;
    // country level
    case 1:
      zoom=4;
      break;
    case 2:
      zoom=7;
      break;
    case 3:
      zoom=9;
      break;
    // city level
    case 4:
      zoom=11;
      break;
    case 5:
      zoom=12;
      break;
    case 6:
      zoom=13;
      break;
    default:      
      zoom=14;
      break;
  }
  
  point = new google.maps.LatLng(response.Placemark[0].Point.coordinates[1], response.Placemark[0].Point.coordinates[0]);
  
  map.setCenter(point, zoom);

  return true;

}

var circleLine;
var circleLine2;

function drawCircle(radius) {
	
	var radius = radius ? radius : 50;
	
	var normalProj = G_NORMAL_MAP.getProjection();
	
	var zoom = map.getZoom();
	var centerPt = normalProj.fromLatLngToPixel(map.getCenter(), zoom);
	
	var searchRadiusPoint = new GLatLng(map.getCenter().lat() + (radius * 0.621371192) / 69.172, map.getCenter().lng()); // convert kms to miles, then miles divided by miles per line of latitude
	
	var radiusPt = normalProj.fromLatLngToPixel(searchRadiusPoint, zoom);
	
	var circlePoints = Array();
	var circlePoints2 = Array();

	with (Math) {
		var radius = floor(sqrt(pow((centerPt.x-radiusPt.x),2) + pow((centerPt.y-radiusPt.y),2)));
		var thickness = min(255,radius);
		
		for (var a = 0 ; a < 361 ; a+=10 ) {
			var aRad = a * (PI/180);
			y = centerPt.y + radius * sin(aRad)
			x = centerPt.x + radius * cos(aRad)
			var p = new GPoint(x,y);
			circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
		}
		
		for (var n = 1 ; n < floor(2* radius / thickness)+1 ; n++ ) {
			radiusB = radius - (thickness / 2) * n;
			
			for (var a = 0; a < 361; a += 10 ) {
				var aRad = a * (PI/180);
				y = centerPt.y + radiusB * sin(aRad)
				x = centerPt.x + radiusB * cos(aRad)
				var p = new GPoint(x,y);
				circlePoints2.push(normalProj.fromPixelToLatLng(p, zoom));
			}
			
			if (circleLine2) {
				map.removeOverlay(circleLine2);
			}
			
			circleLine2 = new GPolyline(circlePoints2, '#009900', thickness, 0.2);
			map.addOverlay(circleLine2);
		}
		
		if (circleLine) {
			map.removeOverlay(circleLine);
		}
		
		circleLine = new GPolyline(circlePoints,'#009900', 2, 1);
		map.addOverlay(circleLine);
	}
	
	return circleLine;
	
}

function mapsLoaded() {
	
	window.setTimeout(drawCircle, 500);
	
}

function get_directions(starting_point) {
	
	geocoder = new google.maps.ClientGeocoder();
	geocoder.setBaseCountryCode('CA');  
	geocoder.getLocations(map_directions_to, set_geocoded_directions_to);
	geocoder.getLocations(starting_point, set_directions);
	
}

function set_geocoded_directions_to(response) {
	
	map_directions_to = response.Placemark[0].Point.coordinates[1] + ', ' + response.Placemark[0].Point.coordinates[0];

}

var map_directions;
function set_directions(response) {
	
	if (response.Status.code != 200) {
		
		alert('Oops... please try a different starting address');
		return false;
		
	}
	
	panel = $('directionsPanel');
	
	if (!map_directions) {
		
		map_directions = new GDirections(map, panel);
		
	}
	
	var map_directions_from = response.Placemark[0].Point.coordinates[1] + ', ' + response.Placemark[0].Point.coordinates[0];
	
	map_directions.load(map_directions_from + ' to ' + map_directions_to);
	
	return true;
	
}

var zoomed = false;

function mapZoomToFit(map, force) {
	
	map.checkResize();
	
	if (zoomed && !force) {
		
		return;
		
	}
	
	zoomed = true;
	
	var bounds = new GLatLngBounds();
	
	all_markers.each(function(marker) {
		
		bounds.extend(marker.getLatLng());
		
	});
	
	var g_maps = Array(map);
	
	g_maps.each(function(map) {
		
		map.setCenter(bounds.getCenter());
		
		// only change the zoom if the map has more than one point
		if ((all_markers.length / g_maps.length) > 1) {
			
			var zoomMapLevel = map.getBoundsZoomLevel(bounds) - 1;
			zoomMapLevel = zoomMapLevel > 13 ? 13 : zoomMapLevel;
			
			map.setZoom(zoomMapLevel);
			
		}
		
	});
	
}

// window.onunload = GUnload;



