/*

	Pacific Power Energy Efficiency Calculator Javascript
	ISITE Design

	Assumes global.js is loaded which includes jquery noconflict code assigned jQuery to $j.

*/

// settings
// name matches class on corresponding row of table which is used for look up
var multipliers = {
	'thermostat' : 		{ 'dollars' : 210, 'co2' : 1965 },
	'computer' : 		{ 'dollars' : 40, 'co2' : 800 },
	'bulbs' : 			{ 'dollars' : 4, 'co2' : 76 },
	'tvs' : 			{ 'dollars' : 11, 'co2' : 204 },
	'refridgerator' : 	{ 'dollars' : 73, 'co2' : 1368 },
	'washer' : 			{ 'dollars' : 18, 'co2' : 345 }
};

// Dom loaded / jQuery
jQuery(function($) {
	
	// some actions are yes/no, others require input values
	// separate handlers for each
	$('.check-toggle').live('click',toggleAction);
	$('.calc-input').live('keyup',evaluateAction).inputClear();
		
});// document ready

// yes/no calculations
// receives event object
function toggleAction(e) {			
	e.preventDefault();								  
	var $tr = $j(this).parents('tr'),
		type = $tr.attr('class').split(' ')[0], // get the first class only, used for multiplier look up
		state = $tr.hasClass('on');
		
	$tr.toggleClass('on');
	
	// if on, put in value. otherwise, put in 0
	$tr.find('.result-dollars .savings span').text(state ? '0' : addCommas(multipliers[type]['dollars']));
	$tr.find('.result-co2 .savings span').text(state ? '0' : addCommas(multipliers[type]['co2']));	

	total();
}

// value calculations
// receives event object
function evaluateAction(e){		
	e.preventDefault();	
	var $tr = $j(this).parents('tr'),
		type = $tr.attr('class').split(' ')[0], // get the first class only, used for multiplier look up
		state = $tr.hasClass('on'),
		value = normalize(removeCommas($j(this).val())), // comma-less value for computations, normalized for reason
		num = !isNaN(value);
	
	$tr[num && value !=0 ? 'addClass' : 'removeClass']('on');

	// if validate numeric input, put in comma-ized value. otherwise, put in 0
	$tr.find('.result-dollars .savings span').text(num ? addCommas(value * multipliers[type]['dollars']) : '0');
	$tr.find('.result-co2 .savings span').text(num ? addCommas(value * multipliers[type]['co2']) : '0');

	total();	
}

// display total savings of all selected actions
function total() {	
	var dollars = calculateTotal('dollars'),
		co2 = calculateTotal('co2');
	
	$j('.total-dollars .savings span').text(dollars);
	$j('.total-co2 .savings span').text(co2);
	
	$j('.row-total')[removeCommas(dollars) > 0 || removeCommas(co2) > 0 ? 'addClass' : 'removeClass']('on');
}

// return sum of all savings for specified type
// expects: dollars or co2
function calculateTotal(type) {
	var sum = 0;
	$j('.result-'+type+' .savings span').each(function(){	
		sum += removeCommas($j(this).text());								  
	});
	return addCommas(sum);
}

// return number string with commas inserted at thousands
function addCommas(str) {
		str += '';
	var x = str.split('.'),
		x1 = x[0],
		x2 = x.length > 1 ? '.' + x[1] : '',
		rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2');	}
	return x1 + x2;
}

// return integer with commas remove	
function removeCommas(str) { return parseInt(str.replace(/\,/g,'')); }

// normalize number between 0 and 9999
function normalize(num) { return Math.abs(num > 9999 ? 9999 : num); }

// reset inputs if left blank and clear if default when focus
jQuery.fn.inputClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) { this.value = ""; }
	}).blur(function() {
		if( !this.value.length ) { this.value = this.defaultValue; }
	});
};

