$(document).ready(function() {
	if($('input.monthly').length > 0) {
		$('input.monthly').blur(function() {
			$('button.calculate').click();											 
		});
		$('button.calculate').click(function() {
			var total = 0;
			$('input.monthly').each(function() {
				var value = $(this).val().replace('$', "");
				if(value > 0) { } else { value = "0"; }
				total = total + (parseInt(value * 100) / 100);
			});
			$('#monthly_total').html("$" + total).animate({ color:"red" }, 500).animate({ color:"black" }, 500);
		});
	}
	$(".menu_item_riversedgemenu").mouseenter(function() {
		$(this).children("ul").stop(true, true).slideDown();
	});
	$(".menu_item_hearthside").mouseenter(function() {
		$(this).children("ul").stop(true, true).slideDown();
	});
	$(".menu_item_fir").mouseenter(function() {
		$(this).children("ul").stop(true, true).slideDown();
	});
	$("ul#main_menu").mouseleave(function() {
		$(this).find("li ul.menu_title:visible").slideUp();
	});
	
	$link = $("#main_menu").find("a[href='" + document.location + "']");
	if($link.parent().find("ul").length > 0) {
		$link.parent().find("ul").show();
	} else {
		$link.parent().parent().show();
	}
	
	// -------------------------------
	// Rental Lease Agreement Form
	// -------------------------------
	$(".lease-agreement textarea").autogrow();
	
	$("input.numeric").numeric({allow:"."});
	$("input.round").blur(function() {
		var rounded = Math.round($(this).val());
		if(rounded > 0) $(this).val(rounded);		
	});
	
	// Payment Table Calculator
	var columns = ["2","3","4"];
	$.each(columns, function(index, value) {
		var $column = $("#payment-table").find('tr>td.yellow:nth-child(' + value +') input');
		
		$column.keyup(function() {
			var $totalsField = $("#payment-table").find('tr>td.totals:nth-child(' + value +') input');						
			
			var total = 0;
			$column.each(function() {
				if($(this).val() != "") total += parseFloat($(this).val());														
			});
			
			if(total == 0) total = "";
			$totalsField.val(total);
		});
		
		$column.blur(function() {
			var rounded = Math.round($(this).val());
			if(rounded > 0 || $(this).val() == "0") { 
				$(this).val(rounded);		
			} else {
				$(this).val("");		
			}
			$column.trigger("keyup");
		});
	});
	
	// Don't allow "enter" to submit
	$("#lease_agreement input").keypress(function(e) {
		var key;
		if(window.event) {
			key = window.event.keyCode; //IE
		} else {
			key = e.which; //firefox   
		}

		return (key != 13);				
		
	});
	
	// Duplicate Apartment # down the form
	$("#lease_agreement_apartment_number").keyup(function() {
		$(".apt-number").val($(this).val());				
	});
	
});
