﻿Cufon.replace('#portfolio a, #portfolio .head h2');
$('#cf p input.submit').each(function () {
	$(this).corner('5px');
});
$('#portfolio .head h2').each(function () {
	$(this).corner('10px');
});

$(document).ready(function()
{
	//$("a[rel^='portfolio']").prettyPhoto();
	
	$("#cf form").submit(function() {
		markError();
		
		if(this.name.value == '' || this.name.value == 'Your Name') {
			markError("What's your good name please?");
			this.name.focus();
			return false;
		}
		if(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(this.email.value) == false) {
			markError("We can't reply unless you give us your email address");
			this.email.focus();
			return false;
		}
		if(this.subject.value == '') {
			markError("Please select a subject");
			this.subject.focus();
			return false;
		}
		if(this.msg.value == '' || this.msg.value == 'Your Message') {
			markError("Please enter a detailed message");
			this.msg.focus();
			return false;
		}
		if(this.captcha.value == '') {
			markError("Please enter the human verification string");
			this.captcha.focus();
			return false;
		}
		
		myForm = this;
		formElements = $('#formElements');
		formElements.addClass('loading');
		formElements.animate({ 
			opacity: 0.2
		}, 500 );
		$(this.submit).attr('disabled', 'true');
		
		$.ajax({
			type: "POST",
			url: this.getAttribute('action'),
			data: $(this).serialize(),
			success: function(resp) {
				
				$(myForm.submit).removeAttr('disabled');
				formElements.removeClass('loading');
				formElements.animate({ 
					opacity: 1.0
				}, 500 );
				
				if(resp == '1') {
					myForm.reset();
					
					formElements.hide();
					formElements.html("<h3>We got your email.<br />We usually reply back with in a day.<br />Thanks for contacting.</h3>");
					formElements.show();
					//new Effect.Appear(this.formElements, {duration: this.duration});
				}
				else if(resp == 'e')
				{
					imgSrc = $('img#cf_captcha_img').attr('src');
					$('img#cf_captcha_img').attr('src', imgSrc + '&reload=true&rand=' + Math.random())
					markError("Please enter the correct human verification string");
					myForm.captcha.focus();
				}
				else if(resp == '0')
					markError("Oops, something went wrong. Please re submit the form");
				else
					markError(resp);
				
			}
		});
		
	});
});

function markError(err)
{
	if(err)
		$('#cf_error').show();
	else
		$('#cf_error').hide();
	
	$('#cf_error').html(err);
}