var default_form_values = new Array;

$(document).ready(function(){
	field_behavior();
	form_helper_behavior();
	Cufon.replace('h1, h2, h3, .article .title');
});

/* *
 * Name: Form Helper Behavior
 * Description: This function is made for links within labels (with
 *							the 'referral' class to focus on their associated field
 *							when clicked.
 * Parameters: n/a
*/
function form_helper_behavior(){
	$("form label a.referral").click(function(){
		var target_name = $(this).parent().attr('for');
		$(this).parents('form').find('input[name='+target_name+']').focus();
	});
}

/* *
 * Name: Field Behavior
 * Description: This function takes the initial values in the form fields and
 *							copies them to an array, then uses that value depending on the
 *							user's interaction with the field.
 * Parameters: n/a
*/
function field_behavior(){
	$("input.inactive").each(function(){

		// Write initial values to the default_form_values array.
		default_form_values[$(this).attr('id')] = $(this).val();

		// When the field is focused, if the default value is still there,
		// clear it out and remove the inactive class.
		$(this).focus(function(){
			if($(this).val() == default_form_values[$(this).attr('id')]){
				$(this).val('');
				$(this).removeClass('inactive');
			}
		});
		
		// When the field is blurred, if the field is empty,
		// add the inactive class and the default value.
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).addClass('inactive')
				$(this).val(default_form_values[$(this).attr('id')]);
			}
		});
	});
	
	// Make the button link display a loading message and submit the appropriate form 
	// when clicked.
	$("a.submit.button").click(function(){
		$(this).children("span").html("Loading...");
		if(form_submit()){
			$(this).parent("form").submit();		
		}
	});
}

function form_submit(){
	$("input").each(function(){
		if($(this).val() == default_form_values[$(this).attr('id')]){
			$(this).val('');
		}
	});
	return true;
}