// Image Animations for TASS
//Version 1.0  
var iCurrent = 0;  //The index in aPics of the image currently displayed
var iPrev;			//The index in aPics of the image previously displayed
var iNoPics;		//The total number of pictures
var aPics = new Array();  //Holds all the big pictures
var bSlideShow = false;		//True when the slideshow is running
var timeoutID;					//Holds the id of the timeout used in the slideshow, so that it can be cancelled when the slidewhow is stopped.
var iShowTime = 3000; //Time spent on each slide in the slide show, in ms
var iFadeTime = 1000; //Time fading between images when big image clicked, in ms
var i;
var z;

$(document).ready(function()
	{
	aPics = $('#clickpics > img');
	iPrev = aPics.length - 1;
	iNoPics = aPics.length;
	
	z= iNoPics;
	 $('#clickpics img').each(function() { 
     $(this).css('z-index', z); 
	   z--; 
  		});
	 
	//Make all the images visible
	$('#clickpics img').each( function() { $(this).css('display', 'block') } ); 
	
	
	//Set nextPic to be called : is set on the images, not the holding div, to allow the control panel to get clicks
	$('#clickpics > img').click(mainPicClick);
	
		//Set the initial value of the label
	$('#label').text("1 of " + iNoPics);
	
		//Set slideShow to be called when the link is clicked
	$('#slideshow-link').click(
	function(event){
		event.preventDefault();
		startShow();} );

	
	iPrev = iNoPics - 1;
	
	} );

//Move to the next picture : called by clicking on the main image and by the slideshow
function nextPic()
{
	iPrev = iCurrent;
	iCurrent = ( iCurrent + 1 ) % iNoPics;
	$(aPics[iPrev]).fadeOut(iFadeTime);
	$(aPics[iCurrent]).fadeIn(iFadeTime);
	$('#label').text( (iCurrent + 1 ) +  " of " + iNoPics);

}

//Called when the "start show / Stop show" link is clicked
function startShow()
{
	if ( bSlideShow )
		{
		bSlideShow = false;
		window.clearTimeout(timeoutID)
		$('#slideshow-link').text('Start Slideshow');
		}
	else
		{
		bSlideShow = true;
		$('#slideshow-link').text('Stop Slideshow');
		nextPic();
		timeoutID = window.setTimeout( slideShow, iShowTime );
		}

}

//Called when items are clicked to stop the slideshow if its running
function stopShow()
{
	if ( bSlideShow )
		{
		bSlideShow = false;
		window.clearTimeout(timeoutID)
		$('#slideshow-link').text('Start Slideshow');
		}	
}

//Runs the slideshow
function slideShow()
{	
	if ( bSlideShow )
		{
		nextPic();
		timeoutID = window.setTimeout( slideShow, iShowTime);
		}
}

//Called when one of the main images is clicked
function mainPicClick()
{
//Stop the slideshow, if it's running
if ( bSlideShow )
	stopShow();
else
//Move to the next picture 
	nextPic();
}

