﻿// Javascript file for Web Mapping Application

var reloadTimer;
var webMapAppLeftPanelWidth = 262;
var webMapAppToggleWidth = 31;
var webMapAppTopBannerHeight = 255;
var newLoad = false;
var resizeNow = false;
var webMapAppCloseCallback = "";
var webMapAppCopyrightCallback = "";
var webMapAppMoveFunction = null;
//AK   var webMapAppMapDisplay = null;
//AK   var webMapAppPanelDisplay = null;
//AK   var webMapAppPanelDisplayCell = null;
var webMapAppPanelDisplayTableCell = null;
var webMapAppPanelScrollDiv = null;
var webMapAppToggleDisplay = null;
var webMapAppSpacerDiv = null;
var webMapAppPanelBottomSlider = null;
var webMapAppScaleBar = null;
var webMapAppCopyrightText = null;
var webMapAppWindowWidth = 500;
var webMapAppLeftOffsetX = 0;
var webMapAppRightOffsetX = 0
var webMapAppDefaultMinDockWidth = 125;
var webMapAppMinDockWidth = webMapAppDefaultMinDockWidth;
var webMapAppHasScroll = false;
var webMapAppLastHasScroll = false;
var divLayout = null;
var divLogos = null;
var divGeoguide = null;
var divMap = null;
var divBusyIndicator = null;
var divPlaceholder = null;
var divSidenav = null;
var divtabcontent = null;
var divHeader = null;
var divFooter = null;
var divToolbar = null;
var webMapAppToolbar = null;
var body = null;
var minMapWidth = 700;
var minMapHeight = 330;
var mWidth = 700;
var mHeight = 330;

// function to set initial sizes of page elements
function setPageElementSizes() {

        // set body style 
        document.body.style.overflow = "hidden";
        document.body.style.height = "100%"; 
        // get necessary elements
        divLayout = document.getElementById("layout");
        divLogos = document.getElementById("logosDiv");
        divGeoguide = document.getElementById("geoguide");
        divMap = document.getElementById("map");
        divPlaceholder = document.getElementById("placeholder");
        divHeader = document.getElementById("header");
        divFooter = document.getElementById("footer");
        divToolbar = document.getElementById("toolbardiv");
        divSidenav = document.getElementById("sidenav");
        divtabcontent = document.getElementById("tabcontent");
        body = document.getElementById("bodyid");
        divBusyIndicator = document.getElementById("BusyIndicator");
        
        webMapAppMapDisplay = document.getElementById("placeholder");
        webMapAppScaleBar = document.getElementById("ScaleBar1");
        webMapAppToolbar = document.getElementById("Toolbar1");
        
        // get the set widths and heights
        webMapAppLeftPanelWidth = divSidenav.clientWidth;
    
        AdjustDivSizes(); 

 
}
        
        



// function for adjusting element sizes when brower is resized
function AdjustMapSize() {

 //alert("AdjustMapSize");
    AdjustDivSizes();

//alert('Map - ' +map.height+ ' - ' +map.width)
   // resize the map 
    window.setTimeout("resizeTheMap(" + mWidth + ", " + mHeight + ", false);", 500);

   // update map properties 
    var box = calcElementPosition("placeholder"); 
    map.containerLeft = box.left;
	map.containerTop = box.top;
    return false;
}


function AdjustDivSizes() {

   // get browser window dimensions 
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    
    //Ak added 20080519 to be able to increase the map width by narrowing the left bar
    //alert(divSidenav.clientWidth); //=245
    //divSidenav.style.width = "50px";
    
    // calc dimensions needed for map
    mWidth = sWidth - divSidenav.clientWidth - webMapAppToggleWidth;
    mHeight = sHeight - webMapAppTopBannerHeight;
    webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;

    if (mWidth<minMapWidth) mWidth = minMapWidth;
    if (mHeight<minMapHeight) mHeight = minMapHeight;  
  
    if (mWidth==minMapWidth) divGeoguide.style.width =  (mWidth + divSidenav.clientWidth + webMapAppToggleWidth - 10) + "px";
    if (mHeight==minMapHeight) divGeoguide.style.height = (mHeight+divHeader.clientHeight + 10)  + "px";
    if (mWidth>minMapWidth) divGeoguide.style.width =  (sWidth-10) + "px";
    if (mHeight>minMapHeight) divGeoguide.style.height = (sHeight-divHeader.clientHeight-divFooter.clientHeight-8)  + "px";
    
       if (mWidth==minMapWidth || mHeight==minMapHeight) 
        { divFooter.style.width =  (mWidth + divSidenav.clientWidth + webMapAppToggleWidth - 10) + "px"; 
          divFooter.style.position = "relative";
          divFooter.style.top = (mHeight + 217) + "px";
        }
        else
        {divFooter.style.width = sWidth + "px";
         divFooter.style.position = "absolute";
          divFooter.style.top = (sHeight - divFooter.clientHeight)  + "px";
        }

    body.style.width = sWidth;    
    divHeader.style.width = sWidth;
    divLogos.style.width = mWidth;
    divSidenav.style.height = (mHeight + 80) + "px";
  
    divtabcontent.style.maxHeight= (mHeight + 35) + "px";
    
        
    divPlaceholder.style.width =  mWidth + "px";
    divPlaceholder.style.height = mHeight  + "px";
    divPlaceholder.style.left =  (divSidenav.clientWidth-webMapAppToggleWidth) + "px";

    
    divBusyIndicator.style.top = (mHeight/2) + "px";
    divBusyIndicator.style.left = (mWidth/2) + "px";

    divToolbar.style.width = mWidth + "px";
    
    divLayout.style.width =  (sWidth-10) + "px";
    divLayout.style.height = (sHeight-10)  + "px";
   
    if (webMapAppScaleBar!=null) {
        var sbWidth = webMapAppScaleBar.clientWidth;
        var sbHeight = webMapAppScaleBar.clientHeight;
        webMapAppScaleBar.style.left = (mWidth - 280) + "px";
        webMapAppScaleBar.style.top = (mHeight + 20) + "px";
    } 
    
    //AK Added 20080519
   // refresh();
    

}


// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {

    if (resizeExtent==null) resizeExtent = true;
    map.resize(width, height, resizeExtent);
   // alert('Map - ' +height+ ' - ' +width)
    
   //ak    var div = document.getElementById("LeftPanelCellDiv"); 
    // update overview, if doc panel is expanded 
   //ak if (ov!=null && div.style.display!="none") { 
   //ak     var argument = "ControlType=OverviewMap&EventArg=OverviewZoom";
   //ak     var context = ov.controlName; 
   //ak     eval(ov.callBackFunctionString);
   //ak }

    return false; 
}

//AK Added function from ESRI forum
function refresh() 
{ 
    var map = Maps['Map1'];
    if(typeof map == "[object]")
    {
        map.redraw();
    }
} 


// function to toggle Dock visibility
function togglePanelDock() {
    if (webMapAppPanelDisplay.style.display=="none") {
        expandPanelDock();
    } else {
        collapsePanelDock();
    }     
}

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustMapSize();",1000);
}


function expandPanelDock() {
    var image = document.images["CollapseImage"];
    webMapAppPanelDisplay.style.display = "block";
    image.src = "images/collapse_left.gif";
    image.alt = "Collapse";
    webMapAppPanelSlider.style.cursor = "e-resize";
    webMapAppPanelBottomSlider.style.cursor = "e-resize"; 
    webMapAppMapLeft = webMapAppLeftPanelWidth + webMapAppToggleWidth;
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";
    AdjustMapSize(); 
}

function collapsePanelDock() {
    var image = document.images["CollapseImage"];
    dockWidthString = webMapAppPanelDisplayCell.clientWidth + "px";
    webMapAppPanelDisplay.style.display = "none";
    //webMapAppPanelDisplayCell.style.width = "1px";
    image.src = "images/expand_right.gif";
    image.alt = "Expand";
    webMapAppPanelSlider.style.cursor = "default"; 
    webMapAppPanelBottomSlider.style.cursor = "default"; 
    webMapAppMapLeft = webMapAppToggleWidth; 
    webMapAppMapDisplay.style.left =  webMapAppMapLeft + "px";  
    AdjustMapSize();  
   
}  

// function run at startup
function startUp() {
        //GBS - BusyIndicator Code - 08.04.2008
        //Add busy indicator event handlers to the map
       
        var map = Maps["Map1"];

        //map.divObject.style.display = "none";
        
        
       /// map.height=mHeight;
      ///  map.width=mWidth;
          
        
       
        showBusyIndicator(null);
        map.pendingTiles.add_onRequestsPending(showBusyIndicator);
        map.pendingTiles.add_onRequestsRemove(showPendingTiles);
        map.pendingTiles.add_onRequestsCompleted(hideBusyIndicator);    
        
        ChangeTab('Search');
        ChangeSearch('FindAddressButton');

        // set up identify mode for javascript
        map.ctrlMode = "MapIdentify";
        map.ctrlAction = "Point";
        map.ctrlCursor = "pointer";
        map.ctrlFunction = "MapIdClick(e)";
        
        if (newLoad) {
        //alert("NewLoad");
            // execute only on intial load.... not callbacks
            map.divObject.style.cursor = "wait";
            // move magnifier and measure toolbar to top left corner of map display 
            var box = calcElementPosition("placeholder"); 
            
            //AK added 20090313
           window.clearTimeout(reloadTimer);
	        reloadTimer = window.setTimeout("AdjustMapSize();",100);
//            AdjustMapSize(); //Only resize on initial startup (NewLoad)
            

        } 
        // set window resize event handler
       window.onresize = AdjustMapSizeHandler;
//map.divObject.style.display = "";
}  

// 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 startWebMapAppDockDrag(e) {
    webMapAppMoveFunction = document.onmousemove;
    document.onmouseup = stopWebMapAppDocDrag;  
    if (webMapAppPanelDisplay.style.display!="none") {
        webMapAppWindowWidth = getWinWidth();
        getXY(e); 
        webMapAppLeftOffsetX = mouseX - webMapAppPanelDisplay.clientWidth;
        var box = calcElementPosition("Map_Panel");
        webMapAppRightOffsetX = box.left - mouseX; 
        document.onmousemove = moveWebMapAppDockDrag;
        var ovPanel = document.getElementById("OverviewMap_Panel_BodyRow");
        var ovDisplay =  document.getElementById("OVDiv_OverviewMap_Panel_OverviewMap1");
        // because the panel cell will be as wide as the overview map image, keep the element width larger than the image. 
        if (FloatingPanels["OverviewMap_Panel"]!=null &&FloatingPanels["OverviewMap_Panel"].docked)
            webMapAppMinDockWidth = parseInt(ovDisplay.style.width) + 20;
        else    
            webMapAppMinDockWidth = webMapAppDefaultMinDockWidth; 
    }
    return false;  
}

function moveWebMapAppDockDrag(e) {
    getXY(e);
    webMapAppLeftPanelWidth =  mouseX - webMapAppLeftOffsetX;
    if (webMapAppLeftPanelWidth < webMapAppMinDockWidth) webMapAppLeftPanelWidth = webMapAppMinDockWidth; 
    var mapLeftString =  (webMapAppLeftPanelWidth + webMapAppToggleDisplay.clientWidth) + "px";
    //if (webMapAppPanelDisplayTableCell.clientWidth - webMapAppToggleDisplay.clientWidth > webMapAppLeftPanelWidth) webMapAppLeftPanelWidth = webMapAppPanelDisplayTableCell.clientWidth - webMapAppToggleDisplay.clientWidth;
    var widthString =  webMapAppLeftPanelWidth + "px";
    webMapAppPanelDisplay.style.width = widthString;
    //var width =  webMapAppWindowWidth - webMapAppMapDisplay.clientWidth;
    var width =  webMapAppWindowWidth - webMapAppPanelDisplayCell.clientWidth; 
    webMapAppMapDisplay.style.width = width + "px";
    webMapAppMapDisplay.style.left = mapLeftString;
    return false;
}

function stopWebMapAppDocDrag(e) {
    document.onmousemove = webMapAppMoveFunction;
    document.onmouseup = null;   
    webMapAppCheckPanelWidths();
    AdjustMapSize();  
    return false;
}

function OpenWindow(url) {
    window.open(url);
}

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; 
        }
    }  
     webMapAppPanelDisplay.style.width = maxWidth + "px";
    return false;
}

function webMapAppGetCopyrightText() {
	var argument = "ControlID=Map1&ControlType=Map&EventArg=GetCopyrightText";
	var context = map.controlName;
	eval(webMapAppCopyrightCallback);
	showFloatingPanel('CopyrightText_Panel');
}

function scrollDockToPanel(panelElement) {
    if (panelElement==null) return;
    var yPos = panelElement.offsetTop;
    webMapAppPanelScrollDiv.scrollTop = yPos;      
}

function toggleMagnifier() {
    var mag = document.getElementById("Magnifier1");
    if (mag!=null) { 
            toggleFloatingPanelVisibility('Magnifier1'); 
    } else 
        alert("Magnifier is not available"); 
    
}