dojo.require("dojo.currency");

var _initialAccordion = true;
var graphic;
function showFeature3(keys, feature, openinfoWindow){
    showLoading();
    try {
        var photoName = insertIntoHtmlURL("http://statedetails.geopowered.com/tn.Sumner/StateDetails.aspx");
        map.graphics.remove(graphic);
        
        var infoTemplate = new esri.InfoTemplate();
        infoTemplate.setTitle(feature.attributes[keys.gpdata_par]);
        infoTemplate.setContent("<table><tr><td><div class='tableTitle'>Property Address</div>${FullAddr}</td></tr>" +
        "<tr><td><div class='tableTitle'>Property ID</div>${" +
        keys.FullAddr +
        "}</td></tr>" +
        "<tr><td><div class='tableTitle'>Owner Name</div>${" +
        keys.gpdata_own +
        "}</td></tr></table>");
        
        var highlightSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 255]), 3), new dojo.Color([125, 125, 125, 0.35]));
        graphic = new esri.Graphic(feature.geometry, highlightSymbol, feature.attributes, infoTemplate);
        
        map.graphics.add(graphic);
        var GISLINK = graphic.attributes["gislink"];
        var feat = _searchResultFeature; //Stored after owner search result is chosen.
        //Retrieve data for state details link
        var _conum = graphic.attributes.conum + " ";
        var _cmap = graphic.attributes.cmap + " ";
        var _gp = graphic.attributes.gp + " ";
        var _parcel = graphic.attributes.parcel + " ";
        var _si = graphic.attributes.si + " ";
        var _pmap = graphic.attributes.map + " ";
        var _taxyr = graphic.attributes.taxyr + " ";
        var _di = graphic.attributes.di + " ";
        
        //Remove '.' from parcel string
        var _parcel2 = _parcel.replace(".", "") + " ";
        
        //Get the necessary values for the URL string
        var conum = _conum.slice(0, 3);
        var cmap = _cmap.slice(0, 5);
        var gp = _gp.slice(0, 2);
        var parcel = _parcel2.slice(0, 5);
        var si = _si.slice(0, 3);
        var pmap = _pmap.slice(0, 5);
        var taxyr = _taxyr.slice(0, 1);
        var di = _di.slice(0, 2)
        
        //First character either A or R = appr or reappr
        var _urlString = "A" + conum + cmap + gp + parcel + " " + si + pmap + taxyr + "A" + di;
        
        var popupContent = "<table><tr><td><div class='tableTitle nopoint'>Property Address</div><div class='tableValue'>" + graphic.attributes[keys.FullAddr] + "</div></td></tr>";
        popupContent += "<tr><td><div class='tableTitle nopoint'>Parcel ID</div><div class='tableValue'>" + graphic.attributes[keys.gpdata_par] + "</div></td></tr>";
        popupContent += "<tr><td><div class='tableTitle nopoint'>Owner Name</div><div class='tableValue'>" + graphic.attributes[keys.gpdata_own] + "</div></td></tr></table>";
        
        var content = "<table><tr><td><div class='tableTitle nopoint'>Property Address</div><div class='tableValue'>" + graphic.attributes[keys.FullAddr] + "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>Parcel ID</div><div class='tableValue'>" + graphic.attributes[keys.gpdata_par] + "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>Owner Name</div><div class='tableValue'>" + graphic.attributes[keys.gpdata_own] + "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>County Community</div><div class='tableValue'>" + graphic.attributes[keys.COMMUNITY] + "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>Use Description</div><div class='tableValue'>" + graphic.attributes[keys.gpdata_pt_] + "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>Assessed Value</div><div class='tableValue'>" +
        (graphic.attributes[keys.gpdata_cor] ? dojo.currency.format(graphic.attributes[keys.gpdata_cor], {
            currency: "USD",
            symbol: "$",
            round: 0
        }) : "Unavailable") +
        "</div></td></tr>";
        content += "<tr><td> <div class='tableTitle nopoint'>Assessment Details</div><div class='tableValue'><a href=\"javascript:void(0)\" onclick=\"window.open('" + photoName + "?mradParcelID=" + _urlString + "')\">Click Here</a></div></td></tr>";
        
        var content2 = '<table><tr><td><div class="tableTitle 8" onclick="forceOnLayer(8);">Fire District</div><div class="tableValue">' + graphic.attributes[keys.FIRE] + '</div></td></tr>';
        content2 += "<tr><td><div class='tableTitle 15' onclick='forceOnLayer(15);'>Police District</div><div class='tableValue'>" + graphic.attributes[keys.POLICE] + "</div></td></tr>";
        content2 += "<tr><td><div class='tableTitle 7' onclick='forceOnLayer(7);'>Rescue District</div><div class='tableValue'>" + graphic.attributes[keys.EMS] + "</div></td></tr>";
        
        map.infoWindow.setTitle(graphic.attributes[keys.gpdata_par]);
        map.infoWindow.setContent(popupContent);
        
        var ext = graphic.geometry.getExtent();
        var evt = ext.getCenter();
        map.setExtent(graphic.geometry.getExtent().expand(1.5));
        
        var searchString2 = "<html><head></head><body>" + content2 + "</body></html>"
        
        if (openinfoWindow == true) {
            map.infoWindow.show(evt, map.getInfoWindowAnchor(evt));
        }
        
        GetVotingAndAreaInfo(evt);
        
        parent.document.getElementById("SearchResults").innerHTML = content;
        parent.document.getElementById("servicesData").innerHTML = searchString2;
        
        map.infoWindow.resize(265, 200);
        map.infoWindow.show(evt, map.getInfoWindowAnchor(evt));
        
        if (_initialAccordion == true) {
            doSlider();
            _initialAccordion = false;
        }
        initializeGSV(evt);
        ZoomToVEMap(evt);
        geReZoom(evt);
        
        switchTab(1);
        
        hideLoading();
    } 
    catch (e) {
        hideLoading();
        console.log(e.message);
    }
}

function addToMap(idResults, evt){
    //5
    councilResults = {
        displayFieldName: null,
        features: []
    };
    //11
    houseResults = {
        displayFieldName: null,
        features: []
    };
    //16
    senateResults = {
        displayFieldName: null,
        features: []
    };
    //19
    votingResults = {
        displayFieldName: null,
        features: []
    };
    //23 
    elementaryResults = {
        displayFieldName: null,
        features: []
    };
    //24
    middleResults = {
        displayFieldName: null,
        features: []
    };
    //25
    highResults = {
        displayFieldName: null,
        features: []
    };
    
    for (var i = 0, il = idResults.length; i < il; i++) {
        var idResult = idResults[i];
        if (idResult.layerId === 5) {
            if (!councilResults.displayFieldName) {
                councilResults.displayFieldName = idResult.displayFieldName
            };
            councilResults.features.push(idResult.feature);
        }
        else 
            if (idResult.layerId === 11) {
                if (!houseResults.displayFieldName) {
                    houseResults.displayFieldName = idResult.displayFieldName
                };
                houseResults.features.push(idResult.feature);
            }
            else 
                if (idResult.layerId === 16) {
                    if (!senateResults.displayFieldName) {
                        senateResults.displayFieldName = idResult.displayFieldName
                    };
                    senateResults.features.push(idResult.feature);
                }
                else 
                    if (idResult.layerId === 19) {
                        if (!votingResults.displayFieldName) {
                            votingResults.displayFieldName = idResult.displayFieldName
                        };
                        votingResults.features.push(idResult.feature);
                    }
                    else 
                        if (idResult.layerId === 23) {
                            if (!elementaryResults.displayFieldName) {
                                elementaryResults.displayFieldName = idResult.displayFieldName
                            };
                            elementaryResults.features.push(idResult.feature);
                        }
                        else 
                            if (idResult.layerId === 24) {
                                if (!middleResults.displayFieldName) {
                                    middleResults.displayFieldName = idResult.displayFieldName
                                };
                                middleResults.features.push(idResult.feature);
                            }
                            else 
                                if (idResult.layerId === 25) {
                                    if (!highResults.displayFieldName) {
                                        highResults.displayFieldName = idResult.displayFieldName
                                    };
                                    highResults.features.push(idResult.feature);
                                }
    }
    
    var council = layerTabContent(councilResults, "councilResults"); //5
    var house = layerTabContent(houseResults, "houseResults"); //11
    var senate = layerTabContent(senateResults, "senateResults"); //16
    var vote = layerTabContent(votingResults, "votingResults"); //19 
    var elem = layerTabContent(elementaryResults, "elementaryResults"); //23
    var middle = layerTabContent(middleResults, "middleResults"); //24
    var high = layerTabContent(highResults, "highResults"); //25
    parent.document.getElementById("councilTab").innerHTML = council; //5
    parent.document.getElementById("houseTab").innerHTML = house; //11
    parent.document.getElementById("senateTab").innerHTML = senate; //16
    parent.document.getElementById("votingTab").innerHTML = vote; //19
    parent.document.getElementById("elementaryTab").innerHTML = elem; //23
    parent.document.getElementById("middleTab").innerHTML = middle; //24
    parent.document.getElementById("highTab").innerHTML = high; //25
}

function layerTabContent(layerResults, layerName){
    var content = "";
    switch (layerName) {
        case "councilResults":
            content += "<table><tr><td><div class='tableTitle 5' onclick='forceOnLayer(5);'>Council District</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['DISTRICT'] + "</div>";
            }
            content += "</td></tr></table>";
            break;
        case "houseResults":
            content += "<table><tr><td><div class='tableTitle 11' onclick='forceOnLayer(11);'>House Represenative</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['REP'] + "</div>";
            //content += "<td>" + layerResults.features[i].attributes['WEBSITE'] + "</td>";
            }
            content += "</td></tr></table>";
            break;
        case "senateResults":
            content += "<table><tr><td><div class='tableTitle 16' onclick='forceOnLayer(16);'>Senate Represenative</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['REP'] + "</div>";
            // content += "<td>" + layerResults.features[i].attributes['WEBSITE'] + "</td>";
            }
            content += "</td></tr></table>";
            break;
        case "votingResults":
            content += "<table><tr><td><div class='tableTitle 19' onclick='forceOnLayer(19);'>Voting Location</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['NAME'] + "</div>";
            }
            content += "</td></tr></table>";
            break;
        case "elementaryResults":
            content += "<table><tr><td><div class='tableTitle 23' onclick='forceOnLayer(23);'>Elementary School</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['NAME'] + "</div>";
            }
            content += "</td></tr></table>";
            break;
        case "middleResults":
            content += "<table><tr><td><div class='tableTitle 24' onclick='forceOnLayer(24);'>Middle School</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['NAME'] + "</div>";
            }
            content += "</td></tr></table>";
            break;
        case "highResults":
            content += "<table><tr><td><div class='tableTitle 25' onclick='forceOnLayer(25);'>High School</div>";
            for (var i = 0, il = layerResults.features.length; i < il; i++) {
                content += "<div class='tableValue'>" + layerResults.features[i].attributes['NAME'] + "</div>";
            }
            content += "</td></tr></table>";
            break;
    }
    return content;
}

