// JavaScript Document

 $(function() {



	/** pause the on mouse over the image **/
	$("#slideshowContent").find("div.slideshow").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){ 
			setRotationwhat( pauseindex+1, $("#slideshowContent").find(".slideshow").length );  
	});
	

	/** trigger what and where menu rotaion events **/
	setRotationwhat(1, $("#slideshowContent").find(".slideshow").length ); 
});
 

/** what menu rotation function - timing is set in here **/
function setRotationwhat( index, length ) {
	t = setTimeout( "changeStylewhat("+ index +", "+ length +" )", 4000 );
}

/** what change function... triggered by rotation function **/
function changeStylewhat( index, length ) {
	
	next = index+1; 
	if( next>length)
		next=1; 
	runTransition( next );  
	clearTimeout(t);
	setRotationwhat( next, length ); 
}	 

/** what / where menu transition function used throughout **/ 
function runTransition(thisindex) {
	
	$("#slideshowContent").find("div.slideshow:nth-child("+thisindex+")" ).addClass("ontwo"); 
	$("#slideshowContent").find("div.ontop").fadeOut("slow", function() { 
		$("#slideshowContent").find("div.slideshow").removeClass("ontop"); 
		$("#slideshowContent").find("div.slideshow").removeClass("ontwo");
		$("#slideshowContent").find("div.slideshow:nth-child("+thisindex+")" ).addClass("ontop");
		$("#slideshowContent").find("div.slideshow").fadeIn("fast"); 
	});  
}

/** pause timer and return the previous element index so we can then continue ticking along **/
function pauseTimer() {
	
	clearTimeout(t);
	$("#slideshowContent").find(".slideshow").each( function() {
			if( $(this).hasClass('ontop') )
				theindex = $("#slideshowContent").find(".slideshow").index(this); 
	 });

	return theindex
}