/* Coffermap national page. © 2026 CyberMax. All rights reserved. */
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var esc = function (s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]; }); };
var states = [], counties = [], names = {}, sortS = { k: 'contracts', d: true }, sortC = { k: 'contracts', d: true }, shown = 100;
function tab(t) { $('tab-s').classList.toggle('on', t === 's'); $('tab-c').classList.toggle('on', t === 'c'); $('p-s').hidden = t !== 's'; $('p-c').hidden = t !== 'c'; }
$('tab-s').onclick = function () { tab('s'); };
$('tab-c').onclick = function () { tab('c'); renderC(); };
function renderS() {
var max = Math.max.apply(null, states.map(function (s) { return s.contracts; }));
$('st').tBodies[0].innerHTML = FF.sortBy(states, sortS.k, sortS.d).map(function (s) {
return '
| ' + esc(s.name) + ' | ' + FF.money(s.contracts) + ' | ' + FF.money(s.grants) + ' | ' + (s.perCapita == null ? '' : FF.dollars(s.perCapita)) + ' | ' + (s.population || 0).toLocaleString('en-US') + ' |
';
}).join('');
}
function current() { return FF.sortBy(FF.filterCounties(counties, $('q').value, $('fst').value), sortC.k, sortC.d); }
function renderC() {
var list = current();
$('ccount').textContent = list.length.toLocaleString('en-US') + ' counties' + (list.length > shown ? ' · showing ' + shown : '');
$('ct').tBodies[0].innerHTML = list.slice(0, shown).map(function (c) {
return '| ' + esc(c.county) + ' | ' + esc(c.state) + ' | ' + FF.money(c.contracts) + ' | ' + FF.money(c.grants) + ' | ' + (c.perCapita == null ? '' : FF.dollars(c.perCapita)) + ' |
';
}).join('');
$('more').hidden = list.length <= shown;
}
function sorter(tableId, state, render) {
document.querySelectorAll('#' + tableId + ' th[data-k]').forEach(function (th) {
th.style.cursor = 'pointer';
th.onclick = function () { var k = th.dataset.k; state.d = state.k === k ? !state.d : k !== 'name' && k !== 'county' && k !== 'state'; state.k = k; render(); };
});
}
sorter('st', sortS, renderS);
sorter('ct', sortC, renderC);
['q', 'fst'].forEach(function (id) { $(id).addEventListener('input', function () { shown = 100; renderC(); }); });
$('more').onclick = function () { shown += 200; renderC(); };
$('csv').onclick = function () {
var rows = current().map(function (c) { return { fips: c.fips, county: c.county, state: c.state, contracts_usd: c.contracts, grants_usd: c.grants, population: c.population }; });
var a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([FF.toCsv(rows, ['fips', 'county', 'state', 'contracts_usd', 'grants_usd', 'population'])], { type: 'text/csv' }));
a.download = 'coffermap-counties.csv'; document.body.appendChild(a); a.click(); a.remove();
};
fetch('data.json').then(function (r) { return r.json(); }).then(function (d) {
states = d.states.map(FF.state); counties = d.counties.map(FF.county);
states.forEach(function (s) { names[s.state] = s.name; });
var m = d.meta;
$('built').textContent = 'Fiscal year ' + m.fiscal_year + ' (' + m.window.start_date + ' to ' + m.window.end_date + '), built ' + m.built + '.';
$('stats').innerHTML = [[FF.money(m.contracts_usd), 'federal contracts, FY' + m.fiscal_year], [FF.money(m.grants_usd), 'federal grants, FY' + m.fiscal_year], [m.counties.toLocaleString('en-US'), 'counties with awards'], [m.top_county, 'top county for contract dollars']]
.map(function (s) { return '' + esc(s[0]) + '' + esc(s[1]) + '
'; }).join('');
$('fst').innerHTML = '' + FF.sortBy(states, 'name').map(function (s) { return ''; }).join('');
renderS();
if (location.hash === '#counties') { tab('c'); renderC(); }
}).catch(function () { $('stats').innerHTML = 'Could not load the data. Reload the page.
'; });
})();