/* Gizmo 5: Linked National Debt Clock
By Steve Stoft and Tim Wood - www.zfacts.com

<span id="NDC" class="9,c,0,2">Loading...</span> 
<a href="http://zfacts.com/p/461.html" id="zF12">National Debt</a> 
<script type="text/javascript" src="http://metapager.com/giz/G12ndc.js"></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

/* Changing var names to be consistent (can't change what is above since they
	are autoupdated by a script */
var zF12_GNDwhen = GNDwhen;
var zF12_GND = GND;
var zF12_GNDrate = GNDrate;

var zF12_off= 4;					// GMT offset from DC time, 5 winter, 4 summer
var zF12_timeStart	= (new Date ())/1000;		// Returns # of secs between 1970 GMT (=UTC) and JavaSript (client) start time
var zF12_GNDps 		= zF12_GNDrate*1000000000/(365*24*60*60);
var zF12_GNDstart	= zF12_GND + zF12_GNDps*(zF12_timeStart - str2date12(zF12_GNDwhen,zF12_off));
// startup parameters
var zF12_cps12 = 10;			// default = 10 Clicks per second
var zF12_cents12 = 0;		// default = 0 => no cents
var zF12_dollar12 = 1;		// default = 1 = do NOT show $ sign
var zF12_copies12 = 0;		// default = 1 of clocks on page (use 2 for 2, etc.)

var zF12_ID12 = 'NDC';		// Array of IDs for different variables
var zF12_atNDC = 0;			// pointer to clock tag

setTimeout(validate12, 100);		// setup and run clock

function validate12() {
	self = this;
	rPage = "http://zfacts.com/p/461.html";
	Lnks="";
	links=document.getElementsByTagName('a');
	for(i=0; i<links.length; i++)
	{ Lnk=links[i].href.toLowerCase();
		if(Lnk==rPage || Lnk=="http://zfacts.com")
		{ var txt=links[i].innerHTML;
			if(txt.length > 0 && txt!=' ')
			{ 
				self.ok=true;
				break;
			}
		}
	}
	if(!self.ok) {
		setTimeout(validate12, 500);
	}
	else {
		setup12();
	}
}

function setup12() {
	/* Check if GND vars were set by user */
	if(zF12.GNDwhen != 0)
		zF12_GNDwhen = zF12.GNDwhen;
	if(zF12.GND != 0)
		zF12_GND = zF12.GND;
	if(zF12.GNDrate != 0)
		zF12_GNDrate = zF12.GNDrate;
	
	zF12_GNDps = zF12_GNDrate*1000000000/(365*24*60*60);
	zF12_GNDstart = zF12_GND + zF12_GNDps*(zF12_timeStart - str2date12(zF12_GNDwhen,zF12_off));
	
	
	zF12_atNDC = document.getElementById(zF12_ID12);					// If span id is present
	s = zF12_atNDC.getAttribute("zF");	// get the parameter list
	if (s) {
		for (i=0; i<4; i++)
		{	if (s.length)
			{	ndx = s.indexOf(',')
				if (ndx==-1) { pram = s; s = ''; }
				else 			 
				{	pram = s.substring(0, ndx);
					s = s.substring(ndx+1);
				}
				if (pram=='0') pram = 0;
				if (i==0) zF12_cps12 = pram;		// clicks/sec
				if (i==1) zF12_cents12 = pram;	// show cents
				if (i==2) zF12_dollar12 = pram;	// don't show $ sign
				if (i==3) zF12_copies12 = pram;	// # of clocks
			}
		}
	}
	if (zF12_copies12>19) zF12_copies12 = 19;
	if (zF12_cps12<.01) zF12_cps12=.01; if (zF12_cps12>100) zF12_cps12=100;
	looper12();
}
function looper12()
{	secs = (new Date ())/1000 - zF12_timeStart;
	debt = num2strC12(zF12_GNDstart + zF12_GNDps*secs, zF12_cents12);
	if ( ! zF12_dollar12) debt = '$'+debt;
	toID12(debt); // GND
	setTimeout('looper12();', 1000/zF12_cps12);
}
function toID12(x)	// Write x to tag N  in HTML page
{	zF12_atNDC.firstChild.nodeValue = x;
	if (zF12_copies12>1) 
		for (i=2; i<zF12_copies12+1; i++)
		{	v = document.getElementById(zF12_ID12+i);	// write to sequence of tags in copies12>1
			if (v) v.firstChild.nodeValue = x;	// write to sequence of tags in copies12>1
		}
}
function str2date12(str,zF12_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 + zF12_off;
	return (Date.UTC(dA[1], dA[2], dA[3], dA[4], dA[5], dA[6]))/1000	// date in secs since 1970
}
function num2strC12(xNum,cents) 				// 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;
	if (cents)
	{
		xNum = xNum - xDols;		// number of cents
		var sCents = xNum.toString ();
		i = sCents.indexOf('.');
		if (i==-1)	sCents += '.00';				// if exactly zero, there's no decimal point.
		else			sCents += '00';
		sCents = sCents.substr(1,3);	// take decimal point and 2 digits
		
		return sign + dCom + sCents;	// dollars and cents
	}
	else
		return sign + dCom;			//  dollars not cents
}
function udat_12() {
	var file = 'http://zfacts.com/giz/udat/rec.php?gizName=G12ndc';
	//	var file = 'http://' +locate_16('udatProg')+ '?gizName=' + locate_16('gizName');
	var script = document.createElement('script');
	script.src = file + '&domain=' +document.domain+ '&location=' +document.location;
	script.type = 'text/javascript';				
	document.getElementsByTagName('head').item(0).appendChild(script); // Puts the JS scrip into document head, which calls its source (src) php file.
}

zF12 = new Object();

/* Used to store user's parameters */
zF12.GNDwhen = 0;
zF12.GND = 0;
zF12.GNDrate = 0;
