$(document).ready(function() {
	setRowHeight();
	linkLogos();

	$('#contactLink').on('click',function(e) {
		$('#contactSection').slideDown(200, function() {
			$('html,body').animate({scrollTop: $('#contactSection').offset().top},'500');
		});
		e.preventDefault();
	});
});

// Makes all tiles in a row the same height
function setRowHeight() {
	var numTiles = $('#fc li').length;
	var numRows = Math.ceil(numTiles/3);

	for (i=1; i<=numTiles; i+=3) {
		var tallest = 0;
		for (j=0; j<3; j++) {
			var tile = $('#fc li:nth-child('+(i+j)+')');
			if ($(tile).length) {
				var tileHeight = $(tile).height();
				if (tileHeight > tallest)
					tallest = tileHeight;
			}
		}
		for (j=0; j<3; j++) {
			var tile = $('#fc li:nth-child('+(i+j)+')');
			if ($(tile).length) {
				tile.height(tallest);
			}
		}
	}
}

// Duplicate the link and place it over the logo
function linkLogos() {
	$('#fc li').each(function(idx) {
		$(this).find('a').clone().insertAfter($(this).find('p:last-child')).empty().addClass('logoLink ir');
	});
}

$(document).on('submit', '#contactForm', function(e){
	e.preventDefault();
	var form = this;
	var action = $(form).attr('action');
	$('#submit').attr({
		value: 'Processing',
		disabled: 'disabled'
	});

	$.post(action, {
			name: $('#name').val(),
			email: $('#email').val(),
			phone: $('#phone').val(),
			message: $('#message').val()
		},
		function(data){
			$('#contactForm #submit').removeAttr('disabled').attr('value','Submit');
			$('#response').remove();
			$('#contactForm').before('<div id="response" class="response">'+data+'</div>');
			$('#response').hide().slideDown();
			if(data=='Your message has been sent.') $('#contactForm').slideUp();
		}
	);
});
