application.core.extend({
  calculator: {  
    SavingsCalculator: function(config) {
	  this.storeConfig({ 
	    container: '#savings-calculator'
	  }, config);
		
	  return this.init();
    }
  }
});

application.core.extend(
  application.calculator.SavingsCalculator.prototype, 
  application.base.Base.prototype, 
  {  
  	init: function() {
	  this.ready(function() {
		this.setProp('container', $(this.retrieveConfigValue('container')));
		this.setProp('form', $('form', this.getProp('container')));
		this.setProp('results', $('div.results', this.getProp('container')));		
		
		this.setProp('field1', $('input[name=numberOfEmployees]', this.getProp('form')));
		this.setProp('field2', $('input[name=hoursWorked]', this.getProp('form')));
		this.setProp('field3', $('input[name=employeeHourly]', this.getProp('form')));
		this.setProp('field4', $('input[name=accountantHourly]', this.getProp('form')));
		this.setProp('field5', $('input[name=payPeriods]', this.getProp('form')));		
		this.setProp('field6', $('input[name=payPeriodDays]', this.getProp('form')));		
		
		this.initResults();
		
		this.setProp('message', $('div.message', this.getProp('container')));
		
		this.message('');
		
		this.setProp('result1', $('h1:eq(0) span', this.getProp('results')));
		this.setProp('result2', $('h1:eq(1) span', this.getProp('results')));
		this.setProp('result3', $('h1:eq(2) span', this.getProp('results')));
		this.setProp('result4', $('h1:eq(3) span', this.getProp('results')));		
	
		this.bind();
	  });
	  
	  return this;	
	},
	
	initResults: function() {
	  $('h1', this.getProp('results')).prepend('<span></span>');
	  
	  this.getProp('results').before('<div class="message"></div>');
	},
	
	bind: function() {
	  var self = this;
	  
	  this.getProp('form').bind('submit', this.delegate(this.calculate));
	},
	
	calculate: function() {
	  var WEEKS = 52;
	  var ANNUAL_HUMAN_ERROR = 0.04;
	  var FIVE_MINUTES = 0.083;
	  var TEN_MINUTES = 0.167; 
	  
	  var f1 = this.getValue('field1'), 
	  	  f2 = this.getValue('field2'), 
		  f3 = this.getValue('field3'), 
		  f4 = this.getValue('field4'), 
		  f5 = this.getValue('field5'),
		  f6 = this.getValue('field6');
		  
	  var results = {};
	  
	  results.r1 = f1 * f2 * WEEKS * f3 * ANNUAL_HUMAN_ERROR;
	  results.r2 = (f1 * f5) * (f4 * FIVE_MINUTES);
	  results.r3 = (f5 * f6) * (f1 * TEN_MINUTES) * f3;
	  results.r4 = results.r1 + results.r2 + results.r3;

	  this.display(results);
	},
	
	display: function(results) {
	  if (results.r1 && results.r2 && results.r3) {
	    this.message(this.getLang('savingCalculatorWorking'));
		
		this.timeout(this, function() {
		  this.setValue('result1', results.r1); 
	      this.setValue('result2', results.r2); 
	      this.setValue('result3', results.r3); 
	      this.setValue('result4', results.r4);		
		  
		  this.message(this.getLangFormatted('savingCalculatorDone', [this.formatCurrency(results.r4)]));
		}, 1000);
	  } else {
		this.error();
	  }
	},
	
	message: function(value, loading) {
	  var message = this.getProp('message');
	  
	  message.html(value);

	  if (!value.length) {
	    message.hide();
	  } else {
		message.fadeIn('slow');  
	  }
	},
	
	error: function() {
	  this.clearValues();
	  this.message(this.getLang('savingCalculatorError'));
	},
	
	getValue: function(k) {
	  var v = this.getProp(k).val();
	  v = /^(\d+|\d+\.\d+|\.\d+)$/.test(v) ? parseFloat(v) : 0;

	  return isNaN(v) ? 0 : v;
	},
	
	setValue: function(k, v) {
	  var r = this.getProp(k).hide();  
	  
	  r.html(v ? this.formatCurrency(v) : '');
	  
	  if (v) {
		r.show('slow');  
	  }	  
	},
	
	clearValues: function() {
	  this.setValue('result1'); 
	  this.setValue('result2'); 
	  this.setValue('result3'); 
	  this.setValue('result4');		
	},
	
	formatCurrency: function(c) {
	  return application.util.formatCurrency(c);	
	}
  }  
);   
