// JQuery extra shuffle function
// thanks to http://yelotofu.com/2008/08/jquery-shuffle-plugin/

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

// Config variables...

var sectiontext = [
'In which we learn how to prepare oneself – A closer look at problem questions – Our hero gets top marks without rushing – A revision plan of Herculean proportions (with ample time for tea) – Mentors help the perplexed – Defamation in the theatre' ,

'In which Rosanna and Stacy treat us to the benefit of their mentoring experiences – Learn to thrive in lectures and tutorials – Get excited by the prospect of some work in the real world – Ross aka President fills us in on The Law Society -  Lawbore is introduced in all its glory',

'In which our heroine embarks on her coursework - Much (ex)citation about references - How not to pester your tutor for answers - A student wonders if it\'s possible to beat 80% - Mr Herling (a much respected lecturer) shares his inside story - Miss Yang wins a prestigous prize.',

'In which we wrestle with a skeleton – A profusion of judges – Eavesdrop on mooter’s chat – How to please the judge’s ear – Anita gets speedy – Feel the pain of a judicial intervention – Accept being a ‘loser’ - Many moot points',

'In which we reduce many things to a string of capitals – The Plagiarists Guide to Getting Thrown Out – Databases Rock! – Our detective hunts down some obscure full texts – Ms Vieitez leads us through a maze of practitioner volumes – Grapple with getting information in the web 2.0 world.',

'In which we learn what Emily does on a daily basis - Ali and Matthew descend on Allen & Overy - Why the pros are pro-<i>pro bono</i>.'

];
var sectioncaptions = ['Chapter One<br />EXAMS',
'Chapter Two<br />NEWBIES',
'Chapter Three<br />WRITING',
'Chapter Four<br />MOOTS',
'Chapter Five<br />RESEARCH',
'Chapter Six<br />CAREERS'
];


var timeToSlideshow=2000; // time before slideshow of thoughts starts
var imagesToUse=new Array(1,2,3,4,5,7,8,9,11,12,13,14,15,16,17); // array of which image numbers get used. 
imagesToUse = $.shuffle(imagesToUse);// above array gets shuffled at startup
var sizeOfSlideshow=15; // number of items to use each time from above array 
var slideshowInterval = 5000; // one pic every XXX ms
var slideshowPicDisplay = 2500; // show this pic for how long (must be quite a bit less than interval)

var hovering = false;
var slideshowPic = 0;

var pageJustLoaded=true; // true if page has just been loaded and nothing has changed yet





function mouseoutfunc () {
	
   		$("#sectiontext").fadeOut ("slow");
   		$("#sectioncaption").fadeOut ("slow");
		$("#brainhoveroverlay").css('background-position','0px 0px');
		hovering = false;
		$(document).oneTime(timeToSlideshow,'slideshowstarter', thoughtSlideshow);
	
}
function fadeOutThought () {
	// we only do the fade out if the slideshow has started
	// this doesn't happen for the inital image of greetings text
	if (pageJustLoaded) {
		$('#thoughtimage').hide().empty ();
		pageJustLoaded=false;
	} else {
	
		$('#thoughtimage').fadeOut("slow", function (){$(this).empty();});
	}
}


function displayImage () {
	if (hovering == true) return true;
	//alert ("showing pic "+slideshowPic);
	if (slideshowPic == sizeOfSlideshow) {slideshowPic = 0;} // cycle back to first in array when end reached
	$('#thoughtimage').fadeOut("slow", function (){
	$(this).empty();
	$('<img />').attr('src', 'frontpageimg/thought'+imagesToUse[slideshowPic]+'.png').load(function(){
	$('#thoughtimage').html( $(this) ).fadeIn("slow");
	$(document).oneTime(slideshowPicDisplay,'slideshowPicFadeout',fadeOutThought)});
	slideshowPic++;
	});
}

function thoughtSlideshow () {
	//alert ('start slideshow');
	displayImage ();
	$(document).stopTime ();
	$(document).everyTime (slideshowInterval,'slideshowImageFlip',displayImage);

}

function stopSlideshow () {
	$(document).stopTime();
	fadeOutThought ();
	
}


$(document).ready(function(){



   $("#examsarea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[0]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[0]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -320px');
		hovering = true;
		stopSlideshow ();
   	}
   ,   	mouseoutfunc
   );
   
      $("#newbiesarea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[1]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[1]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -640px');
		hovering = true;
		stopSlideshow ();

   	}
   , mouseoutfunc
   );
   
   
   $("#writingarea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[2]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[2]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -960px');
		hovering = true;
		stopSlideshow ();
   	}
   , mouseoutfunc
   );
   
     
   $("#mootsarea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[3]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[3]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -1280px');
		hovering = true;
		stopSlideshow ();
   	}
   , mouseoutfunc
   );
   
   
     $("#researcharea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[4]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[4]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -1600px');
		hovering = true;
		stopSlideshow ();
   	}
   , mouseoutfunc
   );
   
      
     $("#careersarea").hover (
   	function () {
   		$("#sectiontext").stop(true,true).empty ().append (sectiontext[5]).append ("<p><em>Click for details...</em></p>").fadeIn('slow');
   		$("#sectioncaption").stop(true,true).empty ().append (sectioncaptions[5]).fadeIn('slow');
		$("#brainhoveroverlay").css('background-position','0px -1920px');
		hovering = true;
		stopSlideshow ();
   	}
   , mouseoutfunc
   );
   
     
 });
 
 

 

