﻿//WebMapApp.js

/// <reference assembly="ESRI.ArcGIS.ADF.Web.UI.WebControls" name="ESRI.ArcGIS.ADF.Web.UI.WebControls.Runtime.JavaScript.references.js" />

//IncludeJavaScript
function IncludeJavaScript(jsFile) {
    document.write('<script type="text/javascript" src="' + jsFile + '"></script>');
}
IncludeJavaScript('JavaScript/LJA_ZoomLevel.js'); 

var reloadTimer;
var webMapAppLeftPanelWidth = 262;
var webMapAppToggleWidth = 10;
var webMapAppTopBannerHeight = 100;
var newLoad = false;
var webMapAppCloseCallback = "";
var webMapAppCopyrightCallback = "";
var webMapAppMoveFunction = null;
var webMapAppMapDisplay = null;
var webMapAppPanelDisplay = null;
var webMapAppPanelDisplayCell = null;
var webMapAppPanelDisplayTableCell = null;
var webMapAppPanelScrollDiv = null;
var webMapAppToggleDisplay = null;
var webMapAppToggleHoverDisplay = null;
var webMapAppSpacerDiv = null;
var webMapAppPanelBottomSlider = null;
//var webMapAppScaleBar = null;
var webMapAppCopyrightText = null;
var webMapAppWindowWidth = 500;
var webMapAppLeftOffsetX = 0;
var webMapAppRightOffsetX = 0
var webMapAppDefaultMinDockWidth = 250;
var webMapAppMinDockWidth = webMapAppDefaultMinDockWidth;
var webMapAppMaxDockWidth = null;
var webMapAppMapLeft = 262;
var webMapAppHasScroll = false;
var webMapAppLastHasScroll = false;
var webMapAppFirstScalebarPosition = true;
var webMapAppDockMoving = false;
var m_measureToolbarId = "";
var webMapAppWindowHeight = 0;
var showPanel = 0;
var bottomTextArea = 0;
var logoInterval;

// 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");
        webMapAppPanelDisplay = document.getElementById("LeftPanelCellDiv");
        webMapAppPanelDisplayCell = document.getElementById("LeftPanelCell");
        webMapAppPanelScrollDiv = document.getElementById("LeftPanelScrollDiv");
        webMapAppToggleDisplay = document.getElementById("ToggleCell");
        webMapAppToggleHoverDisplay = document.getElementById("ToggleCellHover");
        webMapAppPanelSlider = document.getElementById("PanelSlider");
        webMapAppPanelDisplayTableCell = document.getElementById("LeftPanelTableCell");
        webMapAppPanelBottomSlider = document.getElementById("PanelSliderBottom");
        var headerDisplay = document.getElementById("PageHeader");
        var linkDisplay = document.getElementById("LinkBar");
        // set scroll on Dock
        webMapAppPanelScrollDiv.style.overflowY = "auto";
        if (isIE) {
            webMapAppPanelDisplay.style.overflow = "hidden";
        } 
        // get the set widths and heights
        webMapAppLeftPanelWidth = webMapAppPanelDisplay.clientWidth;
        webMapAppToggleWidth = parseInt(webMapAppToggleDisplay.style.width);
        webMapAppTopBannerHeight = headerDisplay.clientHeight;
        // get browser window dimensions
        var sWidth = getWinWidth();
        var sHeight = getWinHeight();
        // get body margins
        var bWidth = parseInt(document.body.style.marginLeft) + parseInt(document.body.style.marginRight);
        // set map display dimensions
        var mWidth = sWidth - webMapAppLeftPanelWidth - webMapAppToggleWidth - 2 - bWidth - 16 - 2; //-2 padding on togglewidth, -16 for margins on both ends, -2 border
        var mHeight = sHeight - webMapAppTopBannerHeight - 8 - 2 - bottomTextArea; //-8 for margins on bottom, -2 border
        webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;
        webMapAppMapDisplay.style.width =  mWidth + "px";
        webMapAppMapDisplay.style.height = mHeight  + "px";
        
        // set heights of left panel and toggle bar
        var leftHeight = mHeight + 2;
        var hoverHeight = leftHeight - 10;
        webMapAppToggleDisplay.style.height = leftHeight + "px";
        webMapAppToggleHoverDisplay.style.height = hoverHeight + "px";
        webMapAppPanelScrollDiv.style.height = leftHeight  + "px";
        esriMaxFloatingPanelDragRight = sWidth - 15;
        esriMaxFloatingPanelDragBottom = sHeight - 15;
        //SetTocFullHeight(leftHeight);
        // Save Browser window dimensions
        webMapAppWindowWidth = getWinWidth();
        webMapAppWindowHeight = getWinHeight();
}

function SetTocFullHeight(mapHeight)
{
    var newHeight = mapHeight - 40;
    var TocBody = document.getElementById('Toc_Panel_BodyCell');
    var TocResize = document.getElementById('Toc_Panel_SideResizeCell');
    TocBody.style.height = newHeight + "px";
    TocResize.height = newHeight + "px";
}

// function to toggle Dock visibility
function togglePanelDock() {
    if (webMapAppPanelDisplay.style.display=="none") {
        expandPanelDock();
    } else {
        collapsePanelDock();
    }     
}

function expandPanelDock() {
    var image = document.images["CollapseImage"];
    webMapAppPanelDisplay.style.display = "block";
    image.src = "images/collapse_left.gif";
    image.alt = "Collapse";
    webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";
    AdjustMapSize(); 
    //StartSponsorScroll();
}

function collapsePanelDock() {
    var image = document.images["CollapseImage"];
    dockWidthString = webMapAppPanelDisplayCell.clientWidth + "px";
    webMapAppPanelDisplay.style.display = "none";
    image.src = "images/expand_right.gif";
    image.alt = "Expand"; 
    webMapAppMapLeft = webMapAppToggleWidth; 
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";  
    AdjustMapSize();   
}  

// function for adjusting element sizes when brower is resized
function AdjustMapSize() {
    // set element widths 
    webMapAppPanelDisplay.style.width =  webMapAppLeftPanelWidth + "px";
    webMapAppToggleDisplay.style.width = webMapAppToggleWidth + "px";
    // get browser window dimensions 
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    // calc dimensions needed for map
    var mWidth = sWidth - webMapAppPanelDisplayCell.clientWidth - 16 - 2; //-16 for margins on both ends, -2 border
    var mHeight = sHeight - webMapAppTopBannerHeight - 8 - 2 - bottomTextArea; //-8 for margins on bottom, -2 border
    if (mWidth<5) mWidth = 5;
    if (mHeight<5) mHeight = 5;  
    webMapAppMapDisplay.style.width =  mWidth + "px";
    var mapLeft = webMapAppPanelDisplayCell.clientWidth + 8; //+8 to account for margin
    webMapAppMapDisplay.style.left = mapLeft + "px"; 
    // set heights on elements 
    webMapAppMapDisplay.style.height = mHeight  + "px";
    
    var leftHeight = mHeight + 2;
    var hoverHeight = leftHeight - 10;
    webMapAppToggleDisplay.style.height = leftHeight  + "px";
    webMapAppToggleHoverDisplay.style.height = hoverHeight + "px";
    webMapAppPanelScrollDiv.style.height = leftHeight  + "px";
    
    //SetTocFullHeight(leftHeight);
       
    // resize the map 
    window.setTimeout("resizeTheMap(" + mWidth + ", " + mHeight + ", false);", 500);
    // update map properties 
        
    // Save Browser window dimensions
    webMapAppWindowWidth = getWinWidth();
    webMapAppWindowHeight = getWinHeight();

    return false;
}

// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {
    
    StoreCurrentMapPosition();
    
    if (resizeExtent==null) resizeExtent = true;
    map.resize(width, height, resizeExtent);
    //9.3 (Min 5/26/09)
    map.refresh();
    
    var message = "ControlID=Map1&ControlType=Map&EventArg=Center";
    //9.3 (Min 5/26/09)
    //	var context = map.controlName;
    var context = this;
    ReturnInvocation(message, context);
    
    return false; 
}

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
    // add test for window actually changing size...Fix for ZoomIn Bug(i.e. ZoomIn doesn't always work in IE6
	if (webMapAppWindowWidth != getWinWidth() || webMapAppWindowHeight != getWinHeight())
    {
        reloadTimer = window.setTimeout("AdjustMapSize();",1000); 
    }
}

// function run at startup
function startUp() {

    //9.3 (Min 5/26/09)
    // set up identify mode for javascript
//    map.ctrlMode = "CenterAt";
//    map.ctrlAction = "Point";
//    map.ctrlCursor = "pointer";
//    map.ctrlFunction = "CenterAt(e)";
    
    //doSuggestion = false;
    map = $find("Map1");
    
    if (typeof (WebForm_CallbackComplete) == "function") {
      // set the original version with fixed version
      WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
    }
    
    //9.3 (Min 5/26/09)
//    var serverTransString = GetServerResTrans();
//    CreateTransArray(serverTransString)
//    SetCacheTransparencies();

    maximumLapseTime = 60;
    //9.3 (Min 5/26/09)
//    map.dragLineWidth = 1;
    esriBlankImagePath = "images/blank.gif";
               
    // set window resize event handler
    window.onresize = AdjustMapSizeHandler;

    //9.3 (Min 5/26/09)
    AddMaptipDiv();
    
    //Map Scale Change Handler
    //ESRI.ADF.UI.Map map = new ESRI.ADF.UI.Map(null);
    map.add_zoomStart(LJA_ZoomStart);
    //map.add_zoomCompleted(LJA_ZoomEnd);

    //3) Customize Zoom Level Control
    LJA_DoZoomLevel();

    //4) Control Viewing Extent
    map._moveContainerDiv = LJA_moveContainerDiv;
    
    //5) ScrollWheelZoom Control
    ESRI.ADF.System.addMouseWheelHandler(map.get_element(), LJA_onMouseWheel, map);
}

function startWebMapAppDockDrag(e) {
    if (!webMapAppDockMoving) {
        webMapAppMoveFunction = document.onmousemove;
        webMapAppDockMoving = true;
    }
    document.onmouseup = stopWebMapAppDocDrag;
    map.divObject.style.cursor = "e-resize";
    //	map.containerObject.style.cursor = "e-resize";  //9.2
    map._containerDiv.style.cursor = "e-resize";        //9.3
    webMapAppPanelDisplayTableCell.style.cursor = "e-resize";
    webMapAppWindowWidth = getWinWidth();
    webMapAppMaxDockWidth = webMapAppWindowWidth / 2;
    getXY(e);
    if (webMapAppPanelDisplay.style.display == "none")
        webMapAppLeftOffsetX = mouseX;
    else
        webMapAppLeftOffsetX = mouseX - webMapAppPanelDisplay.clientWidth;
    var box = calcElementPosition("Map_Panel");
    webMapAppRightOffsetX = box.left - mouseX;
    document.onmousemove = moveWebMapAppDockDrag;
    // because the panel cell will be as wide as the overview map image, keep the element width larger than the image.
    webMapAppMinDockWidth = webMapAppDefaultMinDockWidth;
    webMapAppToggleHoverDisplay.style.display = "";
    return false;
}

function moveWebMapAppDockDrag(e) {
    getXY(e);
    var theButton = (isIE) ? event.button : e.which;
    if (theButton == 0) stopWebMapAppDocDrag(e)
    if (mouseX > webMapAppMinDockWidth && mouseX < webMapAppMaxDockWidth) {
        webMapAppLeftPanelWidth = mouseX - webMapAppLeftOffsetX;
        var sWidth = getWinWidth();
        if (webMapAppLeftPanelWidth > sWidth - webMapAppToggleDisplay.clientWidth - 2)
            webMapAppLeftPanelWidth = sWidth - webMapAppToggleDisplay.clientWidth - 2;
        var hoverLeft = webMapAppLeftPanelWidth;
        if (webMapAppLeftPanelWidth < webMapAppMinDockWidth) {
            webMapAppLeftPanelWidth = webMapAppMinDockWidth - 1;
            hoverLeft = 0;
        }
        if (webMapAppLeftPanelWidth > webMapAppMaxDockWidth) {
            webMapAppLeftPanelWidth = webMapAppMinDockWidth + 1;
            hoverLeft = webMapAppMaxDockWidth;
        }

        webMapAppToggleHoverDisplay.style.left = hoverLeft + "px";
    }
    return false;
}

function stopWebMapAppDocDrag(e) {
    document.onmousemove = webMapAppMoveFunction;
    document.onmouseup = null;
    webMapAppDockMoving = false;
    //if (checkForFormElement(document, 0, "MapDebugBox")) document.forms[0].MapDebugBox.value += ("ln 323. " + webMapAppLeftPanelWidth + ", " + webMapAppPanelDisplayCell.clientWidth + "\n");
    if (webMapAppPanelDisplay.style.display == "none") {
        expandPanelDock();
    }
    if (webMapAppLeftPanelWidth < webMapAppMinDockWidth) {
        collapsePanelDock();
        webMapAppLeftPanelWidth = webMapAppMinDockWidth;
    }
    if (webMapAppLeftPanelWidth > webMapAppMaxDockWidth) {
        webMapAppLeftPanelWidth = webMapAppMaxDockWidth;
    }
    webMapAppToggleHoverDisplay.style.display = "none";
    webMapAppPanelDisplayTableCell.style.cursor = "";
    //map.divObject.style.cursor = map.tempCursor;          //9.2
    //map.containerObject.style.cursor = map.tempCursor;    //9.2
    map.divObject.style.cursor = map.get_cursor();          //9.3
    map._containerDiv.style.cursor = map.get_cursor();    //9.3
    webMapAppCheckPanelWidths();
    webMapAppToggleHoverDisplay.style.left = "";
    AdjustMapSize();
    return false;
}

//function webMapAppCheckPanelScroll() {
//    if (webMapAppPanelScrollDiv.scrollHeight>webMapAppPanelScrollDiv.clientHeight) {
//        webMapAppHasScroll = true;
//    }  else {
//        webMapAppHasScroll = false;
//    }  
//    //webMapAppCheckPanelWidths(); 
//    if (webMapAppHasScroll!=webMapAppLastHasScroll)
//        AdjustMapSize();
//    webMapAppLastHasScroll = webMapAppHasScroll;    
//    return false; 
//}

function webMapAppCheckPanelWidths() {
    var maxWidth = 0;
    var node;
    for (var i = 0; i < webMapAppPanelDisplay.childNodes.length; i++) {
        if (webMapAppPanelDisplay.childNodes[i].tagName == "TABLE") {
            node = webMapAppPanelDisplay.childNodes[i];
            if (node.clientWidth > maxWidth) maxWidth = node.clientWidth;
        }
    }
    if (maxWidth > 0) {
        webMapAppPanelDisplay.style.width = maxWidth + "px";
    }
    return false;
}

function scrollDockToPanel(panelElement) {
    if (panelElement == null) return;
    var yPos = panelElement.offsetTop;
    webMapAppPanelScrollDiv.scrollTop = yPos;
}

function ShowStatus(message)
{
    window.status = message;
}

//9.3 (Min 5/26/09)
////////// Transparency for Cached Resources
//var m_resTrans;

//function CreateTransArray(resString)
//{
//    m_resTrans = new Array();
//    var resItems = resString.split('|');
//    for (var i = 0; i < resItems.length; i++)
//    {
//        var resValue = resItems[i].split('*');
//        m_resTrans[i] = new Array();
//        m_resTrans[i][0] = resValue[0];
//        m_resTrans[i][1] = resValue[1];
//    }
//}

//function SetCacheTransparencies()
//{
//    var rc = map.ResourceCollection;
//    for (var i=0; i<rc.length; i++)
//    {
//        var resName = rc[i].id;
//        
//        for (var j = 0; j < m_resTrans.length; j++)
//        {
//            if (m_resTrans[j][0] == resName)
//            {
//                rc[i].ljaTrans = m_resTrans[j][1];          
//            }
//        }                                        
//    } 
//}
//////////// End transparency for Cached Resources

function WebForm_CallbackComplete_SyncFixed() {
  // SyncFix: the original version uses "i" as global thereby resulting in javascript errors when "i" is used elsewhere in consuming pages
  for (var i = 0; i < __pendingCallbacks.length; i++) {
   callbackObject = __pendingCallbacks[ i ];
  if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
   
   if (!__pendingCallbacks[ i ].async) {
     __synchronousCallBackIndex = -1;
   }
   __pendingCallbacks[i] = null;

   var callbackFrameID = "__CALLBACKFRAME" + i;
   var xmlRequestFrame = document.getElementById(callbackFrameID);
   if (xmlRequestFrame) {
     xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
   }
   
   // SyncFix: the following statement has been moved down from above;
   WebForm_ExecuteCallback(callbackObject);
  }
 }
}
