// The Faversham
// Global Javascript functions
// Powered by Prego

$(function()
{
	// Highlights animation
	$("#highlights li").each(function()
	{
		$(this)
			.css("cursor", "pointer")
			.click(function()
			{
				window.location = $("base").attr("href") + $("a", this).attr("href");
			});
		
		var $m = $(".more", this);
		var $h = $("h4", this);
		
		var moreHeight = $m.get(0).offsetHeight;
		$m.css("height", 0);
		
		$(this).hover(
			function(e)
			{
				var headingHeight = $h.get(0).offsetHeight;
				$h.removeClass("closed");
				$m
					.stop()
					.animate({height:moreHeight+"px", marginTop:"-"+headingHeight+"px", paddingTop:headingHeight+"px"}, "medium");
			},
			function(e)
			{
				$h.addClass("closed");
				$m
					.stop()
					.animate({height:"0px", marginTop:"0px", paddingTop:"0px"}, "medium");
			}
		);
	});
	
	// External links
	$("a[href^=http://]").each(function()
	{
		this.target = "_blank";
	});
	
	// Lightbox
	$("div.vevent a.image, div.hentry a.image, div.about a.image").lightBox();
	
	// Registration form validation
	$("#signup-form").submit(function()
	{
		// Check required fields are complete
		var requiredComplete = true;
		$("#signup-form input.required").each(function()
		{
			var val = $(this).attr("value");
			if(typeof(val) == "undefined" || val == "")
				requiredComplete = false;
		});
		
		if(!requiredComplete)
		{
			alert("Please complete all required fields (marked with *)");
			return false;
		}
		
		// Check email addresses are valid
		var email = $("#signup-form input#email-input").attr("value");
		var emailRegEx = /^([a-z0-9]([a-z0-9_-]*\.?[a-z0-9])*)(\+[a-z0-9]+)?@([a-z0-9]([a-z0-9-]*[a-z0-9])*\.)*([a-z0-9]([a-z0-9-]*[a-z0-9]+)*)\.[a-z]{2,6}$/i;
		if(!email.match(emailRegEx))
		{
			alert("The email address you entered isn't valid");
			return false;
		}

		// All validation passed
		return true;
	});
});