﻿// Javascript file for Web Mapping Application
var reloadTimer;
var newLoad = false;
var webMapAppCloseCallback = "";
var webMapAppCopyrightCallback = "";
var webMapAppCopyrightText = null;
var webMapAppLeftPanelWidth = 140;
var webMapAppRightPanelWidth = 160;
var webMapAppTopBannerHeight = 65;
var webMapAppLeftPanelHeight = 1000;
var webMapAppQueryPanelHeight = 115;
var webMapAppFooterHeight = 80;
var webMapAppMapDisplay = null;
var webMapAppRightPanel = null;
var webMapAppQueryPanel = null;
var webMapAppLeftPanel = null;
var webMapAppFooter = null;
var webMapAppScaleBar = null;
var webMapAppFirstScalebarPosition = true;
var webMapAppMapLeft = 140;

// function to set initial sizes of page elements
function setPageElementSizes() {
    // set body style 
    if (document.documentElement) {
        document.documentElement.style.overflow = "hidden";
        document.documentElement.style.height = "100%"; 
    } else {
        document.body.style.overflow = "hidden";
        document.body.style.height = "100%";
    }  
    // get necessary elements
    webMapAppMapDisplay = document.getElementById("Map_Panel");
    webMapAppQueryPanel = document.getElementById("Query_Panel");
    webMapAppLeftPanel = document.getElementById("LeftPanel");
    webMapAppRightPanel = document.getElementById("RightPanel"); 
    webMapAppFooter = document.getElementById("Footer");     
    webMapAppScaleBar = document.getElementById("ScaleBar1");
    webMapAppCopyrightText = document.getElementById("CopyrightTextHolder");
    var headerDisplay = document.getElementById("PageHeader");
    if (isIE) {
        webMapAppLeftPanel.style.overflow = "hidden";
    } 
    // get the set widths and heights
    webMapAppRightPanelWidth = webMapAppRightPanel.clientWidth;
    webMapAppLeftPanelWidth = webMapAppLeftPanel.clientWidth;
    webMapAppQuerytPanelHeight = webMapAppQueryPanel.clientHeight;     
    webMapAppFooterHeight = webMapAppFooter.clientHeight; 
    webMapAppTopBannerHeight = headerDisplay.clientHeight;          
    // get browser window dimensions
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    // set map display dimensions
    var mWidth = sWidth - webMapAppLeftPanelWidth - webMapAppRightPanelWidth;
    var mHeight = sHeight - webMapAppTopBannerHeight - webMapAppFooterHeight - webMapAppQuerytPanelHeight;
    webMapAppMapLeft = webMapAppLeftPanelWidth;
    webMapAppMapDisplay.style.width =  mWidth + "px";
    webMapAppMapDisplay.style.height = mHeight  + "px";
    if (webMapAppScaleBar!=null) {
        //webMapAppScaleBar.style.width = "auto";
        //webMapAppScaleBar.style.height = "auto";       
        positionScalebar();
    } 
    if (webMapAppCopyrightText!=null) {
        webMapAppCopyrightText.onmousedown = webMapAppGetCopyrightText;
        var crtHeight = webMapAppCopyrightText.clientHeight;
        webMapAppCopyrightText.style.left = (webMapAppMapLeft + 10) + "px";
        webMapAppCopyrightText.style.top = (sHeight - crtHeight - 10) + "px";
    }
    // set heights of left panel and right panel
    webMapAppLeftPanel.style.height = webMapAppLeftPanelHeight;
    webMapAppRightPanel.style.height = sHeight - webMapAppTopBannerHeight - webMapAppFooterHeight;
    // set query panel width and position
    webMapAppQueryPanel.style.width = mWidth + "px";
    webMapAppQueryPanel.style.top = mHeight + "px";
    // set footer width and position
    webMapAppFooter.style.width = sWidth - webMapAppLeftPanelWidth + "px";
    webMapAppFooter.style.top =  mHeight + webMapAppQueryPanelHeight + "px";
}

// function for adjusting element sizes when brower is resized
function AdjustMapSize() {
    // set element widths 
    webMapAppLeftPanel.style.width =  webMapAppLeftPanelWidth + "px";
    // get browser window dimensions 
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();    
    // calc dimensions needed for map
    var mWidth = sWidth - webMapAppLeftPanel.clientWidth - webMapAppRightPanel.clientWidth;
    var mHeight = sHeight - webMapAppTopBannerHeight -  webMapAppFooterHeight - webMapAppQuerytPanelHeight;
    if (mWidth<5) mWidth = 5;
    if (mHeight<5) mHeight = 5;  
    webMapAppMapDisplay.style.width =  mWidth + "px";
    webMapAppMapDisplay.style.left =  webMapAppLeftPanel.clientWidth + "px";    
    // set heights on elements 
    webMapAppMapDisplay.style.height = mHeight  + "px";    
    if (webMapAppScaleBar!=null) {
        var sbWidth = webMapAppScaleBar.clientWidth;
        var sbHeight = webMapAppScaleBar.clientHeight;
        if (webMapAppFirstScalebarPosition) {
            window.setTimeout("positionScalebar();", 1000);
            webMapAppFirstScalebarPosition = false; 
        } else
            positionScalebar(); 
    }    
    // resize the map 
    window.setTimeout("resizeTheMap(" + mWidth + ", " + mHeight + ", false);", 500);    
    // update map properties     
    var box = calcElementPosition("Map_Panel"); 
    map.containerLeft = box.left;
	map.containerTop = box.top;
    if (webMapAppCopyrightText!=null) {
        var crtHeight = webMapAppCopyrightText.clientHeight;
        webMapAppCopyrightText.style.left = (box.left + 10) + "px";
        webMapAppCopyrightText.style.top = (sHeight - crtHeight - 10) + "px";
    }    
    // set heights of left panel and right panel
    webMapAppLeftPanel.style.height = webMapAppLeftPanelHeight;
    webMapAppRightPanel.style.height = sHeight - webMapAppTopBannerHeight - webMapAppFooterHeight;    
    // set query panel width and position
    webMapAppQueryPanel.style.width = mWidth + "px";
    webMapAppQueryPanel.style.top = mHeight + "px";    
    // set footer width and position
    webMapAppFooter.style.width = sWidth - webMapAppLeftPanelWidth + "px";
    webMapAppFooter.style.top = mHeight + webMapAppQueryPanelHeight + "px";        
    return false;
}

// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {
    if (resizeExtent==null) resizeExtent = true;
    map.resize(width, height, resizeExtent);
    return false; 
}

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustMapSize();",1000);
}

function positionScalebar() {
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    var sbWidth = webMapAppScaleBar.clientWidth;
    var sbHeight = webMapAppScaleBar.clientHeight;
    webMapAppScaleBar.style.left = (sWidth - sbWidth - webMapAppRightPanel.clientWidth - 10) + "px";
    webMapAppScaleBar.style.top = (sHeight - sbHeight - webMapAppFooterHeight - webMapAppQuerytPanelHeight - 20) + "px";
}

// function run at startup
function startUp() {
    // set up identify mode for javascript
    map.ctrlMode = "MapIdentify";
    map.ctrlAction = "Point";
    map.ctrlCursor = "pointer";
    map.ctrlFunction = "MapIdClick(e)";
    if (newLoad) {
        // execute only on intial load.... not callbacks
        map.divObject.style.cursor = "wait";
        // move magnifier toolbar to top left corner of map display 
        var box = calcElementPosition("Map_Panel"); 
        var mag = document.getElementById("Magnifier1");
        if (mag!=null && typeof(esriMagnifiers)!="undefined" && esriMagnifiers!=null) 
        { 
            floatingPanel = esriMagnifiers["Magnifier1"].floatingPanel; 
            if (floatingPanel!=null) moveTo(box.left, box.top);
        }
    	var fp = document.getElementById("CopyrightText_Panel");
	    if (fp!=null) 
	    {
		 floatingPanel = fp;
		 moveTo(box.left, box.top);
		}
        if (webMapAppScaleBar!=null) window.setTimeout("positionScalebar();", 1000);
    } 
    // set window resize event handler
    window.onresize = AdjustMapSizeHandler;  
}  

// function to request closing of session items.... only called if at least one resource is local non-pooled
function CloseOut() {
	var argument = "ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication";
	var context = map.controlName;
	eval(webMapAppCloseCallback);
}

// response function to close out browser ... request sent to server by CloseOut()
function CloseOutResponse(response, context) {
    window.close(); 
    // if user selects Cancel in close dialog, send to close page 
    document.location = response; 
}

function OpenWindow(url) {
    window.open(url);
}

function webMapAppGetCopyrightText() {
	var argument = "ControlID=Map1&ControlType=Map&EventArg=GetCopyrightText";
	var context = map.controlName;
	eval(webMapAppCopyrightCallback);
	showFloatingPanel('CopyrightText_Panel');
}