var UpdateInterval = 10; // update every UpdateInterval milliseconds
var SlideShows = new Array();

function CreateSlideShow( Identifier, ControlsIdentifier, FadeDuration, AutoDuration )
{
	var ListElement = document.getElementById( Identifier );
	if( ListElement )
	{
		var SlideShow = new Object();
		SlideShow.ListElement = ListElement;
		SlideShow.FadeDuration = FadeDuration;
		SlideShow.AutoDuration = AutoDuration;
		SlideShow.ActiveSlideIndex = 0;
		SlideShow.TransitionInProgress = false;

		SlideShow.Controls = null;
		if( ControlsIdentifier != null && ControlsIdentifier != '' )
		{
			var ControlsElement = document.getElementById( ControlsIdentifier );
			if( ControlsElement )
			{
				var Controls = ControlsElement.getElementsByTagName( 'li' );
				if( Controls )
				{
					SlideShow.Controls = Controls;
				}
			}
		}

		// unhide ul (in case it was hidden)
		ListElement.display = '';

		// hide all but first slide
		var MaxWidth = 0;
		var MaxHeight = 0;
		var Bullets = ListElement.getElementsByTagName('li');
		SlideShow.Slides = new Array();
		var SlideIndex = 0;
		for( var BulletIndex = 0; BulletIndex < Bullets.length; BulletIndex++ )
		{
			if( Bullets[BulletIndex].parentNode == ListElement )
			{
				//Bullets[BulletIndex].style.display = '';
				if( Defined(Bullets[BulletIndex].offsetHeight) )
				{
					MaxHeight = Math.max( MaxHeight, Bullets[BulletIndex].offsetHeight );
				}
				if( Bullets[BulletIndex].style.display!='none' && Defined(Bullets[BulletIndex].offsetWidth) )
				{
					MaxWidth = Math.max( MaxWidth, Bullets[BulletIndex].offsetWidth );
				}
				if( SlideIndex > 0 )
				{
					Bullets[BulletIndex].style.display = 'none';
				}
				Bullets[BulletIndex].style.position = 'absolute';

				SlideShow.Slides[SlideIndex] = Bullets[BulletIndex];
				SlideIndex++;
			}
		}

		//alert( 'MaxWidth=' + MaxWidth + ', MaxHeight=' + MaxHeight );

		// set the width and height of each slide to the maximum slide width and height
		for( SlideIndex = 0; SlideIndex < SlideShow.Slides.length; SlideIndex++ )
		{
			SlideShow.Slides[SlideIndex].style.width = MaxWidth + 'px';
			SlideShow.Slides[SlideIndex].style.height = MaxHeight + 'px';
		}

		SlideShow.PlaceHolder = document.createElement( 'li' );
		SlideShow.PlaceHolder.style.width = MaxWidth + 'px';
		SlideShow.PlaceHolder.style.height = MaxHeight + 'px';
		SlideShow.ListElement.appendChild( SlideShow.PlaceHolder );

		SlideShows[Identifier] = SlideShow;

		if( AutoDuration && AutoDuration > 0 )
		{
			window.setTimeout( 'AutoEvent("' + Identifier + '")', AutoDuration );
		}
	}
	else
	{
		alert( 'CreateSlideShow( "' + Identifier + '", ' + FadeDuration + ' ): List Element "' + Identifier + '" not found.' );
	}
}

function AutoEvent( Identifier )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow && SlideShow.AutoDuration && SlideShow.AutoDuration > 0 && SlideShow.Slides.length > 1 )
	{
		if( !SlideShow.TransitionInProgress )
		{
			var NewSlideIndex = (SlideShow.ActiveSlideIndex + 1) % SlideShow.Slides.length;

			FadeSlide( Identifier, NewSlideIndex );
		}

		// auto again later
		window.setTimeout( 'AutoEvent("' + Identifier + '")', SlideShow.AutoDuration );
	}
}

function SetSlide( Identifier, NewSlideIndex )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow )
	{
		if( SlideShow.AutoDuration )
		{
			SlideShow.AutoDuration = 0;
		}

		if( NewSlideIndex >= 0 && NewSlideIndex < SlideShow.Slides.length )
		{
			if( NewSlideIndex != SlideShow.ActiveSlideIndex )
			{
				FadeSlide( Identifier, NewSlideIndex );
			}
		}
		else
		{
			//alert( 'SetSlide( "' + Identifier + '", ' + NewSlideIndex + ' ): Invalid slide number ' + NewSlideIndex + ' specified.' );
			alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
		}
	}
	else
	{
		//alert( 'SetSlide( "' + Identifier + '", ' + NewSlideIndex + ' ): Slide Show "' + Identifier + '" not found.' );
		alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
	}
}

function NextSlide( Identifier, Wrap )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow )
	{
		if( SlideShow.AutoDuration )
		{
			SlideShow.AutoDuration = 0;
		}

		if( SlideShow.ActiveSlideIndex < SlideShow.Slides.length-1 || (Wrap && SlideShow.Slides.length > 1) )
		{
			var NewSlideIndex = (SlideShow.ActiveSlideIndex + 1) % SlideShow.Slides.length;

			FadeSlide( Identifier, NewSlideIndex );
		}
	}
	else
	{
		//alert( 'NextSlide( "' + Identifier + '", ' + NewSlideIndex + ' ): Slide Show "' + Identifier + '" not found.' );
		alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
	}
}

function PreviousSlide( Identifier, Wrap )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow )
	{
		if( SlideShow.AutoDuration )
		{
			SlideShow.AutoDuration = 0;
		}

		if( SlideShow.ActiveSlideIndex > 0 || (Wrap && SlideShow.Slides.length > 1) )
		{
			var NewSlideIndex = (SlideShow.ActiveSlideIndex + SlideShow.Slides.length - 1) % SlideShow.Slides.length;

			FadeSlide( Identifier, NewSlideIndex );
		}
	}
	else
	{
		//alert( 'PreviousSlide( "' + Identifier + '", ' + NewSlideIndex + ' ): Slide Show "' + Identifier + '" not found.' );
		alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
	}
}

function Defined( Variable )
{
	if( typeof( Variable ) != "undefined" )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function FadeSlide( Identifier, NewSlideIndex )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow )
	{
		if( !SlideShow.TransitionInProgress )
		{
			// move the new slide to the top of the bullet list, so it will show up on top
			SlideShow.NextSlide = SlideShow.ListElement.removeChild( SlideShow.Slides[NewSlideIndex] );

			// insert "NextSlide" at top of listjust before first slide
			var Bullets = SlideShow.ListElement.getElementsByTagName('li');
			SlideShow.Slides[NewSlideIndex] = SlideShow.ListElement.insertBefore( SlideShow.NextSlide, SlideShow.PlaceHolder );

			// start fading to the next slide
			var StartTime = new Date();
			FadeSlideEvent( Identifier, NewSlideIndex, StartTime.getTime() );
		}
	}
	else
	{
		//alert( 'Slide Show "' + Identifier + '" not found.' );
		alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
	}
}

function FadeSlideEvent( Identifier, NewSlideIndex, StartTime )
{
	var SlideShow = SlideShows[Identifier];
	if( SlideShow )
	{
		var NextSlide = SlideShow.NextSlide;

		var CurrentTime = new Date();
		CurrentTime = CurrentTime.getTime();

		var Level = (CurrentTime - StartTime) / SlideShow.FadeDuration;
		if( Level > 1 ){ Level = 1; }
		else { Level = Math.round( Level * 100 ) / 100; }

		var OpacityWasSet = true;

		// CSS 3 (Mozilla 1.7.2+)
		if( Defined( NextSlide.style.opacity ) )
		{
			NextSlide.style.opacity = Level;
		}
		// Mozilla < 1.7.2
		else if( Defined( NextSlide.style.MozOpacity ) )
		{
			NextSlide.style.MozOpacity = Level;
		}
		// IE 5.5+
		else if( Defined( NextSlide.style.filter ) )
		{
			NextSlide.style.filter = 'Alpha(Opacity=' + (Level * 100) + ')';
		}
		// Konqueror & Safari
		else if( Defined( NextSlide.style.KhtmlOpacity ) )
		{
			NextSlide.style.KhtmlOpacity = Level;
		}
		// No opacity support
		else
		{
			OpacityWasSet = false;
		}

		NextSlide.style.display = '';

		if( !OpacityWasSet || CurrentTime >= StartTime + SlideShow.FadeDuration )
		{
			SlideShow.Slides[SlideShow.ActiveSlideIndex].style.display = 'none';

			// update controls
			if( SlideShow.Controls != null )
			{
				// if there is previous/next navigation
				if( SlideShow.Controls.length > SlideShow.Slides.length )
				{
					SlideShow.Controls[SlideShow.ActiveSlideIndex+1].className = '';
					SlideShow.Controls[NewSlideIndex+1].className = 'Active';
				}
				// if there are just slide buttons
				else
				{
					SlideShow.Controls[SlideShow.ActiveSlideIndex].className = '';
					SlideShow.Controls[NewSlideIndex].className = 'Active';							
				}
			}

			SlideShow.ActiveSlideIndex = NewSlideIndex;
			SlideShow.TransitionInProgress = false;
		}
		else
		{
			SlideShow.TransitionInProgress = true;
			// update again in a few milliseconds
			window.setTimeout( 'FadeSlideEvent("' + Identifier + '",' + NewSlideIndex + ',' + StartTime + ')', UpdateInterval );
		}
	}
	else
	{
		//alert( 'Slide Show "' + Identifier + '" not found.' );
		alert( 'Slide show still loading.  Please wait for web page to completely finish loading and try again.' );
	}
}
