﻿//LJA_map.js

/// <reference assembly="ESRI.ArcGIS.ADF.Web.UI.WebControls" name="ESRI.ArcGIS.ADF.Web.UI.WebControls.Runtime.JavaScript.references.js" />

var LJAMaptipId = "LJAMaptip";
var LJA_MapTipDiv_LastVisibile;

var ljaMaxLevel;

/********************************
// Map Panning
/********************************/

var lja_Left;
var lja_Right;
var lja_Top;
var lja_Bottom;

var lja_PanX = true;
var lja_PanY = true;

function LJA_moveContainerDiv(x, y) {
    map = $find("Map1");

    //ESRI.ADF.UI.Map map = new ESRI.ADF.UI.Map(null);
    var extent = map.get_extent();

    Sys.Debug.traceDump(lja_Left, "lja_Left");
    Sys.Debug.traceDump(lja_Top, "lja_Top");
    Sys.Debug.traceDump(lja_Right, "lja_Right");
    Sys.Debug.traceDump(lja_Bottom, "lja_Bottom");
    
    //minimums in screen coord. system, not spatial system
    var extScreenpoint_min = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        extent.get_xmin(), extent.get_ymax(), map.get_spatialReference()));
    //maximums in screen coord. system, not spatial system
    var extScreenpoint_max = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        extent.get_xmax(), extent.get_ymin(), map.get_spatialReference()));

    Sys.Debug.traceDump(extent, "Map extent");
    
    //minimums in screen coord. system, not spatial system
    var ljaScreenpoint_min = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        lja_Left, lja_Top, map.get_spatialReference()));
    //maximums in screen coord. system, not spatial system
    var ljaScreenpoint_max = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        lja_Right, lja_Bottom, map.get_spatialReference()));

    Sys.Debug.traceDump(ljaScreenpoint_min, "LJA Min");
    Sys.Debug.traceDump(ljaScreenpoint_max, "LJA Max");

    var lja_MinX = ljaScreenpoint_min.offsetX;
    var lja_MaxX = ljaScreenpoint_max.offsetX;
    var lja_MinY = ljaScreenpoint_min.offsetY;
    var lja_MaxY = ljaScreenpoint_max.offsetY;

    var new_MinX = extScreenpoint_min.offsetX + x;
    var new_MaxX = extScreenpoint_max.offsetX + x;
    var new_MinY = extScreenpoint_min.offsetY + y;
    var new_MaxY = extScreenpoint_max.offsetY + y;

    var newX=x;
    var newY = y;
    var extScreenpoint_min_offsetX = extScreenpoint_min.offsetX;
    var extScreenpoint_max_offsetX = extScreenpoint_max.offsetX;
    var extScreenpoint_min_offsetY = extScreenpoint_min.offsetY;
    var extScreenpoint_max_offsetY = extScreenpoint_max.offsetY;
    
    Sys.Debug.traceDump(extScreenpoint_min, "Map min");
    Sys.Debug.traceDump(extScreenpoint_max, "Map max");
    Sys.Debug.traceDump(ljaScreenpoint_min, "LJA min");
    Sys.Debug.traceDump(ljaScreenpoint_max, "LJA max");
    
    if (!lja_PanX) {
        newX = 0;
    }
    else if (new_MinX <= lja_MinX && new_MaxX >= lja_MaxX) {
        newX = 0;
        lja_PanX = false;
    }
    else if (new_MinX <= lja_MinX) {
        newX = lja_MinX - extScreenpoint_min_offsetX;
    }
    else if (new_MaxX >= lja_MaxX) {
        newX = lja_MaxX - extScreenpoint_max_offsetX;
    }

    if (!lja_PanY) {
        newY = 0;
    }
    else if (new_MinY <= lja_MinY && new_MaxY >= lja_MaxY) {
        newY = 0;
        lja_PanY = false;
    }
    else if (new_MinY <= lja_MinY) {
        newY = lja_MinY - extScreenpoint_min.offsetY;
    }
    else if (new_MaxY >= lja_MaxY) {
        newY = lja_MaxY - extScreenpoint_max.offsetY;
    }

    Sys.Debug.traceDump(newX, "Move X");
    Sys.Debug.traceDump(newY, "Move Y");
    
    if (newX != 0 || newY != 0) {
        this._containerDivPos[0] -= newX;
        this._containerDivPos[1] -= newY;
        this._containerDiv.style.left = this._containerDivPos[0] + 'px';
        this._containerDiv.style.top = this._containerDivPos[1] + 'px';
        if (this._extent) {
            this._extent._xmin += newX * this._pixelsizeX;
            this._extent._xmax += newX * this._pixelsizeX;
            this._extent._ymin -= newY * this._pixelsizeY;   //in spatial system, Y is opposite
            this._extent._ymax -= newY * this._pixelsizeY;   //in spatial system, Y is opposite 
        }
    }
}




/********************************
// Map Tips
/********************************/

//AddMaptipDiv
function AddMaptipDiv() {

    map = $find("Map1");
    var mapContainerDiv = map._containerDiv;
    var mapAnnotationDiv = map._annotationDiv;   //annotation div has z-index of 100;
    if (mapContainerDiv == null)
        return;


    //add top level div which holds all maptips divs
    var divPanel = document.getElementById(LJAMaptipId);
    if (!divPanel) {
        var maptipDiv = document.createElement('div');
        maptipDiv.setAttribute('id', LJAMaptipId);
        maptipDiv.style.zIndex = 101;               //Brandon says "this is the right way to set style attributes"
        maptipDiv.style.position = 'absolute';
        //        mapContainerDiv.appendChild(maptipDiv);
        mapContainerDiv.insertBefore(maptipDiv, mapAnnotationDiv);
        divPanel = document.getElementById(LJAMaptipId);
    }

    //MoveMapTipsDiv
//    MoveMapTipsDiv();
}

//MoveMapTipsDiv
function MoveMapTipsDiv() {

    map = $find("Map1");
    var mapContainerDiv = map._containerDivPos;
    if (mapContainerDiv != null) {
        var left = mapContainerDiv[0];
        var top = mapContainerDiv[1];

        var maptip = document.getElementById(LJAMaptipId);
        if (maptip != null) {
            var newleft = -left-12;     //-10 so the maptip center will center with the symbology
            var newtop = -top-12;

            maptip.style.left = '' + newleft + "px";
            maptip.style.top = '' + newtop + "px";
        }
    }
}

//ToggleLJAMaptipDiv
function ToggleLJAMaptipDiv(toggleTo) {
    if (toggleTo.toLowerCase() == "on")
        showMaptipPanelByName(LJAMaptipId)
    else
        hideMaptipPanelByName(LJAMaptipId);
}

//show maptips div by name
function showMaptipPanelByName(divName) {
    var tmp = "showMaptipPanelWithTimeoutByName('" + divName + "')";
    window.setTimeout(tmp, 1);   //this helps to hide unwanted intermediate maptip results when switching zoom levels
}

function showMaptipPanelWithTimeoutByName(divName) {
    MoveMapTipsDiv();   //double safety

    //show maptips div by name
    var mapTipPanel = document.getElementById(divName);
    if (mapTipPanel) {
        mapTipPanel.style.display = '';
    }
}

//hide maptips div by name
function hideMaptipPanelByName(divName) {
    //hide maptips panel by name
    var mapTipPanel = document.getElementById(divName);
    if (mapTipPanel)
        mapTipPanel.style.display = 'none';
}

//IsLJAMaptipDivVisible
function IsLJAMaptipDivVisible() {
    var mapTipPanel = document.getElementById(LJAMaptipId);
    if (mapTipPanel) {
        if (mapTipPanel.style.display != null && mapTipPanel.style.display == '')
            return true;
        else
            return false;
    }
}

/********************************
// Zooming Handler
/********************************/

function LJA_ZoomStart() {
    LJA_MapTipDiv_LastVisibile = false;
    if (IsLJAMaptipDivVisible) {
        ToggleLJAMaptipDiv("off");
        LJA_MapTipDiv_LastVisibile = true;
        var divPanel = document.getElementById(LJAMaptipId);
//        if (divPanel != null)
//            divPanel.innerHTML = "";
    }
}

function LJA_ZoomEnd() {
    if (LJA_MapTipDiv_LastVisibile)
        ToggleLJAMaptipDiv("on");
    else
        ToggleLJAMaptipDiv("off");
}

/********************************
// Navigation Control Handler
/********************************/

//LJA_OnNavigationControlPanMap
function LJA_OnNavControlPanMap() {

    var map = this._map;
    if (map == null) return;

    //ESRI.ADF.UI.Map map = new ESRI.ADF.UI.Map(null);
    var extent = map.get_extent();
    var MoveX = true;
    var MoveY = true;
    //ESRI.ADF.Geometries.Envelope env = null;

    var extScreenpoint1 = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        extent.get_xmax(), extent.get_ymax(), map.get_spatialReference()));
    var extScreenpoint2 = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        extent.get_xmin(), extent.get_ymin(), map.get_spatialReference()));
    var ljaScreenpoint1 = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        lja_Right, lja_Top, map.get_spatialReference()));
    var ljaScreenpoint2 = map.toScreenPoint(new ESRI.ADF.Geometries.Point(
        lja_Left, lja_Bottom, map.get_spatialReference()));

    if (this._moveX == 0) MoveX = false;
    else {
        // moveY > 0 (up) | moveY < 0 (down) | moveX==1 (right) | moveX==-1 (left)
        if (this._moveX == 1 && (extScreenpoint1.offsetX + this._moveX * this._speed > ljaScreenpoint1.offsetX))
            MoveX = false;
        else if (this._moveX == -1 && (extScreenpoint2.offsetX + this._moveX * this._speed < ljaScreenpoint2.offsetX))
            MoveX = false;
    }

    if (this._moveY == 0) MoveY = false;
    else {
        if (this._moveY == -1 && (extScreenpoint1.offsetY + this._moveY * this._speed < ljaScreenpoint1.offsetY))
            MoveY = false;
        else if (this._moveY == 1 && (extScreenpoint2.offsetY + this._moveY * this._speed > ljaScreenpoint2.offsetY))
            MoveY = false;
    }

    if (MoveX && MoveY)
        this._map._doContinuousPan(this._moveX * this._speed, this._moveY * this._speed);
    else if (MoveX)
        this._map._doContinuousPan(this._moveX * this._speed, 0);
    else if (MoveY)
        this._map._doContinuousPan(0, this._moveY * this._speed);

}


/********************************
// MouseWheel Handler
/********************************/

//LJA_onMouseWheel
function LJA_onMouseWheel(e) {

    //9.3 min added (05/05)
    // ---------------------------------------------------------------
    map = $find("Map1");
    var curLevel = map.get_layers().getLevelByNearestPixelsize(map._pixelsizeX);
    var minLevel = map.get_layers().get_levels().length - 1;

    //e.wheelDelta < 0 (scroll-down zooms out)
    if ((e.wheelDelta < 0 && curLevel <= ljaMaxLevel)
        || (e.wheelDelta > 0 && curLevel >= minLevel))
        return;
    // ---------------------------------------------------------------

    this._mouseDragState = null;
    //if(this._mouseMode !== ESRI.ADF.UI.MouseMode.Custom) {
    if (e.target.namespaceURI && e.target.namespaceURI === 'http://www.w3.org/2000/svg') {
        //Workaround getLocation bug in MS Ajax when mousewheel'ing on SVG graphics
        e.offsetX += parseInt(this._assotateCanvas._svgRoot.style.left, 10);
        e.offsetY += parseInt(this._assotateCanvas._svgRoot.style.top, 10);
        e.target = this._assotateCanvas._svgRoot.parentNode;
    }
    e = this._makeMouseEventRelativeToMap(e, true);
    var now = new Date();
    //Prevent zooming too many times too fast
    if (!this._lastScrollZoomDate || (now - this._lastScrollZoomDate) > 150) {
        if (Sys.Browser.agent === Sys.Browser.InternetExplorer && this._animationSettings.enableFadeTransition && !this._animationSettings.disableNavigationAnimation && this._layersDiv.filters[0] && this._layersDiv.filters[0].status === 1) {
            return;
        }

        this.zoom(this._getZoomFactor(e.wheelDelta * this._mouseWheelDirection > 0), e.coordinate);
        this._lastScrollZoomDate = now;

    }
}
