// JavaScript Document

$(document).ready(function(){
	//adds class to body tag. Use for CSS rules with js ON/OFF
	$('body').addClass('hasjs');
	
	// switch link attribute rel='external' to target='_blank'
	$('[rel=external]').removeAttr('rel').attr( 'target', '_blank' ); 
	
	//add classes for first and last items in sub nav
	$("#subNav ul li:first").addClass('first-item');
	$("#subNav ul li:last-child").addClass('last-item'); 

	/** set old index high for initial comparison **/ 
	oldindex=100; 
	j=0;
	/** what menu onclick event **/
	$("#introblock").find("ul").find("li").hover(function() {
													
		clearTimeout(j);
		thisindex = $("#introblock").find("ul").find("li").index(this); 
		
		if( thisindex!=oldindex ) { 
			runTransition2( thisindex+1, thisindex+2, "introblock" );  
			clearTimeout(t);
		}
		/** restart the scroll so long as not over the images of the links **/ 
   	    },function() {
			if( $("#introblock").find("div.action").mouseover() )
				clearTimeout(t);
			else if( $("#introblock").find("ul").mouseover() )
				clearTimeout(t);
			else 
			  	setRotationwhat( thisindex+1, $("#introblock").find("ul").find("li").length ); 
	} );

	/** pause the on mouse over the image **/
	$("#introblock").find("div.action").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){ 
			setRotationwhat( pauseindex+1, $("#introblock").find("ul").find("li").length ); 
	});
	
	/** pause on mouse over the menu  **/
	$("#introblock").find("ul").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){
			setRotationwhat( pauseindex+1, $("#introblock").find("ul").find("li").length ); 
	});
	
	/** trigger what and where menu rotaion events **/
	setRotationwhat(1, $("#introblock").find("ul").find("li").length ); 

																							   
});


/** introblock rotation function - timing is set in here **/
function setRotationwhat( index, length ) {
	t = setTimeout( "changeStylewhat("+ index +", "+ length +" )", 12000 );
}

/** what change function... triggered by rotation function **/
function changeStylewhat( index, length ) {
	
	next = index+1; 
	if( next>length)
		next=1; 
	runTransition( next, next+1, "introblock" );  
	clearTimeout(t);
	setRotationwhat( next, length ); 
}	 


/** introblock transition function.. with a pause **/ 
function runTransition2( thisindex, blockindex, block ) {
	
	
	$("#"+block).find("ul:first").find("li").removeClass("active"); 
	$("#"+block).find("ul:first").find("li:nth-child("+thisindex+")").addClass("active"); 	
	j=setTimeout( "changeblock("+ thisindex +", "+ blockindex +")", 300 ); 
}

/** image change for introblock block **/
function changeblock(thisindex, blockindex){ 
	
	$("#introblock").find("div.action:nth-child("+blockindex+")" ).addClass("ontwo"); 
	$("#introblock").find("div.ontop").fadeOut("slow", function() { 
		$("#introblock").find("div").removeClass("ontop"); 
		$("#introblock").find("div").removeClass("ontwo");
		$("#introblock").find("div.action:nth-child("+blockindex+")" ).addClass("ontop");
		$("#introblock").find("div").fadeIn("fast"); 
	}); 
	oldindex = thisindex-1; 
}


/** what / where menu transition function used throughout **/ 
function runTransition( thisindex, blockindex, block ) {
	
	$("#"+block).find("ul:first").find("li").removeClass("active"); 
	$("#"+block).find("ul:first").find("li:nth-child("+thisindex+")").addClass("active"); 	
	$("#"+block).find("div.action:nth-child("+blockindex+")" ).addClass("ontwo"); 
	$("#"+block).find("div.ontop").fadeOut("slow", function() { 
		$("#"+block).find("div").removeClass("ontop"); 
		$("#"+block).find("div").removeClass("ontwo");
		$("#"+block).find("div.action:nth-child("+blockindex+")" ).addClass("ontop");
		$("#"+block).find("div").fadeIn("fast"); 
	});  
}

/** pause timer and return the previous element index so we can then continue ticking along **/
function pauseTimer() {
	
	clearTimeout(t);
	$("#introblock").find("ul").find("li").each( function() {
			if( $(this).hasClass('active') )
				theindex = $("#introblock").find("ul").find("li").index(this); 
	 });

	return theindex
}



