google.load("visualization", "1", {packages:["columnchart"]});
google.setOnLoadCallback(init);

$("input").bind("keyup blur", function(){
	calc();
});

function calc() {
	$("#tooltip").animate({
		top: "-=1000"
	}, 10000);
	
	var balance = parseFloat($("#balance").val());
	var interest = parseFloat($("#interest").val())/100;
	
	var interestPayment = round( balance * interest / 12, 2);
	if (balance * interest) {
		$("#interestPayment").text("$ " + interestPayment);
	} else {
		$("#interestPayment").text("-");
	}
	
	var monthlyPayment_in = parseFloat($("#monthlyPayment_in").val());
	if (monthlyPayment_in > interestPayment) {
		var monthToPayoff = round(nper(interest/12, monthlyPayment_in, -1*balance), 1);
		var yearToPayoff = round(monthToPayoff/12, 2);
		
		var text = monthToPayoff+" months ("+yearToPayoff+" years)";
		
		var totalIntetest = round( (monthToPayoff*monthlyPayment_in - balance), 2);
		var text2 = "$ "+totalIntetest;
	} else {
		var text = "-";
		var text2 = "-";
	}
	$("#timePayoff").text(text);
	$("#totalInterest").text(text2);
	
	var months = parseFloat($("#months").val());
	if (months) {
		var years = round(months/12, 2);
		$("#years").text("("+years+" years)");
		
		var monthlyPayment_out = round(pmt(interest/12, months, 1*balance), 2);
		var totalInterest2 = round((monthlyPayment_out*months - balance), 2);
		$("#monthlyPayment_out").text("$ "+monthlyPayment_out);
		$("#totalInterest2").text("$ "+totalInterest2);
	} else {
		$("#years").text("");
		$("#monthlyPayment_out").text("-");
		$("#totalInterest2").text("-");
	}
	
//	Draw Chart
	if (balance * interest) {
		drawChart(balance, interest);
	} else {
		
	}
}

function round(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function nper(rate, pmt, pv) {
	var fv = null;
	if (fv == null) {
		vfv = 0;
	} else {
		vfv = fv;
	}
	var vNPER = Math.log((vfv + pmt/rate) / (pv + pmt/rate)) / Math.log(1+rate);
	return vNPER;
}

function pmt(IR, NP, PV) {
	var vPMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP));
	return vPMT;
}

function init() {
	$("#payment_chart,#interest_chart").empty();
	drawChart(2000, 0.2);
	$("#tooltip")
		.show()
		.mouseover(function(){
			$(this).animate({
				top: "-=1000"
			}, 10000);
		});
}

function drawChart(balance, interest) {
	var data = [];
	for (var i = 12; i <= 60; i=i+4){
		data.push([
			i+"",
			round( pmt(interest/12, i, 1*balance), 2),
			round(i*pmt(interest/12, i, 1*balance) - balance, 2)
		]);
	};
	
	$("#payment_chart,#interest_chart").empty();
	
	var paymentData = new google.visualization.DataTable();
	var interestData = new google.visualization.DataTable();
	
	paymentData.addColumn("string", "Month To Payoff");
	paymentData.addColumn("number", "Monthly Payment");
	paymentData.addRows(data.length);
	
	interestData.addColumn("string", "Month To Payoff");
	interestData.addColumn("number", "Total Interest");
	interestData.addRows(data.length);
	
	$.each(data, function(i){
		paymentData.setValue(i, 0, this[0]);
		paymentData.setValue(i, 1, this[1]);
		
		interestData.setValue(i, 0, this[0]);
		interestData.setValue(i, 1, this[2]);
	});
	
	var payment_chart = new google.visualization.ColumnChart(document.getElementById('payment_chart'));
	var interest_chart = new google.visualization.ColumnChart(document.getElementById('interest_chart'));
	
	payment_chart.draw(paymentData, {
		width: 300,
		height: 225,
		colors: ["blue"],
		is3D: false,
		legend: "none",
		enableTooltip: true,
		titleX: "Months to Payoff",
		titleY: "Monthly Payment ($)",
		title: 'Payments Required For Accelerated Payoff'
	});
	interest_chart.draw(interestData, {
		width: 300,
		height: 225,
		colors: ["#260"],
		is3D: false,
		legend: "none",
		enableTooltip: true,
		titleX: "Months to Payoff",
		titleY: "Total Interest ($)",
		title: 'Total Interest in 60 Months'
	});
}