$(document).ready(function(){

	$("a.thumb").hover(
	function() {
	$(this).find('img:first').stop().animate({"background-color": "#51b85e;"}, "medium");
	},
	function() {
	$(this).find('img:first').stop().animate({"background-color": "#363636"}, "medium");
	});
	
	
	$("#topbar ul li a:not('.current')").hover(
	function() {
	$(this).stop().animate({"color": "#51b85e;"}, "medium");
	},
	function() {
	$(this).stop().animate({"color": "#fff"}, "medium");
	});
	
	/* FORM SUBMISSION */
	$(".submit").click(function() {
		error = false;
		ErrorString = "";
		$(".errorCross").remove();
		$(this).parent().parent().find(".required").each(function(index) {
			TheLength = $(this).val().length;
			TheFieldName = $(this).parent().find("label").text();
			TheFieldName = TheFieldName.replace("*", "");
			TheFieldName = TheFieldName.replace(":", "");
			if (TheLength < 1)
			{
				error = true;
				$(this).parent().find("label").prepend('<span class="errorCross">X</span>');
				ErrorString = ErrorString + "<li>Please enter a your <strong>" + TheFieldName + "</strong></li>\n";
			}
			else 
			{
				$(this).parent().find(".errorCross").remove();
			}
		});
		/* CHECK IF EMAIL ADDRESS IS VALID */
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		EmailAddress = $(this).parent().parent().find(".email").val();
		if(!emailReg.test(EmailAddress)) {
            ErrorString = ErrorString + "<li>Please enter a valid <strong>Email Address</strong></li>";
            error = true;
			$(this).parent().parent().find(".email").parent().find("label").prepend('<span class="errorCross">X</span>');
        }
		/* SUBMIT FORM IF NO ERRORS */	
		if (error == false){
			$(".errorMessage").remove();
			$(this).parent().parent().submit();
		}
		/* DISPLAY ALERT IF ERRORS */
		else
		{
			$(".errorMessage").remove();
			$(this).parent().parent().prepend('<div class="errorMessage"><p>Errors in form submission data, please review the highlighted fields.</p><ul>' + ErrorString + '</ul></div>');
			return false;
		}
	});
	
	/* NEW WINDOW LINKS */
	$(function(){
		$('a.new-window').click(function(){
			window.open(this.href);
			return false;
		});
	});
	
	$("a.thumb").fancybox({
		'onComplete'	:	function() {
		$("a.thumb img").css('background-color', '#363636');
		}
	});
	
	$('#footer-google').html('<g:plusone callback="http://www.matthewfreeman.co.uk"></g:plusone>');
});
