$(function() {
			
	$('body').addClass('hasjs');
	
	// Cufon rules
	Cufon.replace('h2', {fontSize: '24px'});
	Cufon.replace('.countryintro h2', {fontSize: '36px'});
	Cufon.replace('.storyintro h2', {fontSize: '36px'});
	Cufon.replace('h2 em', {fontSize: '20px'});
	Cufon.replace('#right h3', {fontSize: '20px'});
	Cufon.replace('.story h3', {fontSize: '26px'});
	Cufon.replace('.newsindex .story h3', {fontSize: '20px'});
	Cufon.replace('.action h3', {fontSize: '48px'});
	Cufon.replace('.action .introtext h3', {fontSize: '32px'});
	Cufon.replace('.storylink h4', {fontSize: '26px'});
	Cufon.replace('.stats h2', {fontSize: '22px'});

	// print action
	$("div.storyintro").find("li.print").click(function(event){ 
			event.preventDefault(); 
			window.print();
			return false;
	});

	
	// switch link attribute rel='external' to target='_blank'
	$('[rel=external]').removeAttr('rel').attr( 'target', '_blank' ); 
	
	// put in the 'last' class on the ul. 
	$("#subnav ul li:last-child").addClass('last'); 
	
	// put the active state on the 3rd level navigation
	/** get the file name **/
	var URL = unescape(location.href); 
	var xstart = URL.indexOf("co.uk/")+6; 
	var xstart = URL.lastIndexOf("/")+1; 
	var xend = URL.length
	var hereName = URL.substring(xstart,xend)
	if( hereName )	
		$("#subnav li").find('a[href$="'+ hereName +'"]').parent().addClass("active");
	
	// handle rotation on home page....
	
	/** set old index high for initial comparison **/ 
	oldindex=100; 
	j=0;
	/** what menu onclick event **/
	$("#homeblock").find("ul").find("li").hover(function() {
													
		clearTimeout(j);
		thisindex = $("#homeblock").find("ul").find("li").index(this); 
		
		if( thisindex!=oldindex ) { 
			runTransition2( thisindex, "homeblock" );  
			clearTimeout(t);
		}
		// restart the scroll so long as not over the images of the links
   	    },function() {
			if( $("#homeblock").find("div.action").mouseover() )
				clearTimeout(t);
			else if( $("#homeblock").find("ul").mouseover() )
				clearTimeout(t);
			else 
			  	setRotation( thisindex, $("#homeblock").find("ul").find("li").length ); 
	} );
	
	
	// pause the on mouse over the image 
	$("#homeblock").find("div.action").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){ 
			setRotation( pauseindex, $("#homeblock").find("ul").find("li").length ); 
	});
	
	// pause on mouse over the menu  
	$("#homeblock").find("ul").hover(function() {
			pauseindex = pauseTimer();
		},function(){
			setRotation( pauseindex, $("#homeblock").find("ul").find("li").length ); 
	});

	// trigger the rotation
	setRotation(0, $("#homeblock").find("ul").find("li").length ); 


});



/** what menu rotation function - timing is set in here **/
function setRotation( index, length ) {
	t = setTimeout( "changeStyle("+ index +", "+ length +" )", 10000 );
}

/** what change function... triggered by rotation function **/
function changeStyle( index, length ) {
	
	next = index+1; 
	if( next > length-1)
		next=0 
	runTransition( next, "homeblock" );  
	clearTimeout(t);
	setRotation( next, length ); 
}	 

/** what / where menu transition function used throughout **/ 
function runTransition( thisindex, block ) {
	
	$("#"+block).find("ul:first").find("li").removeClass("active"); 
	$("#"+block).find("ul:first").find("li").eq(thisindex).addClass("active"); 	
	
	$("#"+block).find("div.action").eq(thisindex).addClass("ontwo"); 
	$("#"+block).find("div.ontop").fadeOut("slow", function() { 
		$("#"+block).find("div").removeClass("ontop"); 
		$("#"+block).find("div").removeClass("ontwo");
		$("#"+block).find("div.action").eq(thisindex).addClass("ontop");
		$("#"+block).find("div").show(); 
	});  
}

/** what menu transition function.. with a pause **/ 
function runTransition2( thisindex, block ) {
	
	
	$("#"+block).find("ul:first").find("li").removeClass("active"); 
	$("#"+block).find("ul:first").find("li").eq(thisindex).addClass("active"); 	
	j=setTimeout( "changeblock("+ thisindex +")", 300 ); 
}

/** image change for whatmenu block **/
function changeblock(thisindex){ 
	
	$("#homeblock").find("div.action").eq(thisindex).addClass("ontwo"); 
	$("#homeblock").find("div.ontop").fadeOut("slow", function() { 
		$("#homeblock").find("div").removeClass("ontop"); 
		$("#homeblock").find("div").removeClass("ontwo");
		$("#homeblock").find("div.action").eq(thisindex).addClass("ontop");
		$("#homeblock").find("div").show(); 
	}); 
	oldindex = thisindex-1; 
}

/** pause timer and return the previous element index so we can then continue ticking along **/
function pauseTimer() {
	
	clearTimeout(t);
	$("#homeblock").find("ul").find("li").each( function() {
			if( $(this).hasClass('active') )
				theindex = $("#homeblock").find("ul").find("li").index(this); 
	 });

	return theindex;
}


