﻿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 to request closing of session items.... only called if at least one resource is local non-pooled
function CloseOut(logout) 
{
    var r = Math.random().toString();
    PageMethods.CleanUp(r, CloseOutResponse, CloseOutResponse);
}

// response function to close out browser ... request sent to server by CloseOut()
function CloseOutResponse(response) {
    window.close(); 
    // if user selects No/Cancel in close dialog, send to close page 
    document.location = response; 
}

function LogOut() {
    var r = Math.random().toString();
    PageMethods.CleanUp(r, LogOutResponse, LogOutResponse); 
}

function LogOutResponse(response) {
    // loginstatus should send back to login page
    document.location = "Login.aspx";
}

function toggleMagnifier() {
    var mag = $get("Magnifier1");
    if (mag!=null) { 
            toggleFloatingPanelVisibility('Magnifier1'); 
    } else 
        alert("Magnifier is not available"); 
    
}

function toggleOverviewMap() {
    var ovm = $find("OverviewMap1"); 
    var toolbarobj = $find("Toolbar2"); 
	var toolbar =(toolbarobj!=null) ? Toolbars[toolbarobj._uniqueID] : null;
    var imageTag = "Toolbar2OverviewMapToggleImage";
    var toolbarItemName = "OverviewMapToggle"; 
    var showImage = (isRTL ? "images/show-overview-map2.png" : "images/show-overview-map.png"); 
    var hideImage = (isRTL ? "images/hide-overview-map2.png" : "images/hide-overview-map.png");	
    var img = document.images[imageTag];
    if (ovm) {
        if(ovm.isVisible()) {
            ovm.hide();
            if (toolbar!=null) { 
                toolbar.items[toolbarItemName].selectedImage = showImage;
                toolbar.items[toolbarItemName].defaultImage = showImage;
                toolbar.items[toolbarItemName].hoverImage = showImage; 
                img.alt = "Show OverviewMap";
                img.title = "Show OverviewMap";
                switchImageSourceAndAlphaBlend(img,toolbar.items[toolbarItemName].defaultImage);  
            }   
        } else {
            ovm.show();
            if (toolbar!=null) {   
                toolbar.items[toolbarItemName].selectedImage = hideImage;
                toolbar.items[toolbarItemName].defaultImage = hideImage;
                toolbar.items[toolbarItemName].hoverImage = hideImage;  
                img.alt = "Hide OverviewMap";
                img.title = "Hide OverviewMap";
                switchImageSourceAndAlphaBlend(img,toolbar.items[toolbarItemName].defaultImage); 
            }
            if (isRTL) adjustRTLElements();     
        } 
    }  
}


function toggleConsolePanel(panelName) {
    var panel = $get(panelName + "_Collapse");
    if (panel) {
        if (panel.style.display=="none") {
            expandConsolePanel(panelName);
        } else { 
            collapseConsolePanel(panelName); 
        }
        //arcgisWebApp.setTocHeight(); 
        //arcgisWebApp.setResultsHeight();
        //arcgisWebApp.setDocumentsHeight(); 
        window.setTimeout("arcgisWebApp.MapDisplay.style.left = getMapLeft(); + 'px';", 500) ; 
    }             
}



function expandConsolePanel(panelName) {
    var panel = $get(panelName + "_Collapse");
    var image = $get(panelName + "_Image"); 
    panel.style.display = "";
    image.src = "images/collapse.gif";
    image.alt = "Collapse";
}

function collapseConsolePanel(panelName) {
    var panel = $get(panelName + "_Collapse");
    var image = $get(panelName + "_Image"); 
    panel.style.display = "none";
    image.src = "images/expand.gif";
    image.alt = "Expand";
}

function backForward(value) {
    map = $find("Map1");
    map.stepExtentHistory(value);
}


function setupCopyrightText() {
    var crBounds = Sys.UI.DomElement.getBounds(arcgisWebApp.CopyrightText);
    var pHeight = arcgisWebApp.getPageHeight();
    arcgisWebApp.CopyrightText.style.top = (pHeight-crBounds.height - 10) + "px";  //GFB changed to account for bottom togglebar
    var cr = $find("MapCopyrightText1");
    if (cr!=null){
        var template =  '<div style="width: 300px;background-color:#eee;border: solid 1px #aaa;padding: 0px; margin:0;" ><div style="background:#eee; margin:0; height: 20px; color: black">';
        template += '<span style="float: ' + (isRTL?'right':'left') + '; padding: 0px 0px 0px 2px;"><b>{@title}</b></span>';
        template += '<span style="float: ' + (isRTL?'left':'right') + '; cursor: pointer; padding: 0px 2px 0px 0px;" onclick="ESRI.ADF.UI.MapCopyrightText.closeClick(\''+cr.get_id()+'\');"><img id="CopyrightTextClose" src="images/dismiss.png" alt="Close" /></span>';
        template += '</div><div style="padding: 2px 2px 2px 2px; background-color: white; color: black; font-size:x-small;">{@content}</div></div>';
        cr.get_callout().set_template(template);
        cr.get_callout().set_animate(false);
    }
}

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); 
}


function OnToolSelectHandler(sender, args) {
    if (args.name) {
        var mode = args.name;
        arcgisWebApp.currentMode = mode;
        if (mode!="Measure" && arcgisWebApp.lastMode=="Measure") closeMeasureToolbarTool();
        if (mode!="MapIdentify" && arcgisWebApp.lastMode=="MapIdentify") {
            if (arcgisIdentifyTool!=null) closeIdentifyPanel();
        } 
        arcgisWebApp.lastMode = mode;
    }
}


function adjustMapAndCopyrightPosition(adjustMapDisplay) {
    if (adjustMapDisplay==null) adjustMapDisplay = false;
    var bounds = Sys.UI.DomElement.getBounds(arcgisWebApp.ToggleDisplay);
    var mLeft = bounds.x+ bounds.width;
    if (arcgisWebApp.CopyrightText) { 
        var crLeft =  parseInt(arcgisWebApp.CopyrightText.style.left);
        arcgisWebApp.CopyrightText.style.left = mLeft + "px";
    }
    if (adjustMapDisplay) 
        arcgisWebApp.MapDisplay.style.left =  mLeft + "px";
}


