
function JNDClock(startonLoad, interval){
  function JPNationalDebt(){
    //this.epoch       = 1143817200e3;   // 2006.04.01 00:00 in msec
    this.epoch         = 1143849600e3;   // 2006.04.01 09:00 in msec
    this.initDebt    = 770e12;
    this.debtPerMSec = 5e12 / (365*86400e3); 
    // http://www.soumu.go.jp/c-gyousei/pdf/020918_si2.pdf
    this.households  = 51102005;
    this.population  = 127055025;
    this.currentDebt = function(){
      var elapsed = (new Date()).getTime() - this.epoch; // in msec
      return this.initDebt + Math.round(this.debtPerMSec * elapsed);
    };
    this.perCapita = function(){
      return  Math.round( this.currentDebt() / this.population );
    };
    this.perHousehold = function(){
      return Math.round( this.currentDebt() / this.households );
    };
    this.kansuji = function(s){
    var kan = ['', '万','億', '兆', '京' ];
    s += '' // stringify
    var result = '';
    for (var i = s.length; i > 0; i--){ // a hack indeed
      result = s.charAt(i-1) + 
        ((s.length - i) % 4 ? '' : kan.shift()) + result;
      }
      return result;
    };
    this.kokka = function(){ return this.kansuji(this.currentDebt()) };
    this.kojin = function(){ return this.kansuji(this.perCapita()) };
    this.sedai = function(){ return this.kansuji(this.perHousehold()) };
  };
  function $(id){ return document.getElementById(id) };
  var interval_id = 0;
  var jnb = new JPNationalDebt();
  this.interval = interval || 1000;
  this.tick = function(){
    $('JNDClock.sedai').innerHTML = jnb.sedai();
    $('JNDClock.kojin').innerHTML = jnb.kojin();
    $('JNDClock.kokka').innerHTML = jnb.kokka();
  }
  this.toggle = function(){ this[ interval_id ? 'stop' : 'start' ]() };
  this.start = function(){ 
    interval_id = window.setInterval(this.tick, this.interval)
  };
  this.stop  = function(){ interval_id = window.clearInterval(interval_id) };
  this.html  = [
    '<table cellspacing="0" style="border:outset 0px;font-size:15" ',
    'onClick="_JNDClock.toggle()">',
    '</th></tr></thead>',
    '<tbody style="color:#000000; font-family:sans-serif"',
    'onmouseover="this.style.backgroundColor=\'#bababa\'" ',     
    'onmouseout="this.style.backgroundColor=\'transparent\'" >',
    '<tr><td>日本の借金</td><th align="right" id="JNDClock.kokka"></th><td>円</td></tr>',
    '<tr><td>世帯あたり</td><th align="right" id="JNDClock.sedai"></th><td>円</td></tr>',
    '<tr><td>一人あたり</td><th align="right" id="JNDClock.kojin"></th><td>円</td></tr>',
    '</tbody></table>'
  ].join(' ');
  document.write(this.html);
  this.tick()
  if (startonLoad) this.start();
};


