// set a value for the IE fade filter (in seconds)
var filterDuration = 0.5;

// slideshow speed (in milliseconds)
var speed = 4000;

// thumb classes
var thumbClassOn = 'slideThumbOn';
var thumbClassOff = 'slideThumbOff';

// set-up some counters
var i = 0;
var j = 0;
var start_flag = 0;
var p = mainPic.length;

// set-up the pre-load array
var mainPicPre = new Array();

// populate the pre-load array
for (i=0;i<p;i++)
{
	mainPicPre[i] = new Image();
	mainPicPre[i].src = mainPic[i];
}

// function to advance the slide show
function nextSlide(){

	// advance the counter
	if (j==(p-1))
	{
		j = 0;
	}
	else
	{
		j++;
	}

	// change the slide
	changeSlide(j);
	
	// highlight the currently selected thumb
	highlightThumb(j);
}

// function to reverse the slide show
function lastSlide(){
	
	// rewind the counter
	if (j==0)
	{
		j = (p-1);
	}
	else
	{
		j--;
	}

	// change the slide
	changeSlide(j);
	
	// highlight the currently selected thumb
	highlightThumb(j);
}

// function to show a particular slide show
function showSlide(k)
{
	// set j to equal k (globally) as we have skipped to a different photo
	j = k;

	// change the slide
	changeSlide(j);

	// highlight the currently selected thumb
	highlightThumb(j);
}

// function to highlight the current thumbnail
function highlightThumb(j){

	// check that we can access the image by its element id
	if (document.getElementById)
	{
		for (i=0;i<p;i++)
		{
			thumbToChange = document.getElementById('slideThumb'+i);
			
			// highlight current thumb
			if (j==i)
			{
				thumbToChange.className = thumbClassOn;
			}
			// remove any highlighting from other thumbs
			else
			{
				thumbToChange.className = thumbClassOff;
			}
		}
	}
}

// function to change the image/slide
function changeSlide(j){

	// check that we can access the image by its element id
	if (document.getElementById)
	{
		mainPicToChange = document.getElementById('picChange');
		textToChange = document.getElementById('textChange');
		
		// set up the filters
		try{
			// standard blend transition
			mainPicToChange.style.filter="blendTrans(duration=filterDuration)";
			mainPicToChange.filters.blendTrans.Apply();
		}
		catch(err){}

		// swap the images
		mainPicToChange.src = mainPicPre[j].src;
		mainPicToChange.alt = altText[j];
		textToChange.innerHTML = labelText[j];

		// again check if the image can be filtered
		try{
			// standard blend transition
			mainPicToChange.filters.blendTrans.Play();
		}
		catch(err){}
	}
}

// function to run the slideshow automtically
function runSlideShow()
{
	// this loop kicks in when we first run the function
	// it simply delays the first slide - otherwise it would switch immediately
	if (start_flag == 0)
	{
		j = 1;
		start_flag = 1;
	
		// call the slideshow function
		setTimeout('runSlideShow()', speed);
	}
	// check that the next image in the slideshow have loaded
	else if (mainPicPre[j].complete)
	{

		// change the slide
		changeSlide(j);
		
		// highlight the currently selected thumb
		highlightThumb(j);
		
		// move counters onto next image
		j++;
		if (j>=p) j=0;
		
		// call the slideshow function
		setTimeout('runSlideShow()', speed);
	}
	// images not yet loaded so wait a while
	else
	{
		// call the slideshow function
		setTimeout('runSlideShow()', speed);
	}
}