// instantiate console.log, if not set
if(typeof(console) === 'undefined') {
    var console = {}
    console.log = function() {};
}

// formats number into money (two decimals)
function format_money(mnt){
	mnt = mnt.toString().replace(/,/g, '');
	mnt = (Math.round(mnt*100))/100;
	if(mnt == Math.floor(mnt)) {
		mnt = mnt + '.00';
	} else if(mnt*10 == Math.floor(mnt*10)) {
		mnt = mnt + '0';
	}
	if(isNaN(mnt)) {
		mnt = '0.00';
	}
	if (mnt == '0.00') {
		mnt = null;
	}
	return mnt;
}

// toggle visibility
$.fn.toggleBar = function() {
	var element = $(this);  
	if($(element).hasClass('inactive')) {
		$(element).next().show();
		$(element).removeClass('inactive');
	} else {
		$(element).next().hide();
		$(element).addClass('inactive');
	}
};

$(document).ready(function() {
	
	var url = window.location.toString();
	$('.toggle.inactive').next().hide();
	$('.toggle').click(function() {
		$(this).toggleBar();
	});
	
	$('.subnav .toggle.inactive.current').toggleBar();
	
	// colorbox
	$('.quick-contact, .quick-contact_text').colorbox({width:"725", height:"480", iframe:true});
	$(".iframe").colorbox({width:"1000", height:"630", iframe:true});
	

	// animate homepage banner
	if($('#home #banner').length) { 
		$('#home #banner').innerfade({
			speed: 'slow', 
			timeout: 8000, 
			type: 'sequence', 
			containerheight: '327px' 
		});
	}


	// if #form1, load formvalidator
	if($('#form1').length) {
		$("#form1").validationEngine({
			validationEventTriggers:"submit",
			promptPosition: "centerRight"
		});
		
		// Setup a global toggle to warn users that if they leave the current page
		// the will lose all the changes made to the form.
		var formClicked = false;
		if(!formClicked) {
			$("form").click(function(){formClicked = true;});
		}
		$('form').submit(function(){formClicked = false;});
		window.onbeforeunload = function() {
			if (formClicked) {
				return 'All changes will be lost.';
			}
		}
	}
	
	// format phone fields
	$('input[name*="phone"]').mask("(999) 999-9999");
   
	// format SSN field
	$('#app_b_ssn, #app_cb_ssn').mask("999-99-9999");
   
	// format money fields
	$('input.money').each(function(index) {
		$(this).val(
			format_money($(this).val())
		);
	});
	
	$('input.money').blur(function() {
		$(this).val(
			format_money($(this).val())
		);
	});
	
	$('form').submit(function() {
		$('input.money').each(function() {
			$(this).val(
				format_money($(this).val())
			);
		});
	});

	// if value = 0.00, clear on focus
	$('input.money').focus(function() {
		if($(this).val() == '0.00') {
			$(this).val('');
		}
	});
	
	// on calculators, switch to Analysis and focus on results
	if ($('#CalcForm').length) {
		$('#analysisDiv').show();
		$('#plainEnglishDiv').hide();
		$('.analysis').text('Financial Analysis');
		$('#CalcForm').attr('action','#analysisDiv');
	}
	
	// if user fills in name for coborrower, make all other necessary fields required.  If they remove name, removed required fields
	function checkCoborrower() {
		if ($("#app_cb_first_name").val() != '' || $("#app_cb_last_name").val() != '') {
			
			$('#app_cb_first_name, #app_cb_last_name, #app_cb_ssn, #app_cb_home_phone, #cb_dob_month, #cb_dob_day, #cb_dob_year, #app_cb_present_address, #app_cb_present_city, #app_cb_present_state,#app_cb_present_zip ').addClass('validate[required]');
		} else {
			$('#app_cb_first_name, #app_cb_last_name, #app_cb_ssn, #app_cb_home_phone, #cb_dob_month, #cb_dob_day, #cb_dob_year, #app_cb_present_address, #app_cb_present_city, #app_cb_present_state,#app_cb_present_zip ').removeClass('validate[required]');	
		}
	}
	
	// run when page loads
	checkCoborrower();
	
	$('#app_cb_first_name, #app_cb_last_name').change(function(){
		checkCoborrower();
	});	

	
	// if coborrower is hidden, make fields not required
	if ($(".coborrower").length > 0 && $(".coborrower").is(":hidden")) {
		$(".coborrower input[class^=validate]").attr("class","");
	}

	// add asterisks to required fields
	$('input[class*=required]').prev('label').addClass('asterisk');
	$('td input[class*=required]').parent().prev('th').addClass('asterisk');
	$('td input[class*=optional]').removeClass('asterisk');
	$('.asterisk').append(' <em>*</em>');
	
	// move glossary menu 
	if($("#glossary_menu").length) {
		var html = $("#glossary_menu").html();
		html = '<div id="glossary_menu">' + html + '</div>';
		$("#glossary_menu").remove();
		$("h1:first").after(html);
	}

	// redirect to branch page
	$('#bra_slug.redirect').change(function() {
		if($(this).val().length > 0) {
			window.location = '/' + $(this).val() + '/';
		}
	});
	
	// go back
	$('.backlink').click(function() {
		history.back();
	});

});
