jQuery(function() {
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;
});

$(document).ready(function() {
	
	$('#nav_folio, #nav_foot_folio, #nav_services, #nav_foot_services, #nav_about, #nav_foot_about, #nav_contact, #nav_foot_contact').hover(function() {
		$(this).css('cursor','pointer');
	}, function() {
		$(this).css('cursor','auto');
	});
	
	$('#nav_folio, #nav_foot_folio').click(function() {
		$.scrollTo('#folio', 250);
	});
	$('#nav_services, #nav_foot_services').click(function() {
		$.scrollTo('#services', 250);
	});
	$('#nav_about, #nav_foot_about').click(function() {
		$.scrollTo('#about', 250);
	});
	$('#nav_contact, #nav_foot_contact').click(function() {
		$.scrollTo('#contact', 250);
	});
	
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
	
	$('#contact-form').submit(function() {
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var name = $('#name').val();
		var email = $('#email').val();
		var message = $('#message').val(); 
		var hasErrored = false;
		
		if (name.length <= 0) {
			alert('Please enter your name.');
			hasErrored = true;
		}
		else if (!re.test(email)) {
			alert('Please enter a valid email address.');
			hasErrored = true;
		}
		else if (message.length <= 0) {
			alert('Please enter your message.');
			hasErrored = true;
		}
		
		if (!hasErrored) {
			$('#submit').val('SENDING...');
			$('#submit').attr('disabled', 'disabled');
			
			$.ajax({
				url: "mail.php?name=" + name + "&email=" + email + "&message=" + message,
				context: document.body,
				success: function() {
					$('#email-div').fadeTo(250, 0, function() {
						$('#email-div').html('<h2>Thanks</h2><p>I\'ll get back to you as soon as possible!');
						$('#email-div').fadeTo(250, 1);
					});
					
				}
			});
		}
		
		return false;
	});
});
