//var highlightCircle;
var map; // the Primary Google Map
var circle; // the Circle object

Number.prototype.round = function(precision){
	if(precision==null){precision = 2;}
	var log = precision * 10;
	return Math.round(this * log)/log;
}
Number.prototype.toRad = function() {  // convert degrees to radians
  return this * Math.PI / 180;
}

Number.prototype.toDeg = function() {  // convert radians to degrees (signed)
  return this * 180 / Math.PI;
}

Number.prototype.toBrng = function() {  // convert radians to degrees (as bearing: 0...360)
  return (this.toDeg()+360) % 360;
}

try {
	GLatLng.prototype.pointFromOriginal = function(distance, bearing){
		// http://www.movable-type.co.uk/scripts/latlong.html#destPoint
		var lat1 = this.latRadians();
		var lon1 = this.lngRadians();
		var d = distance; // in kmeters
		var R = 6371; // earth's mean radius in kilometers
		var brng = bearing.toRad();
		// BEARINGS
		// 0 = Due North
		// Math.PI = Due South
		// (Math.PI * 0.5) = Due East
		// (Math.PI * 1.5 ) = Due West
		// (Math.PI * 1.25) = SouthWest
		// (Math.PI * 0.25) = NorthEast
		
		var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d / R) + Math.cos(lat1) * Math.sin(d / R) * Math.cos(brng));
		var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(d / R) * Math.cos(lat1), Math.cos(d / R) - Math.sin(lat1) * Math.sin(lat2));
		var point = new GLatLng(lat2.toDeg(), lon2.toDeg())
		//debug("Drawing point from original: "+point);
		//map.addOverlay( new GMarker(point) );
		return point;
	}
} catch (e) {}

function submitPrivacyInfo(auth_token){
	var parameter_values = {
		authenticity_token: auth_token,
		privacy_radius: circle.radius,
		latitude: circle.center.lat(),
		longitude: circle.center.lng()
	}
	debug(parameter_values);
	new Ajax.Request(
		'/signup/verify_privacy',
		{method: 'post', parameters: parameter_values, evalJS: true}
	)
}

function initSlider(){
	return new Control.Slider(['slider'], 'track', {
		sliderValue: 3,
		minimum: 1,
		maximum: 8,
	    range: $R(1,8),
	    values: $R(1,8),
	    restricted: true,
		onSlide: function(value){
			circle.changeRadius( value /10 );
		},
		onChange: function(value){
			var zoomValue = 19-value;
			if(value < 2){ zoomValue = 17;
			}else if(value < 4){ zoomValue = 16;
			}else if(value < 8){ zoomValue = 15;
			}else{ zoomValue = 14; }
			map.setZoom(zoomValue);
			circle.dragMarker.redraw();
		}
	});
}
