// #########################################################################
//
//  getDetails
//  getBrief
//  getReport
//  adjoinerHeader
//
//
//
// ##########################################################################



dojo.provide("dijits.custom.Parcels");


dijits.custom.Parcels.getDetails = function(graphic, indx) {

    var s = [];
    s.push("<div class=\"detail-section\">");
    s.push("<div class=\"captionbru\">Owner</div>");
    s.push("<div>${AcctName1}</div>");
    s.push("<div>${AcctName2}</div>");
    s.push("<div>${MailCity}, ${MailState} ${MailZipCode}</div>");
    s.push("</div>");

    s.push("<div class=\"detail-section\">");
    s.push("<div><table width=\"100%\"><tbody><tr><td colspan=\"2\" class=\"captionbru\">Account Info</td></tr>");
    s.push("<tr><td>NCPIN: </td><td>${NCPIN}</td></tr>");
    s.push("<tr><td>Account#: </td><td>${AcctNumber}</td></tr>");
    s.push("<tr><td>Parcel-id: </td><td>${PID} </td></tr>");
    s.push("</tbody></table></div>");
    s.push("</div>");

    s.push("<div class=\"detail-section\">");
    s.push("<div><table width=\"100%\"><tbody><tr><td colspan=\"2\" class=\"captionbru\">Property Info</td></tr>");
    s.push("<tr><td>Acreage:</td><td>${TotalCalcAcres}</td></tr>");
    s.push("<tr><td>Land Units:</td><td>${LandUnits} (${UnitsType})</td></tr>");
    s.push("<tr><td>Legal:</td><td>${LegalDesc}</td></tr>");
    s.push("<tr><td>Deed Ref:</td><td>${PlatBook} - ${PlatPage}</td></tr>");
    s.push("<tr><td>Deferred Value:</td><td>${DeferredValue}</td></tr>");
    s.push("<tr><td>Total Value:</td><td>${MarketValue}</td></tr>");
    s.push("<tr><td>Property Address:</td><td>${FullPropAddress}</td></tr>");
    s.push("</tbody></table></div>");
    s.push("</div>");

    s.push("<div class=\"detail-section\">");
    s.push("<table width=\"90%\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding-left: 0.3em\">");
    var e = graphic.geometry.getExtent();
    var zoomcmd = "zoomtoExtent(" + e.xmin + "," + e.ymin + "," + e.xmax + "," + e.ymax + ",1.3);";
    s.push("<tr><td><img src=\"images/details/zoomtofeature.gif\"></td><td><a href=\"#\" onmousedown=\"" + zoomcmd + "\" >Zoom to Feature</a></td></tr>");
    s.push("<tr><td><img src=\"images/details/i_properties.gif\"></td><td><a href=\"http://maps.caldwellcountync.org/cgi-bin2/caldwell/CreatePRC_2.pl?Card=001&recNum=${PID}\" target=\"_blank\">Property Record Card</a></td></tr>");
    //s.push("<tr><td><img src=\"images/details/compsales1.png\"></td><td><a href=\"#\" onmousedown=\"doInitCompSales('${PID}');\">Comp Sales</a></td></tr>");
    s.push("<tr><td><img src=\"images/details/adjoiners.png\"></td><td><a href=\"#\" onmousedown=\"doAdjoiners('${PID}')\">Adjoining Parcels</a>    </td></tr>");
    s.push("<tr><td><img src=\"images/details/buffer.gif\"></td><td><a href=\"#\" onmousedown=\"doSearchByDistance('${PID}')\">Search By Distance</a>    </td></tr>");
    s.push("<tr><td><img src=\"images/details/print.gif\"></td><td><a href=\"#\" onmousedown=\"printQuickReport('${PID}');\">Quick Report</a></td></tr>");
    //s.push("<tr><td><img src=\"images/details/map.gif\"></td><td><a href=\"#\" onmousedown=\"showPrintableMapProducts('${PID}');\">Printed Map Products</a></td></tr>");
    s.push("</tbody></table></div>");

    return s.join("");
}

dijits.custom.Parcels.getBrief = function(graphic, indx) {

    if (indx == null)
        indx = 0

    var s = [];
    s.push("<div class=\"detail-section\">");
    s.push("<div><a href=\"#\" onmousedown=\"showDetails(" + indx + ");\" title=\"more information\">${AcctName1}</a></div>");
    s.push("<div>${AcctName2}</div>");
    s.push("<div>${MailCity}, ${MailState} ${MailZipCode}</div>");
    s.push("</div>");

    s.push("<div class=\"detail-section\">");
    s.push("<div><table><tbody>");
    s.push("<tr><td>NCPIN: </td><td>${NCPIN}</td></tr>");
    s.push("<tr><td>Account#: </td><td>${AcctNumber}</td></tr>");
    s.push("<tr><td>Parcel-id: </td><td>${PID} </td></tr>");
    s.push("<tr><td>Land Units:</td><td>${LandUnits} (${UnitsType})</td></tr>");
    s.push("<tr><td>Property Address: </td><td>${FullPropAddress}</td></tr>");
    s.push("</tbody></table></div>");

    s.push("<div class=\"detail-section\">");
    s.push("<table width=\"90%\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding-left: 0.3em\">");
    s.push("<tr><td>");
    s.push("<input type=\"image\" onclick=\"zoomTo(" + indx + ");\" class=\"resultsicon\" src=\"images/zoomtofeature.gif\" alt=\"zoomto\" title=\"zoom to feature\" />");
    s.push("<a href=\"http://maps.caldwellcountync.org/cgi-bin2/caldwell/CreatePRC_2.pl?Card=001&recNum=${PID}\" target=\"_blank\"><img class=\"resultsicon\" src=\"images/details/i_properties.gif\"  title=\"Show property record card\"></a>");
    //s.push("<tr><td><img src=\"images/details/compsales1.png\"></td><td><a href=\"#\" onmousedown=\"doInitCompSales('${PID}');\">Comp Sales</a></td></tr>");
    s.push("<a href=\"#\" onmousedown=\"doAdjoiners('${PID}');\"><img class=\"resultsicon\" src=\"images/details/adjoiners.png\" title=\"show adjoining parcels\"></a>");
    s.push("<a href=\"#\" onmousedown=\"doSearchByDistance('${PID}')\"><img class=\"resultsicon\" src=\"images/details/buffer.gif\"  title=\"Search by Distance\"></a>");
    s.push("<a href=\"#\" onmousedown=\"printQuickReport('${PID}');\"><img class=\"resultsicon\" src=\"images/details/i_print.png\" title=\"print quick report\"></a>");
    //s.push("<a href=\"#\" onmousedown=\"showPrintableMapProducts('${PID}');\"<img class=\"resultsicon\" src=\"images/details/map.gif\" title=\"create a PDF map product\"></a>");
    s.push("</tbody></table></div>");

    s.push("</div>");

    return s.join("");

}

dijits.custom.Parcels.getReport = function(graphic) {
    var s = [];
    s.push("<table width=\"600px\"><tbody><tr><td>");
    s.push("<div class=\"detail-section\">");
    s.push("<div class=\"captionbru\">Owner</div>");
    s.push("<div>${AcctName1}</div>");
    s.push("<div>${AcctName2}</div>");
    s.push("<div>${MailCity}, ${MailState} ${MailZipCode}</div>");
    s.push("</div>");
    s.push("</td><td>");

    s.push("<div class=\"detail-section\">");
    s.push("<div><table width=\"100%\"><tbody><tr><td colspan=\"2\" class=\"captionbru\">Account Info</td></tr>");
    s.push("<tr><td>NCPIN: </td><td>${NCPIN}</td></tr>");
    s.push("<tr><td>Account#: </td><td>${AcctNumber}</td></tr>");
    s.push("<tr><td>Parcel-id: </td><td>${PID} </td></tr>");
    s.push("</tbody></table></div>");
    s.push("</div>");

    s.push("</td></tr>");
    s.push("<tr><td colspan='2'>");

    s.push("<div class=\"detail-section\">");
    s.push("<div><table width=\"100%\"><tbody><tr><td colspan=\"2\" class=\"captionbru\">Property Info</td></tr>");
    s.push("<tr><td>Acreage:</td><td>${TotalCalcAcres}</td></tr>");
    s.push("<tr><td>Land Units:</td><td>${LandUnits} (${UnitsType})</td></tr>");
    s.push("<tr><td>Legal:</td><td>${LegalDesc}</td></tr>");
    s.push("<tr><td>Total Value:</td><td>${MarketValue}</td></tr>");
    s.push("<tr><td>Property Address:</td><td>${FullPropAddress}</td></tr>");
    s.push("</tbody></table></div>");
    s.push("</div>");

    s.push("</td></tr>");
    s.push("</tbody></table>");

    return s.join("");
}


dijits.custom.Parcels.adjoinerHeader = function(graphic) {

    var s = [];
    s.push("<div class=\"detail-section\">");
    s.push("<h3>Adjoiner Report for: " + getFieldValue(graphic, fields.ncpin) + "</h3>");
    s.push("<div>" + getFieldValue(graphic, fields.owner) + "</div>");
    s.push("<div>" + getFieldValue(graphic, fields.mailingaddress) + "</div>");
    s.push("<div>" + getFieldValue(graphic, fields.mailcity) + " " + getFieldValue(graphic, fields.mailstate) + " " + getFieldValue(graphic, fields.mailzip) + "</div>");
    s.push("</div>");

    return s.join("");

}

dijits.custom.Parcels.getRow = function(graphic, indx, cssClass) {


    var pin = graphic.attributes["NCPIN"];
    var pid = graphic.attributes["PID"];
    var owner = graphic.attributes["AcctName1"];

    var s = [];
    s.push("<tr class=\"" + cssClass + "\">");
    s.push("<td>" + owner + "</td>");
    s.push("<td>" + pin + "</td>");
    s.push("<td>" + pid + "</td>");
    s.push("</tr>");

    return s.join("");

}

dijits.custom.Parcels.getRowHeader = function() {

    var s = [];
    s.push("<tr class=\"titlebar\">");
    s.push("<td width=\"150px\">Owner</td>");
    s.push("<td>NCPIN</td>");
    s.push("<td>Parcel-id</td>");
    s.push("</tr>");

    return s.join("");

}


dijits.custom.Parcels.processAdjoiners = function(results, showNav) {

    var t = [];
    var s = [];
    s.push("<div class=\"gray\">");
    s.push("<table  border=\"0\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\"><tbody>");
    s.push("<tr class=\"titlebar\">");

    if (showNav) {
        s.push("<td>&nbsp;</td><td>&nbsp;</td>");
    }

    //s.push("<td>Parcel ID</td><td>Owner</td></tr>");
    s.push("<td colspan=\"2\">&nbsp;</td></tr>")

    dojo.forEach(results.features, function(graphic, indx) {


        // Get the CSS Class for the row...
        var cssClass = "r1";
        if (indx % 2 == 0)
            cssClass = "r2";

        var propid = "adj_prop_" + indx;

        if (showNav) {
            var zoomcmd = "zoomToAdjoiner(" + indx + ");";
            var detailscmd = "showAdjoinerDetails(" + indx + ");";
        }

        var marker = "<img src='./images/markers/marker" + (indx + 1) + ".png' />";

        s.push("<tr class=\"" + cssClass + "\">");
        s.push("<td rowspan=\"5\" valign=\"center\">" + marker + "</td>");
        s.push("<td><strong>Pin:</strong></td><td>" + getFieldValue(graphic, fields.ncpin) + "</td>");
        s.push("</tr>");

        s.push("<tr class=\"" + cssClass + "\">");

        s.push("<td><strong>Owner:</strong></td><td>" + getFieldValue(graphic, fields.owner) + "</td>");
        s.push("</tr>");

        s.push("<tr class=\"" + cssClass + "\">");
        s.push("<td><strong>Acreage:</strong></td><td>" + getFieldValue(graphic, fields.acreage) + "</td>");
        s.push("</tr>");

        s.push("<tr class=\"" + cssClass + "\">");
        //if (showNav) { s.push("<td>&nbsp;</td><td>&nbsp;</td>"); }
        s.push("<td><strong>Tax Value:</strong></td><td>" + getFieldValue(graphic, fields.taxvalue) + "</td>");
        s.push("</tr>");

        s.push("<tr class=\"" + cssClass + "\">");
        //if (showNav) { s.push("<td>&nbsp;</td><td>&nbsp;</td>"); }
        s.push("<td><strong>Legal Desc:</strong></td><td>" + getFieldValue(graphic, fields.legaldesc) + "</td>");
        s.push("</tr>");

        if (showNav) {
            s.push("<tr class=\"" + cssClass + "\">");
            s.push("<td>&nbsp;</td>");
            s.push("<td><img src=\"images/zoomtofeature.gif\"/><a href=\"javascript:void(0);\" onmousedown=\"" + zoomcmd + "\">Zoom to</a></td>");
            s.push("<td><img id=\"" + propid + "\" src=\"images/i_properties.gif\"  /><a href=\"javascript:void(0);\" onmousedown=\"" + detailscmd + "\">More Info</a></td>");
            s.push("</tr>");
        }




    });
    s.push("</tbody></table></div>");

    //s.push(t.join(""));


    return s.join("");
}


dijits.custom.Parcels.getDataStore = function(features) {

var items = [];

    dojo.forEach(features, function(f, indx) {

        var item = { id: indx,
            ncpin: getFieldValue(f, fields.ncpin),
            pid: getFieldValue(f, fields.pid),
            owner: getFieldValue(f, fields.owner),
            mailingaddress: getFieldValue(f, fields.mailingaddress),
            mailcity: getFieldValue(f, fields.mailcity),
            mailstate: getFieldValue(f, fields.mailstate),
            mailzip: getFieldValue(f, fields.mailzip),
            taxvalue: getFieldValue(f, fields.taxvalue),
            acctnumber: getFieldValue(f, fields.acctnumber),
            acreage: getFieldValue(f, fields.acreage),
            size: getFieldValue(f, fields.size),
            units: getFieldValue(f, fields.units),
            legaldesc: getFieldValue(f, fields.legaldesc),
            address: getFieldValue(f, fields.address)
        }
        items.push(item);
    });

    return items;

}

dijits.custom.Parcels.Export = function(features) {

        var items = dijits.custom.Parcels.getDataStore(features);

        //Create data object to be used in store
        var data = {
            identifier: "id", //This field needs to have unique values
            items: items
        };

        var iframeDocument = dojo.isIE ? printingHiddenFrame.contentWindow.document : printingHiddenFrame.contentDocument;
        iframeDocument.getElementById("exportData").value = dojo.toJson(data);
        iframeDocument.getElementById("export").submit();

}
