$(document).ready(function() {
	resize(); // do column reflow action on initial page load
	$('#qa_search').focus();
	$('#openpanel,#tellus h3').click(function() { // attach handler for mailing list panel open
		$('#blackout').show();
		$('#listform').show();
		$('#name').focus();
	});
	$('#closepanel').click(function() { // attach handler for mailing list panel close
		$('#qa_search').focus();
		$('#blackout').hide();
		$('#listform').hide();
	});
	$('#submitform').click(function() { // submit form via ajax
		var formdata = new Object;
		$('#listform :input').each(function() {
			formdata[$(this).attr('name')] = $(this).val();
		});
		$.post('process_form.php',formdata,function(response) {
			if(response=='Missing fields.') { // what do do if there are missing fields
				$('#listform p.error').remove();
				$('#listform h3').after('<p class="error">Whoops, it looks like you missed some required information. We have highlighted the required fields below.</p>'); // add error message (hidden)
				$('#listform .required').addClass('missing'); // highlight all required fields
				$('#listform .error').slideDown(); // slide in the error message
			} else { // what to do on success
				$('#listform form').html(response); // show the thank you message from process_form.php
				$('#closepanel').click(function() { // re-attach handler for mailing list panel close
					$('#qa_search').focus();
					$('#blackout').hide();
					$('#listform').hide();
				});
				$('#tellus h3').html('Thanks for sharing!'); // change the speech bubble text in the page intro
				$('#tellus p').html('Signing up for our mailing list is a great first step. But why not get in touch with <a href="http://www.southwestern.edu/admission-finaid/adm-counselors.html">your admission counselor</a>?');
			}
		});
		return false; // cancel the click
	});
});

$(window).resize(resize);

function resize() { // resize columns to equal height onresize
	$('#tier2 .column,#tier3 .column').css('height','auto');
	var max=0;
	$('#tier2 .column').each(function() { if($(this).height()>max) max = $(this).height(); });
	$('#tier2 .column').height(max);
	max=0;
	$('#tier3 .column').each(function() { if($(this).height()>max) max = $(this).height(); });
	$('#tier3 .column').height(max);
}