﻿var pointerHeight = 50;
var goodHeight = 275;

var searchWin;
var screenEvtPt;
var identifyTask, identifyParams;
var idGraphics;
var identifyFooterText = "";
var mTolerance = 1;

var clickMode = "ID";
var idworkingDivHTML = "<div style=\"text-align:center;font-size:1.2em; font-weight:bold;\"><p><img src=\"images/callbackActivityIndicator.gif\" alt=\"please wait\"/></p><p>Please Wait...</p></div>"

var idLayer;
//var mapIDLayers;
var idLayers = [];

var activeLayerInfo = "";

var idPointSymbol, idLineSymbol, idPolygonSymbol;

// ###############################################################
function doIdentify(evt) {

    if (currentTool == "MapIdentify") {

        hideLoading();

        // show the info window
        screenEvtPt = evt.screenPoint;
        var content = idworkingDivHTML;
        map.infoWindow.setContent(content);
        map.infoWindow.setTitle("");
        map.infoWindow.resize(125, 125);
        map.infoWindow.show(screenEvtPt, map.getInfoWindowAnchor(screenEvtPt));

        map.graphics.clear();
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = map.extent;
        identifyTask.execute(identifyParams, function(idResults) { identifyTask_onComplete(idResults, evt); }, identify_error);

    }
}



function identify_error(error) {

    console.debug(error);
    var content = "Error on the page...";
    map.infoWindow.setContent(content);
    map.infoWindow.setTitle("");
    map.infoWindow.resize(125, 75);
    map.infoWindow.show(screenEvtPt, map.getInfoWindowAnchor(screenEvtPt));
}

function initIdentifyFunctionality() {

    // first get the layers in the identify task:    
    idPointSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 255, 255]), 1), new dojo.Color([0, 255, 255], 0.25));
    idLineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([0, 255, 255]), 1);
    idPolygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([0, 255, 255]), 2), new dojo.Color([0, 255, 255], 0.25));

    mapIDLayers = defaultIDLayers;

    identifyTask = new esri.tasks.IdentifyTask(identifyURL);
    identifyParams = new esri.tasks.IdentifyParameters();
    identifyParams.tolerance = mTolerance;
    identifyParams.returnGeometry = true;
    identifyParams.layerIds = mapIDLayers;
    identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;

}

function updateIdentifyTolerance() {

    mTolerance = dijit.byId("identifyTolerance").getValue();
    identifyTask = new esri.tasks.IdentifyTask(identifyURL);
    identifyParams = new esri.tasks.IdentifyParameters();
    identifyParams.tolerance = mTolerance;
    identifyParams.returnGeometry = true;
    identifyParams.layerIds = mapIDLayers;
    identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;

}

function identifyTask_onComplete(idResults, evt) {
    
    if (idResults.length == 0) {
        map.graphics.clear();
        map.infoWindow.setContent("No Results Found!!");
        map.infoWindow.setTitle("Location");
        map.infoWindow.resize(200, 200);
        map.infoWindow.show(screenEvtPt, map.getInfoWindowAnchor(screenEvtPt));
        dojo.connect(map.infoWindow, "onHide", function() { map.graphics.clear(); });
        return;
    }
    
    idGraphics = [];
    var graphic, topGraphic;
    screenEvtPt = evt.screenPoint;
    var symbol;
    identifyFooterText = "";

    if (idResults.length > 1) {
        showMany(idResults);       
    }

   
    dojo.forEach(idResults, function(graphic) {
        processIdResult(graphic);
    });

    showIdentify(0);
}

function showMany(idResults) {

    var txt = "";
    txt += "<div style=\"margin:10px 10px 10px 10px\"><table cellspacing=\"1\" cellpadding=\"1\" border=\"0\" width=\"90%\">";
    txt += "<tbody>";
    dojo.forEach(idResults, function(idResult, i) {
        var graphic = idResult.feature;
        txt += "<tr><td>" + idResult.layerName + ":</td><td> <a href=\"#\" onmousedown=\"showIdentify(" + i + ");\" >" + graphic.attributes[idResult.displayFieldName] + "</a></td></tr>";
    });

    txt += "</tbody></table></div>";

    identifyFooterText = txt;

//    var p = getInfoWindowHeight();

//    map.infoWindow.setContent(txt);
//    map.infoWindow.setTitle("Results");
//    map.infoWindow.resize(p.width, p.height);
//    map.infoWindow.show(screenEvtPt, map.getInfoWindowAnchor(screenEvtPt));
//    dojo.connect(map.infoWindow, "onHide", function() { map.graphics.clear(); });

}


function processIdResult(idResult) {

    var graphic = idResult.feature;

    // if parcels
    switch (idResult.layerName) {
        case "Parcels":
            graphic.setSymbol(parcelHighlightSymbol);
            var content = custom.parcels.getDetails(graphic);

            var infoTemplate = new esri.InfoTemplate(idResult.layerName, content);
            graphic.setInfoTemplate(infoTemplate);
            
            
            break;
        default:

            var s = [];
            s.push("<div class=\"gray\">");
            s.push("<table cellspacing=\"1\" cellpadding=\"1\" border=\"0\" width=\"90%\"><tbody>");
            s.push("<tr><td class=\"titlebar2\" align=\"right\" width=\"50%\"><b>FieldName</b></td><td class=\"titlebar2\"><b>Value</b></td></tr>");

            var featureAttributes = graphic.attributes;
            for (att in featureAttributes) {
                s.push("<tr><td class=\"r1\" width=\"50%\" align=\"right\"><b>" + att + ":<b>&nbsp;</td><td class=\"r2\" width=\"50%\" align=\left\">" + featureAttributes[att] + "<td/></tr>");
            }

            s.push("</tbody></table></div>");


            // Create template for the info window
            var infoTemplate2 = new esri.InfoTemplate(idResult.layerName, s.join(""));
            graphic.setInfoTemplate(infoTemplate2);

            //point | multipoint | polyline | polygon | extent
            switch (graphic.geometry.type) {
                case "point":
                    graphic.setSymbol(idPointSymbol)
                    break;
                case "line":
                    graphic.setSymbol(idLineSymbol);
                    break;
                case "polygon":
                    graphic.setSymbol(idPolygonSymbol);
                    break;
            }
            break;
    }

    idGraphics.push(graphic);

}



function showIdentify(index, zoomTo) {

    map.graphics.clear();

    var graphic = idGraphics[index];

    // show the graphic...
    if (graphic != null) {

        map.graphics.add(graphic);

        if (map.height < 300) {
            console.debug("Small map");

//            showSidePanel("details-sidebar");
//            dojo.byId("details-sidebar-content").innerHTML = graphic.getContent();
//            dojo.byId("details-sidebar-header").innerHTML = graphic.getTitle();

        }
        else {
            console.debug(graphic);

            var p = getInfoWindowHeight();
          
            var content = graphic.getContent();
            if (idGraphics.length > 1)
                content = content + "<hr/>" + identifyFooterText + "<p style=\"text-align:center\"><a href=\"javascript:map.infoWindow.hide();\">[ close ]</a>";

            if (zoomTo)
                zoomMap(graphic.geometry);

            map.infoWindow.setContent(content);
            map.infoWindow.setTitle(graphic.getTitle());
            map.infoWindow.resize(p.width, p.height);
            map.infoWindow.show(screenEvtPt, map.getInfoWindowAnchor(screenEvtPt));
            dojo.connect(map.infoWindow, "onHide", function() { map.graphics.clear(); });

        }
    }

}


function getInfoWindowHeight() {

    var w = 325;
    var h = goodHeight;
    var dHeight = (map.height / 2) - pointerHeight;

    if (h > dHeight)
        h = dHeight;

    return { width: w, height: h }

}
















