var circle = false
var points = []

var values = {
			diskRadius: 200,
			createDiskBtn: 'Create Disk',
			fromPoint: 0,
			toPoint: 0,
			addPointBtn: 'Add Line',
			inOut: true,
}

var components = {
			diskRadius:{
				type: 'number',
				label: 'Disk Radius',
				min: 1,
			},
			createDiskBtn:{
				onClick: function(){
					circle = new HyperbolicDisk(document.bounds.center, values.diskRadius, true)
					components.addPointBtn.enabled = true
				}
			},
ruler0:{
	type: 'ruler',
	label: 'LINES',
},
			fromPoint:{
				type: 'number',
				label: 'From',
				units: 'degree',
				steppers: true,
			},
			toPoint:{
				type: 'number',
				label: 'To',
				units: 'degree',
				steppers: true,
			},
			addPointBtn:{
				enabled: false,
				onClick: function(){
					if(points.length == 0){
						points.push(values.fromPoint)
					}
					points.push(values.toPoint)
					checkPoints()
				}
			},
			inOut:{
				type: 'boolean',
				label: 'Inside / Outside',
			}
}

var palette = new Palette('Hyperbolic Disk', components, values)

function onMouseDown(event){
			if(circle){
			    var hitResult = document.hitTest(event.point)
	
			    if(hitResult && hitResult.curve && hitResult.item == circle.Disk){
					newAngle = hitResult.curve.getPoint(hitResult.parameter) - circle.Origin
			    } else {
					newAngle = event.point - circle.Origin
			    }

				newAngle.y *= -1
				points.length == 1 ? values.toPoint = newAngle.angle : values.fromPoint = newAngle.angle
				points.push(newAngle.angle)
				checkPoints()
			}
}

function checkPoints(){
			if(points.length == 2){
				values.toPoint = points[1]
				circle.Line(values.fromPoint, values.toPoint, values.inOut)
				if(circle.Lines.length > 0) circle.Lines[circle.Lines.length - 1].moveBelow(circle.Disk)
				points = []
			}
}








/*
		A Simple Hyperbolic Disk Model v1.0
		by Håkan Lundgren
		© 2011 Monovektor.com



		Create a Hyperbolic Disk by assigning a point of origin, a radius
		and a boolean value. This is nothing special, it's just an ordinary
		circle. Take this for instance:
			var hd = new HyperbolicDisk(document.bounds.center, 200, true)

		The disk is placed at the center of the document with a radius of 200pt.
		The third argument is wether or not to draw the actual circle. Otherwise
		it wouldn't show up on the artboard.


		In order to draw the lines, use the Line() command as such:
			hd.Line(20, 140, true)

		This draws a line starting at angle 20 and ending at angle 140. The
		third argument is used to determine what side of the disk the line
		will be drawn. A true argument will draw the line INSIDE the disk
		while set to false the line will be draw OUTSIDE the disk.
		There is one exception though, if the difference between the two
		angles is exactly 180 deg the line will be drawn inside to avoid
		drawing a circle of infinite radius.


		So, the general usage will be:
			var hd = new HyperbolicDisk(center point, radius, boolean)
			hd.DrawDisk()
			hd.Line(1st angle, 2nd angle, boolean)



		Last Modified:
			2012-01-23 @ 15:36
*/

var HyperbolicDisk = function(Origin, Radius, bool){
			this.Origin 		= 		Origin
			this.Radius 		= 		Radius
			if(bool) this.Disk 	= 		new Path.Circle(this.Origin, this.Radius)
			this.Lines 			= 		[]
}

HyperbolicDisk.prototype.ArcPoint = function(angle1, angle2, bool){
			var point1			= 		this.Origin + new Point(Math.cos(angle1.toRadians()), Math.sin(-angle1.toRadians())) * this.Radius
			var point2			= 		this.Origin + new Point(Math.cos(angle2.toRadians()), Math.sin(-angle2.toRadians())) * this.Radius	
			var theta			= 		((this.Origin - point1).angle + (this.Origin - point2).angle) / 2
			var dTheta			= 		Math.abs((this.Origin - point1).angle - (this.Origin - point2).angle) / 2
			if(dTheta == 90)			return [point1, point2]
			if(dTheta == 0)				return false
			var r				= 		this.Radius * Math.tan(dTheta.toRadians())
			var R				= 		this.Radius / Math.cos(dTheta.toRadians())
			var arcPoint 		= 		this.Origin - new Point({
												length: bool ? R - r : R + r,
												angle: (R - r < 0) ? theta + 180 : theta
										})
			return 						[point1, arcPoint, point2]
}

HyperbolicDisk.prototype.Line = function(angle1, angle2, bool){
			var points 			= 		this.ArcPoint(angle1, angle2, bool)
			if(!points || points.length < 2 || points.length > 3) return
			this.Lines.push(points.length < 3 ? new Path.Line(points[0], points[1]) : new Path.Arc(points[0], points[1], points[2]))
}