// aimsMap.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on ArcIMSparam.js, aimsCommon.js
*  Modified for ParcelsCentral App by BB
*/
// global variables

var enforceFullExtent=true;

// map extents. . . dynamically updated
var eLeft = parent.iStartLeft;
var eRight = parent.iStartRight;
var eTop = parent.iStartTop;
var eBottom = parent.iStartBottom;
/*var fullLeft = eLeft;
var fullRight = eRight;
var fullTop = eTop;
var fullBottom = eBottom;*/
var fullLeft = parent.LimitLeft;
var fullRight = parent.LimitRight;
var fullTop = parent.LimitTop;
var fullBottom = parent.LimitBottom;

// map size . . . dynamically updated
var iWidth = 630;
var iHeight = 512;
// location map size . . . dynamically updated
var i2Width = 150;
var i2Height = 120;

//maximum map extent
var limitLeft = parent.LimitLeft;
var limitRight = parent.LimitRight;
var limitTop = parent.LimitTop;
var limitBottom = parent.LimitBottom;

var imageLimitLeft = limitLeft;
var imageLimitTop = limitTop;
var imageLimitRight = limitRight;
var imageLimitBottom = limitBottom;

var appDir = "";

var xDistance = Math.abs(eRight-eLeft);
var yDistance = Math.abs(eTop-eBottom);
var xHalf = xDistance/2;
var yHalf = yDistance/2;
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = eLeft;
var mapY = eTop;
var lastLeft = eLeft;
var lastRight = eRight;
var lastTop = eTop;
var lastBottom = eBottom;
var fullOVLeft = fullLeft;	// BB: use extent limits, not start
var fullOVRight = fullRight;
var fullOVTop = fullTop;
var fullOVBottom = fullBottom;
var theCursor = "crosshair";

var bDoingPan = false;

var fullWidth = Math.abs(fullRight - fullLeft);
var fullHeight = Math.abs(fullTop - fullBottom);
var fullOVWidth = Math.abs(fullOVRight - fullOVLeft);
var fullOVHeight = Math.abs(fullOVTop - fullOVBottom);
var mapScaleFactor = xDistance / iWidth;

var toolMode = 1;

// save the extent 
function saveLastExtent() {
	lastLeft = eLeft;
	lastRight = eRight;
	lastTop = eTop;
	lastBottom = eBottom;
}

// zoom out to full extent
function fullExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	saveLastExtent();
	eLeft = fullLeft;
	eRight = fullRight;
	eTop = fullTop;
	eBottom = fullBottom;
	//var theString = writeXML();
	sendMapASP();
}

// zoom back to last extent
function zoomBack() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	var left1 = eLeft;
	var right1 = eRight;
	var top1 = eTop;
	var bottom1 = eBottom;
	eLeft = lastLeft;
	eRight = lastRight;
	eTop = lastTop;
	eBottom = lastBottom;
	lastLeft = left1;
	lastRight = right1;
	lastTop = top1;
	lastBottom = bottom1;
	sendMapASP();	
}

// get directory path of URL
function getPath(theFullPath) {
	var theSlash = theFullPath.lastIndexOf("/");
	var theDir = theFullPath.substring(0,theSlash);
	if (theDir==null) theDir="";
	theDir = theDir + "/";
	return theDir;

}

// check for existence of layer
function hasLayer(name) {
	var result = false;
	if (isNav4) {
		if (document.layers[name]!=null) result=true;
	}  else if (isIE) {
		if (eval('document.all.' + name)!=null) result=true;
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) result=true;
		}
    }
	return result;
}

// put up the "RetriveMap" image
function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}

// hide the "RetriveMap" image
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

/*  *****************************************************
*	Various Distance Conversion Functions
*	*****************************************************
*/

// pan using arrow buttons
function panButton(panType) {
	saveLastExtent();
	xDistance = Math.abs(eRight-eLeft);
	yDistance = Math.abs(eTop-eBottom);
	panX = xDistance * panFactor;
	panY = yDistance * panFactor;
	switch(panType) {
	//if (panType == 1) {
	case 1:
		//west
		eLeft = eLeft - panX;
		eRight = eLeft + xDistance;
		break
	case 2:
		// north
		eTop = eTop + panY;
		eBottom = eTop - yDistance;
		break
	case 3:
		// east
		eRight = eRight + panX;
		eLeft = eRight - xDistance;
		break
	case 4:
		// south
		eBottom = eBottom - panY;
		eTop = eBottom + yDistance;
		break
	case 5:
		// southwest
		eTop = eTop - panY;
		eLeft = eLeft - panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 6:
		// northwest
		eTop = eTop + panY;
		eLeft = eLeft - panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 7:
		// northeast
		eTop = eTop + panY;
		eLeft = eLeft + panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
		break
	case 8:
		// southeast
		eTop = eTop - panY;
		eLeft = eLeft + panX;
		eBottom = eTop - yDistance;
		eRight = eLeft + xDistance;
	}
	checkFullExtent();
	//alert("Left:" + left + "\nTop:" + top + "\nRight:" + right + "\nBottom:" + bottom + "\nWidth:" + xDistance + "\nHeight:" + yDistance + "\nPanX:" + panX + "\nPanY:" + panY);
	//var theString = writeXML();
	sendMapASP();
	
}

function checkFullExtent() {
	if (xDistance>fullWidth) xDistance = fullWidth;
	if (yDistance>fullHeight) yDistance = fullHeight;
	if (enforceFullExtent) {
		if (eLeft < limitLeft) {
			eLeft = limitLeft;
			eRight = eLeft + xDistance;
		}
		if (eTop > imageLimitTop) {
			eTop = imageLimitTop;	
			eBottom = eTop - yDistance;
		}	
		if (eRight > imageLimitRight) {
			eRight = imageLimitRight;
			eLeft = eRight - xDistance;
		}
		if (eBottom < imageLimitBottom) {
			eBottom = imageLimitBottom;	
			eTop = eBottom + yDistance;
		}	
	}

}


