$(document).ready(function(){

	// Apply fancybox to the screenshots
	$("a.fancybox").fancybox({
		'titlePosition': 'inside'
	});

	//---------------------------
	//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");
								});
							});
						});
					});
				});
			});
		}
			
	}	


});
