$(function()
{
	// team page
	$('.profile-gallery a').Tooltip
	({
		delay: 0,
		track: true,
		showURL: false,
		showBody: "|"
	});
	
	// external links
	$('a[rel~=external]').each(function()
	{
		var $target = $(this);
		$target.attr('target','_blank');
		$target.click(function()
		{
			var leave = window.confirm('You are now departing from the regulatory site of Insight Mortgage Solutions. Neither Insight Mortgage Solutions nor First Complete Ltd is responsible for the accuracy of information contained within the linked site');
			if (leave) return true
			else return false;
		});
	});
	
	// cycle images
	var imgs = $('#images').children();
	if (imgs.length > 1)
	{
		$('#images').cycle(
		{ 
			fx:      'fade', 
			speed:    1000, 
			timeout:  5000 
		});
	}
	
	// cycle testimonials
	var ts = $('#tscycles').children('.quote');
	if (ts.length > 1)
	{
		$('#tscycles').cycle(
		{ 
			fx:      'fade', 
			speed:    1000, 
			timeout:  7000,
			delay: -1000
		});
		$('#testimonials').click(function()
		{
			top.location.href = r + 'testimonials';
		});
	}
	
	// mortgage form
	if ($('#calcform').is('form'))
	{
		$('#calcform .text').change(function()
		{
			update();
		});
		
		$('#calcform .disabled').focus(function()
		{
			$(this).blur();
		});
		
		$('#calculate').click(function()
		{
			return update();
		});
	}
});

// directions
function directionForm()
{
	var postcode = $('#postcode').val();
	if ( ! postcode)
	{
		alert('Please enter your postcode');
		return false;
	} 
	else
	{
		return true;
	}
}

// calculator
function trunc(val)
{
	return (Math.round(val*100)/100)
}

function update()
{
	calculate();
	return false;
}

function calculate()
{
	var loan = new Number(parseFloat($('#loan').val()));
	var months = $('#term').val();
	var int_rate = new Number(parseFloat($('#interest_rate').val()));
	var factor_a = new Number(int_rate/1200);
	var factor_b = new Number(Math.pow((1 + (int_rate/1200)), months));
	var factor_c = new Number(factor_b -1);
	var monthly_payment = new Number(((factor_a * factor_b)/factor_c)* loan);
	var total_charge_credit = new Number((monthly_payment * months) - loan);
	var total_repayment = new Number(monthly_payment * months);	

	// Interest Only
	var int_only = ((loan * int_rate) / 12)/100;

	if(total_repayment > 0 )
	{
		$('#monthly_payment').val('£'+trunc(monthly_payment));
		$('#total_charge').val('£'+trunc(total_charge_credit));
		$('#total_repayment').val('£'+trunc(total_repayment));
		$('#interest_only').val('£'+trunc(int_only));
	}
}


