// script base url
var base = 'http://www.iowa.gov/recovery';
//var base = 'http://localhost/projects/IowaStats/Recovery';

google.load("visualization", "1", {packages:["gauge"]});

// version info
var version = '1.5.0';

// graph data object
var cached_data = null;
var groups = null;

// execute
$(document).ready( function() {
	
	setTimeout('init();',800);
	
});

// resets everything
function init() {
	
	poll();
	update();
}

// after the updated chart has been created
function constructed() {
	
	$('.fundingicon').click(function () { 
		window.location.href = base + '/impact';
	});
        $('.obligatedicon').click(function () {
                window.location.href = base + '/obligated';
        });
        $('.spendingicon').click(function () {
                window.location.href = base + '/spending';
        });

        $('.funding').click(function () {
                window.location.href = base + '/impact';
        });
        $('.obligated').click(function () {
                window.location.href = base + '/obligated';
        });
        $('.spending').click(function () {
                window.location.href = base + '/spending';
        });


}

// pull data from server
function poll() {

	// locate the data table
	var offset = $('#datanav').position();
	
	// poll obligated and spending data
	var service = base + '/api/obligated.json?' + new Date().getTime();
	$.get(service, {}, function(data) {
		cached_data = data.pop();
		grind();
	}, "json");
	
}

// number crunching
function grind() {

	var funding = cached_data.estimate;
	var billions = Math.round((funding/1000000000)*Math.pow(10,2))/Math.pow(10,2);
	var obligated = cached_data.delivered;
	var spent = cached_data.amount;
	var obligatedpercent =  Math.ceil((obligated/funding)*100);
	var spendingpercent =  Math.ceil((spent/funding)*100);

	spent = Math.round((spent/1000000000)*Math.pow(10,2))/Math.pow(10,2);
        if(spent>1.0) {
                spent+=' billion';
        } else {
                spent = Math.round((cached_data.amount/1000000)*Math.pow(10,2))/Math.pow(10,2);
                spent+=' million';
        }

	obligated = Math.round((obligated/1000000000)*Math.pow(10,2))/Math.pow(10,2);
	if(obligated>1.0) {
		obligated+=' billion';
	} else {
		obligated = Math.round((cached_data.delivered/1000000)*Math.pow(10,2))/Math.pow(10,2);
                obligated+=' million';
        }

	$('div.fundingfill').html('<span class="txtpercent">100%</span>');
	$('div.fundingfill').animate( {'width' : "99.80%"}, { queue :false, duration :700 });
	$('.fundingnumbers').text('$' + billions + ' billion has been awarded.');
	
	$('div.obligatedfill').html('<span class="txtpercent">' + obligatedpercent + '%</span>');
	$('div.obligatedfill').animate( {'width' : (obligatedpercent) + '%'}, { queue :false, duration :700 });
	$('.obligatednumbers').text(dollars(obligated) + ' (' + obligatedpercent + '%) has been obligated, out of $' + billions + ' billion.');
	if(obligatedpercent<4) {
		$('div.obligatedfill span').css('left', '120%');
	}
//here	
	$('div.spendingfill').html('<span class="txtpercent">' + spendingpercent + '%</span>');
	$('div.spendingfill').animate( {'width' : (spendingpercent) + '%'}, { queue :false, duration :700 });
	$('.spendingnumbers').text(dollars(spent) + ' (' + spendingpercent + '%) has been spent, out of $' + billions + ' billion.');
	
	$('span.estimatedfunds').html(billions);
	constructed();
	
}

// polls for updated data and keeps the chart current
function update() {

	// keep data up to date
	var service = base + '/api/obligated.php';
	$.get(service, {}, function(data) {
		// don't display anything
		}, "json");

	// refresh every 30 minutes
	setTimeout('init();',1800000);
}

function dollars(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	var result = x1 + x2;
	return '$' + result;
}


