/**
 * @author Jim Darden
 * @contact jdarden@dardenhome.com
 */
document.write('<div><img alt="Uncle Sam IOU" src="http://www.dardenhome.com/images/uncle_sam_iou.png" /></div>');
document.write('<div id="iou-uncle-sam" style="font-size:25px"></div>');
document.write('<div>Current United States Debt</div>');
var iouSpan = document.getElementById("iou-uncle-sam");

function getCurrentUSDebt() {
	// 2009 debt      12,311,400,000,000
	// 2010 estimated 14,456,300,000,000
	// difference      2,144,900,000,000
	// seconds in a year 31536000
	// $68,014.33 of debt every second
	
	// get the current number of seconds 
	var beginningOfYear = new Date(2010,0,1,0,0);
	var now = new Date();
	var oneDay=1000*60*60*24;
	
	// number of seconds since the beginning of the year
	var secondsSinceBeginningofYear=Math.ceil((now.getTime()-beginningOfYear.getTime())/(1000));
	
	return "$" + commaFormatted(currencyToString((12311400000000) + (68014.33 * secondsSinceBeginningofYear)));	
}

function currencyToString(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';	
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;	
}

function commaFormatted(amount) {
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	/*if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }*/
	amount = n;
	amount = minus + amount;
	return amount;
}

function doDebtCalc() {
	window.iouSpan.innerHTML = getCurrentUSDebt(); 	
}	
// run this once so the amount shows up immediately
doDebtCalc();
// now set the interval
setInterval("doDebtCalc()", 1000);

