// aimsMap.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*       dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js
*/

var aimsMapPresent=true;

// global variables
    // show xml responses
var debugOn = 0;
var setDebug = true;

var useLimitExtent=false;
var getStartingExtent=true;
var getLimitExtent=true;
var enforceFullExtent=false;

// map extents. . . dynamically updated
var left = -180.0;
var right = 180.0;
var top = 90.0;
var bottom = -90.0;
var fullLeft = left;
var fullRight = right;
var fullTop = top;
var fullBottom = bottom;

// map size . . . dynamically updated
var iWidth = 630;
var iHeight = 512;
// location map size . . . dynamically updated
var i2Width = 150;
var i2Height = 120;

var imageLimitLeft = limitLeft;
var imageLimitTop = limitTop;
var imageLimitRight = limitRight;
var imageLimitBottom = limitBottom;

var appDir = "";

var xDistance = Math.abs(right-left);
var yDistance = Math.abs(top-bottom);
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = left;
var mapY = top;
var oldX = "";
var oldY = "";
var lastLeft = left;
var lastRight = right;
var lastTop = top;
var lastBottom = bottom;
var fullOVLeft = left;
var fullOVRight = right;
var fullOVTop = top;
var fullOVBottom = bottom;
var theCursor = "crosshair";
var canLoad=true;
if (imsURL!="") canLoad=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 geocodeX = 0;
var geocodeY = 0;
var showGeocode = false;
var geocodeLabel = "";

var sQuote = "'";
var dQuote = '"';

var toolMode = 1;
var legendVisible=false;
// set legend visible at service load... showTOC=false & hasTOC=true
if ((hasTOC) && (!showTOC)) legendVisible=true;

MapUnits = MapUnits.toUpperCase();
ScaleBarUnits = ScaleBarUnits.toUpperCase();

// save the extent 
function saveLastExtent() {
  lastLeft = left;
  lastRight = right;
  lastTop = top;
  lastBottom = bottom;
}

// zoom out to full extent
function fullExtent() {
  if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
  window.scrollTo(0,0);
  saveLastExtent();
  left = fullLeft;
  right = fullRight;
  top = fullTop;
  bottom = fullBottom;
  sendMapXML();
}

// zoom out extent of Melbourne  nf 06/02
function fullExtentM() {
  if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
  window.scrollTo(0,0);
  saveLastExtent();
  left = melbLeft;
  right = melbRight;
  top = melbTop;
  bottom = melbBottom;
  sendMapXML();
}

function startExtent() {
  if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
  window.scrollTo(0,0);
  saveLastExtent();
  left = startLeft;
  right = startRight;
  top = startTop;
  bottom = startBottom;
  sendMapXML();
}

// zoom back to last extent
function zoomBack() {
  if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
  var left1 = left;
  var right1 = right;
  var top1 = top;
  var bottom1 = bottom;
  left = lastLeft;
  right = lastRight;
  top = lastTop;
  bottom = lastBottom;
  lastLeft = left1;
  lastRight = right1;
  lastTop = top1;
  lastBottom = bottom1;
  sendMapXML();   
}

function zoomToPoint(xIn, yIn, drawIt,theLabel) {
  var mWMargin = 0;
  var mHMargin = 0;
  mWMargin = Math.abs(limitRight-limitLeft) * selectPointMargin;
  mHMargin = Math.abs(limitTop-limitBottom) * selectPointMargin;
  saveLastExtent();
  left = xIn - mWMargin;
  right = xIn + mWMargin;
  top = yIn + mHMargin;
  bottom = yIn - mHMargin;
  if (drawIt) {
    showGeocode=true;
    geocodeX=xIn;
    geocodeY=yIn;
    geocodeLabel=theLabel;
  }
  sendMapXML();
}

function zoomToEnvelope(minXin,minYin,maxXin,maxYin) {
  saveLastExtent();
  left=minXin;
  bottom=minYin;
  right=maxXin;
  top=maxYin;
  sendMapXML()  
}

// zoom to center of fullextent at set scale
function zoomScale(inScale) {
  // inScale is scale factor where 1.0 = 100% of fullWidth and fullHeight
  var halfWidth = fullWidth / 2;
  var halfHeight = fullHeight / 2
  var midX = right - (xDistance / 2);
  var midY = top - (yDistance / 2);
  var newWidth = halfWidth * inScale;
  var newHeight = halfHeight * inScale;
  saveLastExtent();
  left = midX - newWidth;
  right = midX + newWidth;
  top = midY + newHeight;
  bottom = midY - newHeight;
  sendMapXML();   
}

// get URLs and extents from URL
function getCommandLineParams(cmdString) {
  var cmdString2 = cmdString.toUpperCase();    
  if (imsURL!="") {
    imsQueryURL= imsURL + "&CustomService=Query";
    imsGeocodeURL = imsURL + "&CustomService=Geocode";
    canLoad=false;
  } 
}

// 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 existance 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 "RetriveData" image
function showRetrieveData() {
  if (hasLayer("LoadData")) {
    showLayer("LoadData");
  }
}

// hide the "RetriveData" image
function hideRetrieveData() {
  if (hasLayer("LoadData")) {
    hideLayer("LoadData");
  }
}

// put up the "RetriveMap" image
function showRetrieveMap() {
  showLayer("LoadMap");
}

// hide the "RetriveMap" image
function hideRetrieveMap() {
  if (hasLayer("LoadMap")) {
    hideLayer("LoadMap");
  }
}

/*  *****************************************************
*   Various Distance Conversion Functions
*   *****************************************************
*/

// get scale bar distance
function getScaleBarDistance() {
    // get distance from left and right values in map units
    // convert to ScaleBar units then clip to ScaleBar size
    var mUnits = MapUnits;
    var mDistance = right - left;
    var sbDistance = 0
    if (mUnits != ScaleBarUnits) {
        theDist = mDistance
        mDistance = convertUnits(theDist,mUnits,ScaleBarUnits);
    }
    sbDistance = mDistance/5;
    var num1 = 0;
    var num2 = sbDistance;
    if (sbDistance>10000000) {
        num1 = parseInt(sbDistance/5000000);
        num2 = num1 * 5000000;
    } else if (sbDistance>1000000) {
        num1 = parseInt(sbDistance/500000);
        num2 = num1 * 500000;
    } else if (sbDistance>100000) {
        num1 = parseInt(sbDistance/50000);
        num2 = num1 * 50000;
    } else if (sbDistance>10000) {
        num1 = parseInt(sbDistance/5000);
        num2 = num1 * 5000;
    } else if (sbDistance>1000) {
        num1 = parseInt(sbDistance/500);
        num2 = num1 * 500;
    } else if (sbDistance>100) {
        num1 = parseInt(sbDistance/50);
        num2 = num1 * 50;
    } else if (sbDistance>10) {
        num1 = parseInt(sbDistance/5);
        num2 = num1 * 5;
    } else if (sbDistance>1) {
        num1 = parseInt(sbDistance/0.25);
        num2 = num1 * 0.25;
    } else if (sbDistance>0.1) {
        num1 = parseInt(sbDistance/0.025);
        num2 = num1 * 0.025;
    }
    sbDistance = num2;
    if (sbDistance > 2) {
        ScaleBarPrecision = "0";
    } else if (sbDistance > 1) {
        ScaleBarPrecision = "1";
    } else if (sbDistance > 0.1) {
        ScaleBarPrecision = "2";
    } else if (sbDistance > 0.01) {
        ScaleBarPrecision = "3";
    } else {
        ScaleBarPrecision = "4";
    }
    return sbDistance;
}

// calculate distance to current scalebarunits
function calcDistance(mX,mY) {
  if (clickCount>0) {
    var mUnits = MapUnits;
    var mDistance = 0;
    var p = clickCount-1;
    var Lon1 = clickPointX[p] * Math.PI / 180;
    var Lon2 = mX * Math.PI / 180;
    var Lat1 = clickPointY[p] * Math.PI / 180;
    var Lat2 = mY * Math.PI / 180;
    var LonDist = Math.abs(Lon1-Lon2);
    var LatDist = Math.abs(Lat1-Lat2);
    var xD = Math.abs(mX - clickPointX[p]);
    var yD = Math.abs(mY - clickPointY[p]);
    mDistance = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
    var theDist = convertUnits(mDistance,mUnits,ScaleBarUnits);
    var u = Math.pow(10,numDecimals);
    currentMeasure = parseInt(theDist*u+0.5)/u;        
    updateMeasureBox();        
  }
}

// convert the amounts to new units
function convertUnits(theDist1,mUnits,sUnits) { 
  var theDist = parseFloat(theDist1);
  var mDistance = theDist;
  if (mUnits == "FEET") {
    if (sUnits=="MILES") {
      mDistance = theDist / 5280;
    } else if (sUnits == "METERS") {
      mDistance = theDist * 0.304800609601;
    } else if (sUnits == "KILOMETERS") {
      mDistance = theDist * 0.000304800609601;
    }
  } else {
    if (sUnits=="MILES") {
      mDistance = theDist * 0.0006213699;
    } else if (sUnits == "FEET") {
      mDistance = theDist * 3.280833;
    } else if (sUnits == "KILOMETERS") {
      mDistance = theDist / 1000;
    }
  }
  var u = Math.pow(10,numDecimals);
  if (!isNav) mDistance = parseInt(mDistance * u + 0.5) / u
  return mDistance;
}

// set new map extent 
function setExtent(newLeft, newTop, newRight, newBottom) {
  left = newLeft;
  top = newTop;
  right = newRight;
  bottom = newBottom;
}

//  set new full extent 
function setFullExtent(maxLeft, maxTop, maxRight, maxBottom) {
  fullLeft = maxLeft;
  fullTop = maxTop;
  fullRight = maxRight;
  fullBottom = maxBottom;
  fullWidth = Math.abs(fullRight-fullLeft);
  fullHeight = Math.abs(fullTop-fullBottom);
}

function beforeMapRefresh() {
  checkFullExtent()
}
 
function afterMapRefresh() {
}   

// zoom using button.
//      zoomType: 1=in; 2=out
function zoomButton(zoomType) {
  saveLastExtent();
  if (zoomType == 1) {
    // zoom in
    left = left + (xHalf/2);
    right = right - (xHalf/2);
    top = top - (yHalf/2);
    bottom = bottom + (yHalf/2);
  }
  else {
    // zoom out
    left = left - xHalf;
    right = right + xHalf;
    top = top + yHalf;
    bottom = bottom - yHalf;
  }
  checkFullExtent();
  sendMapXML();
}

// pan using arrow buttons
function panButton(panType) {
  saveLastExtent();
  xDistance = Math.abs(right-left);
  yDistance = Math.abs(top-bottom);
  panX = xDistance * panFactor;
  panY = yDistance * panFactor;
  switch(panType) {
  //if (panType == 1) {
    case 1:
      //west
      left = left - panX;
      right = left + xDistance;
      break
    case 2:
      // north
      top = top + panY;
      bottom = top - yDistance;
      break
    case 3:
      // east
      right = right + panX;
      left = right - xDistance;
      break
    case 4:
      // south
      bottom = bottom - panY;
      top = bottom + yDistance;
      break
    case 5:
      // southwest
      top = top - panY;
      left = left - panX;
      bottom = top - yDistance;
      right = left + xDistance;
      break
    case 6:
      // northwest
      top = top + panY;
      left = left - panX;
      bottom = top - yDistance;
      right = left + xDistance;
      break
    case 7:
      // northeast
      top = top + panY;
      left = left + panX;
      bottom = top - yDistance;
      right = left + xDistance;
      break
    case 8:
      // southeast
      top = top - panY;
      left = left + panX;
      bottom = top - yDistance;
      right = left + xDistance;
  }
  checkFullExtent();
  sendMapXML();    
}

function checkFullExtent() {
  if (xDistance>fullWidth) xDistance = fullWidth;
  if (yDistance>fullHeight) yDistance = fullHeight;
  if (enforceFullExtent) {
    if (left < limitLeft) {
      left = limitLeft;
      right = left + xDistance;
    }
    if (top > imageLimitTop) {
      top = imageLimitTop;    
      bottom = top - yDistance;
    }   
    if (right > imageLimitRight) {
      right = imageLimitRight;
      left = right - xDistance;
    }
    if (bottom < imageLimitBottom) {
      bottom = imageLimitBottom;  
      top = bottom + yDistance;
    }   
  }
}

