$(document).ready(function(){
	$("li.submenu").hover(
		function(){
			$(".sub", this).fadeIn("fast");
		},
		function(){
			$(".sub", this).fadeOut("fast");
	
		}
	);

	$("#loan, #annualInterest, #paybackTime").blur(function(){
		calcloan()
	}).numeric(",").keyup(function(){
		calcloan()	
	});
	
	$("#price, #rent, #rentbonus").blur(function(){
		calcincome()
	}).numeric(",").keyup(function(){
		calcincome()	
	});
	
});

function calcloan() {
	var loan  = parseFloat($("#loan").val().replace(",", "."));
	var annualInterestRate = parseFloat($("#annualInterest").val().replace(",", "."));
	var paybackTime = parseFloat($("#paybackTime").val().replace(",", "."));
	
	annualInterestRate = annualInterestRate / 100;
	
	var payments = 12*paybackTime;
	var monthRate = annualInterestRate/12
	var monthlyPay = Math.floor((loan*monthRate)/(1-Math.pow((1+monthRate),(-1*payments)))*100)/100;
	
	monthlyPay = monthlyPay.toString().replace(".", ",");
	
	$("#paymentsPrint").html(payments);
	$("#monthlyPayPrint").html(monthlyPay+" &euro;");
}

function calcincome() {
	var price  = parseFloat($("#price").val().replace(",", "."));
	var rent = parseFloat($("#rent").val().replace(",", "."));
	var rentbonus = parseFloat($("#rentbonus").val().replace(",", "."));
	
	var tax = Math.floor((price*1.6)/100);
	var pricewithtax = price+tax;
	
	var rentperyear = rent*12;
	var rentbonusperyear = rentbonus*12;
	
	var income = rentperyear - rentbonusperyear;
	var incomepercent = Math.round(((income/pricewithtax)*100)*100)/100;
	
	
	incomepercent = incomepercent.toString().replace(".", ",");
	
	$("#incomePrint").html(income+" &euro; / vuosi");
	$("#incomepercentPrint").html(incomepercent+" %");
	
	$("#rentperyear").html("("+rentperyear+" &euro; / vuosi)");
	$("#rentbonusperyear").html("("+rentbonusperyear+" &euro; / vuosi)");
	
	$("#taxPrint").html(tax+" &euro;");
	$("#pricewithtaxPrint").html(pricewithtax+" &euro;");
}
