﻿// JScript File
var arcgisWebApp;
var isIE = (Sys.Browser.agent == Sys.Browser.InternetExplorer);

/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>
Type.registerNamespace('ESRI.ADF.WebMappingApplication');

function startUp() {
    arcgisWebApp.MapId = "Map1";
    arcgisWebApp.map = $find(arcgisWebApp.MapId);
    map = arcgisWebApp.map;
    arcgisWebApp.adjustMapSize();
//    map.add_extentChanging(function() { if (arcgisIdentifyTool != null) closeIdentifyPanel(); }); // for start of continuous panning or zoom animation
//    map.add_extentChanged(function() { if (arcgisIdentifyTool != null) closeIdentifyPanel(); });  // extent changes that do not trigger ExentChanging
    arcgisWebApp.setTocHeight();
    if (arcgisWebApp.Toc == null) arcgisWebApp.TocPanel.style.display = "none";
    window.setTimeout('$addHandler(window,"resize", arcgisWebApp.adjustMapSizeHandler);', 1000);

    var toolbar = $find("Toolbar2");
    if (map != null && toolbar != null) {
       
        map.remove_extentChanged(toolbar._toolbarExtentChangedHandler); // just in case it is already present
        map.add_extentChanged(toolbar._toolbarExtentChangedHandler);
        toolbar.add_onToolSelected(OnToolSelectHandler);
        window.setTimeout("resetMapHistory();", 1500);
    }
}
function resetMapHistory() {
    map = arcgisWebApp.map;
    var toolbar = $find("Toolbar2");
    map._currentExtentHistory = 0;
    var tbElem = Toolbars[toolbar._uniqueID];
    var backButton = tbElem.items[tbElem.btnMapBack];
    if (backButton) { backButton.disabled = true; }
    var forwardButton = tbElem.items[tbElem.btnMapForward];
    if (forwardButton) { forwardButton.disabled = true; }
    tbElem.refreshCommands();
}
function OnToolSelectHandler(sender, args) {
   if (args.name) {
        var mode = args.name;
        arcgisWebApp.currentMode = mode;
        if (mode != "Mesure" && arcgisWebApp.lastMode == "Mesure") closeMeasureToolbarTool();
        if (mode != "Print" && arcgisWebApp.lastMode == "Print") closeMeasureToolbarTool();
        if (mode != "xy" && arcgisWebApp.lastMode == "xy") closeMeasureToolbarTool();
        if (mode != "MapIdentifyJck" && arcgisWebApp.lastMode == "MapIdentifyJck") closeMeasureToolbarTool();
        arcgisWebApp.lastMode = mode;
    }
}
 
function MapCoordsMouseMove(sender, args) {
    var coords = args.coordinate;
    var roundFactor = Math.pow(10, arcgisWebApp.CoordsDecimals);
    window.status = (Math.round(coords.get_x() * roundFactor) / roundFactor) + ", " + (Math.round(coords.get_y() * roundFactor) / roundFactor);
}

function GetElementRectangle(element) {
    var rect = null;
    if (isIE)
        rect = element.getBoundingClientRect();
    else {
        var bounds = Sys.UI.DomElement.getBounds(element);
        rect = { "left": bounds.x, "top": bounds.y, "right": bounds.x + bounds.width, "bottom": bounds.y + bounds.height };
    }
    return rect;
}

function getMapLeft() {
    var mLeft = 256;
    return mLeft;
}

function getMapWidth() {
    var mLeft = getMapLeft();
    var pWidth = arcgisWebApp.getPageWidth();
    var mWidth = pWidth - mLeft - 10;
    if (mWidth < 5) mWidth = 5;
    return mWidth;
}

function setMapWidth() {
    var mLeft = getMapLeft();
    arcgisWebApp.MapDisplay.style.left = mLeft + "px";
    var mWidth = getMapWidth();
    if (mWidth < 5) mWidth = 5;
    arcgisWebApp.MapDisplay.style.width = mWidth + "px";
    arcgisWebApp.CurrentMapWidth = mWidth;
}

function setDividerLeftPosition() {
    var mWidth = getMapWidth();
    arcgisWebApp.MapDisplay.style.left = "0px";
    arcgisWebApp.PanelDisplayCell.style.left = mWidth + "px";
    arcgisWebApp.MapDisplay.style.width = mWidth + "px";
    arcgisWebApp.CurrentMapWidth = mWidth;
}


function adjustMapAndCopyrightPosition(adjustMapDisplay) {
    if (adjustMapDisplay == null) adjustMapDisplay = false;
    var bounds = Sys.UI.DomElement.getBounds(arcgisWebApp.PanelDisplay);
    var mLeft = bounds.x + bounds.width;
    if (arcgisWebApp.CopyrightText) {
        var crLeft = parseInt(arcgisWebApp.CopyrightText.style.left);
        arcgisWebApp.CopyrightText.style.left = mLeft + "px";
    }
    if (adjustMapDisplay) arcgisWebApp.MapDisplay.style.left = mLeft + "px";
}


// Common webpage object for manipulating page elements
ESRI.ADF.WebMappingApplication.WebPage = function () {
    this.MapId = "Map1";
    this.map = null;
    this.MapDisplay = $get("Map_Panel");
    this.PanelDisplay = $get("LeftPanelCellDiv");
    this.PanelDisplayCell = $get("LeftPanelCell");
    this.PanelScrollDiv = $get("LeftPanelScrollDiv");
    this.PanelDisplayTableCell = $get("LeftPanelTableCell");
    this.ResultsPanel = $get("Results");
    this.TocPanel = $get("Toc_Panel");
    this.LegPanel = $get("Leg_Panel");
    this.RecherchePanel = $get("Recherche_Panel");
    this.Results = $get("TaskResults1");
    this.Toc = $get("Toc1");
    this.TocPanelContents = $get("Toc_Panel_Body");
    this.LegPanelContents = $get("Leg_Panel_Body");
    this.NavigationTool = $get("Navigation1");
    this.CopyrightText = $get("Copyright_Panel");
    this.ZoomLevel = $get("ZoomLevel1");

    this.LeftPanelWidth = 256;
    this.TopBannerHeight = 80;
    this.WindowWidth = 500;
    this.LeftOffsetX = 0;
    this.RightOffsetX = 0
    this.NavigationLeft = 0;
    this.CopyrightTextLeft = 0;
    this.DefaultMinDockWidth = 125;
    this.MinDockWidth = this.DefaultMinDockWidth;
    this.MapLeft = 256;
    this.LastMapWidth = 256;
    this.LastMapHeight = 256;
    this.CurrentMapWidth = 256;
    this.CurrentMapHeight = 256;
    this.HasScroll = false;
    this.LastHasScroll = false;
    this.DockMoving = false;
    this.reloadTimer = null;
    this.hasMeasure = false;
    this.Measure = null;
    this.MapUnits = null;
    this.CoordsDecimals = 3;
    this.currentMode = "Pan";
    this.lastMode = "Pan";

    this.setPageElementSizes = function () {
        if (document.documentElement) {
            document.documentElement.style.overflow = "hidden";
            document.documentElement.style.height = "100%";
            this.PanelDisplay.style.overflow = "hidden";
        }
        else {
            document.body.style.overflow = "hidden";
            document.body.style.height = "100%";
        }
        this.PanelScrollDiv.style.overflow = "hidden";
        var headerDisplay = $get("PageHeader");
        var linkDisplay = $get("LinkBar");
        this.TopBannerHeight = headerDisplay.clientHeight + linkDisplay.clientHeight;
        // get browser window dimensions
        var sWidth = this.getPageWidth();
        var sHeight = this.getPageHeight();
        // set map display dimensions
        var mHeight = sHeight - this.TopBannerHeight;
        this.MapLeft = this.LeftPanelWidth; //+ this.ToggleWidth
        var mWidth = getMapWidth();
        this.MapDisplay.style.width = mWidth + "px";
        this.MapDisplay.style.height = mHeight + "px";
        this.CurrentMapWidth = mWidth;
        this.CurrentMapHeight = mHeight;
        // set heights of left panel and toggle bar
        this.PanelScrollDiv.style.height = mHeight + "px";
        esriMaxFloatingPanelDragRight = sWidth - 15;
        esriMaxFloatingPanelDragBottom = sHeight - 15;
        var widthString = this.LeftPanelWidth + "250px";
        this.TocPanel.style.width = "250px";
        this.LegPanel.style.width = "250px";
    }

    // function for adjusting element sizes when brower is resized
    this.adjustMapSize = function () {
        // set element widths 
        this.PanelDisplay.style.width = this.LeftPanelWidth + "px";
        // get browser window dimensions 
        var sWidth = this.getPageWidth();
        var sHeight = this.getPageHeight();
        var mHeight = sHeight - this.TopBannerHeight - 8;
        this.LastMapWidth = this.CurrentMapWidth;
        this.LastMapHeight = this.CurrentMapHeight;
        this.MapDisplay.style.height = mHeight + "px";
        this.PanelScrollDiv.style.height = mHeight + "px";
        this.CurrentMapHeight = mHeight;
        this.setTocHeight();
        // calc dimensions needed for map
        var mWidth = getMapWidth();
        if (mWidth < 50) mWidth = 50;
        if (mHeight < 50) mHeight = 50;
        setMapWidth();
        // set heights on elements 
        var widthString = this.PanelDisplay.style.width;
        this.TocPanel.style.width = "250px";
        this.TocPanelContents.style.width = "250px";
        this.LegPanel.style.width = "250px";
        this.LegPanelContents.style.width = "250px";
        if (this.LastMapWidth != this.CurrentMapWidth || this.LastMapHeight != this.CurrentMapHeight) {
            var cr = $find("MapCopyrightText1");
            if (cr != null)
                cr.get_callout().hide();
            if (arcgisIdentifyTool != null) closeIdentifyPanel();
        }
        // refresh the map 
        var m = $find(this.MapId);
        if (this.map != null) {
            if (this.LastMapWidth != this.CurrentMapWidth || this.LastMapHeight != this.CurrentMapHeight) {
                this.map.checkMapsize();
            }
        } else
            window.setTimeout("arcgisWebApp.adjustMapSize();", 1000);
        return false;
    }

    // handler for window resize
    this.adjustMapSizeHandler = function (e) {
        window.clearTimeout(arcgisWebApp.reloadTimer);
        arcgisWebApp.reloadTimer = window.setTimeout("arcgisWebApp.adjustMapSize();", 1000);
    }

    // get the page width
    this.getPageWidth = function () {
        var width = window.innerWidth;
        if (width == null) {
            if (document.documentElement && document.documentElement.clientWidth)
                width = document.documentElement.clientWidth
            else
                width = document.body.clientWidth;
        }
        return width;
    }


    //get the page height
    this.getPageHeight = function () {
        var height = window.innerHeight;
        if (height == null) {
            if (document.documentElement && document.documentElement.clientHeight)
                height = document.documentElement.clientHeight;
            else
                height = document.body.clientHeight;
        }
        return height;
    }


    // sets the height of the Map Contents panel
    this.setTocHeight = function () {
        var tocLocation = Sys.UI.DomElement.getLocation(this.TocPanelContents);
        var leftLocation = Sys.UI.DomElement.getLocation(this.PanelDisplayCell);
        var pHeight = this.getPageHeight();
        var tHeight = pHeight - tocLocation.y - 21;
        if (isNaN(tHeight) || tHeight < 5) tHeight = 5;
        this.TocPanelContents.style.height = tHeight + "px";
    }

    ESRI.ADF.WebMappingApplication.WebPage.initializeBase(this);
}

ESRI.ADF.WebMappingApplication.WebPage.registerClass('ESRI.ADF.WebMappingApplication.WebPage');

arcgisWebApp = new ESRI.ADF.WebMappingApplication.WebPage();

if (typeof (Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }

