﻿/*
Start JB ADDITIONS, 12/04/08
Description: Methods for Custom Dashboard
*/

var ZOOM_LEVEL = 11;
var FULL_EXTENT;

// Min/Max latitudes and longitudes to define the full extent.
var MAX_LATITUDE = 47.75986817589705;
var MIN_LATITUDE = 47.46059403884126;
var MIN_LONGITUDE = -122.45168426513673;
var MAX_LONGITUDE = -122.1665954589844;

var MAX_LATITUDESPN = 280016.836485041;
var MIN_LATITUDESPN = 172220.677371227;
var MAX_LONGITUDESPN = 1312506.66325748;
var MIN_LONGITUDESPN = 1240104.23869778;

var DISPLAY_EXTENT_BORDER = true;
var _extentBorderAdded = false;
//This belongs in config.js


// adds the crosshair control to the map 
var control2;
function AddCenterPointControl()
{
  if(control2 == null)
  {
    control2 = document.createElement("div"); 
    control2.id = "mapCenterPoint";
    control2.style.position="absolute";
    control2.style.zIndex = 10;
    control2.style.top = $("theMapTop").getHeight()/2+"px";
    control2.style.left = $("theMapTop").getWidth()/2+"px";
    control2.innerHTML = "<img src=\"../images/center_icon.gif\" alt=\"\" border=\"0\"/>";  
    map.AddControl(control2);
  }else{
    $("mapCenterPoint").style.top = $("theMapTop").getHeight()/2+"px";
    $("mapCenterPoint").style.left = $("theMapTop").getWidth()/2+"px";
  }
}

//Adds customized dashboard and attaches a doubleclick event handler for zooming in
var control;
var controlMapMode;
function AddMyControl()
{
  if (control == null)
  {
    control = document.createElement("div"); 
    control.id = "customDashboard";
    control.className = "customDashboard";
    control.style.top = "13px";
    control.style.left = "13px";
    control.style.position="absolute";
    control.innerHTML = "<div style='float:left;'><img id=\"navZoomIn\" onclick=\"zoom(1);\" src='../images/navZoomIn.gif'><br><img id=\"navZoomOut\"onclick=\"zoom(-1);\" style='padding-top:8px' src='../images/navZoomOut_off.gif'></div>";  
    map.AddControl(control);
}
if (controlMapMode == null) {
    controlMapMode = document.createElement("div");
    controlMapMode.id = "customDashboardMapMode";
    controlMapMode.className = "customDashboard";
    controlMapMode.style.top = "13px";
    controlMapMode.style.left = "42px";
    controlMapMode.style.position = "absolute";
    controlMapMode.innerHTML = "<div style='float:left;padding-left:8px' alt='Road View' onclick=\"setMapStyle('Road')\" id='navRoad'><img id='navImgRoad' src='../images/navRoad_on.gif'></div> <div onclick=\"setMapStyle('Aerial')\" style='float:left;padding-left:8px' id='navAerial'><img id='navImgAerial' alt='Aerial View' src='../images/navAerial_off.gif'></div> <div onclick=\"setMapStyle('Hybrid')\" id='navHybrid' style='float:left;padding-left:8px'><img id='navImgHybrid' alt='Hybrid View' src='../images/navHybrid_off.gif'></div>";
    map.AddControl(controlMapMode);
}
  map.AttachEvent("ondoubleclick", function(e){
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = map.PixelToLatLong(pixel);
    if(LL.Latitude > MAX_LATITUDE || LL.Latitude < MIN_LATITUDE || LL.Longitude < MIN_LONGITUDE || LL.Longitude > MAX_LONGITUDE){
      alert("You are trying to navigate outside of the Seattle Area.");
      return true;
    }
  });
}
var miniMapControl;
var miniMapVisible = false;
function AddMiniMap() {
    if (miniMapControl == null) {
        miniMapControl = document.createElement("div");
        miniMapControl.id = "miniMapToggle";        
        miniMapControl.style.top = "10px";
        miniMapControl.style.right = "10px";
        miniMapControl.style.position = "relative";
        miniMapControl.style.zIndex = 201;
        miniMapControl.innerHTML = "<div style='float:right;'><img id=\"overViewToggle\" onclick=\"ToggleOverview();\" src='../images/overview_close.gif'></div>";
        map.AddControl(miniMapControl);
    }
}
    function ToggleOverview() {
        if (miniMapVisible) {
            map.HideMiniMap();
            miniMapVisible = false;
            $('overViewToggle').src = "../images/overview_close.gif";
            $('disclaimerTopRight').style.top = "30px";
        }
        else {
            var _width = document.viewport.getWidth();            
            map.ShowMiniMap(_width - 160, 8);
            miniMapVisible = true;
            $('overViewToggle').src = "../images/overview_open.gif";
            $('disclaimerTopRight').style.top = "165px";
        }

    }

function SetMouseMoveStart(x, y)
{
  _mouseMoveStart.X = x; 
  _mouseMoveStart.Y = y;
}

function SetMouseDown(mouseDown)
{
  _mouseDown = mouseDown;
}

//gets current map extent
function _getExtent(e)
{
  var extent = {};
  var view = map.GetMapView();  
  extent.TopLeftLatLong = view.TopLeftLatLong;
  extent.BottomRightLatLong = view.BottomRightLatLong;
  extent.TopExtentReached = extent.TopLeftLatLong.Latitude >= FULL_EXTENT.TopLeftLatLong.Latitude;
  extent.BottomExtentReached = extent.BottomRightLatLong.Latitude <= FULL_EXTENT.BottomRightLatLong.Latitude;
  extent.LeftExtentReached = extent.TopLeftLatLong.Longitude <= FULL_EXTENT.TopLeftLatLong.Longitude;
  extent.RightExtentReached = extent.BottomRightLatLong.Longitude >= FULL_EXTENT.BottomRightLatLong.Longitude;
  return extent;
};
    
MouseDirection = {
  Up : 7,
  Down : 8,
  Left : 5,
  Right : 6,
  LeftDown : 2,
  LeftUp : 1,
  RightDown : 4,
  RightUp : 3
};

//determines what direction the user mouse has moved    
function getMouseMoveDirection(e) 
{
  var directions = MouseDirection;
  var toX = e.mapX;
  var toY = e.mapY;
  var startX = _mouseMoveStart.X;
  var startY = _mouseMoveStart.Y;
  if(((toY <= (startY) && toY >= (startY - 20)) || (toY >= (startY) && toY <= (startY + 20))) && (toX < startX))
    return directions.Left;
  else if(((toY <= (startY) && toY >= (startY - 20)) || (toY >= (startY) && toY <= (startY + 20))) && (toX > startX))
    return directions.Right;
  else if(((toX <= (startX) && toX >= (startX - 20)) || (toX >= (startX) && toX <= (startX + 20))) && (toY < startY))
    return directions.Up;
  else if(((toX <= (startX) && toX >= (startX - 20)) || (toX >= (startX) && toX <= (startX + 20))) && (toY > startY))
    return directions.Down;                    
  else if(toX < startX && toY < startY)
    return directions.LeftUp;
  else if(toX < startX && toY > startY)
    return directions.LeftDown;
  else if(toX > startX && toY < startY)
    return directions.RightUp;
  else if(toX > startX && toY > startY)
    return directions.RightDown;
};
//Adds the bounding box for extent
function addExtentBorder() 
{
  if(DISPLAY_EXTENT_BORDER && !_extentBorderAdded) 
  { 
    var points = [
      new VELatLong(MAX_LATITUDE, MIN_LONGITUDE),
      new VELatLong(MAX_LATITUDE, MAX_LONGITUDE),
      new VELatLong(MIN_LATITUDE, MAX_LONGITUDE),
      new VELatLong(MIN_LATITUDE, MIN_LONGITUDE)
      ]
    var shape = new VEShape(VEShapeType.Polygon, points);
    shape.SetFillColor(new VEColor(0,0,0,0));
    shape.SetLineColor(new VEColor(0,0,0,1));
    shape.HideIcon();
    map.AddShape(shape);
    _extentBorderAdded = true;
  };
};

//checks to make sure that the user is within the allowable extent
function TestExtent(e)
{
  if(_mouseDown)
  {
    fullExtent();
    var currentExtent = _getExtent(e);
    var mouseDirection = getMouseMoveDirection(e);
    
    switch(mouseDirection) 
    {
      case 1 :
        if(currentExtent.BottomExtentReached || currentExtent.RightExtentReached)
        return true;
      break;
      case 2 :
        if(currentExtent.TopExtentReached || currentExtent.RightExtentReached)
        return true;
      break;
      case 3 :
        if(currentExtent.BottomExtentReached || currentExtent.LeftExtentReached)
        return true;
      break;
      case 4 :
        if(currentExtent.TopExtentReached || currentExtent.LeftExtentReached)
        return true
      break;
      case 5 :
        if(currentExtent.RightExtentReached)
        return true;
      break;
      case 6 :
        if(currentExtent.LeftExtentReached)
        return true;
      break;
      case 7 :
        if(currentExtent.BottomExtentReached)
        return true;
      break;
      case 8 :
        if(currentExtent.TopExtentReached)
        return true;
      break;
      default:
        return false;
    };
  return false;
  };
};


//Set map back to Full Extent
function fullExtent()
{
  if (!FULL_EXTENT) 
  {
    var topLeftLatLong = new VELatLong(MAX_LATITUDE, MIN_LONGITUDE);
    var bottomRightLatLong = new VELatLong(MIN_LATITUDE, MAX_LONGITUDE);
    FULL_EXTENT = new VELatLongRectangle(topLeftLatLong, bottomRightLatLong);
  }
  CenterPointLat = ((MAX_LATITUDE - MIN_LATITUDE)/2)+MIN_LATITUDE
  CenterPointLong = ((MAX_LONGITUDE - MIN_LONGITUDE)/2)+MIN_LONGITUDE
  CenterPoint = new VELatLong(CenterPointLat,CenterPointLong);
  map.SetCenterAndZoom(CenterPoint, ZOOM_LEVEL);
};

//determines zoom in or out 
function zoom(dir)
{
  if(dir==1)
  {
    map.ZoomIn();	
    setZoomControls();
  }else if (dir==-1){
    map.ZoomOut();
    setZoomControls(); 
  }
}

//Handles Zoom on the mousewheel
function handleZoom(e)
{
  if(e.mouseWheelChange < 0) 
  {
    map.vemapcontrol.ZoomOut(); 
    return true;
  };
  return false;
};

//Overrides VE ZoomOUt function
function OverrideMDNMapControlZoomout()
{
  map.vemapcontrol.ZoomOut = function(){
    if(map.GetZoomLevel() - ZOOM_LEVEL == 1){
      fullExtent();
    return true;
    }else if(map.GetZoomLevel() > ZOOM_LEVEL){
      map.SetZoomLevel(map.GetZoomLevel() - 1);
    }else{
      alert("Zooming out of Seattle is Prohibited.")
    return true;
    };
  };
  return false;
}

//RETURNS THE VALUE OF A NAMED PARAMETER IN A QUERY STRING
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(parent.location.href);
    if (results == null) {
        return "";
    }
    else {
        return results[1];
    }
}

//Sets the zoom images to proper state
function setZoomControls() {
    if (map.GetZoomLevel() >= ZOOM_LEVEL + 8) {
        $('navZoomIn').src = "../images/navZoomIn_off.gif";
    }
    else if (map.GetZoomLevel() >= ZOOM_LEVEL + 3 && gup("tabId") == 3) {
        $('navZoomIn').src = "../images/navZoomIn_off.gif";
    }
    else {
        $('navZoomIn').src = "../images/navZoomIn.gif";
    }
    if (map.GetZoomLevel() <= ZOOM_LEVEL) {
        $('navZoomOut').src = "../images/navZoomOut_off.gif";
    } else {
        $('navZoomOut').src = "../images/navZoomOut.gif";
    }
}

//Handles the road/aerial/Hybrid control
function setMapStyle(whatStyle)
{
  var style = map.GetMapStyle();	
  if (style!=VEMapStyle.Road && whatStyle=="Road")
  {
    clearNav();
    map.SetMapStyle(VEMapStyle.Road);
    $('navImgRoad').src="../images/navRoad_on.gif";
  }
  else if(style!=VEMapStyle.Aerial && whatStyle=="Aerial")
  {
    clearNav();
    map.SetMapStyle(VEMapStyle.Aerial);
    $('navImgAerial').src="../images/navAerial_on.gif";
  }
  else if(style!=VEMapStyle.Hybrid && whatStyle=="Hybrid")
  {
	clearNav();
    map.SetMapStyle(VEMapStyle.Hybrid);
    $('navImgHybrid').src="../images/navHybrid_on.gif";
  }
}

function startZoom(){
//Deprecated function
}

// Sets all Road/Aerial/Hybrid buttons to off
function clearNav()
{
  $('navImgRoad').src="../images/navRoad_off.gif";
  $('navImgAerial').src="../images/navAerial_off.gif";
  $('navImgHybrid').src="../images/navHybrid_off.gif";
}

/*function IsMapExtentWithinCityLimits() {
    
    var mapCenter = map.GetCenter();
    var mapCenterX = mapCenter.Longitude;
    var mapCenterY = mapCenter.Latitude;

    if (MIN_LATITUDE <= mapCenterY && mapCenterY <= MAX_LATITUDE && MIN_LONGITUDE <= mapCenterX && mapCenterX <= MAX_LONGITUDE) {
        return true;
    }
    fullExtent();
    return false;

}*/

var MostRecentShapeID;
var MostRecentAreaName;

function GetInfo(e) {    
    if (e.elementID != null && e.elementID != "msftve_1000_200000_50000") {
        var pin = map.GetShapeByID(e.elementID);
        var areaID = pin.GetDescription().substring(pin.GetDescription().lastIndexOf("=") + 1);        
        var areaName = pin.GetTitle();        

        if (e.eventName == "onclick") {
            var statName = self.parent.document.getElementById('StatSelection_statList').options[self.parent.document.getElementById('StatSelection_statList').options.selectedIndex].text;
            if (self.parent.document.getElementById('botPanelMinMax').hasClassName('panelClosed')) {
                //self.parent.moveBotNav2(self.parent.document.getElementById('botPanelMinMax'));
            }
            MostRecentShapeID = areaID;
            MostRecentAreaName = areaName;
            //GroupControlPreLoad("StatGraph", "StatGraph11", "tr_reportStatGraph12", "tr_reportStatGraph11||tr_reportStatGraph12", "tb_reportStatGraph12", "tb_reportStatGraph11||tb_reportStatGraph12");            
            self.parent.RefreshBeatGraph(CurrentStatID, areaID, statName, areaName);
            
        }
    }
}