// unfinished

function SavingsCalculator(id, options) {
	var sc = {
	
		options: {
			hours: 			'hours',
			rate:  			'rate',
			costPerYear: 	'cost-per-year',
			savingPerYear: 	'savings-per-year',
			roi:		 	'roi',
			planSelect: 	'recommended',
			plan1: 			'gst',
			plan2: 			'payroll',
			plan3: 			'bookkeeping',
			plan4: 			'performance'
		},
		
		planCosts: [
			169,
			169,
			169,
			169,
			249,
			249,
			249,
			249,
			249,
			249,
			249,
			249,
		],
		plans_matrix: [ 
		  // g,b,p,r
		 	[1,0,0,0], // TaxBasic
		 	[1,1,0,0], // TaxBasic
		 	[1,0,1,0], // TaxBasic
		 	[1,1,1,0], // TaxBasic
		 	[1,0,0,1], // AdvisorPlus
		 	[0,0,0,1], // AdvisorPlus
		 	[0,0,1,1], // AdvisorPlus
		 	[0,1,0,1], // AdvisorPlus
		 	[0,1,1,1], // AdvisorPlus
		 	[1,0,1,1], // Platnum
		 	[1,1,0,1], // Platnum
		 	[1,1,1,1]  // Platnum
		],
		
		init: function (op) {
			/*
			$("#"+this.options.hours+", #"+this.options.rate).keyup(function () { 
				sc.annualCost();
				sc.showAdditionalCosts(); 
			});
			$(".plantype").click(function () { sc.selectRecommendedPlan() });
			*/
			$('#calculate-button').click(function () {
				sc.selectRecommendedPlan()
				sc.annualSavings();
				$('#calculate-row').css('display','');
				$('.request-quote-rows').css('display','');
				
				$.fancybox.resize();
				
				sc.__trackCalculateEvent();
				return false;
			});
		},
		
		annualCost: function () {
			var hours = $('#'+this.options.hours).val();
			var rate = $('#'+this.options.rate).val();
			if (hours != "" && rate != "") {
				var annualcost = parseInt(hours) * parseInt(rate) * 52;
				$("#"+this.options.costPerYear).text("$"+this.__formatAsDollar(annualcost,0));
				
				return annualcost;
			}
			
			return 0;
		},
		
		annualSavings: function () {
			var annualcost = this.annualCost();
			var totalprice = this.showAdditionalCosts();
			
			if (!isNaN(annualcost) && !isNaN(totalprice)) {
				var savings = annualcost - (totalprice * 12);
				if (savings < 0) savings = 0;
				
				var roi = parseInt(savings / annualcost * 100).toFixed(0);
				
				$("#"+this.options.savingPerYear).text("$"+this.__formatAsDollar(savings,0)+"  ("+roi+"%)");
				return savings;
			}
			
			return 0;
		},
		
		getChosenPlanMatrix: function() {
			return [
				($("#"+this.options.plan1).is(":checked") ? 1 : 0),
				($("#"+this.options.plan2).is(":checked") ? 1 : 0),
				($("#"+this.options.plan3).is(":checked") ? 1 : 0),
				($("#"+this.options.plan4).is(":checked") ? 1 : 0)
			];
		},
		
		getPlan: function() {
			var plan = 0;
			var chosen_matrix = this.getChosenPlanMatrix();
			
			for (var i in this.plans_matrix) {
				if (chosen_matrix.join() == this.plans_matrix[i].join()) {
					plan = this.planCosts[i];
				}
			}
			
			return plan;
		},
		
		selectRecommendedPlan: function() {
			var plan = this.getPlan();
			
			if (plan != 0) {
				$("#"+this.options.planSelect+' option[value='+plan+']').attr('selected','selected');
				$("#rec-plan").text($("#"+this.options.planSelect+' option[value='+plan+']:selected').text()+" monthly plan");
				//$('#recommend-plan-row select').attr('disabled',false);
			} else {
				$("#rec-plan").text("");
				$("#"+this.options.planSelect+' option[value=-]').attr('selected','selected');
				//$('#recommend-plan-row select').attr('disabled','disabled');
			}
			
			this.showAdditionalCosts();
		},
		
		showAdditionalCosts: function(plan) {
			var totalprice = 0;
			var plan = plan || this.getPlan();
			var chosen_matrix = this.getChosenPlanMatrix();
			
			$("#pluspayroll").css('display', (chosen_matrix[1] ? '' : 'none') );
			$("#plusbookkeeping").css('display', (chosen_matrix[2] ? '' : 'none') );
			if (chosen_matrix[1] || chosen_matrix[2]) {
				$("#additional-costs").css('visibility', 'visible');
				
				var hours = parseInt( $('#'+this.options.hours).val() ) || 0;
				var rate = 100;
				var faster = 0.75;
				var split = 1;
				if (chosen_matrix[0] || chosen_matrix[3]) {
					rate = 65
				}
				var split  = chosen_matrix[2] ? 0.6 : 0;
					split +=  chosen_matrix[1] ? 0.125 : 0;
				
					totalprice = plan + (split * hours * rate * faster * 52 * (1/12));
			} else {
				$("#additional-costs").css('visibility', 'visible');
				totalprice = plan;
			}
			$("#plan-price-per-mth").text("$"+this.__formatAsDollar(totalprice, 0));
			
			return totalprice;
		},
		
		__trackCalculateEvent: function () {
			_gaq.push(['_trackEvent', 'Request a Quote', 'Calculate', 'Savings Calculator']);
		},
		
		__formatAsDollar: function(nStr, decimal) {
			var decimal = (decimal === undefined) ? 2 : 0;
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? x[1].substr(0,decimal) : '00'.substr(0,decimal);
			x2 = x2 ? '.'+x2 : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return (x1 + x2);
		}
	
	}
	
	sc.init(id, options);
	return sc;

}

