$(function(){
	$("a.overlayImg").fancybox({'margin':0, 'padding':0, 'transitionIn':'elastic', 'transitionOut':'elastic', 'overlayOpacity':0.5, 'overlayColor':'#fff'});
});


//Competition Form
$(function(){
	
	checkForm();
	$('#competitionForm *').change(checkForm).keyup(checkForm);

});

function checkForm(){
	var form = $('#competitionForm');
	if($('#form_file', form).val() && $('#form_agree:checked').val()=='on'){
		$('#form_submit', form).addClass('enabled').fadeTo(0, 1);
	} else {
		$('#form_submit', form).removeClass('enabled').fadeTo(0, .2);
	}
	
	$('#competitionForm').submit(function(){
										  
		var validResult = formValidation($(this));
		
		if($(this).find('#form_submit').hasClass('enabled')){
		
			if(validResult){
				return true;
			} else {
				//alert('You must fill out all the form details and select a file to upload before submitting your entry.');
				return false;
			}
		
		} else {
			//alert('You must fill out all the form details and select a file to upload before submitting your entry.');
			return false;
		}
	
	});
	
	/*$('#form_submit').click(function(){
				//return 
				
		return false;
	});*/
	
//console.log();	
	//console.log(this);
}



function formValidation($form){
	var errors = 0;

	$('.reqField',$form).each(function(){
		if(!$(this).val() || ($(this).val() && $(this).hasClass('reqEmail') && !validateEmail($(this).val()))){
			errors++;
			$(this).addClass('reqFieldError');
			$(this)
				.click(function(){
					$('#validationNotice',$form).fadeOut(250,function(){ $(this).remove() });
					$(this).removeClass('reqFieldError').unbind('click');
				})
				.focus(function(){
					$('#validationNotice',$form).fadeOut(250,function(){ $(this).remove() });
					$(this).removeClass('reqFieldError').unbind('click');
				});
		}
	});
	
	if(errors){
		$('#validationNotice',$form).remove();
		$('#form_submit', $form).after('<div id="validationNotice" style="color:#f55; padding-top:7px">Please fill out all the required fields and select a file to upload before submitting.<br />The entered email address must be valid.</div>');
		return false;
	}
	
	return true;
}

function validateEmail(val){      
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(val); 
}
