$(document).ready(function(){
	
	$("#nav ul#nav_cat_archive li a").hover(
	// Create div on hover  
	function () {

		$(".level2").show();

	  },
	// Remove div on mouse out
	function () {
		$("#people-preview").remove();
	  }
	);
	

	$("img.people-pic").hide();
	$("img.people-pic").fadeIn("slow");

	
	
	
		
	// Update news archives
	$("ul#yearly-archives li ul li a").click(function(){ 
		$("#news-archive-right h3").replaceWith("<img src=\"/images/ajax/ajax-loader.gif\" alt=\"Ajax Loader\" />");
		$("#news-archive-right ul").hide();
		var trimmedURL = this.href.replace("http://www.fountaincourt.co.uk/news/archives", "");		
		$.ajax({
			type: "GET",
			url: "/ajax/news_archive"+trimmedURL,
			success: function(msg){
			$("#news-archive-right").replaceWith(msg);
		   }
		 });
		return false;
	});

	// Hides form on load
	$('#email-cv').hide();

	// Expands email form on click	
	$('.email-cv').bind("click", function(){
	$('#email-cv').slideDown("slow");
 	return false;
	});	
	
	
	// All members show picture preview
		$(".all-members-ul li").hover(
		// Create div on hover  
		function () {
			//var trimmedURL = this.href.replace("http://www.fountaincourt.co.uk/", "");
			
			var link = $("a", this).attr("href");
			var image = $("img", this).attr("src");
			var title = $("p", this).html();
			var name = $("a", this).text();
						
			$("#people-preview a").attr("href", link);
			$("#people-preview img").attr({ 
				alt: title,
				title: title,
				src: image
			});
			$("#people-preview h4").html(name);
			$("#people-preview p").html(title);

		  },
		// Remove div on mouse out
		function () {
		  }
		);
		



	// Ajax submission of form
	$("#email-cv").submit( function() {
		$("#email-cv").css({ display: "none" });
		$("#ajax-loader").css({ display: "block" });
		// Get inputs
		var inputs = [];
		$(':input', this).each(function(){
			inputs.push(this.name + '=' + escape(this.value))
		})
		
		// Send AJAX request to this page
		$.ajax({
			type: "POST",
			url: this.action,
			data: inputs.join('&'),
			timeout: 2000,
			error: function(){
				console.log("HAS FAIL")
			},	
			// Do some stuff on success
			success: function(){
			$("#ajax-loader").css({ display: "none" });
			$("#email-cv").after("<p>The CV was sent to the email address you submitted.</p>");	
		 }
		});
 	return false;
	} );

	
});
