var		entriesPerPhoto;
var		entriesPerGallery;

var		galleryIndex;
var		photoIndex;
var		galleryCount;
var		totalPhotoCount;

var		nextImage = new Image ();

var		slideshow;
var		prevButton;
var		nextButton;
var		caption;
var		photoNum;

var		prevEnabled;
var		nextEnabled;

var		galleries;
var		startIndex;
var		photoCount;


function initSlideshow (array, cntPerPhoto, cntPerGallery)
	{
	// initialize gallery variables
	galleries			= array;
	entriesPerPhoto		= cntPerPhoto;
	entriesPerGallery	= cntPerGallery;
	galleryCount		= galleries.length / entriesPerGallery;
	galleryIndex		= 0;

	// initialize state of previous and next buttons
	prevEnabled			= true;
	nextEnabled			= true;

	// compute index of first photo in each gallery and total number of photos
	startIndex			= new Array (galleryCount);
	photoCount			= new Array (galleryCount);
	photoIndex			= 0;
	totalPhotoCount		= 0;

	for (var i = 0; i < galleryCount; i++)
		{
		startIndex[i] = totalPhotoCount;
		photoCount[i] = galleries[i * entriesPerGallery].length / entriesPerPhoto;
		totalPhotoCount += photoCount[i];
		}

	// find objects in DOM
	if (document.getElementById)
		{
		slideshow	= document.getElementById('slideshow');
		prevButton	= document.getElementById('prevPhotoButton');
		nextButton	= document.getElementById('nextPhotoButton');
		caption		= document.getElementById('captionId');
		date		= document.getElementById('dateId');
		filename	= document.getElementById('filenameId');
		photoNum	= document.getElementById('photoNumId');
		}
	else if (document.all)
		{
		slideshow	= document.all['slideshow'];
		prevButton	= document.all['prevPhotoButton'];
		nextButton	= document.all['nextPhotoButton'];
		caption		= document.all['captionId'];
		date		= document.all['dateId'];
		filename	= document.all['filenameId'];
		photoNum	= document.all['photoNumId'];
		}
	else if (document.layers)
		{
		slideshow	= document.layers['slideshow'];
		prevButton	= document.layers['prevPhotoButton'];
		nextButton	= document.layers['nextPhotoButton'];
		caption		= document.layers['captionId'];
		date		= document.layers['dateId'];
		filename	= document.layers['filenameId'];
		photoNum	= document.layers['photoNumId'];
		}

	// start with first photo in first gallery
	JumpTo(0);
	}

function Next ()
	{
	if (photoIndex < photoCount[galleryIndex] - 1)
		photoIndex++;							// next photo
	else if (galleryIndex < galleryCount - 1)
		{
		galleryIndex++;							// next gallery
		photoIndex = 0;
		}
	else
		return;									// at end

	slideshow.src = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto];
	Update ();
	PreLoad ();
	}

function Prev ()
	{
	if (photoIndex > 0)
		photoIndex--;								// previous photo
	else if (galleryIndex > 0)
		{
		galleryIndex--;								// previous gallery
		photoIndex = photoCount[galleryIndex] - 1;	// last photo in gallery
		}
	else
		return;										// at beginning

	slideshow.src = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto];
	Update ();
	}

function PreLoad ()
	{
	var		gallery;
	var		photo;

	if (photoIndex < photoCount[galleryIndex] - 1)
		{
		gallery = galleryIndex;						// next gallery
		photo = photoIndex + 1;
		}
	else if (galleryIndex < galleryCount - 1)
		{
		gallery = galleryIndex + 1;					// next gallery
		photo = 0;
		}
	else
		return;										// at end ==> no pre-load

	nextImage.src = galleries[gallery * entriesPerGallery][photo * entriesPerPhoto];
	}

function JumpTo (galleryNum)
	{
	galleryIndex = galleryNum;
	photoIndex = 0;
	slideshow.src = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto];
	Update ();
	PreLoad ();
	}

function Update ()
	{
	var			str;
	var			text;

	if (caption)
		{
		if (entriesPerGallery > 1)					// caption by gallery
			str = galleries[galleryIndex * entriesPerGallery + 1];
		else if (entriesPerPhoto > 1)				// caption by photo
			str = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto + 1];
		else
			str = "";

		text = caption.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

	if (date)
		{
		str = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto + 2];
		text = date.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

	if (filename)
		{
		str = galleries[galleryIndex * entriesPerGallery][photoIndex * entriesPerPhoto];
		i = str.lastIndexOf ('/');
		str = str.substring(i + 1, str.length);
		text = filename.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

	if (photoNum)
		{
		photoNumber = startIndex[galleryIndex] + photoIndex + 1;
		str = photoNumber + " of " + totalPhotoCount;
		text = photoNum.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

	if (prevButton)
		{
		if (photoIndex == 0 && galleryIndex == 0)		// at beginning ==> disable previous button
			{
			if (prevEnabled)
				{
				prevButton.style.opacity = 0.4;
				prevEnabled = false;
				}
			}
		else											// not at beginning ==> enable previous button
			{
			if (!prevEnabled)
				{
				prevButton.style.opacity = 1.0;
				prevEnabled = true;
				}
			}
		}

	if (nextButton)
		{
		if ((photoIndex == photoCount[galleryIndex] - 1)
			&& (galleryIndex == galleryCount - 1))		// at end ==> disable next button
			{
			if (nextEnabled)
				{
				nextButton.style.opacity = 0.4;
				nextEnabled = false;
				}
			}
		else											// not at end ==> enable next button
			{
			if (!nextEnabled)
				{
				nextButton.style.opacity = 1.0;
				nextEnabled = true;
				}
			}
		}
	}

