// costofwar-main.js 


// Set up some global variables:



var curamount=0;  	   // total amount spent on war in dollars
var curscale=0;   	   // number of dollars per alternate scale
var curunit="";   	   // name of the alternate scale
var geographicscale=1; // scale the final number by this amount

// ===========================================================================
// Geographic Scaling

// var states = getStates();
// var geoData = getGeoData();



var costOfAg = 0;
var costOfIraq = 0;
var costOfTotal = 0;

// This function is called from the links below that change the unit.
// It updates the global variables above, which are in turn read by
// the function inc_totals() below which actually modifies the content
// of the web page that you see.
function set_unit (altscale, altunit) {
  curscale = altscale;
  curunit = altunit;
}

var startFiscalYear = new Date('Sat Oct  1 00:00:00 2011');  // start of current fiscal year

 
function calculateIraq (multiplier) {
  var previouslyAllocated = 797310713001;		 // amount previously allocated for all prior years combined
  var ratePerMS = 0.318675948653 ;		// the rate per MS of the war for current fiscal year
  var curDate = new Date();				// today's date
  var diff = curDate - startFiscalYear;			// MS between today and start of fiscal year
  costOfIraq = (previouslyAllocated + (diff * ratePerMS)) * multiplier;    // cost of Iraq war at this time
}

function calculateAg (multiplier) {
  var previouslyAllocated = 459786487495;		// amount previously allocated for all prior years combined
  var ratePerMS = 3.51199622774;		// the rate per MS of the war for current fiscal year
  var curDate = new Date();				// today's date
  var diff = curDate - startFiscalYear;			// MS between today and start of fiscal year
  costOfAg = (previouslyAllocated + (diff * ratePerMS)) * multiplier;    // cost of Afghanistan war at this time
}


function calculateTotal () {
  costOfTotal = (costOfAg + costOfIraq);  // costoftotal = cost of iraq + cost of afghanistan
  if (costOfTotal <= 0) {
    alert ("costofwar.com uses your computers date to calculate the cost. "+
           "Yours must be wrong because according to your computer the war "+
           "hasn't even started yet!");
  }
}

// Converts a number 'n' to a string with commas every three characters.
function number_str (n) {
  //var x = n.toFixed (0);  this doesn't seem to work on some browsers
  var x = n.toString ();
  var dot = x.lastIndexOf ('.');
  x = x.substr (0, dot);
  var l = x.length;
  var res = "";
  for (l -= 3; l > 0; l -= 3) {
    res = "," + x.substr (l, 3) + res;
  }
  res = x.substr (0, l+3) + res;
  return res;
}

// This function actually modifies the web page --- first it determines
// the total dollars spent on the war via calc_amount(), then it modifies
// the two items in the web page, which are identified by their id ('raw'
// and 'alt').  It then sets itself up to be called again in 200 milliseconds
// so as to continuously update the page.
function updateCostOfWar(rate,multiplier) {
  	calculateAg(multiplier);
	calculateIraq(multiplier);   // first calculate total dollars spent.
	calculateTotal(multiplier); 
	
	$('#costOfTotal').text("$" + number_str(costOfTotal));
 	$('#costOfIraq').text("$" + number_str(costOfIraq));     // update all three counters
  	$('#costOfAg').text("$" + number_str(costOfAg)); 
  	setTimeout('updateCostOfWar('+rate+','+multiplier+');', rate); 	// run this function again in 100 milliseconds 
}



