
var ImagesToCache = new Array();	// an array of all the A elements in the gallery_thumbs element.  
									// This is used to get the paths, description, and thumbnails of images
var ImageLoading = 0;				// the image currently being downloaded
var ImageObject = null;				// the image object used to do the asynchronous downloading
var CurrentImage = null;
var NEXT = 1;
var PREVIOUS = -1;

var TimerID = 0;
var SlideshowInterval = 5;
var SlideshowOn = false;

function SetSlideshowInterval(dropdown)
{
	SlideshowInterval = dropdown.options[dropdown.selectedIndex].text;
}

function StopSlideshowTimer()
{
	if (TimerID != 0)
	{
		clearTimeout(TimerID);
		TimerID = 0;
		SlideshowOn = false;
		document.getElementById('gallery_slideshow_startstop').innerHTML = 'Start Slideshow';
	}
}

function StartSlideshowTimer()
{
	if (!SlideshowOn)
	{
		SlideshowOn = true;
		TimerID = setTimeout("ShowSlide(NEXT)", SlideshowInterval * 1000);
		document.getElementById('gallery_slideshow_startstop').innerHTML = 'Stop Slideshow';
	}
}
function StartStopSlideshowTimer()
{
	if (TimerID != 0)
	{
		StopSlideshowTimer();
	}
	else
	{
		StartSlideshowTimer();
	}
}

function SelectImage(img)
{
	CurrentImage = img;
	document.getElementById('gallery_img').src					=	CurrentImage.href;
	document.getElementById('gallery_comment').firstChild.nodeValue =  CurrentImage.childNodes[0].alt;
	document.getElementById('gallery_thumbs').style.display		=	'none';
	document.getElementById('gallery_output').style.display		=	'block';
	document.getElementById('gallery_links').style.display		=	'block';
	document.getElementById('gallery_slideshow').style.display	=	'block';
	return false;
}

function UnselectImage()
{
	StopSlideshowTimer();
	document.getElementById('gallery_thumbs').style.display		=	'block';
	document.getElementById('gallery_output').style.display		=	'none';
	document.getElementById('gallery_links').style.display		=	'none';
	document.getElementById('gallery_slideshow').style.display	=	'none';
	return false;
}

// finds the next/previous sibling ELEMENT, skips #text nodes.
function NextNode(Node, Direction)
{
	var parent = Node.parentNode;
	do
	{
		Node = Direction == NEXT ? Node.nextSibling : Node.previousSibling;
		if (!Node)
		{
			Node = Direction == NEXT ? parent.firstChild : parent.lastChild;
		}
	}
	while (Node.nodeType != 1)
	return Node;
}

// called from the Next and Previous links, and from the timer doing the slideshow
function ShowSlide(Direction) 
{
	SelectImage(NextNode(CurrentImage, Direction));
	if (SlideshowOn)
	{
   		TimerID = setTimeout("ShowSlide(NEXT)", SlideshowInterval * 1000);
	}
	return false;
}

// set the opacity of the element, if the UA supports it
function set_opacity(element, opacity)
{
	element.style.opacity = opacity; 									// the standard way
	element.style.MozOpacity = opacity;									// the old Gecko way (before they supported opacity)
	element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';	// the IE way
}

// simple bind function for the onclick in AttachEvents
function MakeClickHandler(element)
{
	return function() { return SelectImage(element); };
}

function SetStatus(msg)
{
	// mostly for debugging purposes - turn off when we go release
	//document.getElementById('gallery_status').innerHTML = msg;
}

function CheckDownloadStatus()
{
	if (ImageObject.complete) 
	{
		//set_opacity(ImagesToCache[ImageLoading].firstChild, 1.0);
		++ImageLoading;
		SetStatus("Cached " + ImageLoading + " images from a total of " + ImagesToCache.length);
		CacheNextImage();
	}
	else 
	{
		setTimeout("CheckDownloadStatus()", 100);
	}
	return true;
}

function CacheNextImage() 
{
	if (ImageLoading < ImagesToCache.length)
	{
		ImageObject = new Image();
		ImageObject.src = ImagesToCache[ImageLoading].href;
		CheckDownloadStatus();
	}
	else
	{
		SetStatus("All images loaded");
	}
}

// this stuff does the set up, and caching of images.  Nicely it carries on caching 
// images even when you're viewing a different image.
domOnLoad(function()
{
	var thumbs = document.getElementById('gallery_thumbs').getElementsByTagName("A");
	if (thumbs.length == 0)
    { 
        thumbs = document.getElementById('gallery_thumbs').getElementsByTagName("a");
	}
	for (var i=0; i < thumbs.length; ++i) 
	{
		thumbs[i].onclick = MakeClickHandler(thumbs[i]);
		thumbs[i].title = thumbs[i].childNodes[0].alt;
		//set_opacity(thumbs[i].childNodes[0], 0.5);
		ImagesToCache[ImagesToCache.length] = thumbs[i];
	}
	CacheNextImage();
});