/* ------------------------------------------------------
----------------- JAVASCRIPT SLIDE SHOW -----------------
-------------------------------------------------------*/
function slideSwitch($switchSpeed, $newSlide) {
	$newSlide = $newSlide || false;
	var $activeI = $('#rotator DIV.active');
	var $activeT = $('#rotatorTXT DIV.active');
	var $activeB = $('#rotatorBTN A.active');
	
	if ( $activeI.length == 0 ) {
		$activeI = $('#rotator DIV:last');
		$activeT = $('#rotatorTXT DIV:last');
		$activeB = $('#rotatorBTN A:last');
	}
		
	var $nextI =  $activeI.next('DIV').length ? $activeI.next('DIV')
		   : $('#rotator DIV:first');
	var $nextT =  $activeT.next('DIV').length ? $activeT.next('DIV')
		   : $('#rotatorTXT DIV:first');
	var $nextB =  $activeB.next('A').length ? $activeB.next('A')
		   : $('#rotatorBTN A:first');
		if ($nextB.hasClass('pause') || $nextB.hasClass('play')) {
			$nextB = $('#rotatorBTN A:first');
			/* The "select next" picked up the play/pause button, so set it to the first. */
		}
	
	if ($newSlide) {
		$nextI = $('#rotator DIV:nth-child(' + $newSlide + ')');
		$nextT = $('#rotatorTXT DIV:nth-child(' + $newSlide + ')');
		$nextB = $('#rotatorBTN A:nth-child(' + $newSlide + ')');
		pauseSlide(); /* If a button is pushed, clear the interval and swap the button to allow easier reading. */
	}

	$activeI.addClass('last-active');
	$activeT.addClass('last-active');
	$activeB.addClass('last-active');
	
	$nextI.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, $switchSpeed, function() {
			$activeI.removeClass('active last-active');
		});
		
	$activeT.animate({opacity: 0.0}, $switchSpeed);
	$nextT.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, $switchSpeed, function() {
			$activeT.removeClass('active last-active');
		});
		
	$activeB.animate({opacity: 0.5}, $switchSpeed);
	$nextB.css({opacity: 0.5})
		.addClass('active')
		.animate({opacity: 1.0}, $switchSpeed, function() {
			$activeB.removeClass('active last-active');
		});
}

function playSlide() {
	$('#rotatorBTN A#playPauseButton').removeClass('play').addClass('pause');
	$int=self.setInterval("slideSwitch(1000)", 5000);
}

function pauseSlide() {
	$('#rotatorBTN A#playPauseButton').removeClass('pause').addClass('play');
	$int=window.clearInterval($int);
}

/* Slideshow in inactive tab will start to queue instant slide changes. On restore, the slideshow goes crazy. This stops the slideshow and clears the interval when the tab/window does inactive. */
function onBlur() {
		pauseSlide();
}
if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusout = onBlur;
} else {
    window.onblur = onBlur;
}

$("#rotatorBTN A#playPauseButton").click(function(event){
	if ( $(this).hasClass('pause') ) {
		pauseSlide();
	} else if ( $(this).hasClass('play') ) {
		playSlide();
	}
});
var $int = null; /* This needed to be a globally accessible variable for the playSlide() and pauseSlide() functions. */
playSlide();


