$(function() {
	$("body").addClass("jsEnabled");
	
	Cufon.replace('.rockwell');
	Cufon.replace('#entryColumnMain h2');
	
	//Perfect in one pass slidedowns
	$(".viewMore").toggle(
			function(){
				$(this).siblings(".extraContent").slideDown();
			}, function() {
				$(this).siblings(".extraContent").slideUp();
			}
	);
	
	// Setup lightbox
	$("a[rel^='lightbox']").prettyPhoto({
		animationSpeed: 'normal',
		showTitle: false, 
		allowresize: false, 
		theme: 'dark_rounded'
	});
	
	//Setup text popup
	$("a[rel^='popup']").prettyPopin({
		modal : false, 
		width : false,
		height: false,
		opacity: 0.5,
		animationSpeed: 'fast',
		followScroll: true, 
		loader_path: 'assets/images/popup/loader.gif'
	});
	
	/* Applying scroller arrows for used equipment page because CMS deletes empty spans */
	var scrollerArrows = '<span class="galleryLeft" /><span class="galleryRight" />'
	$("#usedEquipment .horizontalGallery").prepend(scrollerArrows);
	
	
	/* Vertical Scroller */
	$(".verticalGallery .galleryInner").scrollable({
		items: '.galleryItems',
		size: 3,
		vertical: true,
		speed: 750,
		next: '.galleryUp',
		prev: '.galleryDown',
		activeClass: "active"
	});
	
	/* Horizontal Scroller */
	$(".horizontalGallery .galleryInner").scrollable({
		items: '.galleryItems',
		size: 1,
		speed: 750,
		next: '.galleryLeft',
		prev: '.galleryRight',
		activeClass: "active"
	});
	
	// Financing Calculator
	$("#financingCalculator").submit(function(){
		$("#monthlyPayment").empty();
		
		var principle = $("#principle").val();
		var interestRate = $("#interestRate").val();
		var term = $("#term").val();
		
		var regex = /[^0-9\.]/g; //Removes all non-number characters except "."
		
		principle = parseFloat(principle.replace(regex,""));
		interestRate = (parseFloat(interestRate.replace(regex,""))/1200);
		term = Number(term.replace(regex,""));
		
		var monthlyPayment = PMT(interestRate, term, -principle); //Runs PMT function to calculate loan payment
		monthlyPayment = monthlyPayment.toFixed(2); //Removes all but last two digits
		monthlyPayment = CommaFormatted(monthlyPayment); //Adds commas to the output
		
		$("#monthlyPayment").append("<p>$ " + monthlyPayment + "</p>");
		return false;
	});
	
	// Make legends into H2s for easier styling, while maintaining semantic markup.
	$("legend").each(function(){
		var $this = $(this); 
		var legendText = $this.text();
		$this.parent("fieldset")
			.before("<h2>" + legendText + "</h2>")
			.end()
			.remove();
	});
	
	// Adding hover in IE for non-anchor elements
	$(".hoverEnabled").hover(
		function() {
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
	
	//Add zebra-striping to tables
	$("table tr:odd").addClass("odd");
	
	//Automatically selects focused field text and adds class for styling
	$('input[type="text"]').focus(function(){
		$(this).select().addClass("focused");
	});
	$('input[type="text"]').blur(function(){
		$(this).removeClass("focused");
	});
	
}); 

//Loan monthly payment function
function PMT(i, n, p) {
	return i * p * Math.pow((1 + i), n) / (1 - Math.pow((1 + i), n)); 
}

//Add commas to currency
function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	
	if(isNaN(i)) {
		return ''; 
	}
	
	var minus = '';
	
	if(i < 0) { 
		minus = '-'; 
	}

	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}


