/* 

FRI - Cost Calculator

Base Cost = $2500.00

*/

$(document).ready(function() {
	var firstTime = true;
	
	$.fn.calculator = function(options) {
		var inputSelection = "#cost-form input#numUsers";
		var userCost = 0;
		var contactUs = '';
		var overLimit = false;
		var noUsers = false;
		
		// // console.log(numUsers);
		
		// options for monetary values. "Base rate" and each "Bracket rate" per number of users
		var options = $.extend({
			baseRate : 2500,
			bracketOne : 125,
			bracketTwo : 100,
			bracketThree : 75,
			bracketFour : 75
		}, options);
		
		var numUsers = 0;
		numUsers = $(inputSelection).val();
		numUsers = numUsers.replace(/,/g,'');
		// // console.log(numUsers);
		
		
		// // console.log(numUsers.length);
		if(numUsers >= 100000) {
			// // // console.log('7 digits long');
			numUsers = '100000';
			$(inputSelection).val('100,000');
		}
		
		if(!numUsers) {
			numUsers = 1;
			$(inputSelection).val('1');
		}
		if(numUsers >= 1 && numUsers <= 25) {
			userCost = 2500;
			overLimit = false;
		} else if(numUsers >= 26 && numUsers <= 100) {
			userCost = numUsers * 100;
			overLimit = false;
		} else if(numUsers >= 101 && numUsers <= 1000) {
			userCost = numUsers * 75;
			overLimit = false;
		} else {
			userCost = numUsers * 75;
			overLimit = true;
		}
		
		strUsers = numUsers.toString();
		if(numUsers.length == 4) {
			numUsers = strUsers.substring(0,1) + ',' + strUsers.substring(1);
		} else if(numUsers.length == 5) {
			numUsers = strUsers.substring(0,2) + ',' + strUsers.substring(2);
		} else if(numUsers.length == 6) {
			numUsers = strUsers.substring(0,3) + ',' + strUsers.substring(3);
		}
				
		// // console.log(userCost);
		totalCost = currency(userCost);
		
		function currency(amount) {
			// currency turns a number into a dollar value
			// make sure to use AFTER any math equations
			//var strLength = amount.val().length;
			// // console.log('amount = ' + amount);
			var strAmount = amount.toString();
			var amountLength = strAmount.length;
			if(amountLength == 4) {
				amount = strAmount.substring(0,1) + ',' + strAmount.substring(1);
			} else if(amountLength == 5) {
				amount = strAmount.substring(0,2) + ',' + strAmount.substring(2);
			} else if(amountLength == 6) {
				amount = strAmount.substring(0,3) + ',' + strAmount.substring(3);
			} else if(amountLength == 7) {
				amount = strAmount.substring(0,1) + ',' + strAmount.substring(1,4) + ',' + strAmount.substring(4);
			}
			amount = ' $' + amount + '.00';
			return amount;
		}
		
		if(overLimit) {
			contactUs = 'Please contact us to discuss using FRI for less.';
		} else {
			contactUs = '';
		}
		
		// if it's the first click, animate the results and cta's in at the same time
		if(firstTime) {
			$('.results, .cost-cta').css({
				'visibility' : 'visible'
			});
			$('.totalCost').append(totalCost);
			$('.over1000').append(contactUs);
			$('.userSpan').append(numUsers);
			$('.results, .cost-cta').animate({
				'opacity' : 1
			});
			firstTime = false;
		} else {
		// else if not first time, fade out results and fade back in with new data
			$('.results').animate({
				'opacity' : 0
			}, 300, function() {
				$('.totalCost').html('');
				$('.over1000').html('');
				$('.userSpan').html('');
				$('.totalCost').append(totalCost);
				$('.over1000').append(contactUs);
				$('.userSpan').append(numUsers);
			});
			$('.results, .cost-cta').animate({
				'opacity' : 1
			}, 300);		
		}
	};	
});
