
 $(function() {
	var URL = ""; 
 	var xstart = ""; 
 	var xend = "";
 	var pageID = "";

	// switch link attribute rel='external' to target='_blank'
	$('[rel=external]').removeAttr('rel').attr( 'target', '_blank' ); 
	
	// switch link attribute rel='ontop' to target='_top'
	$('[rel=ontop]').removeAttr('rel').attr( 'target', '_top' ); 

	// put in the 'last' class on the ul. 
	$("#subnav ul:first-child").find("li:last-child").addClass("last");
	$("#subnav li").filter(":has(ul)").find("li").removeClass("last");
	
	$("#subnav li").find('a[@pid$="'+ pageID +'"]').addClass("active");
	$("#subnav li").find('a[@href$="'+ pageID +'"]').addClass("active");

	/*$("#nav li").hover(function(){
    	$(this).addClass("sfhover");
   	},function(){
    	$(this).removeClass("sfhover");
   	});*/

    /** set old index high for initial comparison **/ 
	oldindex=100; 
	j=0;
	/** what menu onclick event **/
	$("#whatmenu").find("ul").find("li").hover(function() {
													
		clearTimeout(j);
		thisindex = $("#whatmenu").find("ul").find("li").index(this); 
		
		if( thisindex!=oldindex ) { 
			runTransition2( thisindex+1, thisindex+2, "whatmenu" );  
			clearTimeout(t);
		}
		/** restart the scroll so long as not over the images of the links **/ 
   	    },function() {
			if( $("#whatmenu").find("div.action").mouseover() )
				clearTimeout(t);
			else if( $("#whatmenu").find("ul").mouseover() )
				clearTimeout(t);
			else 
			  	setRotationwhat( thisindex+1, $("#whatmenu").find("ul").find("li").length ); 
	} );

	/** pause the on mouse over the image **/
	$("#whatmenu").find("div.action").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){ 
			setRotationwhat( pauseindex+1, $("#whatmenu").find("ul").find("li").length ); 
	});
	
	/** pause on mouse over the menu  **/
	$("#whatmenu").find("ul").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){
			setRotationwhat( pauseindex+1, $("#whatmenu").find("ul").find("li").length ); 
	});
	
	/** where menu onclick event **/
	$("#wheremenu").find("ul:first").find("li").click(function(event) {
		event.preventDefault(); 
		var thisindex = $("#wheremenu").find("ul:first").find("li").index(this); 
		runTransition( thisindex+1, thisindex+2, "wheremenu" );  
	});

	/** trigger what and where menu rotaion events **/
	setRotationwhat(1, $("#whatmenu").find("ul").find("li").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, next+1, "whatmenu" );  
	clearTimeout(t);
	setRotationwhat( next, length ); 
}	 


/** what menu 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 +")", 1 ); 
}

/** image change for whatmenu block **/
function changeblock(thisindex, blockindex){ 
	
	$("#whatmenu").find("div.action:nth-child("+blockindex+")" ).addClass("ontwo"); 
	$("#whatmenu").find("div.ontop").fadeOut("slow", function() { 
		$("#whatmenu").find("div").removeClass("ontop"); 
		$("#whatmenu").find("div").removeClass("ontwo");
		$("#whatmenu").find("div.action:nth-child("+blockindex+")" ).addClass("ontop");
		$("#whatmenu").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);
	$("#whatmenu").find("ul").find("li").each( function() {
			if( $(this).hasClass('active') )
				theindex = $("#whatmenu").find("ul").find("li").index(this); 
	 });

	return theindex
}
