﻿//$get("Debug").innerHTML = "dt.innerHTML=" + dt.innerHTML;

//ESRI.
var isIE = (Sys.Browser.agent == Sys.Browser.InternetExplorer);
Type.registerNamespace('ESRI.ADF.WebMappingApplication');

//JavaScript constants.
var ActiveBackgroundColor = "white";
var ActiveBorderColor = "black";
var HoverColor = "#FDE2B8";
var NumXYDecimals = 2;  //Decimals in xy readout.

//Misc.
var gMap, gToolbar;
var LastTool;
var PrevMainTool;
var ActivePanel="";
var ActiveTask="";
var ReloadTimer;
var WebsiteLoaded = false;

//Framework.
var TopPanel;
var TitleBarPanel;
var MainPanel;
var LeftPanel;
var ShowLeftPanel;
var TaskPanel;
var TaskMenuPanel;
var TaskHeadingPanel;
var TaskContentsPanel;
var HideLeftPanel;
var RightPanel;
var ToolbarPanel;
var MapPanel;
var BottomPanel;
var StatusBarPanel;

var Border = 1;
var Margin;
var Margin2;

var TopPanelHeight;
var TitleBarHeight;
var BottomPanelHeight;
var StatusBarHeight;

var TaskPanelWidth;
var HideLeftPanelWidth;
var LeftPanelWidth;

//Projection.
var CentralMeridianEasting;
var FalseNorthing;
var CentralMeridian;
var Lat1stParallel;
var Lat2ndParallel;
var LatOrigin;
var XYUnit = "Lat/Long DM";

//Full extent.
var XMinFull;
var YMinFull;
var XMaxFull;
var YMaxFull;

//Init website.
function initWebsite() {

   //Show startup errors.
   var errMsg = "";
   var obj = $get("ErrMsg_hid");
   if (obj.value != "") {
      errMsg = "The GIS website is temporarily unavailable. " + obj.value;
      window.setTimeout("showErrMsg('" + errMsg + "');", 100);
   }

   //Get javascript custom settings from server.
   var js_settings = new Array();
   obj = $get("JS_Settings_hid");
   js_settings = obj.value.split("|");

   CentralMeridianEasting = parseFloat(js_settings[0]);
   FalseNorthing = parseFloat(js_settings[1]);
   CentralMeridian = parseFloat(js_settings[2]);
   Lat1stParallel = parseFloat(js_settings[3]);
   Lat2ndParallel = parseFloat(js_settings[4]);
   LatOrigin = parseFloat(js_settings[5]);

   XMinFull = parseFloat(js_settings[6]);
   YMinFull = parseFloat(js_settings[7]);
   XMaxFull = parseFloat(js_settings[8]);
   YMaxFull = parseFloat(js_settings[9]);
   
   //Set panel objects.
   TopPanel = $get("Top_Panel");
   TitleBarPanel = $get("TitleBar_Panel");
   MainPanel = $get("Main_Panel");
   LeftPanel = $get("Left_Panel");
   ShowLeftPanel = $get("ShowLeft_Panel");
   TaskPanel = $get("Task_Panel");
   TaskMenuPanel = $get("TaskMenu_Panel");
   TaskHeadingPanel = $get("TaskHeading_Panel");
   TaskContentsPanel = $get("TaskContents_Panel");
   HideLeftPanel = $get("HideLeft_Panel");
   RightPanel = $get("Right_Panel");
   ToolbarPanel = $get("ToolBar_Panel");
   MapPanel = $get("Map_Panel");
   BottomPanel = $get("Bottom_Panel");
   StatusBarPanel = $get("StatusBar_Panel");

   //Margin.
   Margin = parseInt(MainPanel.style.marginRight);
   Margin2 = parseInt(Margin / 2);

   //Add window events.
   window.onresize = resizeElements;

   //Add control events.
   map = $find("Map1"); //For esri measure tool.

   gMap = $find("Map1");
   gMap.add_extentChanged(map_ExtentChanged);
   gMap.add_mouseMove(map_MouseMove);

   gToolbar = $find("Toolbar1");
   gToolbar.add_onToolSelected(OnToolSelectHandler);

   obj = $find("Overview_Map_Task");
   if (obj) {
      obj.add_show(showOverview);
      obj.add_hide(hideOverview);
   }
   //Zoom to start extent.
   resizeElements();
   zoomFullExtent();

   //Init controls.
   showTask("Select On Map");
   hideOverview();
   initMapTipPoints();

   //Timeout minutes. Should match web.config.
   maximumLapseTime = 20;
   if (errMsg == "") {
      WebsiteLoaded = true;
   }
}
//Show task or floating panel.
function showTask(task) {
   var obj, panelID, taskID;

   //Custom floating panels.
   if ((task == "Print") ||
       (task == "Overview Map") ||
       (task == "About Website")) {

      task = task.replace(/\ /g, "_") + "_Task";
      showFloatingPanel(task);

      //Left panel tasks.
   } else {
      if (TaskPanel.style.width == "0px") {
         toggleLeftPanel();
      }
      if (task == "Select On Map" || task == "Address/PIN Search" || task == "Advanced Search") {
         panelID = "Selection_Task";
         taskID = task.replace(/\ /g, "_") + "_Task";

         if (task == "Address/PIN Search") {
            focusSimpleSearch();
         }

      } else {
         panelID = task.replace(/\ /g, "_") + "_Task";
         taskID = "";
      }
      if (panelID != ActivePanel) {
         obj = $get(ActivePanel);
         if (obj) {
            obj.style.display = "none";
         }
         ActivePanel = panelID;

         obj = $get(ActivePanel);
         if (obj) {
            obj.style.display = "block";
         }
      }
      if (taskID != "") {
         if (taskID != ActiveTask) {
            obj = $get(ActiveTask);
            if (obj) {
               obj.style.display = "none";
            }
            ActiveTask = taskID;

            obj = $get(ActiveTask);
            if (obj) {
               obj.style.display = "block";
            }
         }
      }

      $get("TaskHeading_Label").innerHTML = task;
      if (task == "Address/PIN Search") {
          $get("TaskHeading_Label").innerHTML = "Address/PIN Search of Qualified Sales";
      }
   }
}
//Show busy.
function showBusy(toolName) {
   var obj = $get(toolName + "Busy");
   if (obj) {
      obj.style.visibility = "visible";
   } else {
      alert(toolName + "Busy image not found");
   }
}
//Hide busy.
function hideBusy(toolName) {
   var obj = $get(toolName + "Busy");
   if (obj) {
      obj.style.visibility = "hidden";
   } else {
      alert(toolName + "Busy image not found");
   }
}
//Show startup error messages from server.
function showErrMsg(errMsg) {
   alert(errMsg);
}
//Show or hide titlebar.
function toggleTitlePanel() {
   var obj = $get("TopResize_img");

   if (TitleBarPanel.style.height == "0px") {
      TopPanel.style.height = TopPanelHeight;
      TitleBarPanel.style.height = TitleBarHeight;
      obj.src = "images/UpArrow.gif";

   } else {
      TopPanelHeight = TopPanel.style.height;
      TitleBarHeight = TitleBarPanel.style.height;
      TopPanel.style.height = (parseInt(TopPanelHeight) - parseInt(TitleBarHeight)) + "px";
      TitleBarPanel.style.height = "0px";
      obj.src = "images/DownArrow.gif";

      if (StatusBarPanel.style.height != "0px") {
         toggleStatusBarPanel();
      }
   }
   resizeElements();
}
//Show or hide statusbar.
function toggleStatusBarPanel() {
   var obj = $get("BottomResize_img");

   if (StatusBarPanel.style.height == "0px") {
      BottomPanel.style.height = BottomPanelHeight;
      StatusBarPanel.style.height = StatusBarHeight;
      obj.src = "images/DownArrow.gif";

   } else {
      BottomPanelHeight = BottomPanel.style.height;
      StatusBarHeight = StatusBarPanel.style.height;
      BottomPanel.style.height = (parseInt(BottomPanelHeight) - parseInt(StatusBarHeight)) + "px";
      StatusBarPanel.style.height = "0px";
      obj.src = "images/UpArrow.gif";

      if (TitleBarPanel.style.height != "0px") {
         toggleTitlePanel();
      }
   }
   resizeElements();
}
//Show or hide left panel.
function toggleLeftPanel() {

   if (TaskPanel.style.width == "0px") {
      TaskPanel.style.width = TaskPanelWidth;
      HideLeftPanel.style.width = HideLeftPanelWidth;
      LeftPanel.style.width = LeftPanelWidth;
      ShowLeftPanel.style.height = "0px";

   } else {
      TaskPanelWidth = TaskPanel.style.width;
      HideLeftPanelWidth = HideLeftPanel.style.width;
      LeftPanelWidth = LeftPanel.style.width;

      TaskPanel.style.width = "0px";
      HideLeftPanel.style.width = "0px";
      LeftPanel.style.width = ShowLeftPanel.style.width;
      ShowLeftPanel.style.height = "100%";
   }
   resizeElements();
}

//Resize mouse down.
function resizeDown(e) {
   if (!e) e = window.event;
   if (!e) return;
   document.onmousemove = resizeMove;
   document.onmouseup = resizeUp;
   return false;
}
//Resize mouse move.
function resizeMove(e) {
   if (!e) e = window.event;
   if (!e) return;
   if (e.clientX > (Margin * 2) && e.clientX < (getWinWidth() - Margin)) {
      TaskPanel.style.width = (e.clientX - ((Margin * 2) - Border)) + "px";
      LeftPanel.style.width = (e.clientX + (Border * 3)) + "px";
      resizeElements();
   }
   return false;
}
//Resize mouse up.
function resizeUp(e) {
   if (!e) e = window.event;
   if (!e) return;
   document.onmousemove = null;
   document.onmouseup = null;   
   return false;
}
//Resize elements.
function resizeElements() {
   var i;
   var obj;
   var wd;
   var ht;

   //Main panel.
   var tpHt = parseInt(TopPanel.style.height);
   var bpHt = parseInt(BottomPanel.style.height);
   var mpHt = getWinHeight() - (tpHt + bpHt - 1);

   if (mpHt < Margin) {
      mpHt = Margin;
   }
   MainPanel.style.top = TopPanel.style.height;
   MainPanel.style.width = (getWinWidth() - (Margin - 3)) + "px"; //Reduce right margin.
   MainPanel.style.height = mpHt + "px";

   //Left panel elements.
   TaskPanel.style.height = (mpHt - (Border * 2)) + "px";
   if (ShowLeftPanel.style.height != "0px") {
      ShowLeftPanel.style.height = TaskPanel.style.height;
   }
   //Task heading panel.
   TaskHeadingPanel.style.top = TaskMenuPanel.style.height;

   //Task contents panel.
   wd = parseInt(TaskPanel.style.width);
   if (wd < Margin) {
      wd = Margin;
   }
   ht = parseInt(TaskPanel.style.height) - (parseInt(TaskMenuPanel.style.height) + parseInt(TaskHeadingPanel.style.height));
   if (ht < Margin) {
      ht = Margin;
   }
   TaskContentsPanel.style.width = wd + "px";
   TaskContentsPanel.style.height = ht + "px";
   TaskContentsPanel.style.top = parseInt(TaskMenuPanel.style.height) + parseInt(TaskHeadingPanel.style.height) + "px";

   //Debug statements.
   //$get("j1").innerHTML = 
   //   "Left_Panel=" + $get("Left_Panel").style.width + ", " +
   //   "Task_Panel=" + $get("Task_Panel").style.width + ", " +
   //   "Tab1_div=" + $get("Tab1_div").style.width + ", " +
      
   //Right panel.
   var lpWd = parseInt(LeftPanel.style.width);
   var rpWd = getWinWidth() - (lpWd + Margin); //Reduce right margin.

   if (rpWd < Margin) {
      rpWd = Margin;
   }
   RightPanel.style.left = LeftPanel.style.width;
   RightPanel.style.width = rpWd + "px";
   RightPanel.style.height = TaskPanel.style.height;

   //Toolbar panel.
   var tbw = rpWd - (Margin + Border);
   if (tbw < Margin) {
      tbw = Margin;
   }
   ToolbarPanel.style.width = tbw + "px";

   //Map panel.
   mpHt = parseInt(RightPanel.style.height) - (parseInt(ToolbarPanel.style.height) + (Border * 3) + 2);
   if (mpHt < 1) {
      mpHt = 1;
   }
   MapPanel.style.height = mpHt + (Border * 2) + "px";
   MapPanel.style.width = RightPanel.style.width;

   if (WebsiteLoaded) {
      window.clearTimeout(ReloadTimer);
      ReloadTimer = window.setTimeout("fillMapPanel()", 1000);
   }

   //Navigation tool.
   var nav = $get("Navigation1");
   nav.style.left = "1px";
   nav.style.top = (parseInt(ToolbarPanel.style.height) + 7) + "px";

   //Zoom level.
   obj = $get("ZoomLevel");
   obj.style.left = Margin + 4 + "px";
   obj.style.top = (parseInt(nav.style.top) + 52) + "px";

   //Bottom panel.
   BottomPanel.style.top = parseInt(MainPanel.style.top) + parseInt(MainPanel.style.height) - (Border * 1) + "px";
   
   //Scale bar.
   obj = $get("ScaleBar1");
   obj.style.left = Margin + "px";
   obj.style.top = parseInt(RightPanel.style.height) - (parseInt(obj.style.height) + Margin2 + 2) + "px";
}
//Recenter map.
function recenterMap(x, y, animate) {
   if (WebsiteLoaded) {
      var pnt = new ESRI.ADF.Geometries.Point(x, y);
      gMap.panTo(pnt, animate);
   }
}
//Zoom to extent.
function zoomToExtent(xMin,yMin,xMax,yMax) {
   gMap.set_extent(new ESRI.ADF.Geometries.Envelope(xMin, yMin, xMax, yMax));
}
//Zoom full extent.
function zoomFullExtent() {
   if (XMinFull != 0) {
      gMap.set_extent(new ESRI.ADF.Geometries.Envelope(XMinFull, YMinFull, XMaxFull, YMaxFull));
   }
}
//Map extent changed event.
function map_ExtentChanged() {
   refreshMapTipGraphics();
}
//Refresh map.
function fillMapPanel() {
   gMap.checkMapsize();
}
//Map mouse move event.
function map_MouseMove(sender, args) {
   var obj,txt;

   if (WebsiteLoaded) {
      var xMap = args.coordinate.get_x();
      var yMap = args.coordinate.get_y();
      
      //Show coordinates.
      if (XYUnit == "Lat/Long DM") {
         txt = getLatLon(xMap,yMap,"DM");

      } else if (XYUnit == "Lat/Long DMS") {
         txt = getLatLon(xMap,yMap,"DMS");

      } else {
         txt = "State Plane X,Y: " + roundNumber(xMap,2) + ", " + roundNumber(yMap,2);
      }
      obj = $get("XYReadout");
      if (obj) {
         obj.innerHTML = txt;
      }
   }
   return false;
}
//Enable main tool.
function enableMainTool() {
   disableCustomTools();

   var toolbar = Toolbars["Toolbar1"];
   var mf = document.forms[0];
   mf.elements[toolbar.currentToolField].value = PrevMainTool;
   toolbar.refreshGroup();
   toolbar.selectTool();

   if (PrevMainTool == "MapPan") {
      gMap.set_mouseMode(ESRI.ADF.UI.MouseMode.Pan);
   }
}

//Disable main tool.
function disableMainTool() {
	var toolbar = Toolbars["Toolbar1"];
   var mf = document.forms[0];
   PrevMainTool = mf.elements[toolbar.currentToolField].value;
   var toolIndex = gMap.get_mouseMode();

   if (toolIndex == ESRI.ADF.UI.MouseMode.Custom) {
      PrevMainTool = "MapPan"
   }
   mf.elements[toolbar.currentToolField].value = "";
   toolbar.refreshGroup();
   toolbar.selectTool();
}

//Disable all custom tools.
function disableCustomTools() {
   disableActiveSelectTool();
}

//Main tool depressed event.
function OnToolSelectHandler(sender, args) {
   if (args.name) {
      var toolName = args.name;
      disableCustomTools();
      LastTool = toolName;
   }
}
//Enable overview.
function showOverview(sender, args) {
   var ov = $find("Overview_Map_Task_OverviewMap1");
   if (ov) {
      ov.show();
   }
}
//Disable overview.
function hideOverview(sender, args) {
   var ov = $find("Overview_Map_Task_OverviewMap1");
   if (ov) {
      ov.hide();
   }
}
if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }

