/**
 * Funky slidy thingymajig
 */
$(document).ready(function() {
	setTimeout(function() {
		delayed_slider();
	}, 2000);
});
function delayed_slider(){  
	// Not the pretiest of code, but it will auto advance 
	// Until the user hovers over something, in which case it will then only respond to hover events
	var manual = false;
	$('.gallery-box.captionfull').hover(function() {
    	manual = true;
    	$('.gallery-box.captionfull').each(function() {
			$(this).find(".cover").stop().animate({top:'220px'},{queue:false,duration:400});
		});
    	$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});  
	}, function() {
    	$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:400});
	});

	var index = 2;
	(function slider() {
		the_timer = setTimeout(function() {
			doSlide($('.gallery-box.captionfull:nth-child(' + index + ')'));
			if(!manual) index++;
			slider();
		}, 4000);
	})();

	// The above won't start until the timeout so get the ball roling
	doSlide($('.gallery-box.captionfull:nth-child(1)'));
	
	function doSlide(item) {
		if(manual) return;
		$('.gallery-box.captionfull').each(function() {
			$(this).find(".cover").stop().animate({top:'220px'},{queue:false,duration:400});
		});
		$(item).find('.cover').stop().animate({top: '0px'}, {queue: false, duration: 300});
    }
}

/**
 * Clear the inputs on error
 */
$('input.text').live('focusin', function() {
	if(this.value == this.defaultValue) {
		this.value = '';
	}
}).live('focusout', function() {
	if(!this.value.length) {
		this.value = this.defaultValue;
	}
});

/**
 * Form validation
 
var error;
$('form').live('submit', function(event) {
	event.preventDefault()
	
	var form = $(this);
	var thanks = $('#thanks');
	var error_message = $('#error-message');
	
	// Get all the fields
	var first_name	= $('#first-name');
	var last_name	= $('#last-name');
	var email		= $('#email');
	var telephone	= $('#telephone');
	var company		= $('#company');
	var job			= $('#job');
	
	// Check all the fields have been filled in
	error = false;
	error_message.hide();
	
	is_required(first_name);
	is_required(last_name);
	is_required(email);
	is_required(telephone);
	is_required(company);
	is_required(job);
	
	if(!is_valid_email(email.val())) {
		email.addClass('error');
		error = true;
	} else email.removeClass('error');
	
	if(!is_valid_phone(telephone.val())) {
		telephone.addClass('error');
		error = true;
	} else telephone.removeClass('error');
	
	if(error) {
		error_message.show();
	} else {
		$.ajax({
			type: 'post',  
			url: $(this).attr('action'),  
			data: $(this).serialize(),  
			success: function(response) {
				form.slideUp();
				thanks.slideDown();
			}
		});
	}
});

function is_required(field) {
	if(field.val() == field[0].defaultValue || field.val() == '') {
		field.addClass('error');
		error = true;
	} else field.removeClass('error');
}

function is_valid_phone(phonenumber){
	if(phonenumber.length < 11) return false;
	
	if (phonenumber != "") {
		var goodChars = "+- 1234567890()"
		for (i = 0; i < phonenumber.length; i++){   
		    var c = phonenumber.charAt(i);
		    if (goodChars.indexOf(c) < 0) return false;
		}
		return true;
	} else {
		return false;
	}
}

function is_valid_email(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
	   return false;
	}
	
	return true;
}

/**
* SMOOTH SCROLL
*/

$(function() {
        
                function filterPath(string) {
                        return string
                        .replace(/^\//,'')
                        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                        .replace(/\/$/,'');
                }
        
                var locationPath = filterPath(location.pathname);
                var scrollElem = scrollableElement('html', 'body');
        
                // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
                $('a[href*=#]').each(function() {
        
                        // Ensure it's a same-page link
                        var thisPath = filterPath(this.pathname) || locationPath;
                        if (  locationPath == thisPath
                                && (location.hostname == this.hostname || !this.hostname)
                                && this.hash.replace(/#/,'') ) {
        
                                        // Ensure target exists
                                        var $target = $(this.hash), target = this.hash;
                                        if (target) {
        
                                                // Find location of target
                                                var targetOffset = $target.offset().top;
                                                $(this).click(function(event) {
        
                                                        // Prevent jump-down
                                                        event.preventDefault();
        
                                                        // Animate to target
                                                        $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
        
                                                                // Set hash in URL after animation successful
                                                                location.hash = target;
        
                                                        });
                                                });
                                        }
                        }
        
                });
        
                // Use the first element that is "scrollable"  (cross-browser fix?)
                function scrollableElement(els) {
                        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                                var el = arguments[i],
                                $scrollElement = $(el);
                                if ($scrollElement.scrollTop()> 0) {
                                        return el;
                                } else {
                                        $scrollElement.scrollTop(1);
                                        var isScrollable = $scrollElement.scrollTop()> 0;
                                        $scrollElement.scrollTop(0);
                                        if (isScrollable) {
                                                return el;
                                        }
                                }
                        }
                        return [];
                }
        
        });
























