Goverment shutdown

by Igor Cuckovic

HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://shutdown.outline.com/static/js/underscore.string.js"></script>
<div class='home-banner jumbotron' id='template-wrapper'>
    <h1 class='centered'>The U.S. Federal Government has been shut down for 
      <u class='number-figure'>&nbsp;&nbsp;&nbsp;</u> days,
      <u class='number-figure'>&nbsp;&nbsp;&nbsp;</u> hours,
      <u class='number-figure'>&nbsp;&nbsp;&nbsp;</u> minutes,
      <u class='number-figure'>&nbsp;&nbsp;&nbsp;</u> seconds </h1>
    <h1 class='centered'>and so far it has cost <br>
      <u class='number-figure number-figure-huge'></u></h1>
  </div>


<script type='text/template' id='banner-template'>
  <h1 class='centered'>The U.S. Federal Government has been shut down for 
    <u class='number-figure'><%= days %></u> day<%= days_pluralize %>,
    <u class='number-figure'><%= hours %></u> hour<%= hours_pluralize %>,
    <u class='number-figure'><%= minutes %></u> minute<%= minutes_pluralize %>,
    <u class='number-figure'><%= seconds %></u> second<%= seconds_pluralize %> </h1>
  <h1 class='centered'>and so far it has cost <br>
    <u class='number-figure number-figure-huge'><%= cost %>
        </u></h1>
</script>

JavaScript

(function loadHomepageJS(){
    if (!window.location.href.match(/\/$/)) {
        window.location = '/';
    }

    var template = _.template($('#banner-template').html());

    var startDate = 1380600000000; //Math.abs(new Date('2013-10-1 EDT'));

    var SECONDS = 1000,
        MINUTES = SECONDS * 60,
        HOURS = MINUTES * 60,
        DAYS = HOURS * 24;

    var _s = _.string;

    var addCommas = function(str) {
        var tmp = _s.reverse(str);
        tmp = tmp.replace(/([0-9]{3})/g, '$1,');
        var val = _s.reverse(tmp);
        if (val[0] === ',') {
            val = val.substring(1, val.length);
        }   
        return val;
    };

    function numberFormat(n) {
        var str = Math.round(n) + '';
        return '$' + addCommas(str);
    }


    function updateValues() {
        var currentDate = Math.abs(new Date()),
            timeDiff = currentDate - startDate;
        //console.log('=======>', currentDate, startDate, timeDiff);
        var days = Math.floor(timeDiff / DAYS),
            hours = Math.floor((timeDiff % DAYS) / HOURS),
            minutes = Math.floor((timeDiff % HOURS) / MINUTES),
            seconds = Math.floor((timeDiff % MINUTES) / SECONDS);
        var cost = timeDiff / HOURS * 12500000;
        var vals = {days: days,
                    hours: hours,
                    minutes: minutes,
                    seconds: seconds,
                    cost: numberFormat(cost),

                    // Pluralizations
                    days_pluralize: days === 1 ? '' : 's',
                    hours_pluralize: hours === 1 ? '' : 's',
                    minutes_pluralize: minutes === 1 ? '' : 's',
                    seconds_pluralize: seconds === 1 ? '' : 's'
                   };
        $('#template-wrapper').html(template(vals));
    }

    updateValues();
    window.setInterval(updateValues, 1000);
    console.log(HOURS);
    
})();