$(document).ready(function() {
	
	$("input.required, textarea.required, select.required").blur(function() {
		if (contact_validate($(this))) {
			$(this).parent("p").addClass("form_error").removeClass("form_okay");
		} else {
			$(this).parent("p").addClass("form_okay").removeClass("form_error");
		}
	});
	
	if ($("#contact #subject").val() == "Other") {
		$("#subject_other").show();
	}
	
	$("#contact #subject").change(function() {
		if ($(this).val() == "Other") {
			$("#subject_other").show();
			$("#subject_other").focus();
		} else {
			$("#subject_other").hide();
		}
		
		contact_validate($(this));
	});
	
	// $("#contact a[href^='mailto']").addClass("email");
	
	$("#contact form").submit(function() {
		var okay = true;
		$("input.required, textarea.required, select.required").each(function(i) {
			if (contact_validate($(this))) { 
				$(this).parent("p").addClass("form_error").removeClass("form_okay");
				okay = false; 
			}
		});

		// if (contact_validate($(this))) {
		// 	$(this).parent("p").addClass("form_error").removeClass("form_okay");
		// } else {
		// 	$(this).parent("p").addClass("form_okay").removeClass("form_error");
		// }


		return okay;
	});
	
	
	$('.slideshow').cycle({
		fx: 'fade',
		speed: 1000,
		timeout: 3000,
		pause: 1
	});
    

});


function contact_validate(e) {
	// returns TRUE if errors in the field
	var errors = false;
	switch ($(e).attr("id")) {
		case "email" :
			if (($(e).val() == "") || ($(e).val().indexOf("@") < 0) || ($(e).val().indexOf(".") < 0)) errors = true;
			break;
		case "subject_other" :
			if (($(e).val() == "") && ($("#subject").val() == "Other")) errors = true;
			break;
		default :
			if ($(e).val() == "") errors = true;
			break;
	}
	return errors;
}


// function toggleRequired() {
// 	
// 	var p = $("label:not('.required')").parent("p");
// 	$(p).slideToggle(200);
// 	
// }



