/*
City of Saint Paul Open Source License

LICENSE

Copyright © 2006 City of Saint Paul, Minnesota. 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following terms and conditions:

1.The above copyright notice and this permission notice shall be included in all copies of this Software or works derived from this Software.
2.Whenever a Recipient distributes or redistributes the Software, a copy of this agreement must be included with each copy of the Software
3.If Recipient distributes or redistributes the Software in any form other than source code, Recipient must also make the source code freely available, and must provide with each copy of the Software information on how to obtain the source code in a reasonable manner on or through a medium customarily used for software exchange
4.A Recipient may not make any representation in the Software or in any promotional, advertising or other material that may be construed as an endorsement by the City of Saint Paul of any product or service provided by Recipient, or that may seek to obtain commercial advantage by the fact of the City of Saint Paul’s participation in this Agreement.

DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION

No Warranty:  The Software is provided “as is” without any warranty of any kind, either expressed, implied, or statutory, including, but not limited to, any warranty that the Software will conform to specifications, any implied warranties of merchantability, fitness for a particular purpose, or freedom from infringement, any warranty that the Software will be error free, or any warranty that documentation, if provided, will conform to the Software. This agreement does not, in any manner, constitute an endorsement by the City of Saint Paul or any prior recipient of any results, resulting designs, hardware, software products or any other applications resulting from use of the Software. Further, the City of Saint Paul disclaims all warranties and liabilities regarding third-party software, if present in the original Software, and distributes it “as is.”

Waiver and Indemnity:  Recipient agrees to waive any and all claims against the City of Saint Paul, its contractors and subcontractors, as well as any prior recipient.  If recipient's use of the Software results in any liabilities, demands, damages, expenses or losses arising from such use, including any damages from products based on, or resulting from, recipient's use of the Software, recipient shall indemnify and hold harmless the City of Saint Paul, it’s contractors and subcontractors, as well as any prior recipient, to the extent permitted by law. Recipient's sole remedy for any such matter shall be the immediate, unilateral termination of this agreement.

SUPPORT STATEMENT 

The Software is freely provided by the City of Saint Paul as a public service.  The recipient is herby notified that the provision and distribution of the Software should in no way be construed to mean that the City of Saint Paul is obligated to provide any level of support for the Software, now or in the future.  Support in this context shall be construed to mean providing coordination, technical assistance, responding to questions, releasing bug fixes, providing future releases, or otherwise obligating the City of Saint Paul to commit resources to maintain this Software or assist in its implementation by any person or entity.  The City of Saint Paul may, at it’s sole option, and without notice or obligation to the recipients, provide varying levels of support for the Software or terminate the agreement.  Provision of such support shall not alter the condition that the Software is provided without support as stated above.
*/

/*
 * I just wanted to clean up the main file a little bit.  So there's no special objects or library
 * functions here, it's just so that the main-code is more about the interface definition and the
 * external files keep most of the math/operational code in them.
 */

function selectionRectUp(evnt) {
	if(window.event) { evnt = window.event; }

	var zoomLine = new LineSegment(zoomStartPoint, zoomEndPoint);

	if(ZoomBox) {
		var myPoly = new Polygon();
		var name = 'Rectangle Area';
		var shape = 0;

		zoomLine.pointA = pointToGISCoords(zoomLine.pointA);
		zoomLine.pointB = pointToGISCoords(zoomLine.pointB);

		var OtherPointA = new Point(zoomLine.pointA.x, zoomLine.pointB.y);
		var OtherPointB = new Point(zoomLine.pointB.x, zoomLine.pointA.y);

		var newLine = new LineSegment(zoomLine.pointA, OtherPointB);
		var newLine2 = new LineSegment(zoomLine.pointB, OtherPointA);


		var points = new Array();
		points[0] = zoomLine.pointA;
		points[1] = OtherPointB;
		points[2] = zoomLine.pointB;
		points[3] = OtherPointA;

		//name = createSelectionArea(name, shape, newLine.toString('+')+'+'+newLine2.toString('+')+'+'+zoomLine.pointA.toString('+'));
		var newPoly = new Polygon();
		newPoly.buildFromPoints(points);

		name = createSelectionArea(name, newPoly);

		var p = ZoomBox.parentNode;
		p.removeChild(ZoomBox);

		ZoomBox = null;
	}
	zoomStartPoint = null;
	zoomEndPoiint = null;

}

function selectionCircleDown(evnt) {
	if(window.event) { evnt = window.event; }
	zoomStartPoint = new Point(evnt.clientX, evnt.clientY);
	if(ZoomBox == null) {
		ZoomBox = document.createElement('div');
		ZoomBox.className = 'ZoomBox';
		ZoomBox.style.position = 'absolute';
		ZoomBox.style.border = 'solid 1px red';
		ZoomBox.style.zIndex = 49;
		document.getElementsByTagName('body')[0].appendChild(ZoomBox);
	}

}

function selectionCircleMove(evnt) {
	if(window.event) { evnt = window.event; }
	if(zoomStartPoint && ZoomBox) {
		zoomEndPoint = new Point(evnt.clientX, evnt.clientY);

		var testLine = new LineSegment(zoomStartPoint, zoomEndPoint);
	
		var minX = zoomStartPoint.x, minY = zoomStartPoint.y;
		var dimension = testLine.getHeight();
		var w = testLine.getWidth();

		if(dimension > w) {
			dimension = w;
		}

		if(zoomEndPoint.x < zoomStartPoint.x) {
			minX -= dimension;
		}
		if(zoomEndPoint.y < zoomStartPoint.y) {
			minY -= dimension;
		}

		var StartPoint = testLine.pointA;
		var EndPoint = testLine.pointB;
		ZoomBox.style.left = minX + 'px';
		ZoomBox.style.top = minY + 'px';
		ZoomBox.style.width = dimension + 'px';
		ZoomBox.style.height = dimension + 'px';
	}
}

function selectionCircleRadiusMove(evnt) {
	if(window.event) { evnt = window.event; }
	if(zoomStartPoint && ZoomBox) {
		zoomEndPoint = new Point(evnt.clientX, evnt.clientY);

		var testLine = new LineSegment(zoomStartPoint, zoomEndPoint);
	
		var minX = zoomStartPoint.x, minY = zoomStartPoint.y;

		var dimension = testLine.getHeight();
		var w = testLine.getWidth();

		if(dimension > w) {
			dimension = w;
		}

		minX = zoomStartPoint.x - dimension;
		minY = zoomStartPoint.y - dimension;

		ZoomBox.style.left = minX + 'px';
		ZoomBox.style.top = minY + 'px';
		ZoomBox.style.width = (dimension *2) + 'px';
		ZoomBox.style.height = (dimension *2 )+ 'px';
	}
}


function selectionCircleRadiusUpGeneric(Radius) {
	var name = 'Circle Area';
	var shape = 2;

	var zoomLine = new LineSegment(zoomStartPoint, zoomEndPoint);

	var dimension = zoomLine.getHeight();
	var w = zoomLine.getWidth();

	if(dimension > w) {
		dimension = w;
	}
	if(Radius) {
		dimension = Radius * 2;
	}

	zoomLine.pointA = pointToGISCoords(new Point(zoomStartPoint.x - dimension, zoomStartPoint.y - dimension));
	zoomLine.pointB = pointToGISCoords(new Point(zoomStartPoint.x + dimension, zoomStartPoint.y + dimension));

	var poly = new Circle();
	poly.Center = pointToGISCoords(new Point(zoomStartPoint.x, zoomStartPoint.y));

	if(!Radius) {
		poly.Radius = dimension*mapScale;
	} else {
		poly.Radius = Radius;
	}

	name = createSelectionArea(name, poly);
}

function selectionCircleRadiusUpPrompt(evnt) {
	if(window.event) { evnt = window.event; }


	if(ZoomBox) {
		selectionCircleRadiusUpGeneric(parseFloat(prompt('Radius of Selection Circle in Ground Units?')));

		var p = ZoomBox.parentNode;
		p.removeChild(ZoomBox);
		ZoomBox = null;
	}
	zoomStartPoint = null;
	zoomEndPoiint = null;

}

function selectionCircleRadiusUp(evnt) {
	if(window.event) { evnt = window.event; }


	if(ZoomBox) {
		selectionCircleRadiusUpGeneric();

		var p = ZoomBox.parentNode;
		p.removeChild(ZoomBox);
		ZoomBox = null;
	}
	zoomStartPoint = null;
	zoomEndPoiint = null;
}

function selectionCircleUp(evnt) {
	if(window.event) { evnt = window.event; }

	var zoomLine = new LineSegment(zoomStartPoint, zoomEndPoint);

	if(ZoomBox) {

		var name = 'Circle Area';
		var shape = 2;

		var dimension = zoomLine.getHeight();
		var w = zoomLine.getWidth();

		if(dimension > w) {
			dimension = w;
		}
		var radius = dimension / 2;

		zoomLine.pointA = pointToGISCoords(zoomLine.pointA);
		zoomLine.pointB = pointToGISCoords(zoomLine.pointB);

		var p = ZoomBox.parentNode;
		p.removeChild(ZoomBox);

		var poly = new Circle();
		poly.Center = pointToGISCoords(new Point((zoomStartPoint.x + zoomEndPoint.x) / 2, (zoomStartPoint.y + zoomEndPoint.y) / 2));
		poly.Radius = radius * mapScale;
		
		name = createSelectionArea(name, poly);


		ZoomBox = null;
	}
	zoomStartPoint = null;
	zoomEndPoiint = null;
}




var SelectionPoints = new Array();
var SelectionPs = new Array();

function drawSelectionPoint(parent,x,y) {
	var newPoint = document.createElement('div');
	newPoint.style.position = 'absolute';
	newPoint.style.overflow = 'hidden';
	newPoint.style.left = x + 'px';
	newPoint.style.top = y + 'px';
	newPoint.style.background = 'blue';
	newPoint.style.width = '4px';
	newPoint.style.height = '4px';
	newPoint.style.border = 'solid 2px red';
	newPoint.left = x;
	newPoint.top = y;
	parent.appendChild(newPoint);
	SelectionPoints[SelectionPoints.length] = newPoint;
	SelectionPs[SelectionPs.length] = pointToGISCoords(new Point(x,y));
}

function clearSelectionPoints() {
	if(SelectionPoints.length > 0) {
		for(var i = SelectionPoints.length - 1; i >= 0; i--) {
			if(SelectionPoints[i] && SelectionPoints[i].parentNode) {
				var p = SelectionPoints[i].parentNode;
				p.removeChild(SelectionPoints[i]);
				delete SelectionPoints[i];
			}
		}
	}
}

function selectionPolygonDown(evnt) {
	if(window.event) { evnt = window.event; }
	zoomStartPoint = new Point(evnt.clientX, evnt.clientY);
}

	
function selectionPolygonMove(evnt) {
	if(window.event) { evnt = window.event; }
	if(zoomStartPoint) {
		if(SelectionPoints.length > 0) {
			var xDiff = (zoomEndPoint.x - evnt.clientX);
			var yDiff = (zoomEndPoint.y - evnt.clientY);		
			var dist = 25;
			if(xDiff > dist || yDiff > dist || xDiff < -1*dist || yDiff < -1*dist) {
				drawSelectionPoint(document.getElementsByTagName('body')[0], evnt.clientX, evnt.clientY);
				zoomEndPoint = new Point(evnt.clientX, evnt.clientY); 
			}

		} else {
			drawSelectionPoint(document.getElementsByTagName('body')[0], evnt.clientX, evnt.clientY);
			zoomEndPoint = new Point(evnt.clientX, evnt.clientY); 
		}

	}
}

function selectionPolygonUp(evnt) {
	if(window.event) { evnt = window.event; }

	if(zoomStartPoint) {
		var newPoly = new Polygon();
		newPoly.buildFromPoints(SelectionPs);

		var name = createSelectionArea('Polygon Area', newPoly);
	}
	clearSelectionPoints();
	SelectionPs = new Array();
	SelectionPoints = new Array();

	zoomStartPoint = null;
}


function selectionLineUp(evnt) {
	if(window.event) { evnt = window.event; }
	if(zoomStartPoint && SelectionPs.length > 0) {
		var bufferSize = parseFloat(prompt('Please Enter the Buffer Radius in Ground Units.'));
		if(bufferSize) {
			var newPoly = new Polygon();
			newPoly.buildFromPoints(SelectionPs);
			var polyString = SelectionPs[0].toString('+');
			var linePolys = new Array();
			for(var i = 0; i < SelectionPs.length; i++) {
				var circ = new Circle();
				circ.Center = SelectionPs[i];
				circ.Radius = bufferSize;
				SelectionPs[i].clean();
				linePolys[linePolys.length] = SelectionPs[i].buffer(bufferSize);
			}

			for(var i = 0; i < SelectionPs.length -1; i++) {
				var line = new LineSegment(SelectionPs[i], SelectionPs[i+1]);
				linePolys[linePolys.length] = line.buffer(bufferSize);
			}

			var name = createSelectionArea('Buffered Line', linePolys);
		}
	}
	clearSelectionPoints();
	SelectionPs = new Array();
	SelectionPoints = new Array();

	zoomStartPoint = null;
}


function markPoint(mPoint, msg) {
	createSelectionArea(msg, mPoint);
}

function selectionPointUp(evnt) {
	if(window.event) { evnt = window.event; }
	var point = pointToGISCoords(new Point(evnt.clientX, evnt.clientY));
	var pName = prompt('What would you like to name this point?');
	if(pName) {
		//createSelectionArea(pName, point);
		markPoint(point, pName);
	}
}


function loadSelectionCircle() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, selectionCircleDown, selectionCircleUp, selectionCircleMove, null);
}

function loadSelectionCircleRadius() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, selectionCircleDown, selectionCircleRadiusUp, selectionCircleRadiusMove, null);
}


function loadSelectionCircleRadiusPrompt() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, selectionCircleDown, selectionCircleRadiusUpPrompt, null, null);
}



function loadSelectionRect() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, zoomInDown, selectionRectUp, zoomInMove, null);
}

function loadSelectionPolygon() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, selectionPolygonDown, selectionPolygonUp, selectionPolygonMove, null);
}

function loadSelectionLine() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, selectionPolygonDown, selectionLineUp, selectionPolygonMove, null);
}

function loadSelectionPoint() {
	var box = document.getElementById('MapTool');
	loadZoomToolFamily(box, null, selectionPointUp, null, null);
}
