var currentImage = 0;
var timeout = 0;
var playing = true;
var delay = 7000;
var restartdelay = 20000;

function showNextImage(mode)
{
	isauto = true;
	if (mode == undefined || mode == null)
	{
		isauto = true;
	}
	else if (mode == "manual")
	{
		isauto = false;
	}
	if (!playing && isauto) return;
	clearTimeout(timeout);

	if (images.length>1)
	{
	
		if (currentImage > -1)
		{
			// Remove highlight from current thumb
			if($('thumb'+currentImage)) $('thumb'+currentImage).removeClassName('active');
		}
		var nextImageIndex = currentImage + 1;
		
		if (nextImageIndex < images.length)
		{
			// Fade in next image
			$('big_photo_'+nextImageIndex).appear({duration:.5});
			currentImage = nextImageIndex;
			
			if (isauto)
			{
				// Set timeout to next image
				playing = true;
				timeout = setTimeout(showNextImage,delay);
			}
		}
		else
		{
			// It's the last image, so hide all the images but the first and last
			for (var i=1; i<images.length-1; i++)
			{
				$('big_photo_'+i).style.display = 'none';
			}
			// Fade out the last image
			$('big_photo_'+(images.length-1)).fade({duration:.5});
			currentImage = 0;
			
			if (isauto)
			{
				// Set timeout to next image
				playing = true;
				timeout = setTimeout(showNextImage,delay);
			}
		}	
		// Highlight current thumb
		if ($('thumb'+currentImage)) $('thumb'+currentImage).addClassName('active');
	}
	else
	{
		$('big_photo_0').appear({duration:.5});
	}
}

function showPreviousImage()
{
	// Stop autoplaying
	playing = false;
	clearTimeout(timeout);

	if (images.length>1)
	{
	
		if (currentImage > -1)
		{
			// Remove highlight from current thumb
			if($('thumb'+currentImage)) $('thumb'+currentImage).removeClassName('active');
		}
		var nextImageIndex = currentImage - 1;
		
		if (nextImageIndex > -1)
		{
			// Show previous image
			$('big_photo_'+nextImageIndex).show();
			
			// Fade out current image
			$('big_photo_'+currentImage).fade({duration:.5});
		
			// Set currentimage index
			currentImage = nextImageIndex;
		}
		else
		{
			// It's the first image, so hide all the images but the last
			$('big_photo_'+(images.length-1)).appear({duration:.5});
			var timeout = setTimeout(function(){
				for (var i=0; i<images.length-2; i++)
				{
					$('big_photo_'+i).style.display = 'none';
				}
			},500);
			
			currentImage = images.length-1;
		}	
		// Highlight current thumb
		if ($('thumb'+currentImage)) $('thumb'+currentImage).addClassName('active');
	}
	else
	{
		$('big_photo_0').appear({duration:.5});
	}
	// Set timeout to restart animation
	timeout = setTimeout(function(){ playing=true; showNextImage() },restartdelay);
}

function showImage(no)
{
	// Stop autoplaying
	playing = false;
	clearTimeout(timeout);
	
	if (currentImage > -1)
	{
		// Remove highlight from current thumb
		if ($('thumb'+currentImage)) $('thumb'+currentImage).removeClassName('active');
	}
	
	var obj = objImages['image'+no];
	if (currentImage == obj.index)
	{
		// It's already visible
		return;
	}
	else if (currentImage < obj.index)
	{
		// Next photo is over
		$('big_photo_'+obj.index).appear({duration:.5});
		currentImage = obj.index;
	}
	else
	{
		// Next photo is under
		$('big_photo_'+obj.index).setStyle({'display':''});
		// Hide all between
		var nextImageIndex = obj.index+1;
		for (var i=nextImageIndex; i<currentImage; i++)
		{
			$('big_photo_'+i).setStyle({'display':'none'});
		}
		// Hide current photo
		$('big_photo_'+currentImage).fade({duration:.5});
		currentImage = obj.index;
	}
	// Highlight current thumb
	if ($('thumb'+currentImage)) $('thumb'+currentImage).addClassName('active');

	// Set timeout to restart animation
	timeout = setTimeout(function(){ playing=true; showNextImage() },restartdelay);
}
