﻿// display_mapidentify.js


var identifyFilePath = "";
var identifyImageType = "png";
var identifyPane;


var m_identGraphicFeature = null;
function removeIdentifyGraphic() {
    if (m_identGraphicFeature != null) {
        var map = $find('Map1');
        map.removeGraphic(m_identGraphicFeature);
        m_identGraphicFeature.dispose();
        m_identGraphicFeature = null;
        map.refreshGraphics();
    }
}

// Set up Identify tool
function MapIdentify(divid) { 
//    MapPoint(divid, "MapIdentify", false);
//    $addHandler($get(divid),"mousedown",MapIdClick);
    var map = $find('Map1');
    map.getGeometry(ESRI.ADF.Graphics.ShapeType.Point, MapIdClick, null, 'black', null, 'pointer', true);
}

// Event handler for Identify
function MapIdClick(geom,e) {
    
    Ext.Msg.wait('Getting Identify Results');
    var map = $find('Map1');
    removeIdentifyGraphic();
    var geomString, type = '';
    var style = null;

    FCMapsResults_NCSIdentify1.Init();
    
    FCMapsResults_NCSIdentify1.ClearResults();
    var theButtons;
    if (limitAccess)
    {
        theButtons = [{ text: 'Zoom to Selected', handler: FCMapsResults_NCSIdentify1.ZoomSelected },
                    { text: 'Clear Results', handler: FCMapsResults_NCSIdentify1.ClearResults },
                    { text: 'Print', handler: FCMapsResults_NCSIdentify1.StartPrint }];
    }
    else
    {
        theButtons = [{ text: 'Zoom to Selected', handler: FCMapsResults_NCSIdentify1.ZoomSelected },
                    { text: 'Clear Results', handler: FCMapsResults_NCSIdentify1.ClearResults },
                    { text: 'Buffer Selected Results', handler: FCMapsResults_NCSIdentify1.ShowBufferWindow },
                    { text: 'Export to CSV', handler: FCMapsResults_NCSIdentify1.StartExcelExport },
                    { text: 'Print', handler: FCMapsResults_NCSIdentify1.StartPrint } ];
    }
    
    if (!identifyPane)
    {
        identifyPane = new Ext.Window({
            id:'identifyPane',
            layout:'fit',
            width:600,
            height:370,
            closeAction:'hide',
            plain: true,
            contentEl: 'IdentifyContainer',
            title: 'Identify Results',
            minimizable: true,
            resizable: false,
            buttons: theButtons
        });
        identifyPane.on('hide', function(){ removeIdentifyGraphic(); FCMapsResults_NCSIdentify1.ClearResults(); }, this, { single:false } );
        identifyPane.on('minimize', MinimizeWindow, identifyPane);
        identifyPane.show();        
    } else {
        identifyPane.show();
    }    
    
    //var map = $find("Map1");
    map.cursor = $get("Map1").style.cursor;
	//map.divObject.style.cursor = "wait";
	//getXY(e);
	var box = calcElementPosition("Map1");
	zleft = mouseX - box.left;
	ztop = mouseY - box.top;

//	map.xMin=zleft;
//	map.yMin=ztop;
//	var div = document.getElementById("IdentifyLocation");
//	if (div==null) {
//	    addIdentifyLocation();
//	}
    if (ESRI.ADF.Geometries.Point.isInstanceOfType(geom)) {
        geomString = geom.toString(':');
        type = 'point';
        style = new ESRI.ADF.Graphics.MarkerSymbol("images/identify-map-icon.png", 11, 23);
    }
    
    m_identGraphicFeature = $create(ESRI.ADF.Graphics.GraphicFeature,
		        { "id": "IdentifyPointIcon", "geometry": geom, "symbol": style });
		        
    map.addGraphic(m_identGraphicFeature);
    
//	map.getTopLeftTile();
	var fpBody = document.getElementById('IdentifyContainer');
	var html = fpBody.innerHTML;
	fpBody.innerHTML = "<div id='identifyWorkingContainer'><img src='images/callbackActivityIndicator.gif' align='middle'/> Getting Information. . .</div>" + html;
//	showFloatingPanel('Results');
	fpBody=document.getElementById('IdentifyContainer');
	if (fpBody.style.display=="none")
	    toggleFloatingPanelState('Results','images/collapse.gif','images/expand.gif');


//	var message = "ControlID=Map1&ControlType=Map&EventArg=MapIdentify&Map1_mode=MapIdentify&minx=" + zleft + "&miny=" + ztop;
//	var context = "Map1";
//	eval(map.identifyCallbackFunctionString);

    var coordString = geomString;
    var context = map.get_id();
    var message = 'ControlID=' + map.get_id() + '&EventArg=MapIdentify&coords=' + geomString;
    
    eval("WebForm_DoCallback('__Page',message,processIdentifyCallbackResult,context,null,true)");
	
	
//	var div = document.getElementById("IdentifyLocation");
//	// point is bottom center... 2 pixels up for shadow
//	var cWidth = Math.floor(div.clientWidth / 2);
//	var cHeight = div.clientHeight;
//	//alert(cWidth + " x " + cHeight); // width and height might not be available on first time.... if so, approximate size needed
//	if (cWidth==0) cWidth = 12;
//	if (cHeight==0) cHeight = 29;
//	var idLeft = zleft - parseInt(map.divObject.style.left) - cWidth;
//	var idTop = ztop - parseInt(map.divObject.style.top) - cHeight + 2; // add two back for icon bottom
//	if (isIE) {
//	    idTop+=2; 
//	    //idLeft+=2;
//	}
//	window.setTimeout('moveLayer("IdentifyLocation", ' + idLeft + ', ' + idTop + '); showLayer("IdentifyLocation");', 0);
//	map.mode = map.tempMode;
//	map.actionType = map.tempAction;
//	map.cursor = map.tempCursor;
	return false;	
}
function addIdentifyLocation() {
    var content = '<div id="IdentifyLocation" style="position: absolute; left: 0px; top: 0px; visibility: hidden;">';
    if (isIE  && ieVersion < 7 && (identifyImageType.toLowerCase()=="png")) 
	    content += '<img src="' + identifyFilePath + 'images/blank.gif" alt="" border="0"  hspace="0" vspace="0" style="filter:  progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + identifyFilePath + 'images/identify-map-icon.png\');" />\n';
	else
	    content += '<img src="' + identifyFilePath + 'images/identify-map-icon.png" alt="" border="0"  hspace="0" vspace="0" />\n';
    content += '</div>';
    map.overlayObject.insertAdjacentHTML("BeforeEnd", content);
}

function processIdentifyCallbackResult(s)
{
    
    Ext.Msg.hide();
    try
    {
        var tryagain = true;
        while (tryagain)
        {
            var workingDiv = document.getElementById('identifyWorkingContainer');
            if (workingDiv == null)
            {
                tryagain = false;
            }
            else
            {
                document.getElementById('IdentifyContainer').removeChild(workingDiv);
            }
        }
    }catch(e){}
    ESRI.ADF.System.processCallbackResult(s);
}
