var Roi = {
	k : {
		fields : {
			1  : .125, // Add, Update Parents and Child Records
			2  : .125, // Update Child Immunization Records
			3  : .1,   // Create, Update School Directories
			4  : .125, // Create Letters & Labels
			5  : .125, // Create, Update Rosters/Attendance Lists
			6  : .125, // Create Permission Slips or Parental Notices
			7  : .125, // Maintain a Waitlist
			8  : .125, // Schedule, Organize Center Tours
			9  : .25,  // Update Schedules for Billing or Reporting
			10 : .125, // Process Tuition
			11 : .05,  // Collection Activities
			12 : .125, // Reconcile Account Ledger
			13 : .125, // Process Paperwork for Food Program
			14 : .1,   // Process Paperwork for Subsidized Program
			15 : 0     // Perform Data Backup
		},
		
		annualRate : 3588 // annual rate for OnCare services: 12 * $299 monthly
	},
	
	vals : {
		before             : {},
		after              : {},
		beforeTotal        : 0,
		afterTotal         : 0,
		hoursSaved         : 0,
		hourlyRate         : 12,
		moneySavedMonthly  : 0,
		moneySavedAnnually : 0,
		paysForItself      : ''
	},
	
	obj : {},
	
	changeField : function( $target ) {
		var val = $target.attr('value');
		
		if ( Roi.checkValid(val) ) {
			var fieldIndex = Roi.getIndex( $target );
						
			Roi.calculate( parseFloat(val), fieldIndex );
		}
		
	},
	
	checkValid : function( val, errorText) {
		if (parseFloat(val) == val) return 1;
		
		else alert( typeof(errorText) == 'undefined' ? 'Please enter hours as a number' : errorText);
	},
	
	getIndex : function( $target ) {
		var targetId = $target.attr('id');
		
		return parseInt( targetId.substr(12) );
	},
	
	calculate : function( val, fieldIndex ) {
		var afterVal = val * Roi.k.fields[ fieldIndex ];

		Roi.vals.before[ fieldIndex ] = val;
		Roi.vals.after[ fieldIndex ]  = afterVal;
		
		
		Roi.vals.beforeTotal = Roi.subtotaler( Roi.vals.before );
		Roi.vals.afterTotal  = Roi.subtotaler( Roi.vals.after );
		
		Roi.vals.hoursSaved = Roi.vals.beforeTotal - Roi.vals.afterTotal;
		
		Roi.changeHourValues( afterVal, fieldIndex );
	},
	
	subtotaler : function( theObj ) {
		var total = 0;
		for ( var theVal in theObj ) total += parseFloat(theObj[theVal]);
		
		return total;
	},
	
	changeHourValues : function( afterVal, fieldIndex ) {
		var $currResult = $('#table-result-' + fieldIndex, Roi.obj.table );
		Roi.animateChange( $currResult, afterVal );
		
		Roi.animateChange( Roi.obj.beforeTotal , Roi.vals.beforeTotal );
		Roi.animateChange( Roi.obj.afterTotal , Roi.vals.afterTotal );
		Roi.animateChange( Roi.obj.hoursSaved , Roi.vals.hoursSaved );
		
		Roi.calculateMoney();
	},
	
	changeHourlyRate : function() {
		var newRate = Roi.obj.hourlyRate.attr('value');
		
		// remove dollar sign
		newRate = newRate.replace(/^\$/, '');
		
		if ( Roi.checkValid( newRate, 'Please enter dollar amount' ) ) {
			Roi.vals.hourlyRate = newRate;
			
			Roi.calculateMoney();
		}
	},
	
	calculateMoney : function() {
		Roi.vals.moneySavedMonthly = Roi.vals.hoursSaved * Roi.vals.hourlyRate;
		Roi.vals.moneySavedAnnually = Roi.vals.moneySavedMonthly * 12;
		
		Roi.vals.paysForItself = Roi.vals.moneySavedMonthly ? Roi.k.annualRate / Roi.vals.moneySavedMonthly : '';
		
		Roi.changeMoneyValues();
	},
	
	changeMoneyValues : function() {
		Roi.animateChange( Roi.obj.moneySavedMonthly , Roi.vals.moneySavedMonthly, 1 );
		Roi.animateChange( Roi.obj.moneySavedAnnually , Roi.vals.moneySavedAnnually, 1 );
		Roi.animateChange( Roi.obj.paysForItself , Roi.vals.paysForItself );
	},
	
	animateChange : function( $node, val, moneyVal ) {

		if ( val == '' ) var formattedVal = 0;
		else var formattedVal = ( typeof(moneyVal) != 'undefined' && moneyVal ) ? '$' + val.toFixed(2) : val.toFixed(2);
		$node.html( formattedVal );
		
		//$node.fadeIn(250);
	},
	
	checkFilledInputs : function() {
		Roi.obj.inputs.each( function() {
			var $this = $(this);
			
			if ( $this.attr('value') ) Roi.changeField( $this );
		});
	},
	
	init : function() {
		// define objects for easy selection
		Roi.obj.table = $('#roi-table');
		Roi.obj.beforeTotal = $('#roi-before-total', Roi.obj.table );
		Roi.obj.afterTotal = $('#roi-after-total', Roi.obj.table );
		Roi.obj.hoursSaved = $('#roi-hours-saved', Roi.obj.table );
		Roi.obj.hourlyRate = $('#roi-hourly-rates', Roi.obj.table );
		Roi.obj.moneySavedMonthly = $('#roi-money-saved-monthly', Roi.obj.table );
		Roi.obj.moneySavedAnnually = $('#roi-money-saved-annually', Roi.obj.table );
		Roi.obj.paysForItself = $('#roi-pays-for-itself', Roi.obj.table );
		
		
		Roi.obj.inputs = $('INPUT.roi-input', Roi.obj.table );
		
		Roi.checkFilledInputs();
		Roi.changeHourlyRate();
		
		Roi.obj.inputs.change( function(ev) {
			$target = $(ev.target);
			
			Roi.changeField($target);
		});
		
		Roi.obj.hourlyRate.change( Roi.changeHourlyRate );
	}
};

$(function() {
	Roi.init();
});