
////pan and zoom to fit
function fit(map, bounds){
	var new_zoom = map.getBoundsZoomLevel(bounds);
	//alert("map zoom:" + map.getZoom());
	//alert("new zoom:" + new_zoom);
	if (map.getZoom() > new_zoom ) {
		map.panTo(bounds.getCenter());
		map.setZoom(new_zoom);
	}
}

function drawSquare(map, center, radius, bounds) {
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	var bottomLat = center.lat() + (radius/latConv * Math.cos(180 * Math.PI/180));
	var topLat = center.lat() + (radius/latConv * Math.cos(0 * Math.PI/180));
    var leftLng = center.lng() + (radius/lngConv * Math.sin(270 * Math.PI/180));
    var rightLng = center.lng() + (radius/lngConv * Math.sin(90 * Math.PI/180));
	
    var points = [];
	points.push(new GLatLng(bottomLat, leftLng));
	bounds.extend(new GLatLng(bottomLat, leftLng)); //this is for fit function
	points.push(new GLatLng(topLat, leftLng));
	bounds.extend(new GLatLng(topLat, leftLng)); //this is for fit function
	points.push(new GLatLng(topLat, rightLng));
	bounds.extend(new GLatLng(topLat, rightLng)); //this is for fit function
	points.push(new GLatLng(bottomLat, rightLng));
	bounds.extend(new GLatLng(bottomLat, rightLng)); //this is for fit function
	points.push(points[0]);
	
	//points.push(points[0]); // Closes the circle, thanks Martin
	var liColor;
	liWidth = 2;
	var liOpa;
	fillColor = "#d0f5ff";
	var fillOpa;
	
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}
	
function drawCircle(map, center, radius, nodes, bounds){
	// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	//Loop 
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step){
		var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), 
							   center.lng() + (radius/lngConv * Math.sin(i * Math.PI/180)));
		points.push(pint);
		bounds.extend(pint); //this is for fit function
	}
	points.push(points[0]); // Closes the circle, thanks Martin
	var liColor;
	liWidth = 2;
	var liOpa;
	fillColor = "#d0f5ff";
	var fillOpa;
	
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
	fit(map, bounds);
}
