var oldPoly = [];

function eraseCircles() {
	if(oldPoly) {
		for (var i = 0; i < oldPoly.length; i++) {
			map.removeOverlay(oldPoly[i]);
		}
		oldPoly = [];
	}
}

function drawCircle(center, radius, color, thickness, opacity) {
	//Function created by Chris Haas
	var circleQuality = 20;			//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);
	oldPoly.push(p);
}

function newMarker(markerLocation, describe, markerIcon) {
	var marker=new GMarker(markerLocation, {title:describe, icon:markerIcon});
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(describe);
	});
	var circle = -1;
	GEvent.addListener(marker, 'dblclick', function() {
               if ( circle < 0){
		      circle = oldPoly.length;
		      drawCircle(marker.getPoint(), 0.005, '#0000FF', 1, 1.0);
		      drawCircle(marker.getPoint(), 0.018, '#FF0000', 1, 1.0);
               } else {
	     	       map.removeOverlay(oldPoly[circle]);
		       map.removeOverlay(oldPoly[circle+1]);
		       circle = -1;
               }
        });
	return marker;
}

