
/////////////////////////////////////////////////
// Budget calculator functions (using buttons) //
/////////////////////////////////////////////////


function displayAsDollars(valueAsNumber) {
/*
 *This JavaScript takes an input number and adds commas in the proper places.
 *As needed by its original purpose, also adds a dollar.
 *Copyright 1998, David Turley <dturley@pobox.com> 
 *Feel free to use and build on this code as long as you include this notice.
 *Last Modified March 3, 1998
 *Adapted and modified 2009 by croose@gmail.com
*/
	var Num = valueAsNumber;
	var newNum = "";
	var newNum2 = "";
	var count = 0; 
 
	//this loop actually adds the commas   
	for (var k = Num.length-1; k >= 0; k--){
	  var oneChar = Num.charAt(k);
	  if (count == 3){
		newNum += ",";
		newNum += oneChar;
		count = 1;
		continue;
	  }
	  else {
		newNum += oneChar;
		count ++;
	  }
   }  //but now the string is reversed!
   
  //re-reverse the string
  for (var k = newNum.length-1; k >= 0; k--){
	  var oneChar = newNum.charAt(k);
	  newNum2 += oneChar;
  }
   
   // valueAsDollars = "$" + newNum2; // add dollar sign (optional)
   valueAsDollars = newNum2;
   return valueAsDollars;
} // end displayAsDollars function



// SPENDING COUNTER
// Adapted from military spending counter at http://www.milspend.org (niko@alum.mit.edu)	
// ===========================================================================
// Number Display and Updater

// NOTE: Eventually add cookies so it's per visit rather than per page load.

function calc_amount ()
{
	var currentTime = new Date ();
	var timeElapsed = currentTime - startTime;
	currentAmount = (timeElapsed * spendingRate);
}

function number_str (n)
{
	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;
}

function inc_totals_at_rate(rate)
{
	calc_amount ();
	$('#spending-counter').html("$" + number_str(currentAmount));
	setTimeout('inc_totals_at_rate('+rate+');', rate);
}
function inc_rounded_totals_at_rate(rate)
{
	calc_amount ();
	roundedAmount = currentAmount / 1000; // round this number to the tens of thousands
	// roundedAmount = round(roundedAmount);
	// roundedAmount = roundedAmount * 10000;
	$('#spending-counter-rounded').html("$" + number_str(roundedAmount) + ",000");
	// $('#spending-counter-rounded').html("$" + roundedAmount);
	setTimeout('inc_rounded_totals_at_rate('+rate+');', rate);
}

function inc_totals()
{
	inc_totals_at_rate(100); // counter will update every this number of milliseconds
}
function inc_rounded_totals()
{
	inc_rounded_totals_at_rate(500); // counter will update every this number of milliseconds
}



// jGrow jQuery plug-in auto-expands textareas as content grows
/*
* jGrow 0.2
* 08.02.2008
* 0.2 release: 04.03.2008
* 0.2.1 release: 15.09.2008	
*/
(function($) {
	$.fn.jGrow = function(options) {
		var opts = $.extend({}, $.fn.jGrow.defaults, options);
		return this.each(function() {
			$(this).css({ overflow: "hidden" }).bind("keyup", function() {
				$this = $(this);
				var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
				if(o.rows == 0 && (this.scrollHeight > this.clientHeight)) {
					this.rows += 1;
				} else if((this.rows <= o.rows) && (this.scrollHeight > this.clientHeight)) {
					this.rows += 1;
				} else if(o.rows != 0 && this.rows > o.rows) {
					$this.css({ overflow: "auto" });
				}
				$this.html();
			});
		});
	}

	$.fn.jGrow.defaults = { rows: 0 };

})(jQuery);
	
	

// When page loaded, invoke functions
// ==================================

$(document).ready(function () {   
	
	// Use SWFObject to embed Flash into selected elements once page loads
	$('#logo').flash({   
		swf: 'http://www.oneminuteforpeace.org/templates/oneminuteforpeace-logo.swf',
		width: 485,
		height: 130
	});  
	$('#progress-meter').flash({   
		swf: '/templates/progress-meter.swf',
		width: 64,
		height: 64
	});  
	
	
	
	// Create and start spending counter:
	
	spendingRate      = 30.86806; // military spending rate, in dollars per millisecond
	startTime         = new Date ();
	
	// create container for counter
	inc_totals();
	inc_rounded_totals();
	// $('#spending-counter').html("TEST");
	
	// Apply the jGrow jQuery function so ALL textareas will be auto-expanding
	$("textarea").jGrow({ rows: 30 });
	
	

	
});  // End $(document).ready() 																																																											