// aimsNavigation.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*   dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*   aimsLayers.js, aimsDHTML.js
*   aimsClick.js
*/
aimsNavigationPresent=true;

/*
***************************************************************************************

Map Navigation functions - Zoom , Pan, etc.

***************************************************************************************
*/
        
 var mouseString1 = "";
  var Zone = ""; 
  var Zonestring = "";     
// Jason - stuff from TOB for Pseudo to AMG

function pseudotoamg(East,North){
        
        var Zone = (54 + 2 / 3);
        var k0 = 1;
        var Lat_deg = "";
        var Long_deg = "";
         
     grid_spheroid(East,North,Zone,k0,Lat_deg,Long_deg);  
         
} 


function pseudotoamg2(Lat_deg,Long_deg,East,North){          
      if ((Long_deg > 138) && (Long_deg <= 144)) {
         Zone = 54;
        }
      if ((Long_deg > 144) && (Long_deg <= 150)) {
        Zone = 55;
}
      k0= 0.9996;
      
      Zonestring = "AMG Zone " + Zone + " - ";
      spheroid_grid(Lat_deg,Long_deg,Zone,k0,East,North);
      
  // OUTPUT
  // Conversion of AMG to pseudoAMG
  //' INPUT:
  //    East = East 'ie from above in this demo
  //    North = North 'ie from above in this demo
  //    Zone = 55
 // ' COMPS
//      grid_spheroid East, North, Zone, 0.9996, Lat_deg, Long_deg
  //    spheroid_grid Lat_deg, Long_deg, (54# + 2# / 3#), 1, East, North
 // ' OUTPUT (Should be what we started with)
 //     MsgBox (East)
 //     MsgBox (North)

}


function grid_spheroid(East,North,Zone,k0,Lat_deg,Long_deg) {
   rad2deg = 57.29577951;
   a = 6378160;
   f = 1/298.25;
   g = 111133.348785;
   f2 = 0.0025188877;
   f4 = 0.0000037011;
   f6 = 0.0000000074;
   e = Math.sqrt(f * (2.0 - f));
   False_east = 500000.0;
   False_north = 10000000.0;

   East_dash = East - False_east;
   North_dash = North - False_north;
   
   m = North_dash / k0;
   
   jh = m/g;
   sigma = jh/rad2deg;
   ft_term1 = sigma;
   ft_term2 = f2 * Math.sin(2 * sigma);
   ft_term3 = f4 * Math.sin(4 * sigma);
   ft_term4 = f6 * Math.sin(6 * sigma); 
   
   lat_foot = ft_term1 + ft_term2 + ft_term3 + ft_term4;

   denom = (1 - e * e * Math.sin(lat_foot) * Math.sin(lat_foot));
   upsilon = a / Math.sqrt(denom);
   rho = a * (1 - e * e) / (denom * Math.sqrt(denom));
   psi = upsilon / rho;
   t = Math.sin(lat_foot) / Math.cos(lat_foot);
   t_k0_rho = t / (k0 * rho);
   jh1 = k0 * upsilon;
   ecks = East_dash/jh1;

   lat_term1 = lat_foot;
   lat_term2 = -t_k0_rho * ecks * East_dash / 2;
   lat_term3 = t_k0_rho * (ecks * ecks * ecks * East_dash / 24) * (-4 * psi * psi + 9 * psi * (1 - t * t) + 12 * t * t);
   lat_term4 = -t_k0_rho * (ecks * ecks * ecks * ecks * ecks * East_dash / 720) * (8 * psi * psi * psi * psi * (11 - 24 * t * t) - 12 * psi * psi * psi * (21 - 71 * t * t) + 15 * psi * psi * (15 - 98 * t * t + 15 * t * t * t * t) + 180 * psi * (5 * t * t - 3 * t * t * t * t) + 360 * t * t * t * t);
   lat = lat_term1 + lat_term2 + lat_term3 + lat_term4;
   var Lat_deg = lat * rad2deg;
   

   jh_long = Math.cos(lat_foot);
   long_term1 = ecks / jh_long;
   long_term2 = -(1 / Math.cos(lat_foot)) * (ecks * ecks * ecks / 6) * (psi + 2 * t * t);
   long_term3 = (1 / Math.cos(lat_foot)) * (ecks * ecks * ecks * ecks * ecks / 120) * (-4 * psi * psi * psi * (1 - 6 * t * t) + psi * psi * (9 - 68 * t * t) + 72 * psi * t * t + 24 * t * t * t * t);
   long_term4 = -(1 / Math.cos(lat_foot)) * (ecks * ecks * ecks * ecks * ecks * ecks * ecks / 5040) * (61 + 662 * t * t + 1320 * t * t * t * t + 720 * t * t * t * t * t * t);
   long1 = long_term1 + long_term2 + long_term3 + long_term4;
  
   var Long_deg = long1 * rad2deg + (6 * Zone - 183);
   pseudotoamg2(Lat_deg,Long_deg,East,North);  
}

function spheroid_grid(Lat_deg,Long_deg,Zone,k0,East,North) {
   rad2deg = 57.29577951;
   a = 6378160;
   f = 1/298.25;
   g = 111133.348785;
   a2 = -16038.9546;
   a4 = 16.8331;
   a6 = -0.0218;
   a0 = g;
   e = Math.sqrt(f * (2 - f));
   False_east = 500000;
   False_north = 10000000;

   omega = (Long_deg - (6 * Zone - 183)) / rad2deg;
   lat = Lat_deg / rad2deg;

   denom = (1 - e * e * Math.sin(lat) * Math.sin(lat));
   upsilon = a / Math.sqrt(denom);
   rho = a * (1 - e * e) / (denom * Math.sqrt(denom));
   psi = upsilon / rho;
   t = Math.sin(lat) / Math.cos(lat);
   t_k0_rho = t / (k0 * rho);
   ecks = East_dash / (k0 * upsilon);

   E_term1 = upsilon * omega * Math.cos(lat);
   E_term2 = upsilon * (omega * omega * omega / 6) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * (psi - t * t);
   E_term3 = upsilon * (omega * omega * omega * omega * omega / 120) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * (4 * psi * psi * psi * (1 - 6 * t * t) + psi * psi * (1 + 8 * t * t) - psi * (2 * t * t) + t * t * t * t);
   East_dash = k0 * (E_term1 + E_term2 + E_term3);
   East = East_dash + False_east;

   m_term1 = a0 * rad2deg * lat;
   m_term2 = a2 * Math.sin(2 * lat);
   m_term3 = a4 * Math.sin(4 * lat);
   m_term4 = a6 * Math.sin(6 * lat);
   m = m_term1 + m_term2 + m_term3 + m_term4;

   N_term1 = m;
   N_term2 = upsilon * Math.sin(lat) * (omega * omega / 2) * Math.cos(lat);
   N_term3 = upsilon * Math.sin(lat) * (omega * omega * omega * omega / 24) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * (4 * psi * psi + psi - t * t);
   N_term4 = upsilon * Math.sin(lat) * (omega * omega * omega * omega * omega * omega / 720) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * Math.cos(lat) * (8 * psi * psi * psi * psi * (11 - 24 * t * t) - 28 * psi * psi * psi * (1 - 6 * t * t) + psi * psi * (1 - 32 * t * t) - psi * 2 * t * t + t * t * t * t);
   North_dash = k0 * (N_term1 + N_term2 + N_term3 + N_term4);
   North = North_dash + False_north;
   
   // Jason used to go to the nearest integer and dropping the decimal places.
   North = Math.ceil(North);
   East = Math.ceil( East);
  mouseString1 = East + ", " + North + " ";
}

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
    mouseX = xIn;
    pixelX = xDistance / iWidth;
    mapX = pixelX * mouseX + left;
    mouseY = iHeight - yIn;
    pixelY = yDistance / iHeight;
    mapY = pixelY * mouseY + bottom;
}

function getImageXY(e) {
  if (isNav) {
    mouseX=e.pageX;
    mouseY=e.pageY;
  } else {
    mouseX=event.clientX + document.body.scrollLeft;
    mouseY=event.clientY + document.body.scrollTop;
  }
  mouseX = mouseX-hspc;
  mouseY = mouseY-vspc;
} 

function getOVImageXY(e) {
  if (isNav) {
    mouseX=e.pageX;
    mouseY=e.pageY;
  } else {
    mouseX=event.clientX + document.body.scrollLeft;
    mouseY=event.clientY + document.body.scrollTop;
  }
  // subtract offsets from page left and top
  mouseX = mouseX-ovHspc;
  mouseY = mouseY-ovVspc;
}

// get coordinates on ov map and reset display
function ovMapClick(x,y) {
  var ovWidth = i2Width;
  var ovHeight = i2Height;
  var ovXincre = fullOVWidth / ovWidth;
  var ovYincre = fullOVHeight / ovHeight;
  var ovX = x;
  var ovY = ovHeight - y;
  var ovmapX = ovX * ovXincre + fullOVLeft;
  var ovmapY = ovY * ovYincre + fullOVBottom;
  saveLastExtent();
  left = ovmapX - xHalf;
  right = ovmapX + xHalf;
  top = ovmapY + yHalf;
  bottom = ovmapY - yHalf;
  sendMapXML();
}

// get click on OVmap and move display there
function ovMap2Click(e) {
  getOVImageXY(e);
  mouseY = mouseY - ovBorderWidth;
  zooming=false;
  panning=false;
  selectBox=false;
  ovMapClick(mouseX,mouseY);
}


// zoom in around mouse click
function zoomin(e) {
  getMapXY(mouseX,mouseY);
  var tempLeft=lastLeft;
  var tempRight=lastRight;
  var tempTop=lastTop;
  var tempBottom=lastBottom;
  saveLastExtent();

  left = mapX - (xHalf/zoomFactor);
  right = mapX + (xHalf/zoomFactor);
  top = mapY + (yHalf/zoomFactor);
  bottom = mapY - (yHalf/zoomFactor);
  
  enforceMinimumExtent();
  sendMapXML();
}

// zoom in around mouse click
function AMGzoomin(mouseX,mouseY) {
  getMapAMG(mouseX,mouseY);
  var tempLeft=lastLeft;
  var tempRight=lastRight;
  var tempTop=lastTop;
  var tempBottom=lastBottom;
  saveLastExtent();

  left = mapX - (xHalf/zoomFactor);
  right = mapX + (xHalf/zoomFactor);
  top = mapY + (yHalf/zoomFactor);
  bottom = mapY - (yHalf/zoomFactor);
  
  sendMapXML();
}


// zoom out from mouse click
function zoomout(e) {
  getMapXY(mouseX,mouseY);
  var tempLeft=lastLeft;
  var tempRight=lastRight;
  var tempTop=lastTop;
  var tempBottom=lastBottom;
  saveLastExtent();

  left = mapX - (xDistance*zoomFactor/2);
  right = mapX + (xDistance*zoomFactor/2);
  top = mapY + (yDistance*zoomFactor/2);
  bottom = mapY - (yDistance*zoomFactor/2);
  
  sendMapXML();
} 

// get the coords at mouse position
function getMouse(e) {
  window.status="";     
  getImageXY(e);
  if (isIE) {
    if ((hasOVMap) && (ovIsVisible) && (ovMapIsLayer)) {    
      if ((mouseX<i2Width+2) && (mouseY<i2Height)) {    
        document.all.theTop.style.cursor = "default";
      } else {
        document.all.theTop.style.cursor = theCursor;
      }
    }
  }
  
  if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0) || ((hasOVMap) && (ovIsVisible) && (mouseX<i2Width+ovBoxSize) && (mouseY<i2Height+ovBoxSize) && (ovMapIsLayer))) {
    chkMouseUp(e);
    
  } else {
  
    if ((zooming) || (selectBox)) {
      x2=mouseX;
      y2=mouseY;
      setClip();
    } else if (panning) {
      x2=mouseX;
      y2=mouseY;
      panMouse(); 
    }
    pixelX = xDistance / iWidth;
    mapX = pixelX * mouseX + left;
    var theY = iHeight - mouseY;
    pixelY = yDistance / iHeight;
    mapY = pixelY * theY + bottom;
    
    if (toolMode==20) {
      calcDistance(mapX,mapY);  
    } 
    else if (showXYs) {
      var u = Math.pow(10,numDecimals);
      var uX = parseInt(mapX * u + 0.5) / u
      var uY= parseInt(mapY * u + 0.5) / u
      var East = uX;
      var North = uY;
    
    // Jason added to display the AMG coords and zone
    pseudotoamg(East,North);  
    
    //  var mouseString = "Easting: " + uX + " , Pseudo Y: " + uY + " -- Image: " + mouseX + " , " + mouseY;  
    //  if (showScalePercent) mouseString = mouseString + " -- ScaleFactor: " + mapScaleFactor;
      
      //var mouseString = "Pseudo X: " + uX + " , Pseudo Y: " + uY; 
      //if (showScalePercent) mouseString = mouseString + " -- ScaleFactor: " + mapScaleFactor;
      //window.status = mouseString;
      
      //mouseString = "Easting: " + uX + " , Northing: " + uY;  
      uX = Math.ceil(uX);
      uY = Math.ceil(uY);
      mouseString = uX + ", " + uY;
      Zonestring = "AMG" + Zone;
      if (showScalePercent) mouseString = Zonestring + " " + mouseString1 + " "  + "Pseudo" + " " + mouseString;
        window.status = mouseString;
    }
    
  }
  return false;
}

// start zoom in.... box displayed
function startZoomBox(e) {
  moveLayer("theMap",hspc,vspc);
  getImageXY(e);  
  if ((mouseX<iWidth) && (mouseY<iHeight)) {
    if (zooming) {
      stopZoomBox(e);
    } else {
      x1=mouseX;
      y1=mouseY
      x2=x1+1;
      y2=y1+1;
      zleft=x1;
      ztop=y1;
      zbottom=y1;
      zright=x1
      
      boxIt(x1,y1,x2,y2);

      zooming=true;
      showLayer("zoomBoxTop");
      showLayer("zoomBoxLeft");
      showLayer("zoomBoxRight");
      showLayer("zoomBoxBottom");     
    }
  }
  return false;  
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
  zooming=false;
  if ((zright <zleft+2) && (zbottom < ztop+2)) {
    zoomin(e);
  } else {
    var tempLeft=lastLeft;
    var tempRight=lastRight;
    var tempTop=lastTop;
    var tempBottom=lastBottom;
    saveLastExtent();
    pixelX = xDistance / iWidth;
    var theY = iHeight - ztop;
    pixelY = yDistance / iHeight;
    top = pixelY * theY + bottom;
    right = pixelX * zright + left;
    left = pixelX * zleft + left;
    theY = iHeight - zbottom;
    pixelY = yDistance / iHeight;
    bottom = pixelY * theY + bottom;
    window.scrollTo(0,0);
  
    enforceMinimumExtent();
    sendMapXML();
    hideLayer("zoomBoxTop");
    hideLayer("zoomBoxLeft");
    hideLayer("zoomBoxRight");
    hideLayer("zoomBoxBottom");
  }
  return true;
}

// start zoom out... box displayed
function startZoomOutBox(e) {
  moveLayer("theMap",hspc,vspc);
  getImageXY(e);
  // keep it within the MapImage
  if ((mouseX<iWidth) && (mouseY<iHeight)) {
    
    if (zooming) {
      stopZoomOutBox(e);
    } else {
      x1=mouseX;
      y1=mouseY
      x2=x1+1;
      y2=y1+1;
      zleft=x1;
      ztop=y1;
      zbottom=y1;
      zright=x1
      boxIt(x1,y1,x2,y2);
      /*
      clipLayer("zoomBoxTop",x1,y1,x2,y2);
      clipLayer("zoomBoxLeft",x1,y1,x2,y2);
      clipLayer("zoomBoxRight",x1,y1,x2,y2);
      clipLayer("zoomBoxBottom",x1,y1,x2,y2);
      */
      zooming=true;
      showLayer("zoomBoxTop");
      showLayer("zoomBoxLeft");
      showLayer("zoomBoxRight");
      showLayer("zoomBoxBottom");
    }
  //} else {
  //  if (zooming) stopZoomOutBox(e);
  }
  return false;
  
}

// stop zoom out box. . . zoom out
function stopZoomOutBox(e) {
  zooming=false;
  if ((zright <zleft+2) && (zbottom < ztop+2)) {
    zoomout(e);
  } else {  
    var tempLeft=lastLeft;
    var tempRight=lastRight;
    var tempTop=lastTop;
    var tempBottom=lastBottom;
    saveLastExtent();
    var zWidth = Math.abs(zright-zleft);
    var zHeight = Math.abs(ztop-zbottom);
    var xRatio = iWidth / zWidth;
    var yRatio = iHeight / zHeight;
    var xAdd = xRatio * xDistance / 2;
    var yAdd = yRatio * yDistance / 2;
    left = left - xAdd;
    right = right + xAdd;
    top = top + yAdd;
    bottom = bottom - yAdd;
    window.scrollTo(0,0);
    if (enforceFullExtent) {
      if ((right-left)>fullWidth) {
        left = fullLeft;
        right = fullRight;
        top = fullTop;
        bottom = fullBottom;
        lastLeft=tempLeft;
        lastRight=tempRight;
        lastTop=tempTop;
        lastBottom=tempBottom;
      }
    }
    //var theString = writeXML();
    sendMapXML();
    hideLayer("zoomBoxTop");
    hideLayer("zoomBoxLeft");
    hideLayer("zoomBoxRight");
    hideLayer("zoomBoxBottom");
  }
  return true;
}

// clip zoom box layer to mouse coords
function setClip() {  
  var tempX=x1;
  var tempY=y1;
  if (x1>x2) {
    zright=x1;
    zleft=x2;
  } else {
    zleft=x1;
    zright=x2;
  }
  if (y1>y2) {
    zbottom=y1;
    ztop=y2;
  } else {
    ztop=y1;
    zbottom=y2;
  }
  
  if ((x1 != x2) && (y1 != y2)) {
    //clipLayer("zoomBox",zleft,ztop,zright,zbottom);
    boxIt(zleft,ztop,zright,zbottom);
    /*
    clipLayer("zoomBoxTop",zleft,ztop,zright,ztop+ovBoxSize);
    clipLayer("zoomBoxLeft",zleft,ztop,zleft+ovBoxSize,zbottom);
    clipLayer("zoomBoxRight",zright-ovBoxSize,ztop,zright,zbottom);
    clipLayer("zoomBoxBottom",zleft,zbottom-ovBoxSize,zright,zbottom);
    */
  }
  //return false;
}

// start pan.... image will move
function startPan(e) {
  moveLayer("theMap",hspc,vspc);

  getImageXY(e);
  // keep it within the MapImage
  if ((mouseX<iWidth) && (mouseY<iHeight)) {
    if (panning) {
      stopPan(e);
    } else {
      x1=mouseX;
      y1=mouseY
      x2=x1+1;
      y2=y1+1;
      panning=true;
    }
  }
  return false;

}

// stop moving image.... pan 
function stopPan(e) {
  window.scrollTo(0,0);
  panning=false;
  var tempLeft=lastLeft;
  var tempRight=lastRight;
  var tempTop=lastTop;
  var tempBottom=lastBottom;
  saveLastExtent();
  var ixOffset = x2-x1;
  var iyOffset = y1-y2;
  pixelX = xDistance / iWidth;
  var theY = iHeight - ztop;
  pixelY = yDistance / iHeight;
  var xOffset = pixelX * ixOffset;
  var yOffset = pixelY * iyOffset;
  top = top - yOffset;
  right = right - xOffset;
  left = left - xOffset;
  bottom = bottom - yOffset;
  
  checkFullExtent();
  
  hideLayer("theMap");
  if (hasLayer("theMapClicks")) {
    document.theClickImage.src = blankImage;

  }
  //var theString = writeXML();
  sendMapXML();
  //window.setTimeout('moveLayer("theMap",hspc,vspc);',1000);
  //window.setTimeout('showLayer("theMap");',1500);
  //window.setTimeout('clipLayer("theMap",0,0,iWidth,iHeight);',1500); 
  return true; 
}

// move map image with mouse
function panMouse() {
  var xMove = x2-x1;
  var yMove = y2-y1;
  var cLeft = -xMove;
  var cTop = -yMove;
  var cRight = iWidth;
  var cBottom = iHeight;
  if (xMove>0) {
    cLeft = 0;
    cRight = iWidth - xMove;
  }
  if (yMove>0) {
    cTop = 0;
    cBottom = iHeight - yMove;
  }
  clipLayer("theMap",cLeft,cTop,cRight,cBottom);
  moveLayer("theMap",xMove+hspc,yMove+vspc);
  if (hasLayer("theMapClicks")) {
    clipLayer("theMapClicks",cLeft,cTop,cRight,cBottom);
    moveLayer("theMapClicks",xMove+hspc,yMove+vspc);

  }
  //return false;
}

// pan to mouse click
function pan(e) {
  getMapXY(mouseX,mouseY);
  var tempLeft=lastLeft;
  var tempRight=lastRight;
  var tempTop=lastTop;
  var tempBottom=lastBottom;
  saveLastExtent();

  left = mapX - xHalf;
  right = mapX + xHalf;
  top = mapY + yHalf;
  bottom = mapY - yHalf;
  //var theString = writeXML();
  sendMapXML();
} 


function setZoomColor() {
  setLayerBackgroundColor("zoomBoxTop", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxLeft", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxRight", zoomBoxColor);
  setLayerBackgroundColor("zoomBoxBottom", zoomBoxColor);

  setLayerBackgroundColor("zoomOVBoxTop", zoomBoxColor);
  setLayerBackgroundColor("zoomOVBoxLeft", zoomBoxColor);
  setLayerBackgroundColor("zoomOVBoxRight", zoomBoxColor);
  setLayerBackgroundColor("zoomOVBoxBottom", zoomBoxColor);
}