var fadeTimeOut=6; //Set timeout between attractor panels
var scrollSpeed=500; //Set scroll speed for news carousel

//JQuery document enhancement
$(document).ready(function(){

	//Email address obfuscation
	$("a.email").each(function(){
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text(e);
	});
	
	//Open link in new window
	$("a.external").click(function(){
		window.open(this.href);
		return false;
	});
	
	//Image rollovers
	$("img.rollover,input.rollover").hover(
		function(){
			if($(this).attr("src").indexOf("-over")==-1) {
				var newSrc = $(this).attr("src").replace(".gif","-over.gif");
				newSrc = newSrc.replace(".png","-over.png");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("-over")!=-1) {
				var oldSrc = $(this).attr("src").replace("-over.gif",".gif");
				oldSrc = oldSrc.replace("-over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);

	$("input.input-text").each ( //Define default text for each input element
		function() {
			this.rel=this.value;
		}
	);

	$("input.input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$("input.input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});

	//Call latest news carousel function
	if ($(".carousel").length) {
		newsCarousel();
	}
	
	//Call home page publications carousel function
	if ($("#col-right-home .pub-carousel").length) {
		publicationsHomeCarousel();
	}
	
	//Call details publications carousel function
	if ($("#col-right .pub-carousel").length) {
		publicationsCarousel();
	}
	
	//Call home page left colpublications carousel function
	if ($("#col-left .pub-carousel").length) {
		publicationsHomeCarousel();
	}
	
	
	
	
	
	
	
	//Call attractor rotation function
	if ($("#attractor").length) {
		attractorRotator();
	}
	
});

//News carousel function
function newsCarousel() {
	var numberOfItems=0; //Initialise function globals
	var currentItem=0;

	numberOfItems=$(".scroller div.item").length; //Define number of items

	$("a#item-prev").click(function(){ //Previous item
		prevItem();
		return false;
	});
	
	$("a#item-next").click(function(){ //Next item
		nextItem();
		return false;
	});
	
	function nextItem() { //Scroll functions
		if (currentItem<numberOfItems-1) {
			currentItem++;
			scrollCarousel();
		}
	};

	function prevItem() {
		if (currentItem>0) {
			currentItem--;
			scrollCarousel();
		}
	};

	function scrollCarousel() { //Tween carousel to destination
		$(".scroller").animate({left: (192*(currentItem)*-1)}, {duration: scrollSpeed, easeMethod: "easeInOut"});

		setThumbnails();
		
	};

	function setThumbnails() { //Set active thubnail
		$("ul.carousel-controls li.thumbnail img").attr("src","/images/hp_images/updated/carousel-grey.png");
		$("ul.carousel-controls li.thumbnail img:eq("+currentItem+")").attr("src","/images/hp_images/updated/carousel-blue.png");
		$(".scroller p.response").css("display","none"); 
		$("div.answer").css("display","block");
		$("img.qmark").css("display","block");
	};

	
	$("a#factMyth").click(function(){ //Previous item
		reveal();
		return false;
	});
	
	$("a#factMyth1").click(function(){ //Previous item
		reveal();
		return false;
	});
	
	
	
	$(".scroller p.response").css("display","none"); //Hide all panels except first


	function reveal() { //Interval fade function
		
		$(".scroller p.response:eq("+currentItem+")").css("display","block");
		$("div.answer").css("display","none");
		$("img.qmark").css("display","none");
		
	};
	
	
	
	
	
};

//Home page publications carousel function
function publicationsHomeCarousel() {
	var numberOfItems=0; //Initialise function globals
	var currentItem=0;

	numberOfItems=$(".pub-scroller div.publication").length; //Define number of items

	$("a#pub-prev").click(function(){ //Previous publication
		prevItem();
		return false;
	});
	
	$("a#pub-next").click(function(){ //Next publication
		nextItem();
		return false;
	});
	
	function nextItem() { //Scroll functions
		if (currentItem<numberOfItems-1) {
			currentItem++;
			scrollCarousel();
		}
	};

	function prevItem() {
		if (currentItem>0) {
			currentItem--;
			scrollCarousel();
		}
	};

	function scrollCarousel() { //Tween carousel to destination
		$(".pub-scroller").animate({left: (192*(currentItem)*-1)}, {duration: scrollSpeed, easeMethod: "easeInOut"});

		setThumbnails();
	};

	function setThumbnails() { //Set active thubnail
		$("ul.pub-carousel-controls li.thumbnail img").attr("src","/images/buttons/thumbnail.gif");
		$("ul.pub-carousel-controls li.thumbnail img:eq("+currentItem+")").attr("src","/images/buttons/thumbnail-over.gif");
	};
};

//Publications carousel function
function publicationsCarousel() {
	var numberOfItems=0; //Initialise function globals
	var currentItem=0;

	numberOfItems=$(".pub-scroller div.publication").length; //Define number of items

	$("a#pub-prev").click(function(){ //Previous publication
		prevItem();
		return false;
	});
	
	$("a#pub-next").click(function(){ //Next publication
		nextItem();
		return false;
	});
	
	function nextItem() { //Scroll functions
		if (currentItem<numberOfItems-1) {
			currentItem++;
			scrollCarousel();
		}
	};

	function prevItem() {
		if (currentItem>0) {
			currentItem--;
			scrollCarousel();
		}
	};

	function scrollCarousel() { //Tween carousel to destination
		$(".pub-scroller").animate({left: (192*(currentItem)*-1)}, {duration: scrollSpeed, easeMethod: "easeInOut"});

		setThumbnails();
	};

	function setThumbnails() { //Set active thubnail
		$("ul.pub-carousel-controls li.thumbnail img").attr("src","/images/buttons/thumbnail-blue.gif");
		$("ul.pub-carousel-controls li.thumbnail img:eq("+currentItem+")").attr("src","/images/buttons/thumbnail-blue-over.gif");
	};
};

//Attractor rotator function
function attractorRotator() {
	var numberOfItems=0; //Initialise function globals
	var currentItem=0;
	
	$("#attractor div.attractor-image:gt(0)").css("display","none"); //Hide all panels except first

	numberOfItems=$("#attractor div.attractor-image").length; //Define number of items

	function fadePanels() { //Interval fade function
		$("#attractor div.attractor-image:eq("+currentItem+")").fadeOut("slow");
		
		currentItem++; //Get next panel or go back to start
		if (currentItem==numberOfItems) {
			currentItem=0;
		}
		

		$("#attractor div.attractor-image:eq("+currentItem+")").fadeIn("slow");
		
		setTimeout(fadePanels,fadeTimeOut*1000);
	};
	
	setTimeout(fadePanels,fadeTimeOut*1000);
};



function factMythShow() {
	var numberOfItems=0; //Initialise function globals
	var currentItem=0;
	
	$("a#factMyth").click(function(){ //Previous item
		reveal();
		return false;
	});
	
	
	
	$(".scroller div.response").css("display","none"); //Hide all panels except first

	numberOfItems=$(".scroller div.response").length; //Define number of items

	function reveal() { //Interval fade function
		
		$(".scroller div.response:eq("+currentItem+")").css("display","show");
		
	};
	
};







