var		galleryIndex = 0;
var		photoIndex = 0;
var		nextImage = new Image ();
var		elementCnt = 0;
var		photoCount = 0;

var		compliant = (document.getElementById);
var		ie4 = (!compliant && document.all);
var		ns4 = (!compliant && document.layers);


function init(elements)
	{
	elementCnt = elements;
	for (var i = 0; i < galleries.length; i++)
		photoCount += galleries[i].length / elementCnt;
	}

function Next ()
	{
	if (photoIndex == galleries[galleryIndex].length / elementCnt - 1)
		{
		if (galleryIndex == galleries.length - 1)	// at end
			return;
		else									// next gallery
			{
			galleryIndex++;
			photoIndex = 0;
			}
		}
	else										// next photo
		photoIndex++;

	document.images["slideshow"].src = galleries[galleryIndex][photoIndex * elementCnt];
	UpdateCaption ();
	PreLoad ();
	}

function Prev ()
	{
	if (photoIndex == 0)
		{
		if (galleryIndex == 0)					// at beginning
			return;
		else
			{
			galleryIndex--;						// previous gallery
			photoIndex = galleries[galleryIndex].length / elementCnt - 1;	// last photo
			}
		}
	else										// previous photo
		photoIndex--;

	document.images["slideshow"].src = galleries[galleryIndex][photoIndex * elementCnt];
	UpdateCaption ();
	}

function PreLoad ()
	{
	gallery = galleryIndex;
	photo = photoIndex;

	if (photo == galleries[gallery].length / elementCnt - 1)
		{
		if (galleryIndex == 0)					// at beginning
			return;
		else									// next gallery
			{
			gallery++;
			photo = 0;
			}
		}
	else										// next photo
		photo++;

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

function GoToGallery (galleryNum)
	{
	window.scrollTo (0, 0);
	galleryIndex = galleryNum - 1;
	photoIndex = 0;
	document.images["slideshow"].src = galleries[galleryIndex][photoIndex * elementCnt];
	UpdateCaption ();
	PreLoad ();
	}

function UpdateCaption ()
	{
	var str;
	var caption;
	var date;
	var photoNum;
	var photoNumber;
	var filename;
	var i;

	if (compliant)
		{
		caption = document.getElementById('captionId');
		date = document.getElementById('dateId');
		photoNum = document.getElementById('photoNumId');
		filename = document.getElementById('filenameId');
		}
	else if(ie4)
		{
		caption = document.all['captionId'];
		date = document.all['dateId'];
		photoNum = document.all['photoNumId'];
		filename = document.all['filenameId'];
		}
	else if(ns4)
		{
		caption = document.layers['captionId'];
		date = document.layers['dateId'];
		photoNum = document.layers['photoNumId'];
		filename = document.layers['filenameId'];
		}

	if (caption)
		{
		str = galleries[galleryIndex][photoIndex * elementCnt + 1];
		text = caption.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

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

	if (photoNum)
		{
		photoNumber = photoIndex + 1;	
		for (var i = 0; i < galleryIndex; i++)
			photoNumber += galleries[i].length / elementCnt;
	
		str = photoNumber + " of " + photoCount;
		text = photoNum.childNodes[0];
		if (text.data != str)
			text.data = str;
		}

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