//[#4]  File Name:    FadeImage.js
//[#4]  Author:       Scott Taylor, Seedash Computing
//[#4]  Date:         1/11/05
//[#4]  Description:  Script to fade between different images using timers
//[#4]
//[#4]  Note: Images must be named imgCycle1, imgCycle2, imgCycle3 etc
//[#4]
//[#4]  Revisions <Date, Author, Description>:
//[#4]         16/11/05    ST    Added c_intFadeStep variable

var c_intMaxImages ;
c_intMaxImages = 2 ;  //[#4] Number of images in cycle (must be named imgCycle1, imgCycle2, imgCycle3 etc)

var c_intFadeTimeout ;
c_intFadeTimeout = 30 ;  //[#4] Time (in ms) between updates of fading; smaller = faster

var c_intFadeStep ;
c_intFadeStep = 2 ;  //[#4] Amount to increase opacity every c_intFadeTimeout milliseconds; usually set to 1 or 2

var c_intChangeTimeout ;
c_intChangeTimeout = 12000 ;  //[#4] Time (in ms) that each image will remain still after fading in; smaller = faster

var c_intStartDelay ;
c_intStartDelay = 2000 ;  //[#4] Time (in ms) that the cycle process will wait before starting; smaller = faster

var imgFadingIn ;  //[#4] Image currently being faded in
var imgBackground ;  //[#4] Image currently in the background (being faded over)
var intFadeAmount ;  //[#4] Amount (0-100) of opacity for image currently being faded in
var intCurrentImage ;  //[#4] Integer (1-xx) representing which image is being faded in (imgCycle1, imgCycle2 etc)

//[#4] Select and initialise the first image
intCurrentImage = 1 ;
intFadeAmount = 0 ;
imgFadingIn = document.images ["imgCycle" + intCurrentImage] ;
imgFadingIn.style.zIndex ++ ;
imgFadingIn.style.visibility = "visible" ;
imgBackground = null ;

//[#4] Function: ChangeImage ()
//[#4] Purpose: Switches to the next image to be faded in over the top of the old one, then calls FadeImage()
function ChangeImage ()
{
	//[#4] If an image exists behind the recently faded one, set it to transparent, hide it, and move it back one level
	if (imgBackground != null)
	{
		SetOpacity (imgBackground, 0) ;
		imgBackground.style.visibility = "hidden" ;
	}
	//[#4] Reset the current fade amount
	intFadeAmount = 0 ;
	//[#4] Store this newly faded image as the new background
	imgBackground = imgFadingIn ;
	//[#4] Put the new background image to the back
	imgBackground.style.zIndex -- ;
	//[#4] Select next image
	intCurrentImage ++ ;
	if (intCurrentImage > c_intMaxImages)  //[#4] Loop back to first image if necessary
		intCurrentImage = 1 ;
	//[#4] Find the new image to fade in
	imgFadingIn = document.images ["imgCycle" + intCurrentImage] ;
	//[#4] Set new image opacity to transparent
	SetOpacity (imgFadingIn, 0) ;
	//[#4] Bring current image to front
	imgFadingIn.style.zIndex ++ ;
	//[#4] Allow the current image to be shown
	imgFadingIn.style.visibility = "visible" ;
	//[#4] Begin fading in new image
	setTimeout ("FadeImage ()", c_intFadeTimeout) ;
}

//[#4] Function: SetOpacity ()
//[#4] Purpose: Sets the opacity of a given image, browser-compatible
//[#4] Arguments: imgImage, image to be set
//[#4]            intAmount, opacity level (0-100)
function SetOpacity (imgImage, intAmount)
{
	//[#4] Check that the amount is valid
	if (intAmount < 0)
		intAmount = 0 ;
	else if (intAmount > 100)
		intAmount = 100 ;
	
	//[#4] Set image opacity for CSS3, old Mozilla, or IE
	if (typeof imgImage.style.opacity == "string")
	{
		//[#4] CSS3 standard
		imgImage.style.opacity = intAmount / 101 ;
	}
	else if (typeof imgImage.style.MozOpacity == "string")
	{
		//[#4] Old Mozilla method
		imgImage.style.opacity = intAmount / 101 ;
	}
	else if ("filters" in imgImage && typeof imgImage.filters.alpha.opacity == "number")
	{
		//[#4] IE method
		imgImage.filters.alpha.opacity = intAmount ;
	}
}

//[#4] Function: FadeImage ()
//[#4] Purpose: Fades current image in by a small amount; will call ChangeImage() when completed
function FadeImage ()
{
	//[#4] Increase opacity of image being faded in
	intFadeAmount += c_intFadeStep ;
	//[#4] Set new opacity of the image
	SetOpacity (imgFadingIn, intFadeAmount) ;
	
	//[#4] Check if the image is fully opaque yet
	if (intFadeAmount < 100)
		setTimeout ("FadeImage ()", c_intFadeTimeout) ;  //[#4] Keep fading
	else
		setTimeout ("ChangeImage ()", c_intChangeTimeout) ;  //[#4] Switch to next image
}

//[#4] Begin fading in the first image (after c_intStartDelay ms)
setTimeout ("FadeImage ()", c_intStartDelay) ;
