// (c) Copyright 2012, The Software Ranch LLC and Morpho-Graphics LLC. All rights reserved

// Sizes
var gs = 10;			// Grabber size
var gs2 = gs / 2;
var gs3 = gs / 3;
var toolSize = 200;		// Formerly OSSize

function Point(x, y)
{
	this.x = x;
	this.y = y;
}

function Point2(x1, y1, x2, y2)
{
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
}

function Line(x1, y1, x2, y2, type)
{
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.angle = 0;
	this.type = type;
	this.annotation = null;
	
	// Current mouse position.
	getMouseXY(fireFoxEvent);
	this.mouseX = mouseX;
	this.mouseY = mouseY;
}

function moveLine(line, x, y)
{
	line.x1 += x;
	line.y1 += y;
	line.x2 += x;
	line.y2 += y;
}

function zoomLine(line, zoomFactor)
{
	line.x1 = line.x1 * zoomFactor;
	line.y1 = line.y1 * zoomFactor;
	line.x2 = line.x2 * zoomFactor;
	line.y2 = line.y2 * zoomFactor;
}

function zoomPointAround(x, y, zoomFactor, point)
{
	x = point.x + ((x - point.x) * zoomFactor);
	y = point.y + ((y - point.y) * zoomFactor);
}

function zoomLineAround(line, zoomFactor, point)
{
	line.x1 = point.x + ((line.x1 - point.x) * zoomFactor);
	line.y1 = point.y + ((line.y1 - point.y) * zoomFactor);
	line.x2 = point.x + ((line.x2 - point.x) * zoomFactor);
	line.y2 = point.y + ((line.y2 - point.y) * zoomFactor);
}

function drawGrabber(x, y, dc, color)
{
	dc.setColor(color);
	dc.fillEllipse(Math.round(x-gs2), Math.round(y-gs2), gs, gs);
	dc.setColor("000000");
	dc.drawEllipse(Math.round(x-gs2), Math.round(y-gs2), gs, gs);
}

function drawRotator(x, y, angle, dc, color)
{
	// Compute the points.
	var x1 = -gs2;
	var y1 = 0;
	var x2 = 0;
	var y2 = -gs;
	var x3 = gs2;
	var y3 = 0;
	var x4 = 0;
	var y4 = gs;

	// Do the rotation.
	var rad = degToRad(angle);
	rotMatrix(rad);
	vecMult(x1, y1);
	x1 = Math.round(pointX + x);
	y1 = Math.round(pointY + y);
	vecMult(x2, y2);
	x2 = Math.round(pointX + x);
	y2 = Math.round(pointY + y);
	vecMult(x3, y3);
	x3 = Math.round(pointX + x);
	y3 = Math.round(pointY + y);
	vecMult(x4, y4);
	x4 = Math.round(pointX + x);
	y4 = Math.round(pointY + y);

	// Draw 'em.
	var xPoints = new Array(x1, x2, x3, x4);
	var yPoints = new Array(y1, y2, y3, y4);
	dc.setColor(color);
	dc.fillPolygon(xPoints, yPoints);
	dc.setColor("000000");
	dc.drawPolygon(xPoints, yPoints);
}

function drawSizer(x, y, angle, dc, color)
{
	// Compute the points.
	var x1 = -gs;
	var y1 = 0;
	var x2 = 0;
	var y2 = -gs2;
	var x3 = gs;
	var y3 = 0;
	var x4 = 0;
	var y4 = gs2;

	// Do the rotation.
	var rad = degToRad(angle);
	rotMatrix(rad);
	vecMult(x1, y1);
	x1 = Math.round(pointX + x);
	y1 = Math.round(pointY + y);
	vecMult(x2, y2);
	x2 = Math.round(pointX + x);
	y2 = Math.round(pointY + y);
	vecMult(x3, y3);
	x3 = Math.round(pointX + x);
	y3 = Math.round(pointY + y);
	vecMult(x4, y4);
	x4 = Math.round(pointX + x);
	y4 = Math.round(pointY + y);

	// Draw 'em.
	var xPoints = new Array(x1, x2, x3, x4);
	var yPoints = new Array(y1, y2, y3, y4);
	dc.setColor(color);
	dc.fillPolygon(xPoints, yPoints);
	dc.setColor("000000");
	dc.drawPolygon(xPoints, yPoints);
}

function drawLine(x1, y1, x2, y2, width, dc, color)
{
	dc.setColor(color);
	dc.setStroke(width);
	dc.drawLine(x1, y1, x2, y2);
}

function drawCross(x, y, size, width, dc, color)
{
	size = size / 2;
	dc.setColor(color);
	dc.setStroke(width);
	dc.drawLine(x-size, y, x+size, y);
	dc.drawLine(x, y-size, x, y+size);
}

function drawEndpoints(x1, y1, x2, y2, dc, color, drawStart)
{
	var ang = 180 - angle(x1, y1, x2, y2) - 90;

	if (drawStart)
	{
		var point1 = crank1Point(gs, gs2, ang, 0, 0, 1);
		var point2 = crank1Point(gs, -gs2, ang, 0, 0, 1);
		dc.setColor(color);
		dc.drawLine(x1, y1, Math.round(x1 + point1.x), Math.round(y1 + point1.y));
		dc.drawLine(x1, y1, Math.round(x1 + point2.x), Math.round(y1 + point2.y));
	}

	point1 = crank1Point(-gs, gs2, ang, 0, 0, 1);
	point2 = crank1Point(-gs, -gs2, ang, 0, 0, 1);
	dc.drawLine(x2, y2, Math.round(x2 + point1.x), Math.round(y2 + point1.y));
	dc.drawLine(x2, y2, Math.round(x2 + point2.x), Math.round(y2 + point2.y));
}

