$(document).ready(function(){
	
	$('a[id^=lnkTrainingHero]').click(function(e){
		e.preventDefault();
		stopRotate();
		var arr = $(this).attr('id').split("-");
		var id = arr[1];
		$('div[id^=divTrainingHero]').hide();
		$("#divTrainingHero-"+id).show();
		MM_swapImgRestore();
	});
	
	$('div[id^=divTrainingHero]').click(function(e){
		stopRotate();
	});
	
	$('div[id^=divTrainingHero]').hover(
		function(){
			stopRotate();
		},
		function(){
			$('div[id^=divTrainingHero]').hide();
			$('div[id^=divTrainingHero].show').show();
			startRotate();
		}
	);

	// Start on load
	theRotator();

});

var intval="";

function theRotator() {
	
	$('div[id^=divTrainingHero]').hide();
	$('div[id^=divTrainingHero]:first').show();
	$('div[id^=divTrainingHero]:first').addClass('show');
	
	startRotate();
}

function rotate(){
	
	//Get the first image
	var current = ($('div[id^=divTrainingHero].show')?  $('div[id^=divTrainingHero].show') : $('div[id^=divTrainingHero]:first'));
	
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div[id^=divTrainingHero]:first') :current.next()) : $('div[id^=divTrainingHero]:first'));
	
	//get id
	var arr = next.attr('id').split("-");
	var id = arr[1];
	//Highlight related thumb
	MM_swapImgRestore();
	MM_swapImage('thumbs_r1_c'+id,'','../images/09/training/tindex_01-01-11/hero/thumbs_r1_c'+id+'_f2.png',1);
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.show()
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show')
	.hide();
}

function stopRotate(){
	
	clearInterval(intval);
	$('div[id^=divTrainingHero]').css({opacity: 1.0});
	MM_swapImgRestore();
}

function startRotate(){
	
	var current = $('div[id^=divTrainingHero].show');
	//get id
	var arr = current.attr('id').split("-");
	var id = arr[1];
	//Highlight related thumb
	MM_swapImgRestore();
	MM_swapImage('thumbs_r1_c'+id,'','../images/09/training/tindex_01-01-11/hero/thumbs_r1_c'+id+'_f2.png',1);
	
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	intval = setInterval('rotate()',5000);
}
