// CivXplorer code to process callbacks
// Suzanne Fliege, Allied Information Solutions, Inc.
var callbackToolMode = "nothing";
var idX = 0;
var idY = 0;
var idPic = new Image();
// selection result storage
var lastSelectLayer = "";
var selectedLayers = new Array();
var selectedLayerResults = new Array();

//called at the beginning of the handling the map client state sent back from the server in an out-of-band callback
function MDNPreMapStateChangeComplete(state)
{
    //alert(state.Html);
    // pop-up errors
    if (state.Error&&state.Error.length>0)
    {
        //alert(state.Error);
    }
}

//called at the end of the handling the map client state sent back from the server in an out-of-band callback
function MDNPostMapStateChangeComplete(state)
{
    // hide protector overlay
    ProtectorOff();
    // post HTML if it exists
    if (state.Html)
    {
        if (callbackToolMode == 'identify') {
            updateContent('cxIdentify', state.Html);
            IdentifyBubbleMove(idX,idY);
            document.getElementById('cxIdentify').style.visibility = "visible";
            document.getElementById('cxProgressImage').style.visibility = "hidden";
        } else if (callbackToolMode == 'select' || callbackToolMode == 'buffer' || callbackToolMode == 'startup select') {
            ProcessSelectionResults(state.Html);
            if (callbackToolMode == 'startup select') {
                callbackToolMode = "nothing";
		        InitSizeMap();
		    }
		} else if (callbackToolMode == 'clearselect') {
		    ClearSelectionResults(state.Html);
		}
    }
    callbackToolMode = "nothing";
}

// initialize map size during startup
function InitializeMapAfterStartupParams() {
    InitSizeMap();
}

// ******************************
//    popup display (URLs, results, reports, maps)
// ******************************
// close popup window
function closeEntryPageDisplay() {
    document.getElementById('cxEntryPage').style.display = "none";
    return false; 
}

// close popup window
function closePopupDisplay() {
    document.getElementById('cxPopupMask').style.display = "none";  
    document.getElementById('cxPopup').style.display = "none";
    return false; 
}

// open popup window with specified URL
function openPopupDisplay(theURL) {
    // hide protector overlay
    ProtectorOff();
    clickFunction("nothing");
    if (theURL.toUpperCase().indexOf("ERROR") == -1) {
        var theString = '<iframe id="InlinePrintFrame" width="100%" height="100%" scrolling="auto" frameborder="no" src="'+theURL+'" border="0"></iframe>';
        updateContent('cxPopup',theString);
        document.getElementById('cxPopup').style.overflow = "hidden";
        document.getElementById('cxPopupMask').style.display = "inline";  
        document.getElementById('cxPopup').style.display = "inline";
    } else {
        alert(theURL);
    }
}

// display tool dialogs (reports, buffering, etc)
function PopupDialogCallback(returnString) {
    // hide protector overlay
    ProtectorOff();
    clickFunction("nothing");
    updateContent('cxPopup',returnString);
    document.getElementById('cxPopup').style.overflow = "auto";
    document.getElementById('cxPopupMask').style.display = "inline";  
    document.getElementById('cxPopup').style.display = "inline";
}

// open popup window with specified URL
function openNewWindow(theURL) {
    // hide protector overlay
    ProtectorOff();
    if (theURL.toUpperCase().indexOf("ERROR") == -1) {
		var newWin = open(theURL,"","menubar,toolbar,location,status,resizable,scrollbars");
    } else {
        alert(theURL);
    }
}

// display tool dialogs (reports, buffering, etc)
function VEBirdsEyeCallback(returnLatLong) {
    // hide protector overlay
    ProtectorOff();
    if (returnLatLong.toUpperCase().indexOf("ERROR") == -1) {
        var coordArray = returnLatLong.split("!");
        var veHeight = winHeight-headerHeight-borderWidth-50;
        var veWidth = winWidth-(sidebarWidth+frameWidth*2)-borderWidth*3-50;
        var theURL = "CustomPages/Default/VEBirdsEyeMap.html?velat=" + coordArray[0] + "&velong=" + coordArray[1] + "&veheight=" + veHeight + "&vewidth=" + veWidth;
        var theString = '<iframe id="InlinePrintFrame" width="100%" height="100%" scrolling="auto" frameborder="no" src="'+theURL+'" border="0"></iframe>';
        updateContent('cxPopup',theString);
        document.getElementById('cxPopup').style.overflow = "hidden";
        document.getElementById('cxPopupMask').style.display = "inline";  
        document.getElementById('cxPopup').style.display = "inline";
    } else {
        alert(theURL);
    }
}

// save selection result HTML for compilation of multi-layer selection display
function ProcessSelectionResults(theHTML) {
    var latestIndex = 0;
    if (selectedLayers.length == 0) {// if no layers selected yet, add it
        selectedLayers[0] = lastSelectLayer;
        selectedLayerResults[0] = theHTML;
    } else {// see if its been selected on before
        var notFound = true;
        for (var i=0;i<selectedLayers.length;i++)
        {
            if (selectedLayers[i] == lastSelectLayer) {
                selectedLayerResults[i] = theHTML;
                latestIndex = i;
                notFound = false;
            }
		}
		if (notFound) {// if not selected on before, add it
		    latestIndex = selectedLayers.length;
            selectedLayers[selectedLayers.length] = lastSelectLayer;
            selectedLayerResults[selectedLayerResults.length] = theHTML;
		}
    }
    // display all selections
    var tempHTML = selectedLayerResults[latestIndex];
    for (var i=0;i<selectedLayerResults.length;i++) { if (i != latestIndex) { tempHTML += selectedLayerResults[i]; } }
    updateContent('cxSidebar2', tempHTML);
    showSidebar(2);
}

// clear selection result HTMLs
function ClearSelectionResults(theHTML) {
    for (var i=0;i<selectedLayerResults.length;i++) { selectedLayerResults[i] = ""; }
    updateContent('cxSidebar2', theHTML);
    showSidebar(2);
}