//###########################################

/*
	Based on:
	"Slideshow v2 Wizard"
	http://www.ricocheting.com/js/slide2.html
*/

// transition code variables

var dotrans = 1; 				// if value = 1 then there are transitions played
var transtype = 'blendTrans';	// only 'blendTrans' supported
var mouseoverstop = 1;          // if value = 1 then slideshow is stopped while mouse over image
var playing;                    // timeout ID
var playspeed;			        // the playspeed determines the delay between images
try
{
	playspeed = SlideDelay;		// from configuration file
}
catch(ex)
{
	alert("Missing configuration file!");
	playspeed = 5000;	// default
}

// Preloads images and starts slideshow running.
function do_startup()
{
	//preload images into browser
	preloadImages();

	//set transitions
	SetTrans();

	//autoplay
	PlaySlide();
}

// Sets the transition to apply to the images.
function SetTrans()
{
	if (dotrans != 0)
	{
		document.images.Img1.style.filter="blendTrans(duration=1,transition=1)";
	}
}

// Set the next image (slide) to show.
function SetSlide() 
{
	the_image = GetNextImage()          // set index of next image to use (in 'the_image')

	// switch the image

	if (navigator.appName == "Microsoft Internet Explorer")
	{
		if (document.all && (dotrans == 1))
		{
			eval('document.images.Img1.filters.' + transtype + '.Apply()');
		}

		AutoImage(the_image);  // select next image

		if (document.all && (dotrans == 1))
		{
			eval('document.images.Img1.filters.' + transtype + '.Play()');
		}
	}
	else	// non-I.E. browser
	{
		if (dotrans == 1)
		{
			FadeInImage("Img1", the_image, "Img1Bgnd");
		}
		else
		{
			AutoImage(the_image);  // select next image
		}
	}
}

// Run the slideshow.
function PlaySlide() 
{
	// Note: The use of clearTimeout() prevents Firefox from double-firing the timeout event!???
	playing = setTimeout('clearTimeout(playing); SetSlide(); PlaySlide();', playspeed);
}

// Show specified slide image, enabling a click event for it.
//  - img = index of image to show (0-based)
function AutoImage(img)
{
	var image = document.getElementById("Img1");
	var bkgimage = document.getElementById("Img1Bgnd");
    
	image.src = images_path[img];       // change the main/foreground image
    bkgimage.src = images_path[img];    // put image in background also
	the_image = img;                    // save index to selected image
    setImageLink(img, image);           // set click event image link
}

// Set click event image link, and mouseover and mouseout events, and title for specified image.
//  - img = index of image to set link for (0-based)
//  - id = element ID of image to set link for
function setImageLink(img, id)
{
    try
    {
        if (images_link[img] != "")
        {
            try
            {
                id.onclick = function() { gotoURL(images_link[img]) };
            }
            catch(er)
            { ; }
        }
        if (images_desc[img] != "")
        {
            try
            {
                id.title = images_desc[img];
            }
            catch(er)
            { ; }
       }
    }
    catch(er)
    { ; }
    
    if (mouseoverstop == 1)
    {
        id.onmouseover = function() { clearTimeout(playing); };     // stop timeout
        id.onmouseout = function() { PlaySlide(); };
    }
}

// Goto specified URL.
//  - img = name/path of image to retrieve/return
function gotoURL(theURL)
{
   location = theURL;
}

//###########################################

/*
	Non-IE browsers - based on:
	"How to implement an automatic slide show using JavaScript"
	http://www.cryer.co.uk/resources/javascript/script12slideshow.htm
*/

var FadeDurationMS = 1200;
var save_foreground;
var save_background;

// Set the specified opacity for the specified object.
//  - object = the object to set the opacity for
//  - opacityPct = the percent of opacity to set (0 to 100)
function SetOpacity(object, opacityPct)
{
	// Old Mozilla and Firefox
	object.style.MozOpacity = opacityPct / 100;
	// Everything else.
	object.style.opacity = opacityPct / 100;
}

// Gradually change the opacity of the specified element for the specified duration.
//  - id = element ID of image to fade
//  - msDuration = duration (in ms) of fade transition
//  - msStart = time of start of fade
//  - fromO = opacity at start of fade
//  - toO = opacity at end of fade
function ChangeOpacity(id, msDuration, msStart, fromO, toO)
{
	var element = document.getElementById(id);
	var msNow = (new Date()).getTime();
	var opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;

	if (opacity >= 100)
	{
		SetOpacity(element, 100);               // fully show image
		window.clearTimeout(element.timer);     // stop timeout
		element.timer = undefined;
	}
	else if (opacity <= 0)
	{
		SetOpacity(element, 0);                 // hide image
		window.clearTimeout(element.timer);     // stop timeout
		element.timer = undefined;
		save_foreground.src = save_background.src;  // put old image in foreground
		SetOpacity(save_foreground, 100);           // fully show image
	}
	else 
	{
		SetOpacity(element, opacity);               // fully show image
		element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + 
														msStart + "," + fromO + "," + toO + ")", 10);
	}
}

// Fades out old image while fading in new image.
//  - foregroundID = element name of foreground image (for old image)
//  - newImg = index of the new image to fade to (0-based)
//  - backgroundID = element name of background image (for new image)
function FadeInImage(foregroundID, newImg, backgroundID)
{
	var foreground = document.getElementById(foregroundID);
    var newImage = images_path[newImg];

	if (foreground.timer)
	{
		window.clearTimeout(foreground.timer);
	}
	if (backgroundID)
	{
		var background = document.getElementById(backgroundID);
		if (background)
		{
			if (background.src)
			{
				foreground.src = background.src;	// put old image in foreground
				SetOpacity(foreground, 100);
			}
			background.src = newImage;				// put new image in background
			background.style.backgroundImage = 'url(' + newImage + ')';
			background.style.backgroundRepeat = 'no-repeat';
			var startMS = (new Date()).getTime();
			foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "'," + 
																FadeDurationMS + "," + startMS + ",100,0)", 10);
			save_foreground = foreground;
			save_background = background;
		}
	} 
	else
	{
		foreground.src = newImage;
	}
    setImageLink(newImg, foreground);               // set click event image link
}

//###########################################
