﻿// JScript File
window.moveTo(0, 0)
window.resizeTo(screen.width, screen.height - 30)

var arcgisWebApp;
//var isRTL = (document.documentElement.dir=="rtl"); // find out if the document has been set to right-to-left
var isIE = (Sys.Browser.agent == Sys.Browser.InternetExplorer);

/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>
Type.registerNamespace('ESRI.ADF.WebMappingApplication');
// function run at startup
function startUp() {
        // set window resize event handler
        $addHandler(window,"resize", arcgisWebApp.adjustMapSizeHandler); 
        arcgisWebApp.MapId = "Map1";
        arcgisWebApp.map = $find(arcgisWebApp.MapId);
        map = arcgisWebApp.map;
        var toolbar = $find("Toolbar1");
		if (map != null && toolbar!=null)
		{
			window.setTimeout("resetMapHistory();", 1500);
			map.set_mouseMode(2); //set initail map client action: 	Pan : 1,ZoomIn : 2,	ZoomOut : 3,
		}
        map.add_mouseMove(MapCoordsMouseMove);
}

function resetMapHistory() {
    map = arcgisWebApp.map;
    var toolbar = $find("Toolbar1"); 
	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 backForward(value) {
    map = $find("Map1");
    map.stepExtentHistory(value);
}

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); 
}

// Common webpage object for manipulating page elements
ESRI.ADF.WebMappingApplication.WebPage = function() {
    this.MapId = "Map1";
    this.map = null;
    this.Panel_Map = $get("Panel_Map");
    this.Panel_Right = $get("Panel_Right");
    this.Panel_Toolbar = $get("Toolbar1");
    this.Panel_Mapback = $get("Panel_MapBack");
    this.Panel_CopyRight = $get("Panel_CopyRight");
    this.Panel_Sales = $get("Panel_Sales");
    this.Panel_Banner = $get("Panel_Banner");
    this.Panel_Link = $get("Panel_Link");

    this.PanelTitleHeight = 20;
    this.PanelCopyRightHeight = 60;
    this.PanelSalesHeight = 240;
    this.BottomBannerHeight = 5;
    this.LeftPanelWidth = 10;
    this.RightPanelWidth = 320;
    this.MapBorderWidth = 10;
    this.ToolbarHeight = 35;
    this.BannerHeight = 0;
    this.WindowWidth = 500;
    this.LeftOffsetX = 0;
    this.RightOffsetX = 0
    this.NavigationLeft = 0;
    this.CopyrightTextLeft = 0;
    this.DefaultMinDockWidth = 125;
    this.MinDockWidth = this.DefaultMinDockWidth;
    this.MapLeft = 130;
    this.LastMapWidth = 512;
    this.LastMapHeight = 512;
    this.CurrentMapWidth = 512;
    this.CurrentMapHeight = 512;
    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 = "ZoomIn";
    this.lastMode = "ZoomIn";

    this.setPageElementSizes = function() {
        //        this.LeftPanelWidth = this.Panel_Left.clientWidth;
        this.RightPanelWidth = this.Panel_Right.clientWidth;
        this.ToolbarHeight = this.Panel_Toolbar.clientHeight;
//        this.PanelSalesHeight = this.Panel_Sales.clientHeight;
//        this.BannerHeight = this.Panel_Banner.clientHeight;

        // get browser window dimensions
        var sWidth = this.getPageWidth();
        var sHeight = this.getPageHeight();
        // set map display dimensions
        var mWidth = sWidth - this.RightPanelWidth - this.MapBorderWidth - this.MapBorderWidth - 25;
        var mHeight = sHeight - this.ToolbarHeight - this.MapBorderWidth - this.MapBorderWidth - this.PanelSalesHeight;
        if (mWidth < 512) mWidth = 512;
        if (mHeight < 512) mHeight = 512;
        var mBWidth = mWidth + 20;
        var mBHeight = mHeight + 20;

        //        this.MapLeft = this.LeftPanelWidth + this.MapBorderWidth;
        this.Panel_Map.style.width = mWidth + "px";
        this.Panel_Map.style.height = mHeight + "px";
        this.Panel_Mapback.style.width = mBWidth + "px";
        this.Panel_Mapback.style.height = mBHeight + "px";

        this.CurrentMapWidth = mWidth;
        this.CurrentMapHeight = mHeight;
        // set heights of left panel and toggle bar
        this.Panel_Right.style.height = mHeight + "px";
        this.Panel_CopyRight.style.width = mBWidth + "px";
        this.Panel_CopyRight.style.top = mBHeight + this.ToolbarHeight + 5 + "px";
        this.Panel_Sales.style.width = mBWidth + "px";
        this.Panel_Sales.style.top = mBHeight + this.ToolbarHeight + this.PanelCopyRightHeight + 2 + "px";
        this.Panel_Link.style.left = mBWidth + 20 + "px";

    }

    // function for adjusting element sizes when brower is resized
    this.adjustMapSize = function() {
        // set element widths 
        this.RightPanelWidth = this.Panel_Right.clientWidth;
        this.ToolbarHeight = this.Panel_Toolbar.clientHeight;
//        this.PanelSalesHeight = this.Panel_Sales.clientHeight;
//        this.BannerHeight = this.Panel_Banner.clientHeight;
        // get browser window dimensions 
        var sWidth = this.getPageWidth();
        var sHeight = this.getPageHeight();
        this.LastMapWidth = this.CurrentMapWidth;
        this.LastMapHeight = this.CurrentMapHeight;
        // calc dimensions needed for map
        var mWidth = sWidth - this.RightPanelWidth - this.MapBorderWidth - this.MapBorderWidth - 25;
        var mHeight = sHeight - this.ToolbarHeight - this.MapBorderWidth - this.MapBorderWidth - this.PanelSalesHeight;
        if (mWidth < 512) mWidth = 512;
        if (mHeight < 512) mHeight = 512;
        var mBWidth = mWidth + 20;
        var mBHeight = mHeight + 20;
        this.Panel_Map.style.width = mWidth + "px";
        this.Panel_Map.style.height = mHeight + "px";
        this.Panel_Mapback.style.width = mBWidth + "px";
        this.Panel_Mapback.style.height = mBHeight + "px";
        this.CurrentMapWidth = mWidth;
        this.CurrentMapHeight = mHeight;

        this.Panel_Right.style.height = mHeight + "px";
        this.Panel_CopyRight.style.width = mBWidth + "px";
        this.Panel_CopyRight.style.top = mBHeight + this.ToolbarHeight + 5 + "px";
        this.Panel_Sales.style.width = mBWidth + "px";
        this.Panel_Sales.style.top = mBHeight + this.ToolbarHeight + this.PanelCopyRightHeight + 2 + "px";
        this.Panel_Link.style.left = mBWidth + 20 + "px";

        // refresh the map 
        var m = $find(this.MapId);
        if (this.map != null)
            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;

    }

    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(); }


var scrollTop;
//Register Begin Request and End Request 
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
//Get The Div Scroll Position
function BeginRequestHandler(sender, args) {
    var m = document.getElementById('Panel_Sales');
    scrollTop = m.scrollTop;
}
//Set The Div Scroll Position
function EndRequestHandler(sender, args) {
    var m = document.getElementById('Panel_Sales');
    m.scrollTop = scrollTop;
} 