// $Revision: 2534 $
/*
COPYRIGHT 1995-2003 ESRI

TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the 
Copyright Laws of the United States.

For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373

email: contracts@esri.com
*/

// Adapted from common.js

// defaults
var lineWidth = 3;
var divColor = "#FF0000";

// delete the last vector in a polyline or polygon
function DeleteVector() {
	if ((vectorMode[areaDivName]==1) || (vectorMode[areaDivName]==2)) {
		vo[areaDivName].clearObjects();
		if (vectorCount[areaDivName]>0) vectorCount[areaDivName]--;
		if (xycoord[areaDivName].x.length>0) {
			var n = xycoord[areaDivName].x.length - 1;
			xycoord[areaDivName].x.length = n;
			xycoord[areaDivName].y.length = n;
			if (vectorMode[areaDivName]==1) {
				vo[areaDivName].polyline(xycoord[areaDivName].x,xycoord[areaDivName].y);
			} else {
				if (vectorCount[areaDivName]>2) vo[areaDivName].polygon(xycoord[areaDivName].x,xycoord[areaDivName].y)
				 else vo[areaDivName].polyline(xycoord[areaDivName].x,xycoord[areaDivName].y);
			}
			vo[areaDivName].drawObjects();
		}
		
	}
}

// restart the polyline or polygon
function RestartVectors() {
	vectorCount[areaDivName] = 0;
	xycoord[areaDivName].x = new Array();
	xycoord[areaDivName].y = new Array();
	areaDivObj.onmousemove = null;
	areaDivObj.ondblclick = null;
	lineDivObj.onmousemove = null;
	lineDivObj.ondblclick = null;
	vo[areaDivName].clearObjects();
	vo[areaDivName].drawObjects();
}

// stops and clears out all vectors in an object
function CancelVectors() {
	vectorCount[areaDivName] = 0;
	xycoord[areaDivName].x = new Array();
	xycoord[areaDivName].y = new Array();
	areaDivObj.onmousemove = null;
	areaDivObj.ondblclick = null;
	areaDivObj.onclick = null;
	lineDivObj.onmousemove = null;
	lineDivObj.ondblclick = null;
	lineDivObj.onclick = null;
	vo[areaDivName].clearObjects();
	vo[areaDivName].drawObjects();
}

// empty the object string
function clearObjects() {
	this.objString = "";
}

// put the objects on the page within the line div
function drawObjects() {
	this.divObj.innerHTML = this.objString;
}

// set the objects' color
function setObjectColor(color) {
	this.divColor = color;
}

// add to the object string... adds a new div definition
function addToObject(divleft, divtop, divwidth, divheight) {
	this.objString += '<div style="position: absolute;left: ' + divleft + 'px;top: ' + divtop + 'px;width: ' + divwidth + 'px;height: ' + divheight + 'px;overflow:hidden;background-color: ' + this.divColor + ';"><\/div>';
}

// add to the object string... adds a new div definition
function addToObjectColor(divleft, divtop, divwidth, divheight, divcolor) {
	if ((divcolor==null) || (divcolor=="")) divcolor = this.divColor;
	this.objString += '<div style="position: absolute;left: ' + divleft + 'px;top: ' + divtop + 'px;width: ' + divwidth + 'px;height: ' + divheight + 'px;overflow:hidden;background-color: ' + divcolor + ';"><\/div>';
}

// create a new line segment
function line(inX1, inY1, inX2, inY2) {
	if (this.lineWidth < 1) this.lineWidth = 1;
	if (inX1 > inX2) {
		var tempX = inX2;
		var tempY = inY2;
		inX2 = inX1;
		inY2 = inY1;
		inX1 = tempX;
		inY1 = tempY;
	}
	var pwidth = inX2 - inX1;
	var pheight = Math.abs(inY2 - inY1);
	var x = inX1;
	var y = inY1;
	var increment = 1;
	if (inY1 > inY2) increment =  -1;
	var xoffset = x;
	var yoffset = y;
	var size = this.lineWidth;
	var flex_size = size;
	var size_adjust = 0;
	var rl1 = pheight * 2;
	var rl2 = rl1 - (pwidth * 2);
	var rl3 = rl1 - pwidth;
	
	if (pwidth >= pheight) {
		if (size > 1) {
			if (size > 3) {
				flex_size = (size * pwidth * Math.sqrt(1 + pheight * pheight / (pwidth * pwidth)) - pwidth - (size >> 1) * pheight) / pwidth;
				flex_size = (!(size > 4) ? Math.ceil(flex_size) : Math.round(flex_size)) + 1;
			}
			size_adjust = Math.ceil(size / 2);
		}
		while (pwidth > 0) {
			++x;
			if (rl3 > 0) {
				this.addToObject(xoffset, y, x - xoffset + size_adjust, flex_size);
				y += increment;
				rl3 += rl2;
				xoffset = x;
			} else {
				rl3 += rl1;
			}
			pwidth--;
		}
		this.addToObject(xoffset, y, inX2 - xoffset + size_adjust + 1, flex_size);
		
	} else	{
		size_adjust = 1;
		if (size > 1) {
			if (size > 3) {
				flex_size = (size * pheight * Math.sqrt(1 + pwidth * pwidth / (pheight * pheight)) - (size >> 1) * pwidth - pheight) / pheight;
				flex_size = (!(size > 4) ? Math.ceil(flex_size) : Math.round(flex_size)) + 1;
			}
			size_adjust = Math.round(size / 2);
		}
		rl1 = pwidth * 2;
		rl2 = rl1 - (pheight * 2);
		rl3 = rl1 - pheight;
		if (inY2 <= inY1) {

			while (pheight > 0) {
				if (rl3 > 0) {
					this.addToObject(x, y, flex_size, yoffset - y + size_adjust);
					x++;
					y += increment;
					rl3 += rl2;
					yoffset = y;
				} else {
					y += increment;
					rl3 += rl1;
				}
				pheight--;
			}
			this.addToObject(inX2, inY2, flex_size, yoffset - inY2 + size_adjust);
		} else {
			if (size == 1) size_adjust = 0;
			while (pheight > 0) {
				y += increment;
				if (rl3 > 0) {
					this.addToObject(x, yoffset, flex_size, y - yoffset + size_adjust);
					x++;
					rl3 += rl2;
					yoffset = y;
				} else {
					rl3 += rl1;
				}
				pheight--;
			}
			this.addToObject(inX2, yoffset, flex_size, inY2 - yoffset + size_adjust + 1);
		}
	}
}

// create a new VML line segment
function ie_line(inX1, inY1, inX2, inY2) {
	if (this.lineWidth < 1) this.lineWidth = 1;
	this.objString += '<v:line from="' + inX1 + ',' + inY1 + '" to="' + inX2 + ',' + inY2 + '"><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /></v:line>\n';
}

// create a multi-segment line
function polyline(xArray, yArray) {
	for (var i = xArray.length-1;i > 0;i--) {
		this.line(xArray[i], yArray[i], xArray[i-1], yArray[i-1]);
	}
}

function ie_polyline(xArray, yArray) {
	var str = "";
	for (var i = 0;i<xArray.length;i++) {
		if (i>0) str += " ";
		str += xArray[i] + " " + yArray[i];
	}
	this.objString += '<v:polyline points="' + str + '"><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /><v:fill on="false" opacity="0.0" /></v:polyline>\n';
}

// create a closed polygon
function polygon(xArray, yArray) {
	var lastVector = xArray.length - 1;
	this.polyline(xArray, yArray);
	this.line(xArray[lastVector], yArray[lastVector], xArray[0], yArray[0]);
}

function ie_polygon(xArray, yArray) {
	var str = "";
	for (var i = 0;i<xArray.length;i++) {
		if (i>0) str += " ";
		str += xArray[i] + " " + yArray[i];
	}
	str += " " + xArray[0] + " " + yArray[0];
	this.objString += '<v:polyline points="' + str + '"><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /><v:fill on="false" opacity="0.0" /></v:polyline>\n';
}

// create an oval
function oval(ovleft, ovtop, inwidth, inheight) {
	if (this.lineWidth < 1) this.lineWidth = 1;
	var size = this.lineWidth;
	var ovwidth = inwidth + size - 1;
	var ovheight = inheight +  size - 1;
	var halfwidth = Math.floor(ovwidth / 2);
	var halfheight = Math.floor(ovheight / 2);
	var ovw = ovwidth & 1; 
	var ovh = (ovheight & 1 ) + 1;
	var centerx = ovleft + halfwidth;
	var centery = ovtop + halfheight;
	var x = 0; 
	var y = halfheight;
	var w;
	var h;
	var xoff = 0;
	var yoff = halfheight;
	var hwsq = (halfwidth * halfwidth) * 2;
	var hhsq = (halfheight * halfheight) * 2;
	var chpoint = Math.floor(hwsq / 2) * (1 - (halfheight * 2)) + hhsq;
	var chpoint2 = Math.floor(hhsq / 2) - hwsq * ((halfheight * 2) - 1);
	if ((size == 1) || (inwidth <= 1) || (inheight - size <= 1)) {
		do {
			if (chpoint < 0)	{
				chpoint += hhsq * ((x * 2) + 3);
				x++;
				chpoint2 += (hhsq * 2) * x;
			} else if (chpoint2 < 0) {
				chpoint += hhsq * ((x * 2) + 3) - (hwsq * 2) * (y - 1);
				x++;
				chpoint2 += (hhsq * 2) * x - hwsq * ((y * 2) - 3);
				y--;
				w = x - xoff;
				h = yoff - y;
				if (w & 2 && h & 2) {
					this.addToOval(centerx, centery, -x + 1, xoff + ovw, -yoff, yoff - h + 1 + ovh, w, 1);
					this.addToOval(centerx, centery, -x + 1, x - 1 + ovw, -y - 1, y + ovh, 1, 1);
				}
				else {
					this.addToOval(centerx, centery, -x + 1, xoff + ovw, -yoff, yoff - h + ovh, w, h);
				}
				xoff = x;
				yoff = y;
			} else {
				chpoint2 -= hwsq * ((y * 2) - 3);
				y--;
				chpoint -= (hwsq * 2) * y;
			}
		} while (y > 0);
		this.addToObject(centerx - halfwidth, centery - yoff, halfwidth - xoff + 1, (yoff * 2) + ovh);
		this.addToObject(centerx + xoff + ovw, centery - yoff, halfwidth - xoff + 1, (yoff * 2) + ovh);
	} else {
		var left_point, top_point, bottom_point, right_point;
		if (size < 3 && ((size > 2) || ovwidth > 33 && ovheight > 33)) {
			do {
				if (chpoint < 0) {
					chpoint += hhsq * ((x * 2) + 3);
					x++;
					chpoint2 += (hhsq * 2) * x;
					
				} else if (chpoint2 < 0) {
					chpoint += hhsq * ((x * 2) + 3) - (hwsq * 2) * (y - 1);
					x++;
					chpoint2 += (hhsq * 2) * x - hwsq * ((y * 2) - 3);
					y--;
					
					w = x - xoff;
					h = yoff - y;
					if (w - 1) {
						right_point = w + 1 + (size & 1);
						h = size;
					} else if (h - 1) {
						right_point = size;
						h += 1 + (size & 1);
					} else {
						right_point = size;
						h = size;
					}
					this.addToOval(centerx, centery, -x + 1, xoff - right_point + w + ovw, -yoff, -h + yoff + ovh, right_point, h);
					xoff = x;
					yoff = y;
				} else {
					chpoint2 -= hwsq *((y * 2) - 3);
					y--;
					chpoint -= (hwsq * 2) * y;
				}
			} while (y > 0);
			this.addToObject(centerx - halfwidth, centery - yoff, size, (yoff * 2) + ovh);
			this.addToObject(centerx + halfwidth + ovw - size + 1, centery - yoff, size, (yoff * 2) + ovh);
		} else {
			var hw = Math.floor((ovwidth - ((size - 1) * 2)) /2);
			var hh = Math.floor((ovheight - ((size - 1) * 2)) /2);
			var xx = 0;
			var yy = hh;
			var hwsq2 = (hw * hw) * 2;
			var hhsq2 = (hh * hh) * 2;
			var chpoint3 = Math.floor(hwsq2 / 2) * (1 - (hh * 2)) + hhsq2;
			var chpoint4 = Math.floor(hhsq2 / 2) - hwsq2 * ((hh * 2) - 1);
			left_point = new Array();
			top_point = new Array();
			bottom_point = new Array();
			left_point[0] = 0;
			top_point[0] = halfheight;
			bottom_point[0] = hh - 1;
			
			do {
				if (chpoint < 0) {
					chpoint += hhsq * ((x * 2) + 3);
					x++;
					chpoint2 += (hhsq * 2) * x;
					left_point[left_point.length] = x;
					top_point[top_point.length] = y;
				} else if (chpoint2 < 0) {
					chpoint += hhsq * ((x * 2) + 3) - (hwsq * 2) * (y - 1);
					x++;
					chpoint2 += (hhsq * 2) * x - hwsq * ((y * 2) - 3);
					y--;
					left_point[left_point.length] = x;
					top_point[top_point.length] = y;
				} else {
					chpoint2 -= hwsq * ((y * 2) - 3);
					y--;
					chpoint -= (hwsq * 2) * y;
				}
				if (yy > 0) {
					if (chpoint3 < 0) {
						chpoint3 += hhsq2 * ((xx * 2) + 3);
						xx++;
						chpoint4 += (hhsq2 * 2) * xx;
						bottom_point[bottom_point.length] = yy - 1;
					} else if (chpoint4 < 0) {
						chpoint3 += hhsq2 * ((xx * 2) + 3) - (hwsq2 * 2) * (yy - 1);
						xx++;
						chpoint4 += (hhsq2 * 2) * xx - hwsq2 * ((yy * 2) - 3);
						yy--;
						bottom_point[bottom_point.length] = yy - 1;
					} else {
						chpoint4 -= hwsq2 * ((yy * 2) - 3);
						yy--;
						chpoint3 -= (hwsq2 * 2) * yy;
						bottom_point[bottom_point.length-1]--;
					}
				}
			} while (y > 0);
	
			var yoff2 = bottom_point[0];
			var l = left_point.length;
			for (var i = 0; i < l; i++) {
				if (bottom_point[i] != null) {
					if (bottom_point[i] < yoff2 || top_point[i] < yoff) {
						x = left_point[i];
						this.addToOval(centerx, centery, -x + 1, xoff + ovw, -yoff, yoff2 + ovh, x - xoff, yoff - yoff2);
						xoff = x;
						yoff = top_point[i];
						yoff2 = bottom_point[i];
					}
				} else {
					x = left_point[i];
					this.addToObject(centerx - x + 1, centery - yoff, 1, (yoff * 2) + ovh);
					this.addToObject(centerx + xoff + ovw, centery - yoff, 1, (yoff * 2) + ovh);
					xoff = x;
					yoff = top_point[i];
				}
			}
			this.addToObject(centerx - halfwidth, centery - yoff, 1, (yoff * 2) + ovh);
			this.addToObject(centerx + xoff + ovw, centery - yoff, 1, (yoff * 2) + ovh);
		}
	
	}
}

// create an oval
function ie_oval(ovleft, ovtop, inwidth, inheight) {
	this.objString += '<v:oval style="width:' + inwidth + 'px; height:' + inheight + 'px; position:absolute; top:' + ovtop + 'px; left:' + ovleft + 'px;">';
	this.objString += '<v:fill on="false" opacity="0.0" /><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /></v:oval>';
}

// adds 4 vectors to the oval object diagonally from each other
function addToOval(centerx, centery, dleft, dright, dtop, dbottom, dwidth, dheight) {
	this.addToObject(centerx + dleft, centery + dtop,  dwidth , dheight);
	this.addToObject(centerx + dright, centery + dtop, dwidth , dheight);
	this.addToObject(centerx + dright, centery + dbottom,  dwidth , dheight);
	this.addToObject(centerx + dleft, centery + dbottom,  dwidth , dheight);
}

// create a circle using centroid and radius
	// places an optional crosshair at centroid
function circle(centerX, centerY, radius, addcrosshair) {
	var ovwidth = radius * 2;
	var ovleft = centerX - radius;
	var ovtop = centerY - radius;
	if (addcrosshair) {
		if (isIE) {
			this.line(centerX - 4, centerY, centerX + 4, centerY);
			this.line(centerX, centerY - 4, centerX, centerY + 4);
		} else {
			this.addToObject(centerX - 4, centerY, 9, 1);
			this.addToObject(centerX, centerY - 4, 1, 9);
		}
	}
	this.oval(ovleft, ovtop, ovwidth, ovwidth);
}

function box(leftX, topY, rightX, bottomY) {
	var xArray = new Array();
	var yArray = new Array();
	xArray[0] = leftX;
	yArray[0] = topY;
	xArray[1] = rightX;
	yArray[1] = topY;
	xArray[2] = rightX;
	yArray[2] = bottomY;
	xArray[3] = leftX;
	yArray[3] = bottomY;
	this.polygon(xArray, yArray);
}

// vector object
function vectorObjects(divid) {	
	this.objString = "";
	this.divColor = divColor;
	this.divId = divid
	this.divObj = document.getElementById(divid);
	this.lineWidth = lineWidth;
	this.clearObjects = clearObjects;
	this.drawObjects = drawObjects;
	this.setObjectColor = setObjectColor;
	this.circle = circle;
	this.box = box;
	if (isIE) {
		// use VML if using IE
		this.addToObject = null;
		this.line = ie_line;
		this.polyline = ie_polyline;
		this.polygon = ie_polygon;
		this.oval = ie_oval;
		this.addToOval = null;
	} else {
		// otherwise use the vector code	
		this.addToObject = addToObject;
		this.line = line;
		this.polyline = polyline;
		this.polygon = polygon;
		this.oval = oval;
		this.addToOval = addToOval;
	}
}

// creates an object for holding screen coordinates for mouse movement and clicks
function pixelObject(controlname, inleft, intop, inwidth, inheight) {
	// initial click
	this.x1 = 0;
	this.y1 = 0;
	// subsequent clicks
	this.x2 = 0;
	this.y2 = 0;
	// remember the last click
	this.lastX = -99999;
	this.lastY = -99999;
	// position and dimensions of associated area div
	this.divLeft = inleft;
	this.divTop = intop;
	this.divWidth = inwidth;
	this.divHeight = inheight;
	// name of control
	this.controlname = controlname;
}

// creates an object for holding a history list of coordinates of click locations
function coordList() {
	this.x = new Array();
	this.y = new Array();
}
