﻿var adjoinersTask, adjoinersQuery;
var adjoinerFeature = null;
var adjoinerFeatures = null;
var adjoinerGraphics = null;
var adjoinerPointGraphics = null;
var adjoinerSymbol, adjoinerParcelSymbol;

function adjoiners_startup() {

    adjoinersTask = new esri.tasks.QueryTask(searchURL + "/" + custom.parcels.layerID);
    adjoinersQuery = new esri.tasks.Query();
    adjoinersQuery.spatialRelationship = esri.tasks.Query.SPATIAL_REL_TOUCHES;
    adjoinersQuery.outFields = custom.parcels.returnFields;
    adjoinersQuery.returnGeometry = true;

    adjoinerGraphics = new esri.layers.GraphicsLayer({ id: "adjoiners", displayOnPan: dojo.isIE ? false : true })
    adjoinerPointGraphics = new esri.layers.GraphicsLayer({ id: "adjoinerspoints", displayOnPan: dojo.isIE ? false : true })
    map.addLayer(adjoinerPointGraphics);
    map.addLayer(adjoinerGraphics);

    // Create symbols for result features
    adjoinerSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 255, 255]), 3);
    adjoinerParcelSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 2);
}

function doAdjoiners(pid) {

    showPanel("search");
    showSearchPanel("adjoiners");
    console.debug(parcelGraphics.graphics.length);
    if (parcelGraphics.graphics.length > 0) {
        dojo.style("adjoinersBackButton", { display: "block" });
    } else {
        dojo.style("adjoinersBackButton", { display: "none" });
    }

    clearCurrentTool();
    parcelGraphics.hide();
    bulletGraphics.hide();
    allSelectionGraphics.hide();
    adjoinerGraphics.clear();
    adjoinerPointGraphics.clear();
    map.infoWindow.hide();

    var queryTask = new esri.tasks.QueryTask(searchURL + "/" + custom.parcels.layerID);
    var query = new esri.tasks.Query();
    query.where = "Parcel_ID = '" + pid + "'";
    query.outFields = custom.parcels.returnFields;
    query.returnGeometry = true;

    //Execute task and call showResults on completion
    queryTask.execute(query, function(results) {
        var graphic = results.features[0];
        adjoinerFeature = graphic;

        // first write the header for the adjoiners
        dojo.byId("adjoiners-header").innerHTML = custom.parcels.adjoinerHeader(graphic);
        adjoinersQuery.where = "Parcel_ID <> '" + pid + "'";
        adjoinersQuery.geometry = graphic.geometry;
        adjoinersTask.execute(adjoinersQuery, adjoinersCompleted, function(error) { console.debug(error); alert("error"); });

    }, function(error) { console.debug(error); });
}

function adjoinersCompleted(results) {

    if (results.features.length > 0) {
        adjoinerFeatures = results.features;

        var report = custom.parcels.processAdjoiners(results, true);
        dojo.byId("adjoiners-report").innerHTML = report;

        // add the graphics to the map...
        dojo.forEach(adjoinerFeatures, function(graphic, indx) {

            // add the point to the map;
            var fExtent = graphic.geometry.getExtent();
            var centerPt = new esri.geometry.Point;
            centerPt = fExtent.getCenter();
            var marker = "images/markers/marker" + (indx + 1) + ".png";
            var mSym = new esri.symbol.PictureMarkerSymbol(marker, 20, 34);
            var gPt = new esri.Graphic(centerPt, mSym);
            adjoinerPointGraphics.add(gPt);

            graphic.setSymbol(adjoinerSymbol);
            adjoinerGraphics.add(graphic);

        });

        adjoinerFeature.setSymbol(adjoinerParcelSymbol);
        adjoinerGraphics.add(adjoinerFeature);
        adjoinerGraphics.show();
        adjoinerPointGraphics.show();

        try {
            var fullExt = esri.graphicsExtent(adjoinerGraphics.graphics);
            if (fullExt != null)
                map.setExtent(fullExt.expand(1.1));
        } catch (Error) {
            console.warn("An Error occurred in the application in routine 'zoomToSelected'", Error);
        }

        adjoiners_show();

        console.debug("done");
    } 

}

function adjoiners_GoBack() {

    parcelGraphics.show();
    bulletGraphics.show();
    allSelectionGraphics.show();
    adjoinerGraphics.clear();
    adjoinerPointGraphics.clear();
    adjoinerGraphics.hide();
    adjoinerPointGraphics.hide();
    showSearchPanel("results");

}

function clearAdjoiners() {

    dojo.byId("adjoiners-details").innerHTML = "";
    dojo.byId("adjoiners-report").innerHTML = "";

    adjoinerGraphics.clear();
    adjoinerPointGraphics.clear();
}

function showAdjoinerDetails(index) {

    // zoom to the graphic and highlight
    try {
        var detailGraphic = adjoinerGraphics.graphics[index];
        if (detailGraphic != null) {

            var parcels_content = custom.parcels.getDetails(detailGraphic);
            infoTemplate = new esri.InfoTemplate("parcels", parcels_content);
            detailGraphic.setInfoTemplate(infoTemplate);
            dojo.byId("adjoiners-details-content").innerHTML = detailGraphic.getContent(); ;
            map.infoWindow.hide();
            if (detailGraphic.geometry != null) {
                // Create template for the info window
                try {

                    // this is a work around for problem with dojo shape is null when shape is not in the current extent
                    //  problem occurred at v1.4
                    zoomAfterLoadEvntHandle = dojo.connect(map, "onExtentChange", function() {
                        //                        try {
                        //                            detailGraphic.setSymbol(parcelHighlightSymbol);
                        //                        } catch (Error) {
                        //                            console.debug("Still getting the error...Try Again?");
                        //                        }
                        dojo.disconnect(zoomAfterLoadEvntHandle);
                    });

                    //
                    mapHelper.zoomMap(detailGraphic.geometry.getExtent().expand(1.3));
                } catch (Error) {
                    console.debug("error");
                }
            }

            adjoiners_show("details");
        }
    }

    catch (Err) {
        console.debug(Err)
    }

}

function zoomToAdjoiner(index) {

    try {

        var selectedGraphic = adjoinerGraphics.graphics[index];
        if (selectedGraphic != null) {
            mapHelper.zoomMap(selectedGraphic.geometry.getExtent().expand(1.3));
        }
    }
    catch (Err) {
        console.debug(Err)
    }

}

function zoomToAllAdjoiners() {

    try {
        var fullExt = esri.graphicsExtent(adjoinerGraphics.graphics);
        if (fullExt != null)
            map.setExtent(fullExt.expand(1.1));
    } catch (Error) {
        console.warn("An Error occurred in the application in routine 'zoomToSelected'", Error);
    }
}

function adjoiners_show(panel) {

    if (panel == "details") {
        dojo.style("adjoiners-details", { display: "block" });
        dojo.style("adjoiners-report", { display: "none" });

    } else {
        dojo.style("adjoiners-details", { display: "none" });
        dojo.style("adjoiners-report", { display: "block" });
    }
}




function exportAdjoiners() {
    custom.parcels.Export(adjoinerFeatures);
}


