$(document).ready(function (){													
	//clear any loan values in case of a page refresh
	$('.loan-amount').val(0);
	$(function() {
		$("#slider-range-min").slider({
			range: "min",
			value: 200,
			min: 1,
			max: 4000,
			animate: true,
			slide: function(event, ui) {
			  $('#neg-payment-amount').css('display', 'none');
				$("#payment-amount").val(ui.value);
				calculatePayTime(ui.value);
			}
		});
		$("#payment-amount").val($("#slider-range-min").slider("value"));
	});
	
	//create handler for changing payment amount, check for positive value
	$('#payment-amount').change(function() {
	  var v = parseFloat(this.value.replace(/\,/g,''));
	  if (v > 0) {
		  this.value = v;
			$('#neg-payment-amount').css('display', 'none');
	    $("#slider-range-min").slider('value', v);
			calculatePayTime(v);
		}
		else {
		  $('#payment-amount').val(1);
			$("#slider-range-min").slider('value', 1);
			$('#neg-payment-amount').fadeIn('fast');
		}
	});
	
	//create handlers for changing loan amounts, check for positive value
	$('.loan-amount').change(function () {
		var v = parseFloat(this.value.replace(/\,/g,'')).toFixed(2);
		if (v >= 0){
			this.value = v;
			$('#neg-loan-amount').css('display', 'none');
			calculatePayTime(parseFloat($('#payment-amount').val()));
		}
		else {
			this.value = 0;
			$('#neg-loan-amount').fadeIn('fast');
		}
	});
	
	//create handlers for changing APR amounts, check for positive value between 0 and 100
	$('.apr').change(function () {
	  var v = parseFloat(this.value.replace(/\,/g,'')).toFixed(3);
		if (v >= 0 && v <= 100) {
			$('#apr-error').css('display', 'none');
			this.value = v;
			calculatePayTime(parseFloat($('#payment-amount').val()));
		}
		else {
			this.value = 0;
			$('#apr-error').fadeIn('fast');
		}
	});
	
	var addCount = 1;
	$('#add-loan-button').bind('click', function() {
	  if (addCount < 5) {
		  $('#loan-row-' + addCount).fadeIn('fast');
			addCount ++;
		}
		if (addCount == 5) {
		  $('#add-loan-button').unbind().addClass('disabled');
		}
	});
});

//calculates the payment time
calculatePayTime = function(payAmt) {
	var loans = new Array();
	$('.loan-amount').each(function(i) {
		var v = parseFloat(this.value);
		if (v >= 0 ) {
			loans[i] = new Array(v, parseFloat($('#apr'+ i).val()));
		}
	});
	var balance = 0;
	for(var i=0; i<loans.length; i++) {
		balance += loans[i][0];
	}
	for(var i=0; i<loans.length; i++) {
		loans[i][2] = loans[i][0]/balance;
	}
	var weightedAPR = 0;
	for(var i=0; i<loans.length; i++) {
		weightedAPR += loans[i][1] * loans[i][2];
	}
	var startingBalance = balance;
	var months = 0;
	var totalPayment = 0;
	if ((balance * Math.pow(Math.E, ((weightedAPR/100) * (1/12)))) - payAmt < balance) {
		while(balance > 0) {
			var balance = (balance * Math.pow(Math.E, ((weightedAPR/100) * (1/12)))) - payAmt;
			months ++;
	    if (balance > 0)
			  totalPayment += payAmt;
			else {
			  totalPayment += (payAmt + balance);
			}
			$('#month-result').text(Math.ceil(months));
			$('#year-result').text((months/12).toFixed(2));
			$('#total-paid').text(totalPayment.toFixed(2));
			$('#interest-paid').text((totalPayment - startingBalance).toFixed(2));
		}
	}
	else {
	  $('#year-result').text('Infinite!');
		$('#month-result').text('Infinite!');
		$('#total-paid').text('Infinite!');
		$('#interest-paid').text('Infinite!');
	}
}