(function($){

var calculations = {
	'save_water' : function(inValue) {
		return parseInt(inValue) * (7000/551);
	},
	'save_trees' : function(inValue) {
		return parseInt(inValue) * (17/551);
	},
	'save_energy' : function(inValue) {
		return parseInt(inValue) * (4100/551);
	},
	'save_gas' : function(inValue) {
		return parseInt(inValue) * (178/551);
	},
	'save_trees2' : function(inValue) {
		return parseInt(inValue) * 0.0909;
	},
	'save_forest' : function(inValue) {
		return parseInt(inValue) * 0.0023;
	}
};

$(function(){

// Calculations
	function doCalculations(sender, calcType) {
		var inVal = $('#' + calcType).get(0).value;
		if (inVal == null || inVal == '')
			return;
		
		$('tr', $(sender).parent().next('table')).each(function(){
			var calcVal = Math.round(calculations[this.id](inVal)),
				outCell = $('td.amount', this);
			outCell.text(calcVal);
			outCell.css('opacity', 0);
			outCell.animate({'opacity': 1}, 650);
		});
	}
	
	$('#sheetsCalc').click(function(e){
		doCalculations(this, 'sheets');
	});
	
	$('#palletsCalc').click(function(e){
		doCalculations(this, 'pallets');
	});

// User interface
	var calculatorHeight = $('#calculator').outerHeight();
	$('#homeCalc > a').click(function(e){
		e.preventDefault();
		// slide down
		$('#calculator').css('height', '0px').animate({ 'height': calculatorHeight + 'px' });	
	});
	
	$('.calcClose').click(function(e){
		e.preventDefault();
		// slide up
		$('#calculator').animate({ 'height': '0px' });
	});
});


}(jQuery));
