﻿
// Javascript file for Web Mapping Application
var tempMode = null;
var tempAction = null;
var tempCursor = null;

var custom_argument= null;

var reloadTimer;
var webMapAppLeftPanelWidth = 0;
var webMapAppToggleWidth = 0;
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 webMapAppSpacerDiv = null;
var webMapAppPanelBottomSlider = null;
var webMapAppScaleBar = null;
var webMapAppCopyrightText = null;
var webMapAppWindowWidth = 1280;
var webMapAppLeftOffsetX = 0;
var webMapAppRightOffsetX = 0;
var webMapAppDefaultMinDockWidth = 300;
var webMapAppMinDockWidth = webMapAppDefaultMinDockWidth;
var webMapAppMapLeft = 0;
var webMapAppHasScroll = false;
var webMapAppLastHasScroll = false;
var webMapAppFirstScalebarPosition = true;
var webMapAppDockMoving = false;
var m_measureToolbarId = "";
var webMapAppZoomBar = null;
var webMapAppToolbar = null;
var webMapAppTechSupport = null;

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// function to set initial sizes of page elements
function setPageElementSizes() 
{     
        // Preload images
        MM_preloadImages('images/progress_active.gif');
        
        // get necessary elements
        webMapAppMapDisplay = document.getElementById("mapcontrols");
        webMapAppPanelDisplay = document.getElementById("aside");
        webMapAppPanelDisplayCell = document.getElementById("LeftPanelCell");
        webMapAppPanelScrollDiv = document.getElementById("LeftPanelScrollDiv");
        webMapAppToggleDisplay = document.getElementById("ToggleCell");
        webMapAppPanelSlider = document.getElementById("PanelSlider");
        webMapAppPanelDisplayTableCell = document.getElementById("LeftPanelTableCell");
        webMapAppPanelBottomSlider = document.getElementById("PanelSliderBottom");
        webMapAppScaleBar = document.getElementById("ScaleBar1");
        webMapAppCopyrightText = document.getElementById("CopyrightTextHolder");
        webMapAppToolbar = document.getElementById("MapToolBar");
        webMapAppZoomBar = document.getElementById("divZoomBar");
        webMapAppTechSupport = document.getElementById("divTechSupport");
        var headerDisplay = document.getElementById("PageHeader");
        var linkDisplay = document.getElementById("LinkBar");
        
        // get the set widths and heights
        if ( webMapAppPanelDisplay != null ) 
            webMapAppLeftPanelWidth = webMapAppPanelDisplay.clientWidth;
            
        if ( webMapAppToggleDisplay != null ) 
            webMapAppToggleWidth = parseInt(webMapAppToggleDisplay.style.width);
            
        webMapAppTopBannerHeight = ( headerDisplay != null) ? headerDisplay.clientHeight : 0 + 
            ( linkDisplay != null ) ? linkDisplay.clientHeight : 0;
        
    var box = calcElementPosition("mapcontrols");
    
    if ( webMapAppToolbar != null )
    {
        //var crtHeight = webMapAppToolbar.clientHeight;
        var crtWidth = webMapAppToolbar.clientWidth;
        webMapAppToolbar.style.left = ( box.width - crtWidth  ) + "px";
        //webMapAppToolbar.style.top = ( sHeight - webMapAppTopBannerHeight - crtHeight - 16 ) + "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";
    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 for adjusting element sizes when brower is resized
function AdjustMapSize() 
{
    
   // get map placeholder dimensions
   var box = calcElementPosition("mapcontrols"); 
   
    var sWidth = box.width;
    var sHeight = box.height;
    
    // calc dimensions needed for map
    var mWidth  = sWidth;
    var mHeight = sHeight;
    
    if ( webMapAppPanelDisplayCell != null )
       mWidth = mWidth - webMapAppPanelDisplayCell.clientWidth;
       
    if (mWidth<5) mWidth = 5;
    if (mHeight<5) mHeight = 5;  
    //webMapAppMapDisplay.style.width  =  ( mWidth ) + "px";
    //webMapAppMapDisplay.style.height =  ( mHeight ) + "px";
   
    
   // resize the map 
   window.setTimeout("resizeTheMap(" + mWidth + ", " + mHeight + ", false);", 500);
              
    if ( webMapAppToolbar != null )
    {
        //var crtHeight = webMapAppToolbar.clientHeight;
        var crtWidth = webMapAppToolbar.clientWidth;
        webMapAppToolbar.style.left = ( box.width - crtWidth  ) + "px";
        //webMapAppToolbar.style.top = ( sHeight - webMapAppTopBannerHeight - crtHeight - 16 ) + "px";
    }
            
    return true;
}

// function for resizing map in Web Map App
function resizeTheMap(width, height, resizeExtent) {
    if (resizeExtent==null) resizeExtent = true;
    map.resize(width, height, resizeExtent);
    var div = document.getElementById("LeftPanelCellDiv"); 
    // update overview, if doc panel is expanded 
    if (ov!=null && div.style.display!="none") { 
        var argument = "ControlType=OverviewMap&EventArg=OverviewZoom";
        var context = ov.controlName; 
        eval(ov.callBackFunctionString);
    }
    return true; 
}

// handler for window resize
function AdjustMapSizeHandler(e) {
    window.clearTimeout(reloadTimer);
	reloadTimer = window.setTimeout("AdjustMapSize();",1000);
}

// show info during startup
function ProcessArguments()
{
    //alert( custom_argument );
    
    if ( custom_argument != null )
    {        
        ShowLoading();
        map = Maps["Map1"];
        var argument = custom_argument;
        var context = map.controlName;
        eval(identifyCallbackFunctionString);
    }    
}

// function run at startup
function startUp() 
{
        // set up identify mode for javascript
        map.ctrlMode = "IdentifyAll";
        map.ctrlAction = "Point";
        map.ctrlCursor = "pointer";
        map.ctrlFunction = "MapIdentify(e)";
        
        // loading object
        // map.loadingObject = document.getElementById("BusyIndicator");
        
        var box = calcElementPosition("mapcontrols");
        
        if (newLoad) {
        
            ShowLoading();
            // 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("mapcontrols"); 
            var mag = document.getElementById("Magnifier1");
            var tb = document.getElementById(m_measureToolbarId);
			if (tb!=null) {
			    //tb.style.left = box.left + "px";
			    //tb.style.top = box.top + "px";
			}
			var fp = document.getElementById("CopyrightText_Panel");
			if (fp!=null) {
			    floatingPanel = fp;
			    moveTo(box.left, box.top);
			}
            if (webMapAppScaleBar!=null) window.setTimeout("positionScalebar();", 1000);                       
            
            // process link
            window.setTimeout( "ProcessArguments();", 1000 );            
        } 
        // set window resize event handler
        window.onresize = AdjustMapSizeHandler;
}  

function startWebMapAppDockDrag(e) {
    if (!webMapAppDockMoving) {
        webMapAppMoveFunction = document.onmousemove;
        webMapAppDockMoving = true;
    }
    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);
    var theButton = (isIE) ? event.button : e.which;      
    if (theButton==0) stopWebMapAppDocDrag(e)
    webMapAppLeftPanelWidth =  mouseX - webMapAppLeftOffsetX;
    var sWidth = getWinWidth();
    if (webMapAppLeftPanelWidth>sWidth-webMapAppToggleDisplay.clientWidth-2) webMapAppLeftPanelWidth=sWidth-webMapAppToggleDisplay.clientWidth-2;
    if (webMapAppLeftPanelWidth < webMapAppMinDockWidth) webMapAppLeftPanelWidth = webMapAppMinDockWidth; 
    var mapLeftString =  (webMapAppLeftPanelWidth + webMapAppToggleDisplay.clientWidth) + "px";
    var widthString =  webMapAppLeftPanelWidth + "px";
    webMapAppPanelDisplay.style.width = widthString;
    var width =  webMapAppWindowWidth - webMapAppPanelDisplayCell.clientWidth; 
    if (width<1) width = 1; 
    webMapAppMapDisplay.style.width = width + "px";
    webMapAppMapDisplay.style.left = mapLeftString;
    return false;
}

function stopWebMapAppDocDrag(e) {
    document.onmousemove = webMapAppMoveFunction;
    document.onmouseup = null;
    webMapAppDockMoving = false;    
    webMapAppCheckPanelWidths();
    AdjustMapSize();  
    return false;
}

function OpenWindow(url) {
    window.open(url);
}

function webMapAppCheckPanelScroll() {
    if ( webMapAppPanelScrollDiv != null && 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;
    if ( webMapAppPanelScrollDiv != null ) 
        webMapAppPanelScrollDiv.scrollTop = yPos;  
}

function toggleMagnifier() {
    var mag = document.getElementById("Magnifier1");
    if (mag!=null) { 
            toggleFloatingPanelVisibility('Magnifier1'); 
    } else 
        alert("Magnifier is not available"); 
    
}

function positionScalebar() 
{
    var sWidth = getWinWidth();
    var sHeight = getWinHeight();
    var sbWidth = webMapAppScaleBar.clientWidth;
    var sbHeight = webMapAppScaleBar.clientHeight;
    webMapAppScaleBar.style.left = (sWidth - sbWidth - 10) + "px";
    webMapAppScaleBar.style.top = (sHeight - sbHeight - 10) + "px";
}

function adjustLinkPanel( id )
{    
   var toolBox = calcElementPosition( 'Toolbar1Url' );
    
   var varPanel = document.getElementById( 'linkDivForTool' );
   if ( varPanel != null )
   {
       varPanel.style.left = toolBox.left + "px";
       varPanel.style.top =  toolBox.top + 1 + "px";  
   }
      
   
   varPanel = document.getElementById( 'linkDivForTool_1' );
   if ( varPanel != null )
   {
       varPanel.style.left = toolBox.left + 1 + "px";
       varPanel.style.top  = toolBox.top +  toolBox.height + "px";  
   }
   
   var toolBox = calcElementPosition( 'Map1' );   
   varPanel = document.getElementById( 'linkDiv' );
   if ( varPanel != null )
   {
       varPanel.style.top = toolBox.top + "px";
   }      
}

function isMapBusy()
{
    alert('Еще не получен ответ на предыдуший запрос. Подождите!');
}  


// быстрый вызов информаци в точке
function MapIdentify( e )
{    
    if (getSessionLapse() < maximumLapseTime)
    {
        if( isLayerVisible("BusyIndicator") )
            return true;    
        
        map.cursor = map.divObject.style.cursor;
        //map.divObject.style.cursor = "wait";
        getXY(e);
        var box = calcElementPosition(map.containerDivId);
        zleft = mouseX - box.left;
        ztop = mouseY - box.top;

        showBusyIndicator(null);

        map.xMin=zleft;
        map.yMin=ztop;
        map.getTopLeftTile();

        var argument = "ControlID=Map1&ControlType=Map&EventArg=MapIdentify&Map1_mode=IdentifyAll&minx=" + zleft + "&miny=" + ztop;
        var context = map.controlName;
        eval(map.identifyCallbackFunctionString);

        //map.mode = map.tempMode;
        //map.actionType = map.tempAction;
        //map.cursor = map.tempCursor;        
	}
    else
        showLapseAlert();
        
    return false;	
}
