/*
COPYRIGHT 1995-2005 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

20050921

email: contracts@esri.com
*/


var screenScaleFactor = 1;
var adjustVMLPositions = true;
var v_checkLoad;

if (isIE) {
	// set default brower page zoom to 1.0
	//document.writeln('<style type="text/css">\n');
	//document.writeln('		body {zoom:1.0;}\n');
	//document.writeln(' </style>\n');
	// check if browser is scaling the display.... IE on some newer laptops set to non-standard dpi does this (standard=96dpi)
	if (adjustVMLPositions) screenScaleFactor = screen.deviceXDPI / screen.logicalXDPI;
	// if screenScaleFactor != 1, then adjustments must be made to vector point locations passed to vml
	// on some laptops where the display is scaled, vml is not in sync
}

//////////////////////////////////// Vector functions /////////////////////////////////////

// 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 CoordsObject() {
	this.x = new Array();
	this.y = new Array();
}

// vector object
function VectorObject(divid) {	
	this.objString = "";
	this.divColor = "Black";
	this.divId = divid
	this.divObject = document.getElementById(divid);
	this.lineWidth = 2;
	
	this.clear = function() {
		this.objString = "";
	}

	this.draw = function() {
		if (this.objString.length==0 && isIE) 
            window.setTimeout(this.divObject.id + '.innerHTML = "";',0); 
		else 
		    this.divObject.innerHTML = this.objString;
	}

	this.color = function(color) {
		this.divColor = color;
	}

	this.circle = function(centerX, centerY, radius, addcrosshair) {
		var ovwidth = radius * 2;
		var ovleft = centerX - radius;
		var ovtop = centerY - radius;
		if (addcrosshair) {
		    this.crosshair(centerX, centerY);
		}
		this.oval(ovleft, ovtop, ovwidth, ovwidth);
	}
	
	this.crosshair = function(centerX, centerY) {
		if (isIE) {
			this.line(centerX - 4, centerY, centerX + 4, centerY);
			this.line(centerX, centerY - 4, centerX, centerY + 4);
		} else {
			this.add(centerX - 4, centerY, 9, 1);
			this.add(centerX, centerY - 4, 1, 9);
		}
	}


	this.box = function (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);
	}

	if (isIE) {
		// use VML if using IE
		this.add = null;
		
		this.line = function(inX1, inY1, inX2, inY2) {
			if (this.lineWidth < 1) this.lineWidth = 1;
			this.objString += '<v:line from="' + (inX1*screenScaleFactor) + ',' + (inY1*screenScaleFactor) + '" to="' + (inX2*screenScaleFactor) + ',' + (inY2*screenScaleFactor) + '"><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /></v:line>\n';
		}

		this.polyline = function(xArray, yArray) {
			var str = "";
			for (var i = 0;i<xArray.length;i++) {
				if (i>0) str += " ";
				str += (xArray[i]*screenScaleFactor) + " " + (yArray[i]*screenScaleFactor);
			}
			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';
		}

		this.polygon = function(xArray, yArray) {
			var str = "";
			for (var i = 0;i<xArray.length;i++) {
				if (i>0) str += " ";
				str += (xArray[i]*screenScaleFactor) + " " + (yArray[i]*screenScaleFactor);
			}
			str += " " + (xArray[0]*screenScaleFactor) + " " + (yArray[0]*screenScaleFactor);
			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';
		}

		this.oval = function(ovleft, ovtop, inWidth, inHeight) {
			this.objString += '<v:oval style="width:' + (inWidth*screenScaleFactor) + 'px; height:' + (inHeight*screenScaleFactor) + 'px; position:absolute; top:' + (ovtop*screenScaleFactor) + 'px; left:' + (ovleft*screenScaleFactor) + 'px;">';
			this.objString += '<v:fill on="false" opacity="0.0" /><v:stroke weight="' + this.lineWidth + 'px" color="' + this.divColor + '" /></v:oval>';
		}

		this.ovalPortion = null;
		// ie specific
	} else {
		// otherwise use the vector code	
		// netscape/mozilla specific
		this.add = function(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>';
		}

		this.line = function(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.add(xoffset, y, x - xoffset + size_adjust, flex_size);
						y += increment;
						rl3 += rl2;
						xoffset = x;
					} else {
						rl3 += rl1;
					}
					pwidth--;
				}
				this.add(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.add(x, y, flex_size, yoffset - y + size_adjust);
							x++;
							y += increment;
							rl3 += rl2;
							yoffset = y;
						} else {
							y += increment;
							rl3 += rl1;
						}
						pheight--;
					}
					this.add(inX2, inY2, flex_size, yoffset - inY2 + size_adjust);
				} else {
					if (size == 1) size_adjust = 0;
					while (pheight > 0) {
						y += increment;
						if (rl3 > 0) {
							this.add(x, yoffset, flex_size, y - yoffset + size_adjust);
							x++;
							rl3 += rl2;
							yoffset = y;
						} else {
							rl3 += rl1;
						}
						pheight--;
					}
					this.add(inX2, yoffset, flex_size, inY2 - yoffset + size_adjust + 1);
				}
			}
		}

		this.polyline = function(xArray, yArray) {
			for (var i = xArray.length-1;i > 0;i--) {
				this.line(xArray[i], yArray[i], xArray[i-1], yArray[i-1]);
			}
		}

		this.polygon = function(xArray, yArray) {
			var lastVector = xArray.length - 1;
			this.polyline(xArray, yArray);
			this.line(xArray[lastVector], yArray[lastVector], xArray[0], yArray[0]);
		}

		this.oval = function(ovleft, ovtop, inWidth, inHeight) {
			if (this.lineWidth < 1) this.lineswidth = 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.ovalPortion(centerX, centerY, -x + 1, xoff + ovw, -yoff, yoff - h + 1 + ovh, w, 1);
							this.ovalPortion(centerX, centerY, -x + 1, x - 1 + ovw, -y - 1, y + ovh, 1, 1);
						}
						else {
							this.ovalPortion(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.add(centerX - halfwidth, centerY - yoff, halfwidth - xoff + 1, (yoff * 2) + ovh);
				this.add(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.ovalPortion(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.add(centerX - halfwidth, centerY - yoff, size, (yoff * 2) + ovh);
					this.add(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.ovalPortion(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.add(centerX - x + 1, centerY - yoff, 1, (yoff * 2) + ovh);
							this.add(centerX + xoff + ovw, centerY - yoff, 1, (yoff * 2) + ovh);
							xoff = x;
							yoff = top_point[i];
						}
					}
					this.add(centerX - halfwidth, centerY - yoff, 1, (yoff * 2) + ovh);
					this.add(centerX + xoff + ovw, centerY - yoff, 1, (yoff * 2) + ovh);
				}
			
			}
		}

		this.ovalPortion = function(centerX, centerY, dleft, dright, dtop, dbottom, dwidth, dheight) {
			this.add(centerX + dleft, centerY + dtop,  dwidth , dheight);
			this.add(centerX + dright, centerY + dtop, dwidth , dheight);
			this.add(centerX + dright, centerY + dbottom,  dwidth , dheight);
			this.add(centerX + dleft, centerY + dbottom,  dwidth , dheight);
		}
		

	} // netscape/mozilla specific
	
	this.restart = function(clearDrawnVectors) {
	    if (clearDrawnVectors==null) clearDrawnVectors = true;
		this.vectorCount = 0;
		this.xyCoord.x = new Array();
		this.xyCoord.y = new Array();
		//map.divObject.onmousemove = null;
		map.divObject.onmousemove = map.tempMoveFunction;
		map.divObject.ondblclick = null;
		map.vectorObject.divObject.onmousemove = null;
		map.vectorObject.divObject.ondblclick = null;
		this.clear();
		if (clearDrawnVectors) this.draw();
	}

	this.cancel = function() {
		this.vectorCount = 0;
		this.xyCoord.x = new Array();
		this.xyCoord.y = new Array();
		map.divObject.onmousemove = map.tempMoveFunction;
		map.divObject.ondblclick = null;
		map.divObject.onclick = null;
		map.vectorObject.divObject.onmousemove = null;
		map.vectorObject.divObject.ondblclick = null;
		map.vectorObject.divObject.onclick = null;
		this.clear();
		this.draw();
	}

	this.deletePart = function() {
		if ((this.mode==1) || (this.mode==2)) {
			this.clear();
			if (this.vectorCount>0) this.vectorCount--;
			if (this.xyCoord.x.length>0) {
				var n = this.xyCoord.x.length - 1;
				this.xyCoord.x.length = n;
				this.xyCoord.y.length = n;
				if (this.mode==1) {
					this.polyline(this.xyCoord.x,this.xyCoord.y);
				} else {
					if (this.vectorCount>2) this.polygon(xycoord.x,xycoord.y)
					 else this.polyline(this.xyCoord.x,this.xyCoord.y);
				}
				this.draw();
			}
			
		}
	}

	
	this.pixelObject = null;
	this.xyCoord = "";
	this.vectorCount = 0;
	this.mode = "";
}  // VectorObject

function addVectorStartUp() {
        var s = '<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v"/>';
        document.body.insertAdjacentHTML("afterbegin",s);
        addToDynamicStyleSheet('v\\:*', 'behavior: url(#default#VML)');
}

function addVectorToolbar() {
    
}

if (isIE) window.setTimeout("addVectorStartUp()",1500);

//////////////////////////////////// Vector functions /////////////////////////////////////
