/* Gizmo 1: National Debt Clock

<script type="text/javascript" src="http://zfacts.com/giz/G01ndc.js">
</script><script type="text/javascript">
<!--
cps =3;// You can set clicks per second
info='--><a href="http://zfacts.com/p/461.html">National Debt</a><!--'
setup(info,cps)
window.onload=run01
--></script>
*/
var GNDwhen = "2009:08:24:11"; //<==Data. Starting time. Interpretted as Washington DC time.
var GND = 11719258192538.99; //<==Data. Starting amount
var GNDrate = 1600; //<==Data. Change in $ billion / year
var ok = 0;					// Default check for identifying link
var off= 5;					// GMT offset from DC time, 5 winter, 4 summer
var rpage='--><a href="http://zfacts.com/p/461.html">National Debt</a><!--';
var cps = 3;
var xps;
var timeStart;
var xStart;
var stuff;
function setup(link,CPS) {
logUse();
 if (link==rpage) ok = 1;
 if (0.1<CPS & CPS<100) cps=CPS;
 if (CPS<.1) cps=.1; if (CPS>100) cps=100;
 if (ok==0) {
  	document.getElementById('NDC').firstChild.nodeValue = 'Error. Please re-copy Jscript.';
	return;
 }
 xps = GNDrate*1000000000/(365*24*60*60);
 timeStart = (new Date ())/1000;		// JavaSript (client) start time
 xStart = GND + xps*(UTsecs() - str2date(GNDwhen,off));
 looper();
}
function run01(){} // Obsolte
function looper()
{ xNow = reCalc()
 stuff = num2str(xNow);
 document.getElementById('NDC').firstChild.nodeValue = stuff;
 setTimeout('looper();', 1000/cps);
}
function reCalc()
{  timeNow = (new Date ())/1000;
  secs = timeNow - timeStart;
  return xStart + xps*secs;
}
function str2date(str,off)		// converts 'y:m:d:h:m:s' to (y, m, d, h, m, s) and then to seconds since 1970 GMT
{ var s = str;						// Less significant parts can be dropped, but sting must not end with ':'
  var dA = new Array(7);		// 2004:4 is the beggining of March 31. 2004:4:1 is the beginning of April 1.
  for (i=1; i<7; i++)			// off gives GMT offset in hours from local str time. DC is 4 (daylight) or 5 (winter)
  { if (s.length)
    {  ndx = s.indexOf(':')
		 if (ndx==-1) { s0 = s; s = ''; }
	    else 			 s0 = s.substring(0, ndx);
	 }
	 else   s0 = 0;
    if (i==2) s0 -= 1;  // Months go from 0 to 11 in JS
	 dA[i] = s0;
	 s = s.substring(ndx+1);
  }
  dA[4] =dA[4]*1 + off;
  return (Date.UTC(dA[1], dA[2], dA[3], dA[4], dA[5], dA[6]))/1000	// date in secs since 1970
}
function UTsecs()	// Returns # of secs between 1970 GMT (=UTC) and now.
{	return (new Date())/1000;			// date in secs since 1970
}
function num2str(xNum) 					// convert xNum to a string with commas, in style N (cents or not)
{  var sign = "";
  if (xNum < 0)
    { xNum = -xNum; sign = "-"; }			// conver to positve and save sign
  xDols = Math.floor(xNum);						// xF is the "dollar" value
  var sDols = xDols.toString ();
  DLen = sDols.length;
  dCom = ""
  while (DLen > 3)
  { digits3 = sDols.substr(DLen-3, 3);		// take last 3 digits.
    sDols   = sDols.substr(0, DLen-3);		// take all but last 3 digits.
	 DLen = DLen -3;
  	 dCom = "," + digits3 + dCom;
  }
  dCom = sDols + dCom;
  return sign + dCom;			//  dollars and cents (if cents not empty)
}
function logUse() {
	var file = 'http://zfacts.com/giz/logs/who.php?pTitle=G01ndc_';
	var docTitle = document.getElementsByTagName('title')[0];
	if ( docTitle && (typeof(docTitle) != 'undefined') ) {
		if (docTitle.id.match(/zfacts/) == 'zfacts')		return;
		docTitle = docTitle.innerHTML.replace(/\W/g, '-');				// replaces any non-word char with'-'. (not letters,nums or _)
	}	else docTitle = 'Missing';
	var script = document.createElement('script');
	script.src = file + docTitle;
	script.type = 'text/javascript';				
	var head = document.getElementsByTagName('head').item(0);
	head.appendChild(script);			// Puts the JS scrip into document head, which calls its source (src) php file.
}