$(function () {

// prepare document
	$('span.email, label, form br').hide();
	$('span.email').after('<a href=\"mailto:hi@oliver-breuer.com\">hi@oliver-breuer.com</a>');
	$('a[href^="http"]').attr("target","_blank");
	$('.timeago').timeago();
	$('a.lightbox').fancybox();
	

// navigation
	$('a[href*="#"]').click(function() {
		var target = $(this).attr("hash");
		$.scrollTo(target, 1000, {offset: -40});
		return false;
	});
	
// trigger fancybox	
	$('img[src*="sample"]').click(function() {
		var group = $(this).attr("name");
		$('p[title="' + group + '"] a:first').trigger('click');
	});

/*
// load project for Thickbox and execute
	$('a.preview').click(function() {
		var project = $(this).children('img').attr('name');
		return false;
	});
*/

	
// contact form
	$('label').each(function(){
		label = $(this).html();
		complement = "#"+$(this).attr("for");
		$(complement).val(label);
	});
	$('<p class="note"><strong>Thank you!</strong> Your message was successfully sent.</p>')
		.hide()
		.insertBefore('form')
		.click(function(){
			$(this).fadeOut();
		});
	
	
	// hide and reveal description
	$('input,textarea').focus(function() {
		// hide success message in case 
		// it is still visible after previous submit:
		$('.note').fadeOut();
		
		value = $(this).val();
		reference = $(this).attr("id");
		this.defaultValue = $('label[for=' + reference + ']').html();
		if (value == this.defaultValue){
			this.value = '';
		}
		if(value != this.defaultValue){
			this.select();
		}
	}).blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
	
	// form validation
	$("#submit").click(function(){
		$('.error').removeClass("error");
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var nameVal = $("#sender-name").val();
		if(nameVal == $('label[for=sender-name]').html()) {
			$("#sender-name").addClass("error");
			hasError = true;
		}
		
		var emailVal = $("#sender-email").val();
		if(emailVal == $('label[for=sender-email]').html()) {
			$("#sender-email").addClass("error");
			hasError = true;
		} else if(!emailReg.test(emailVal)) {	
			$("#sender-email").addClass("error").val('I am afraid, this email appears to be invalid.');
			hasError = true;
		}
		
		var verificationVal = $("#verification").val();
		if(verificationVal != 7) {
			$("#verification").addClass("error").val('Your answer is incorrect. How many days make a week?');
			hasError = true;
		}
		
		var messageVal = $("#message").val();
		if(messageVal == $('label[for=message]').html()) {
			$("#message").addClass("error");
			hasError = true;
		}
		
		// submission
		if(hasError == false) {
			$(this).hide();	// this = #submit
			$("#submit").after('<img src="bin/loading.gif" alt="Loading ..." id="loading" />');
			
			$.post("bin/sendemail.php",
   				{ mailFrom: emailVal, sender: nameVal, message: messageVal },
   					function(data){
						$('form img').fadeOut("normal", function() {
							$('.note').fadeIn();
							$('label').each(function(){	// reset form
								label = $(this).html();
								complement = "#"+$(this).attr("for");
								$(complement).val(label);
							});
							$("#submit").show();
						});
   					}
				 );
		}
		
		return false;
	});
});
