﻿//Treeview variables.
var SelectionNodeIDs = new Array();

var OverTV;
var ZoomToPopup;
var ShowZoomTo;
var ShowMapTip;
var PS_tr;

//Map tip variables.
var MaxTipsPerPage;
var TotNumTips = 0;
var BegTip = 0;
var EndTip = 0;

var OverTrTip;
var OverMapTip;
var ActiveTip;
var ActiveTipIndex = -1;

var MapTipMBRarray = new Array();

var XMinTip,YMinTip,XMaxTip,YMaxTip;
var XTip,YTip;
var XTipPix,YTipPix;
var XTipList = "0";
var YTipList = "0";

//Init.
function initMapTipPoints() {
   var obj = $get("MapTipPoints1_MaxTips_hid");
   MaxTipsPerPage = parseInt(obj.value);

   OverTrTip = $get("OverTR_Tip");
   OverMapTip = $get("OverMap_Tip");
   ActiveTip = $get("Active_Tip");
   ZoomToPopup = $get("ZoomToPopup");
   SR_tr = $find("SelectResults1_Selection_TaskResults");
}
//Init selection task results.
function initSelectionTR() {
   var srCtrl = "SelectResults1_";

   TotNumTips = $get(srCtrl + "tipCnt_hid").value;
   var mbrList = $get(srCtrl + "mbrList_hid").value;
   var nodeIdStr = $get(srCtrl + "nodeIdStr_hid").value;
   
   ActiveTipIndex = -1;
   BegTip = 1;
   EndTip = Math.min(TotNumTips,MaxTipsPerPage);
   
   MapTipMBRarray = mbrList.split("|");
   SelectionNodeIDs = nodeIdStr.split("|");

   hideActiveTip();
   refreshMapTipGraphics();
   prevNextButtonVisibility();
}
//Clear task results.
function clearTaskResults() {
   SR_tr = $find("SelectResults1_Selection_TaskResults");
   if (SR_tr) {
      SR_tr.clearAllNodes();
   }
}
//Next TR page.
function doNextTR(nodeID) {
   if (EndTip != TotNumTips) {
      if (SR_tr) {
         SR_tr._nextPage(nodeID);
         BegTip += MaxTipsPerPage;
         EndTip = BegTip + MaxTipsPerPage - 1;
         EndTip = Math.min(TotNumTips,EndTip);

         hideActiveTip();
         refreshMapTipGraphics();
         prevNextButtonVisibility();
      }
   }
}
//Previous TR page.
function doPrevTR(nodeID) {
   if (BegTip > 1) {
      if (SR_tr) {
         SR_tr._previousPage(nodeID);
         BegTip -= MaxTipsPerPage;
         EndTip = BegTip + MaxTipsPerPage - 1;
         EndTip = Math.min(TotNumTips,EndTip);

         hideActiveTip();
         refreshMapTipGraphics();
         prevNextButtonVisibility();
      }
   }
}
//Tip paging.
function prevNextButtonVisibility() {
   window.setTimeout("waitPrevNext();", 200);
   window.setTimeout("waitPrevNext();", 400);
}
//Tip paging buttons.
function waitPrevNext() {
   var p = $get("PrevTR");
   if (p) {
      if (BegTip == 1) {
         p.style.visibility = "hidden";
      } else {
         p.style.visibility = "visible";
      }
   }
   var n = $get("NextTR");
   if (n) {
      if (EndTip == TotNumTips) {
         n.style.visibility = "hidden";
      } else {
         n.style.visibility = "visible";
      }
   }
}
//Refresh map tips.
function refreshMapTipGraphics() {
   var i,j,tp,tn,ti,obj;
   var beg,end;
   var numTip;
   
   if (BegTip == 0) {
      return;
   }

   i = 0;
   beg = BegTip - 1;
   end = beg + MaxTipsPerPage;
   numTip = EndTip - BegTip + 1;
   XTipList = "";
   YTipList = "";

   for (j=beg; j<end; j++) {

      tp = $get("TPt" + i);
      tn = $get("TN" + i);
      ti = $get("TI" + i);

      if (tp) {
         if (i < numTip) {
            tp.style.visibility = "visible";
            setMapTipExtent(j);
            
            //Build xy tip lists.
            if (XTipList != "") {
               XTipList += ",";
               YTipList += ",";
            }
            XTipList += XTip;
            YTipList += YTip;
            
            //Set tip locations.
            tp.style.left = (XTipPix - 9) + "px";
            tp.style.top = (YTipPix - 9) + "px";
            ti.style.width = "";
            ti.style.height = "";
            tn.innerHTML = "" + (j + 1);

            if (j < 9) {
               tn.style.left = "6px";
            
            } else if (j < 99) {
               tn.style.left = "1px";

            } else {
               ti.style.width = "30px";
               ti.style.height = "20px";
            }

         } else {
            tp.style.visibility = "hidden";
         }
      }
      i += 1;
   }
   //Active tip.
   if (ActiveTipIndex != -1) {
      i = ActiveTipIndex;
      setMapTipExtent(i);
      ActiveTip.style.left = getMapTipXPix(i);
      ActiveTip.style.top = getMapTipYPix(i,ActiveTip);
   }
}
//Set map tip extent.
function setMapTipExtent(i) {
   var mapTipMBR = new Array();
   var pixPnt = null;
   
   if (MapTipMBRarray[i]) {
      mapTipMBR = MapTipMBRarray[i].split(",");
      XMinTip = parseInt(mapTipMBR[0]);
      YMinTip = parseInt(mapTipMBR[1]);
      XMaxTip = parseInt(mapTipMBR[2]);
      YMaxTip = parseInt(mapTipMBR[3]);
      XTip = (XMinTip + XMaxTip) / 2;
      YTip = (YMinTip + YMaxTip) / 2;

      var mapPnt = new ESRI.ADF.Geometries.Point(XTip, YTip);
      pixPnt = gMap.toScreenPoint(mapPnt);

      XTipPix = pixPnt.offsetX;
      YTipPix = pixPnt.offsetY + parseInt(ToolbarPanel.style.height);

      if (XTipPix < 8) {
         XTipPix = -99;
      }
      if (YTipPix < 40) {
         YTipPix = -99;
      }
   }
}
//Get tip heading.
function getMapTipHeading(e) {
   var txt = e.innerHTML.toLowerCase();
   var pos1 = txt.indexOf("</span>") + 7;
   var pos2 = txt.indexOf("<div ");
   var head = e.innerHTML.slice(pos1, pos2);
   head = '<span class="TipHead">' + head + '</span>';
   return head;
}
//Get full tip.
function getFullMapTip(i) {
   var head = "";
   var e = $get("TVH" + i);
   if (e) {
      head = getMapTipHeading(e);
   
      var txt = e.innerHTML.toLowerCase();
      var pos = txt.indexOf("<div ");
      if (pos != -1) {
         head += e.innerHTML.slice(pos);
      }
      //Add custom map tip links here.

      //Zoom to link.
      head += '<div id=ZTDiv><span id="TipZoomTo" onclick="ztClick()">';
      head += '<img style="vertical-align:middle" src="images/ZoomTo.gif" alt="" /> Zoom to';
      head += '</span></div>';
   }
   return head;
}
//Get x pix of map tip text.
function getMapTipXPix(i) {
   var j = getTipPntIdx(i);
   var xPix = "0px";
   var obj = $get("TPt" + j);
   if (obj) {
      if (obj.style.visibility == "visible") {
         xPix = parseInt(obj.style.left) + 3 + "px";
      }
   }
   return xPix;
}
//Get y pix of map tip text.
function getMapTipYPix(i,e) {
   var j = getTipPntIdx(i);
   var yPix = "0px";
   var obj = $get("TPt" + j);
   if (obj) {
      if (obj.style.visibility == "visible") {
         yPix = (parseInt(obj.style.top) - e.clientHeight) + "px";
      }
   }
   return yPix;
}
//Get tip point index.
function getTipPntIdx(i) {
   var p = parseInt(i / MaxTipsPerPage);
   var j = i - (p * MaxTipsPerPage);
   return j;
}
//Treeview over.
function tvOver(e) {
   OverTV = e;
   
   e.style.backgroundColor = "#FDE281";
   e.style.borderColor = "Gray";

   if (OverTrTip) {
      var i = e.id.slice(3);
      OverTrTip.style.visibility = "visible";
      OverTrTip.innerHTML = getMapTipHeading(e);
      OverTrTip.style.left = getMapTipXPix(i);
      OverTrTip.style.top = getMapTipYPix(i,OverTrTip);
   }
   if (ZoomToPopup) {
      ShowZoomTo = true;
      ZoomToPopup.style.visibility = "visible";

      var box = GetElementRectangle(e);
      var tbh = parseInt(TitleBarPanel.style.height);

      if ((e.scrollWidth < 200) || (i == (BegTip - 1) && TotNumTips > MaxTipsPerPage)) {
         ZoomToPopup.style.left = (e.scrollWidth + 41) + "px";  //Shown on right
         ZoomToPopup.style.top = (box.top + TaskContentsPanel.scrollTop - tbh - 60) + "px";
      } else {
         ZoomToPopup.style.left = (e.scrollWidth - 33) + "px";  //Shown above
         ZoomToPopup.style.top = (box.top + TaskContentsPanel.scrollTop - tbh - 83) + "px";
      }
   }
}
//Treeview move.
function tvMove() {
   if (ZoomToPopup) {
      ShowZoomTo = true;
      ZoomToPopup.style.visibility = "visible";
   }
   if (OverTrTip) {
      OverTrTip.style.visibility = "visible";
   }
}
//Treeview out.
function tvOut() {
   if (OverTrTip) {
      ShowZoomTo = false;
      OverTrTip.style.visibility = "hidden";
   }
   if (ZoomToPopup) {
      window.setTimeout("hideZoomTo();", 300);
   }
   if (OverTV) {
      OverTV.style.backgroundColor = "";
      OverTV.style.borderColor = "";
   }
}
//ZoomTo move.
function ztMove() {
   tvMove();
   if (OverTV) {
      OverTV.style.borderColor = "Gray";
   }
}
//ZoomTo out.
function ztOut() {
   tvOut();
}
//ZoomTo click.
function ztClick() {
   var i = OverTV.id.slice(3);
   setMapTipExtent(i);

   ZoomToPopup.style.visibility = "hidden";
   OverMapTip.style.visibility = "hidden";

   if (ActiveTip) {
      ActiveTipIndex = i;
      ActiveTip.style.visibility = "visible";

      ActiveTip.innerHTML = getMapTipHeading(OverTV) + '<img style="height:14px; width:16px; vertical-align:top" src="images/dismiss.png" />';
      ActiveTip.style.left = getMapTipXPix(i);
      ActiveTip.style.top = getMapTipYPix(i,OverTrTip);
   }
   OverTrTip.style.visibility = "hidden";
   var env = new ESRI.ADF.Geometries.Envelope(XMinTip, YMinTip, XMaxTip, YMaxTip);
   gMap.set_extent(env);

   //var junk = $get("j1");
   //junk.innerHTML = "box.top=" + box.top + "   e.top=" + e.top + "   e.scrollHeight=" + e.scrollHeight;
}
//Tip point move.
function tpMove(e) {
   if (OverMapTip) {
      ShowMapTip = true;
      var i = BegTip + parseInt(e.id.slice(3)) - 1;
      OverMapTip.innerHTML = getFullMapTip(i);
      if (OverMapTip.innerHTML != "") {
         OverMapTip.style.visibility = "visible";
         OverMapTip.style.left = getMapTipXPix(i);
         OverMapTip.style.top = getMapTipYPix(i,OverMapTip);
         OverTV = $get("TVH" + i);
         highlightTV();
      }
   }
}
//Tip point out.
function tpOut() {
   if (OverMapTip) {
      ShowMapTip = false;
      window.setTimeout("hideOverMapTip();", 300);
   }
   if (OverTV) {
      OverTV.style.backgroundColor = "";
      OverTV.style.borderColor = "";
   }
}
//Tip point click.
function tpClick(e) {
   var i = BegTip + parseInt(e.id.slice(3)) - 1;
   var nodeID = SelectionNodeIDs[i];
   if (SR_tr) {
      SR_tr._toggleNodeState(nodeID); //Expand treeview.
   }
}
//Map tip text over.
function tpMapMove(e) {
   ShowMapTip = true;
   highlightTV();
}
//Hide map tip.
function hideOverMapTip() {
   if (!ShowMapTip) {
      OverMapTip.style.visibility = "hidden";
   }
}
//Highlight treeview.
function highlightTV(i) {
   if (OverTV) {
      OverTV.style.backgroundColor = "White";
      OverTV.style.borderColor = "Gray";
   }
}
//Hide active tip.
function hideActiveTip() {
   if (ActiveTip) {
      ActiveTip.style.visibility = "hidden";
   }
}
//Hide zoom to.
function hideZoomTo() {
   if (!ShowZoomTo) {
      ZoomToPopup.style.visibility = "hidden";
   }
}
