// 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 **/
	$("#landingblock").find("ul").find("li").hover(function() {
													
		clearTimeout(j);
		thisindex = $("#landingblock").find("ul").find("li").index(this); 
		
		if( thisindex!=oldindex ) { 
			runTransition2( thisindex+1, thisindex+2, "landingblock" );  
			clearTimeout(t);
		}
		/** restart the scroll so long as not over the images of the links **/ 
   	    },function() {
			if( $("#landingblock").find("div.action").mouseover() )
				clearTimeout(t);
			else if( $("#landingblock").find("ul").mouseover() )
				clearTimeout(t);
			else 
			  	setRotationwhat( thisindex+1, $("#landingblock").find("ul").find("li").length ); 
	} );

	/** pause the on mouse over the image **/
	$("#landingblock").find("div.action").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){ 
			setRotationwhat( pauseindex+1, $("#landingblock").find("ul").find("li").length ); 
	});
	
	/** pause on mouse over the menu  **/
	$("#landingblock").find("ul").hover(function() {
			pauseindex = pauseTimer(); 
		},function(){
			setRotationwhat( pauseindex+1, $("#landingblock").find("ul").find("li").length ); 
	});
	
	/** trigger what and where menu rotaion events **/
	setRotationwhat(1, $("#landingblock").find("ul").find("li").length ); 


	/** handle hover event over the image block **/
	$("#introblock").find("ul").hover(function() {
			pauseImageIndex = pauseImageTimer(); 
		},function(){
			setImageRotation( pauseImageIndex+1, $("div#introblock").find("li").length ); 
	});

	/** trigger image rotaion events **/
	setImageRotation( 1, $("div#introblock").find("li").length )
																							   



	// handle the links....	
	var showhtml = "<p class='show'><a href='#'>Show the data</a></p>"; 
	$("div.data").attr('style', 'display: none'); 
	$("div.data").after(showhtml); 

	$("div#content").find("p").click(function(event) {
		
		event.preventDefault(); 	
		if( $(this).hasClass("hide") )
		{
			$(this).addClass('show');	
			$(this).removeClass('hide');	
			$(this).html("<a href='#'>Show the data</a>"); 
			$(this).prev("div.data").slideToggle("slow"); 
		}
		else if( $(this).hasClass("show") ) 
		{
			$(this).removeClass('show');
			$(this).addClass('hide');	
			$(this).html("<a href='#'>Hide the data</a>");
			$(this).prev("div.data").slideToggle("slow"); 
		}		
    	
	});

});

/** landingblock rotation function - timing is set in here **/
function setImageRotation( imgindex, imglength ) {
	timg = setTimeout( "changeImage("+ imgindex +", "+ imglength +" )", 5000 );
}

function changeImage( imgindex, imglength ){
	
	imgnext = imgindex+1; 
	if( imgnext>imglength)
		imgnext=1; 
	runImageTransition( imgnext  );  
	clearTimeout(timg);
	setImageRotation( imgnext, imglength ); 
}

function runImageTransition( imgIndex ){

	gasIndex = imgIndex; 
	$("div#introblock").find("li:nth-child("+ gasIndex +")" ).addClass("ontwo"); 
	$("div#introblock").find("li.ontop").fadeOut("slow", function() { 
		$("div#introblock").find("li").removeClass("ontop"); 
		$("div#introblock").find("li").removeClass("ontwo");
		$("div#introblock").find("li:nth-child("+ gasIndex +")" ).addClass("ontop");
		$("div#introblock").find("li").fadeIn("slow"); 
	}); 
}

/** pause timer and return the previous element index so we can then continue ticking along **/
function pauseImageTimer() {
	
	clearTimeout(timg);
	//alert( gasIndex ); 
	$("div#introblock").find("ul").find("li").each( function() {
			if( $(this).hasClass('ontop') )
				theindex = $("div#introblock").find("ul").find("li").index(this); 
	 });
	return theindex
}






/** landingblock 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, "landingblock" );  
	clearTimeout(t);
	setRotationwhat( next, length ); 
}	 


/** landingblock 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 landingblock block **/
function changeblock(thisindex, blockindex){ 
	
	$("#landingblock").find("div.action:nth-child("+blockindex+")" ).addClass("ontwo"); 
	$("#landingblock").find("div.ontop").fadeOut("slow", function() { 
		$("#landingblock").find("div").removeClass("ontop"); 
		$("#landingblock").find("div").removeClass("ontwo");
		$("#landingblock").find("div.action:nth-child("+blockindex+")" ).addClass("ontop");
		$("#landingblock").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);
	$("#landingblock").find("ul").find("li").each( function() {
			if( $(this).hasClass('active') )
				theindex = $("#landingblock").find("ul").find("li").index(this); 
	 });

	return theindex
}


