$(document).ready(function(){

	// Show play button on hover
	$(".videoPreview").hover(function(){
	
		$(this).find("img").stop().animate({
			opacity: 0.7
		}, 300);
		
	},function(){
	
		$(this).find("img").stop().animate({
			opacity: 1
		}, 300);	
		
	}).click(function(){
	
		$.fancybox({
			'autoScale'		: false,
			'title'			: this.title,
			'width'			: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
		return false;
		
	});
	
	
	// Apply fancybox to the Mini-Gallery
	$(".gallery").find("li").find("a").fancybox();
	

	// Autofill email field in the subscription area
	var emailValue = $("#subscribeEmail").val();
	$("#subscribeEmail").focus(function(){
    	if($(this).val() == emailValue) $(this).val("");
	}).blur(function(){
	    if($(this).val() == "") $(this).val(emailValue);
	});
	
	
	//---------------------------
	//Ajax Contact Form
	//---------------------------	
    
    if($("#contactForm").length){
    
		$("#contactForm").submit(function(){
		    
		    // Cache variables
		    var ContactForm = $(this),
		    	errors = 0,
		        loader = $("#loader"),
		        result = $("#result");
		    
		    // Show the loading image
		    loader.fadeIn();
		    // Clear the results
		    result.find(".fail, .success").hide();
		    
		    // Validate fields
		    ContactForm.find(".required").each(function(){
		        if($(this).hasClass("email")){
		            errors += $(this).validateEmail();
		        }else{
		            //Must contain at least 3 characters
		            errors += $(this).validateLength(3);
		        }
		    });
		    
		    //If there are no errors, send the email
		    if(errors === 0){
		        var URL = ContactForm.attr("action");
				$.ajax({
					type: 'post',
					url: URL,
					data: ContactForm.serialize(),
					success: function(results) {
						// Hide the loading image
						loader.fadeOut(function() {
							// If the email was sent
							if(/email sent/.test(results)) {
							
								//display the success text
								result.find(".success").fadeIn();
								// Clear the fields
								ContactForm.find(".form").each(function(){
								    $(this).val("");
		                        });
		                        
							} else {
								// else, display the failure text
								result.find(".fail").fadeIn();
							}
						});
					}
				});
		    }else{
		    	// else, nudge the incorrect fields
		    	ContactForm.find(".notRight").each(function(){
		    		$(this).nudge();
		    	});
		    }
		    // Hide the loading image
		    loader.fadeOut();     
	        return false;    
	    });
	
	    // Content length validation
	    $.fn.validateLength = function(l){
	        if(this.val().length < l) {
	        	this.addClass("notRight");
	        	return 1;
	        } else {
	            this.removeClass('error');
	            return 0;
	        }
	    };
	
		// eMail validation
		$.fn.validateEmail = function(){
			var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
			if(filter.test(this.val())){
				this.removeClass("error");
				return 0;
			}else{
				this.addClass("notRight");
				return 1;
			}
		}
				
		// The Nudge effect
		$.fn.nudge = function(){
			$(this).animate({
				'right' : -5
			}, 100, function(){
				$(this).animate({
					'right' : 5
				}, 100, function(){
					$(this).animate({
						'right' : -5
					}, 100, function(){
						$(this).animate({
							'right' : 5
						}, 100, function(){
							$(this).animate({
								'right' : -5
							}, 100, function(){
								$(this).animate({
									'right' : 0
								}, 100, function(){
									$(this).delay(100).removeClass('notRight').addClass("error");
								});
							});
						});
					});
				});
			});
		}
			
	}	


});
