// set the starting image.
var i = 0;			

// The array of div names which will hold the images.
var image_slide = new Array();

// The time to wait before moving to the next image. Set to 3 seconds by default.
var wait = 4000;

// the onload event handler that starts the fading.
function StartSlideShow(NumSlides) {
	
	var NumSlides, SlidesCount, imageShow, imageHide;
	
	i = 0;	
	
	if (typeof play != 'undefined') {
		clearInterval(play);	
		//alert('This got by check for play');
	}
	
	image_slide.length = 0; 

	
	SlidesCount = 1;

	//alert(SlidesCount);
	
	if (NumSlides > 0) {
		while (SlidesCount <= NumSlides) {	
	
			image_slide.push('image-'+ SlidesCount);
			//alert('image-'+ SlidesCount);
			
			SlidesCount++;
		}
	}
	
	PlayMe();				
}

function updatecounter() { 
	//var NumOfImages;
	NumOfImages  = image_slide.length;	
	var textIn = i+1 + ' of ' + NumOfImages; 
	document.getElementById('Counter').innerHTML = textIn; 
}

function PlayMe() {
	
	//Play();
	
	play = setInterval('Play()',wait);
		
	//$('PlayButton').hide();
	//$('PauseButton').appear({ duration: 0});

}

// The Fade Function
function SwapImage(x,y) {		
	$(image_slide[x]).appear({ duration: 0.5 });
	$(image_slide[y]).fade({duration: 0.5});
}


function Play() {
	var NumOfImages;
	
	// The number of images in the array.
	if (typeof NumOfImages == 'undefined') {
		NumOfImages  = image_slide.length;	
		//alert('This got by check for play');
	}

	imageShow = i+1;
	imageHide = i;
		
	//alert (imageShow + ' and ' + imageHide + ' of ' + NumOfImages);
	
	if (imageShow == NumOfImages) {
		SwapImage(0,imageHide);	
		i = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		i++;
	}
	
	$('PlayButton').hide();
	$('PauseButton').appear({ duration: 0});
	updatecounter();
	
}

function Stop () {
	if (typeof play != 'undefined') {
		clearInterval(play);	
		//alert('This got by check for play');
	}				
	$('PlayButton').appear({ duration: 0});
	$('PauseButton').hide();
}

function GoNext() {
	//if (typeof play != 'undefined') {
		clearInterval(play);	
		//alert('This got by check for play');
	//}
	
	if (typeof NumOfImages == 'undefined') {
		NumOfImages  = image_slide.length;	
		//alert('This got by check for play');
	}
	
	$('PlayButton').appear({ duration: 0});
	$('PauseButton').hide();
	
	var imageShow, imageHide;

	imageShow = i+1;
	imageHide = i;
	
	//alert (imageShow + ' and ' + imageHide + ' of ' + NumOfImages);
	
	if (imageShow == NumOfImages) {
		SwapImage(0,imageHide);	
		i = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		i++;
	}
	
//	alert (imageShow + ' and ' + imageHide + ' of ' + NumOfImages);
}

function GoPrevious() {
	//if (typeof play != 'undefined') {
		clearInterval(play);	
		//alert('This got by check for play');
	//}
	
	if (typeof NumOfImages == 'undefined') {
		NumOfImages  = image_slide.length;	
		//alert('This got by check for play');
	}
	
	$('PlayButton').appear({ duration: 0});
	$('PauseButton').hide();

	var imageShow, imageHide;
				
	imageShow = i-1;
	imageHide = i;
	
	if (i <= 0) {
		SwapImage(NumOfImages-1,imageHide);	
		i = NumOfImages-1;		
		
		//alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
					
	} else {
		SwapImage(imageShow,imageHide);			
		i--;
		
	}
	
	//alert (imageShow + ' and ' + imageHide + ' of ' + NumOfImages);
}