//=================
// Activate a tool.
//=================
function activateTool(inTool) {
   if (!top.PostBackDone) return;

   if (inTool.id == ActiveTool.id) {
      if (top.ToggleTools) {
         if (inTool.id != "zoomIn") {
            activateTool(document.getElementById("zoomIn"));
         }
      }
      return;
   }
   pressTool(false); // Unpress current tool.
   clearMeasureVar();
   
   // Set the cursor style
   if (inTool.id == "zoomIn") {
      top.ActiveCursor = "default";
      ActiveDragCursor = ZoomInCursor_img;
      CursorOffsetX = 15;
      CursorOffsetY = 0;

   } else if (inTool.id == "zoomOut") {
      top.ActiveCursor = "default";
      ActiveDragCursor = ZoomOutCursor_img;
      CursorOffsetX = 15;
      CursorOffsetY = 0;

   } else if (inTool.id == "pan") {
      top.ActiveCursor = "move";
      ActiveDragCursor = null;

   } else if (inTool.id == "selectFeature") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "selectByPoly") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "identify") {
      top.ActiveCursor = "default";
      ActiveDragCursor = IdentifyCursor_img;
      CursorOffsetX = 13;
      CursorOffsetY = -2;

   } else if (inTool.id == "measure") {
      top.ActiveCursor = "default";
      ActiveDragCursor = MeasureCursor_img;
      CursorOffsetX = 15;
      CursorOffsetY = 0;
   
   } else if (inTool.id == "addPoly") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "addCircle") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "addLine") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "addPoint") {
      top.ActiveCursor = "default";
      ActiveDragCursor = null;

   } else if (inTool.id == "addText") {
      top.ActiveCursor = "crosshair";
      ActiveDragCursor = null;
      CursorOffsetX = 10;
      CursorOffsetY = -24;
      addText();

   } else if (inTool.id == "addXY") {
      top.ActiveCursor = "crosshair";
      ActiveDragCursor = null;
      CursorOffsetX = 10;
      CursorOffsetY = -24;
      addXY();
   } else if (inTool.id == "addXYCoord") {
   	addXY();
   } else {
      alert("Invalid tool: " + inTool.id)
   }
   // Update variables.
   Map_img.style.cursor = top.ActiveCursor;

   if (top.IeFilters) {
      Poly_vml.style.cursor = top.ActiveCursor;

      APoly_vml.style.cursor = top.ActiveCursor;
      ALine_vml.style.cursor = top.ActiveCursor;
      APoint_vml.style.cursor = top.ActiveCursor;

      ACircle1_vml.style.cursor = top.ActiveCursor;
      ACircle2_vml.style.cursor = top.ActiveCursor;
      ACircle3_vml.style.cursor = top.ActiveCursor;
      ACircle4_vml.style.cursor = top.ActiveCursor;
      ACircle5_vml.style.cursor = top.ActiveCursor;
   }
   ActiveTool = inTool;

   // Depress the new tool button.
   pressTool(true);
}
//=========================
// Press or unpress a tool.
//=========================
function pressTool(depress) {
   if ( (ActiveTool.id == "zoomIn") ||
        (ActiveTool.id == "zoomOut") ||
        (ActiveTool.id == "pan") ||
        (ActiveTool.id == "selectFeature") ||
        (ActiveTool.id == "selectByPoly") ||
        (ActiveTool.id == "identify") ||
        (ActiveTool.id == "addPoly") ||
        (ActiveTool.id == "addCircle") ||
        (ActiveTool.id == "addLine") ||
        (ActiveTool.id == "addPoint") ||
        (ActiveTool.id == "addText") ||
        (ActiveTool.id == "addXY") ||
        (ActiveTool.id == "addXYCoord") ||
        (ActiveTool.id == "measure") ) {

      if (depress) {
         ActiveTool.src = "images/" + ActiveTool.id + "_2.gif";
         ActiveTool.className = "opaque";
         ActiveTool.style.backgroundColor = top.MenuBackgroundColor;
         ActiveTool.style.border = "1px solid " + top.MenuBackgroundColor;
      } else {
         ActiveTool.src = "images/" + ActiveTool.id + "_1.gif";
         ActiveTool.className = "notOpaque";
      }
   }
}
//=====================
// Make zoom-in active.
//=====================
function setMainTool() {
   if ( (ActiveTool.id != "zoomIn") &&
        (ActiveTool.id != "zoomOut") &&
        (ActiveTool.id != "pan") &&
        (ActiveTool.id != "selectFeature") &&
        (ActiveTool.id != "identify") &&
        (ActiveTool.id != "measure") ) {
      activateTool(document.getElementById("zoomIn"));
   }
}
//==============================
// Process tool mouseover event.
//==============================
function toolOver(inTool) {
   window.status = inTool.title;
   if (top.PostBackDone) {
      if (inTool.src != "") {
         if (inTool.className != "disableClass" && inTool.id != ActiveTool.id) {
            highlightTool(inTool);

            if (inTool.id == "zoomFullExtent") {
               if (Map_img.style.zIndex == 2) {
                  fullExtentOver();
               }
            }
         }
      }
   }
}
//=============================
// Process tool mouseout event.
//=============================
function toolOut(inTool) {
   if (top.PostBackDone) {
      window.status = "";
      if (inTool.src != "") {
         if (inTool.className != "disableClass" && inTool.id != ActiveTool.id) {
            unHighlightTool(inTool);

            if (inTool.id == "zoomFullExtent") {
               fullExtentOut();
            }
         }
      }
   }
}
//========================
// Full extent over event.
//========================
function fullExtentOver() {
   if (FullExtentImage != "") {
      if (FullExtentImage != MapHistoryImage) {
         OverMap = true;
         if (top.IeFilters) {
            saveVML();
         }
         CheckSizeImage = new Image();
         CheckSizeImage.src = FullExtentImage;
         window.setTimeout("showOverview()", 500);
      }
   }
}
//===================
// Show overview map.
//===================
function showOverview() {
   if (OverMap) {
      if (CheckSizeImage.fileSize < 1) {
         FullExtentImage = "";
         return;
      }
      if (top.IeFilters) {
         clearVML();
         top.doBlendTran(FullExtentImage);
      } else {
         top.AnimateMap = true;
         Map_img.src = FullExtentImage;
      }

      var totalArea = (XMaxMapFE - XMinMapFE) * (YMaxMapFE - YMinMapFE);
      var overviewArea = (XMaxMap - XMinMap) * (YMaxMap - YMinMap);

      if (overviewArea < (totalArea / 1.2)) {

         //Draw a point
         if (overviewArea < (totalArea / 2000)) {
            var cenX = XMinMap + ((XMaxMap - XMinMap) / 2);
            var cenY = YMinMap + ((YMaxMap - YMinMap) / 2);
            getXyPix(cenX, cenY);
            Overview_img.style.left = (XPix - 16) + "px";
            Overview_img.style.top = (YPix - 16) + "px";
            Overview_img.style.visibility = "visible";

         //Draw a box
         } else {
            if (top.IeFilters) {
               getXyPix(XMinMap, YMinMap);
               var wPath = "m " + XPix + "," + YPix + " l ";
               
               getXyPix(XMinMap, YMaxMap);
               wPath += XPix + "," + YPix + " ";

               getXyPix(XMaxMap, YMaxMap);
               wPath += XPix + "," + YPix + " ";

               getXyPix(XMaxMap, YMinMap);
               wPath += XPix + "," + YPix + " ";

               getXyPix(XMinMap, YMinMap);
               wPath += XPix + "," + YPix + " e";
               Overview_vml.path = wPath;
            }
         }
      }
   }
}
//=======================
// Full extent out event.
//=======================
function fullExtentOut() {
   if (FullExtentImage != MapHistoryImage) {
      OverMap = false;
      Overview_img.style.visibility = "hidden";

      if (top.IeFilters) {
         Overview_vml.path = "m 0 0 l 0,0 0,0 0,0 0,0 e";
         top.doBlendTran(MapHistoryImage);
      } else {
         top.AnimateMap = true;
         Map_img.src = MapHistoryImage;
      }

      if (top.IeFilters) {
         restoreVML();  
      }
   }
}
//Convert map to pixel coordinates.
function getXyPix(x,y) {
   var xPixPerFoot = MapImageWidth / (XMaxMapFE - XMinMapFE);
   XPix = parseInt((x - XMinMapFE) * xPixPerFoot);

   var yPixPerFoot = MapImageHeight / (YMaxMapFE - YMinMapFE);
   YPix = parseInt(((YMaxMapFE - y) * yPixPerFoot) + top.MainMenuHeight);
}
//==================
// Highlight a tool.
//==================
function highlightTool(inTool) {
   try {
      inTool.className = "opaque";
      inTool.style.border = "1px solid " + top.HoverBorderColor;
      inTool.style.backgroundColor = top.HoverBackgroundColor;
   } catch(ex) {
   }
}
//====================
// Unhighlight a tool.
//====================
function unHighlightTool(inTool) {
   try {
      inTool.className = "notOpaque";
      inTool.style.backgroundColor = top.MenuBackgroundColor;
      inTool.style.border = "1px solid " + top.MenuBackgroundColor;
   } catch(ex) {
   }
}
//===============
// Enable a tool.
//===============
function enableTool(inTool) {
   if (inTool) {
      inTool.className = "notOpaque";
      try {
         if (!inTool.filters) {
            inTool.style.opacity = 0.99;
         }
      } catch(ex) {
         inTool.style.visibility = "visible";
      }
   }
}
//================
// Disable a tool.
//================
function disableTool(inTool) {
   unHighlightTool(inTool);
   inTool.className = "disableClass";
   try {
      if (!inTool.filters) {
         inTool.style.opacity = 0.3;
      }
   } catch(ex) {
      inTool.style.visibility = "hidden";
   }
}
//============================
// Compress history variables.
//============================
function compressMapHistory() {
   var i;
   for (i=2; i<=MapHistoryCount; i++) {
      MapHistory[i-1].xMin = MapHistory[i].xMin;
      MapHistory[i-1].yMin = MapHistory[i].yMin;
      MapHistory[i-1].xMax = MapHistory[i].xMax;
      MapHistory[i-1].yMax = MapHistory[i].yMax;
      MapHistory[i-1].url = MapHistory[i].url;
   }
   MapHistoryCount -= 1;
   if (MapHistoryPosition > 1) {
      MapHistoryPosition -= 1;
   }
}
//=============================
// Increment history variables.
//=============================
function addToMapHistory(inSrc) {
   var i;
   if (MapHistoryCount == MaxMapHistory) {
      compressMapHistory();
   }
   MapHistoryCount += 1;
   MapHistoryPosition = MapHistoryCount;
   i = MapHistoryPosition;

   if (i > 1) {
      enableTool(ZoomPreviousImg);
      enableRow("viewZoomPrevious");
   }
   MapHistory[i] = new mapHistoryObj();
   MapHistory[i].xMin = XMinMap;
   MapHistory[i].yMin = YMinMap;
   MapHistory[i].xMax = XMaxMap;
   MapHistory[i].yMax = YMaxMap;

   MapHistoryImage = Map_img.src;
   if (inSrc) {
      if (inSrc != "") {
         MapHistoryImage = inSrc;
      }
   }
   MapHistory[i].url = MapHistoryImage;

   //Initialize full extent variables
   if (SaveFullExtent) {
      setFullExtent();
   }
   disableTool(ZoomNextImg);
   disableRow("viewZoomNext");
   //alert("addToMapHist: Pos=" + MapHistoryPosition + "  Count=" + MapHistoryCount + "  URL=" + MapHistory[i].url); 
}
//=====================
// Set full map extent.
//=====================
function setFullExtent() {
   SaveFullExtent = false;

   FullExtentImage = MapHistoryImage;
   XMinMapFE = XMinMap;
   YMinMapFE = YMinMap;
   XMaxMapFE = XMaxMap;
   YMaxMapFE = YMaxMap;
}
//=====================
// Add full map extent.
//=====================
function addFullExtent() {
   top.doBlendTran(FullExtentImage);

   //Set mbr
   XMinMap = XMinMapFE;
   YMinMap = YMinMapFE;
   XMaxMap = XMaxMapFE;
   YMaxMap = YMaxMapFE;
   top.setMapScales();

   addToMapHistory();

   if (top.IeFilters) {
      clearVML();
      saveVML();
      Overview_vml.path = "m 0 0 l 0,0 0,0 0,0 0,0 e";
   }
   Overview_img.style.visibility = "hidden";

   if (top.data_frame.document.getElementById("layers_form")) {
      top.data_frame.grayOutLayers();
   }
   if (top.data_frame.document.getElementById("measure_form")) {
      top.data_frame.clearMeasureAll();
   }
   if (top.data_frame.document.getElementById("mv_form")) {
      top.getMapViews();
   }
}
//=====================
// Previous map extent.
//=====================
function zoomPrevious() {
   var i;
   if (MapHistoryPosition > 1) {
      MapHistoryPosition -= 1;
      i = MapHistoryPosition;
      MapHistoryImage = MapHistory[i].url;
      top.doBlendTran(MapHistoryImage);

      //Set mbr
      XMinMap = MapHistory[i].xMin;
      YMinMap = MapHistory[i].yMin;
      XMaxMap = MapHistory[i].xMax;
      YMaxMap = MapHistory[i].yMax;
      top.setMapScales();

      enableTool(ZoomNextImg);
      enableRow("viewZoomNext");

      if (top.IeFilters) {
         clearVML();
      }
      if (top.data_frame.document.getElementById("layers_form")) {
         top.data_frame.grayOutLayers();
      }
      if (top.data_frame.document.getElementById("measure_form")) {
         top.data_frame.clearMeasureAll();
      }
      //alert("zoomPrevious: Pos=" + MapHistoryPosition + "  Count=" + MapHistoryCount + "  URL=" + MapHistory[MapHistoryPosition].url); 
   }
   if (i == 1) {
      disableTool(ZoomPreviousImg);
      disableRow("viewZoomPrevious");
   }
}
//=================
// Next map extent.
//=================
function zoomNext() {
   var i;
   if (MapHistoryPosition < MapHistoryCount) {
      MapHistoryPosition += 1;
      i = MapHistoryPosition;
      MapHistoryImage = MapHistory[i].url;
      top.doBlendTran(MapHistoryImage);

      //Set mbr
      XMinMap = MapHistory[i].xMin;
      YMinMap = MapHistory[i].yMin;
      XMaxMap = MapHistory[i].xMax;
      YMaxMap = MapHistory[i].yMax;
      top.setMapScales();

      enableTool(ZoomPreviousImg);
      enableRow("viewZoomPrevious");

      if (top.IeFilters) {
         clearVML();
      }
      if (top.data_frame.document.getElementById("layers_form")) {
         top.data_frame.grayOutLayers();
      }
      if (top.data_frame.document.getElementById("measure_form")) {
         top.data_frame.clearMeasureAll();
      }
      //alert("zoomNext: Pos=" + MapHistoryPosition + "  Count=" + MapHistoryCount + "  URL=" + MapHistory[MapHistoryPosition].url); 
   }
   if (i == MapHistoryCount) {
      disableTool(ZoomNextImg);
      disableRow("viewZoomNext");
   }
}
//==============
// Flash a tool.
//==============
function flashTool(inTool) {
   if (top.PostBackDone) {
      FlashImage = inTool;
      window.setTimeout("flashOff()", 100);
      window.setTimeout("flashOn()",  200);
      window.setTimeout("flashOff()", 300);
      window.setTimeout("flashOn()",  400);
      window.setTimeout("flashOff()", 500);
   }
}
function flashOn() {
   highlightTool(FlashImage);
}
function flashOff() {
   unHighlightTool(FlashImage);
}
