$( function() {
	
	// Make the main level of the menu not link to the page. (June 9, 2010)
	$("#mainMenu > li > a").click(function(){ return false; });
	
	//Display print function for browsers with jsenabled
	$(".actionsBar li.print").css("visibility" , "visible");
	
	// Automatically call out linked PDF documents so we don't surprise users
	/*
	 * $('a').each(function(){ var href = $(this).attr('href');
	 * if(href.indexOf('.pdf') != -1){ $(this).addClass('pdf'); $(this).append('
	 * (pdf)'); } });
	 */
	// Make the whole region clickable while keeping the HTML valid
	/*
	 * $('ul.news li').each(function(){ var href = $(this).find('p.more
	 * a').attr('href'); $(this).addClass('clickable').click(function(){
	 * window.location = href; }); });
	 */
	
	//For Sale Callout Image Cycle
	$("#categories, #flashArea").cycle({
		fx: 'fade',
		timeout: 3500,
		pause: true
	});
	
	//Homepage animation image cycle
	/*
	$("#flashArea").cycle({
		fx: 'fade',
		timeout: 2500
	});
	*/
	//Online Banking Login Form
	
	$("#location").val("St.Cloud"); //Sets the default of the select area to blank
	
	$("#loginLocation").show(); //Shows the select when js is enabled
	
	$("#loginLocation select").change(function(){
		var location = $(this).val();
		var username = $("#username").val();
		$("#formArea").load("online-banking-forms.html #loginUsername", {location: location}, function(){
			$("#username").val(username);
		});
	});
	
	//Tax Stimulus Calculator 
	$("#taxStimulusCalculator .commandButton").click(function(){
			$("#results span").empty();
			var costOfEquipment = $('#taxStimulusCalculator .inputText').val();
			
			costOfEquipment = stripAlphaChars(costOfEquipment); 
			
			$('#taxStimulusCalculator .inputText').val(costOfEquipment);
			
			var section179 = costOfEquipment;
			var totalFirstYearDeduction = section179;
			var cashSavings = 0.35 * costOfEquipment;
			var loweredCost = costOfEquipment - cashSavings;
			
			var section179 = Math.round(section179*100)/100;
			var totalFirstYearDeduction = Math.round(totalFirstYearDeduction*100)/100;
			var cashSavings = Math.round(cashSavings*100)/100;
			var loweredCost = Math.round(loweredCost*100)/100;
			
			$("#section179").append("$" + section179);
			$("#totalFirstYearDeduction").append("$" + totalFirstYearDeduction);
			$("#cashSavings").append("$" + cashSavings);
			$("#loweredCost").append("$" + loweredCost);
			
			$("#results").slideDown();
			return false;
	});
	
	//For Sale Item Page Image switcher 
	$('#itemPhotoAlt a').click(function(){
		var newImg = $(this).attr('href');
		$('#itemPhoto').children('img').fadeOut(500, function(){
			$(this).remove();
			$('#itemPhoto').append('<img src="' + newImg + '"/>').hide().fadeIn(500);
		});
		return false;
	});
	
	// Toggle with minimal markup. Good for FAQs
	$('.faq div').hide();
	$('.faq h3').wrapInner('<a href="#"></a>');
	$('.faq a').toggle( function() {
		$(this).parent('h3').next('div').slideDown();
	}, function() {
		$(this).parent('h3').next('div').slideUp();
	});

	//Print Function
	$('.print a').click(function(){
		window.print();
		return false;
	});
	
	//Open PDFs in a different window
//	$('a').each(function(){
//		if( $(this).attr('href') != "" ) {
//			var href = $(this).attr('href');
//			if(href.indexOf('.pdf') != -1){
//				$(this).click(function(){
//					window.open(this.href);
//					return false;
//				});
//			}
//		} else {
//			return false;
//		}
//	});
});

function stripAlphaChars(costOfEquipment) { 
	return new String(costOfEquipment).replace( /\D/g, "" ); 
}