$(document).ready(function(){
	$('.clearfield').each(function(){
		$(this).attr('data-orig-value',$(this).val());
	});
	$('.clearfield').focus(function(){
		if($(this).val() == $(this).attr('data-orig-value'))
			$(this).val('');
	}).blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('data-orig-value'));
	});
	
	function validation_failed(message,field_id){
		
		$('#'+field_id).focus();
		$('#validation_message').hide().html(message).fadeIn('slow');
		setTimeout('$("#validation_message").fadeOut("slow");',5000);		
	}
	$('#contact_form').submit(function(){
		
		var name    = $('#contact_name').val();
		var email   = $('#contact_email').val();
		var message = $('#contact_message').val(); 
		
		if(name == '' || name == 'your name'){
			validation_failed('your name is required','contact_name');
		}
		else if(email == '' || email == 'your email' || !email.match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")){
			validation_failed('valid email is required','contact_email');
		}
		else if(message =='' || message == 'your message/quote request'){
			validation_failed('message is required','contact_message');
		}
		else{
			
			//Submit the form
			$.post('index.php',{'ajax':'1', 'contact_name':name, 'contact_email': email,'contact_message':message},function(data){
				
				if(data == "1"){
					$('#validation_message').hide().html('Your message has been sent successfully').fadeIn('slow');
					$('#contact_form')[0].reset();
				}
				else
					$('#validation_message').hide().html('Error occured, email me at hello@omarmohamed.com').fadeIn('slow');
					
				setTimeout('$("#validation_message").fadeOut("slow");',15000);				
			});
		}
		return false;
	});
	$('.works-container h2 a').click(function(){
		
		/*if(!$('works-container ul').hasClass('projects-visible'))
			fade_projects(0);
		*/
		//return false;
	});
	$('#project-mentos a,#project-growbox a').click(function(){
		
		alert('Project is offline now, I\'ll upload screenshots in a few days!');
		return false;
	});
});
function fade_projects(index){
	
	$('.works-container ul').addClass('projects_visible');
	
	if(index < $('.works-container ul li').size()){
		$('.works-container ul li').eq(index).fadeIn();
		
		setTimeout('fade_projects('+(index+1)+')',150);
	}
	else{
		$('.works-container h2 a').fadeOut('fast',function(){
			$('.works-container h2 a').html('I\'VE BUILT WEBSITES FOR THESE GUYS');
			$('.works-container h2 a').fadeIn();
		});
	}
}
