$(document).ready(function() {
	$('body').addClass('hasJQ');
	checkFormField();
	cycle_setup();
	navTidy(); //remove the divider line on active in main_nav
	hovTidy(); //remove the divider line on hover
	moveLinks();
	moveLinksII();
});

// gets the keywords input fields default value and checks
// before submission that it has been changed.
function checkFormField() {
	var defVal = $('#as_q').val();
	formClear();
	$('#search_form').submit(function () {
		if ($('#as_q').val() == defVal) {
			return false;
		};
	});
};

// Is called from the checkFormField function
function formClear () {
	var textContent = $('#keywords').val();
	$('#keywords').focus(function() {
		if (this.value == textContent) {
			$(this).val('');
		};
	}).blur(function() {
		if (this.value == '') {
			$(this).val(textContent);
		};
	});
};

function navTidy() {
	var prevNav = $('#main_nav li.current_page_item').prev();
	var ancNav = $('#main_nav li.current_page_ancestor').prev();
	if (prevNav) {
		prevNav.css("background-position","-1000px 50%");
	};
	if (ancNav) {
		ancNav.css("background-position","-1000px 50%");
	};
	$("#footer a:last").addClass('last');
	$("#main_nav li:last").addClass('last');
	$('#page_nav li:last').addClass('last');
};

function hovTidy() {
	$("#main_nav li:not('.current_page_item','.current_page_ancestor')").hover (
		function () {
			$(this).prev().css("background-position","-1000px 50%");
		},
		function () {
			$(this).prev().css("background-position","100% 50%");
		}
	);
};

function moveLinks () {

		$('.event_summary a.more').each(function() {
			
			var link = $(this);
			var numOfPs = link.parent().find('p').size();
			var iDpenP = (numOfPs - 2);
			var lastP = link.parent().find('p:last');
			var penP = link.parent().find('p:eq(' + (iDpenP) + ')');
			
			if (!$('body').hasClass('logged-in')) {
				link.appendTo(lastP);
			} else {
				link.appendTo(penP);
			};
			
		});
};

function moveLinksII () {
	jQuery('a.lead_summary').each(function() {
		var that = jQuery(this);
		that.appendTo(that.prev());
	});
}

function cycle_setup() {
	
	var t ='';
	var cycle = '';
	var current_li = 0;
	var previous_li = 0;		
	$('#thumbs li:first').find('a').addClass('active');
	changeImg();//initial call to the function to move it from the loading gif
	rotate();
		
	function rotate() {
		clearTimeout(t);
		cycle =	setInterval(function() {
		var numLis =  $('#thumbs li').size();//gets the amount of li's
		current_li = (previous_li + 1) % numLis;//move to the next image, if at last image, start again
		changeImg();//call the function to move to next li
		previous_li = current_li;//move the counter along one
		}, 10000);
	};	
	
	$("#thumbs ul li").click(function(){
		clearInterval(cycle);
		clearTimeout(t);
		current_li = $("#thumbs ul li").index(this); //get the index of the clicked li
		previous_li = current_li; //move the pointer to this, rotate resumes from this point (optional)
		changeImg(); //change the img
		t = setTimeout(function() { //resume rotation after delay (in milliseconds)
			rotate(); 
		},10000); 
		return false; 
	});
	
	function changeImg() {
		//capture the details we need for the swap
		var imgAlt = $('#thumbs li:eq(' + current_li + ')').find('img').attr("alt"); //Get Alt Tag of Image
		var imgURL = $('#thumbs li:eq(' + current_li + ')').find('a').attr("href"); //Get URL for main image
		var imgCaption = $('#thumbs li:eq(' + current_li + ')').find('p').html();  //Get HTML of the caption container

		//perform the swap
		$("#image_caption_wrapper img").attr({ src: imgURL , alt: imgAlt}); //swap out the image and alt
		  $("#image_caption_wrapper #caption p").html(imgCaption); //swap the html of the block
		  $("#thumbs ul li a").removeClass('active'); //Remove class of 'active' on all list-items a tags
		  $('#thumbs li:eq(' + current_li + ')').find('a').addClass('active');  //Add class of 'active' on the selected list item's a tagchangeImg
	};
	
};