/******************************************************************* 
* File    : JSFX_ImageFadeSwap.js  © JavaScript-FX.com
* Created : 2001/08/31 
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com 
* Purpose : To create a more dynamic image swap using opacity
* History 
* Date         Version        Description 
* 2001-08-09	1.0		First version
* 2001-08-31	1.1		Got it working with NS6 - You must use opaque
*					GIF's and use a STYLE attrib in the main
*					HTML Page - Thanks Owl.
* 2001-08-31	1.2		Added different FadIn/FadeOut and converted
*					all vars to JSFX name space.
* 2001-09-01	1.3		Make it so you only need one onMouseOver
*					onMouseOut in the main document.
* 2001-09-09	1.4		Allow you to do a "Swap Other Image" so
*					you can swap the same image with different pictures.
***********************************************************************/ 

/****** User may alter these to change the fade effect ********/
var FadeInStep 	= 50;     // was 20
var FadeOutStep  =  25;   // was 4
/****** Don't alter anything else **************/


/*** Create some global variables ***/
if(!window.JSFX)
	JSFX=new Object();

JSFX.ImageFadeRunning=false;
JSFX.ImageFadeInterval=4;

/*******************************************************************
*
* Function    : imgFadeIn
*
* Description : This function is based on the turn_on() function
*		      of animate2.js (animated rollovers from www.roy.whittle.com).
*		      Each image object is given a state. 
*			OnMouseOver the state is switched depending on the current state.
*			Current state -> Switch to
*			===========================
*			null		->	OFF.
*			OFF		->	FADE_IN
*			FADE_OUT	->	FADE_IN
*			FADE_OUT	->	FADE_OUT_IN (if the new image is different)
*			FADE_IN_OUT->	FADE_IN (if the image is the same)
*****************************************************************/
JSFX.imgFadeIn = function(img, imgSrc)
{
	if(img) 
	{
		if(img.state == null) 
		{
			img.state = "OFF";
			img.index = 0;
			img.next_on    = null;
		}

		if(img.state == "OFF")
		{
			img.src=imgSrc;
			img.currSrc = imgSrc;
			img.state = "FADE_IN";
			JSFX.startFading();
		}
		else if( img.state == "FADE_IN_OUT"
			|| img.state == "FADE_OUT_IN"
			|| img.state == "FADE_OUT")
		{
			if(img.currSrc == imgSrc)
				img.state = "FADE_IN";
			else
			{
				img.next_on = imgSrc;
				img.state="FADE_OUT_IN";
			}
		}
	}
}
/*******************************************************************
*
* Function    : imgFadeOut
*
* Description : This function is based on the turn_off function
*		      of animate2.js (animated rollovers from www.roy.whittle.com).
*		      Each image object is given a state. 
*			OnMouseOut the state is switched depending on the current state.
*			Current state -> Switch to
*			===========================
*			ON		->	FADE_OUT.
*			FADE_IN	->	FADE_IN_OUT.
*			FADE_OUT_IN	->	FADE_IN. (after swapping to the next image)
*****************************************************************/
JSFX.imgFadeOut = function(img) {
  if(img) {
    if(img.state=="ON") {
      img.state="FADE_OUT";
      JSFX.startFading();
    } else if(img.state == "FADE_IN") {
      img.state="FADE_IN_OUT";
    } else if(img.state=="FADE_OUT_IN") {
      img.next_on == null;
      img.state = "FADE_OUT";
    }
  }
}
/*******************************************************************
*
* Function    : startFading
*
* Description : This function is based on the start_animating() function
*	        	of animate2.js (animated rollovers from www.roy.whittle.com).
*			If the timer is not currently running, it is started.
*			Only 1 timer is used for all objects
*****************************************************************/
JSFX.startFading = function()
{
	if(!JSFX.ImageFadeRunning)
		JSFX.ImageFadeAnimation();
}

/*******************************************************************
*
* Function    : ImageFadeAnimation
*
* Description : This function is based on the Animate function
*		    of animate2.js (animated rollovers from www.roy.whittle.com).
*		    Each image object has a state. This function
*		    modifies each object and (possibly) changes its state.
*****************************************************************/
JSFX.ImageFadeAnimation = function()
{
	JSFX.ImageFadeRunning = false;
	for(i=0 ; i<document.images.length ; i++)
	{
		var img = document.images[i];
		if(img.state)
		{
			if(img.state == "FADE_IN")
			{
				if(img.index < 100)
					img.index+=FadeInStep;
				else
					img.index = 100;

				if(img.filters)
					img.filters.alpha.opacity = img.index;
				else
					img.style.MozOpacity = img.index + "%"

				if(img.index == 100)
					img.state="ON";
				else
					JSFX.ImageFadeRunning = true;
			}
			else if(img.state == "FADE_IN_OUT")
			{
				if(img.index < 100)
					img.index+=FadeInStep;
				else
					img.index = 100;

				if(img.filters)
					img.filters.alpha.opacity = img.index;
				else 
					img.style.MozOpacity = img.index + "%";

	
				if(img.index == 100)
					img.state="FADE_OUT";

				JSFX.ImageFadeRunning = true;
			}
			else if(img.state == "FADE_OUT")
			{
				if(img.index > 0)
					img.index-=FadeOutStep;
				else
					img.index = 0;
				if(img.filters)
					img.filters.alpha.opacity = img.index;
				else
					img.style.MozOpacity = img.index + "%";


				if(img.index == 0)
					img.state="OFF";
				else
					JSFX.ImageFadeRunning = true;
			}
			else if(img.state == "FADE_OUT_IN")
			{
				if(img.index > 0)
					img.index-=FadeOutStep;
				else
					img.index = 0;
				if(img.filters)
					img.filters.alpha.opacity = img.index;
				else
					img.style.MozOpacity = img.index + "%";


				if(img.index == 0)
				{
					img.src = img.next_on;
					img.currSrc = img.next_on;
					img.state="FADE_IN";
				}
				JSFX.ImageFadeRunning = true;
			}
		}
	}
	/*** Check to see if we need to animate any more frames. ***/
	if(JSFX.ImageFadeRunning)
		setTimeout("JSFX.ImageFadeAnimation()", JSFX.ImageFadeInterval);
}
/*******************************************************************
*
* Function    : findImg
*
* Description : In Netscape 4 images could be in layers so we might
*		    have to recurse the layers to find the image
*
*****************************************************************/
JSFX.findImg = function(n, d) {
  var img = d.images[n];
  if(!img && d.layers)  
    for(var i=0 ; !img && i<d.layers.length ; i++) 
      img=JSFX.findImg(n,d.layers[i].document);
  return img;
}

/*******************************************************************
*
* Function    : fadeIn /fadeOut
*
* Description : Detects browser that can do opacity and fades the images
*		    For browsers that do not support opacity it just does an image swap.
*		    (I only know about NS4 but maybe IE on a Mac also ?)
*		    For these functions to work you need to name the image
*			e.g. for an image named "home" you need
*			<IMG .... NAME="home">
*		    and you need 2 images
*			home_on.gif
*			home_off.gif
*****************************************************************/
JSFX.fadeIn = function(imgName, imgSrc) {
  if(imgSrc == null) imgSrc=imgName+"_on.gif";
  if(document.layers) {
    var img = JSFX.findImg(imgName, document);
    if(img.offSrc==null) img.offSrc=img.src;
    img.src=imgSrc;
  } else JSFX.imgFadeIn(document.images[imgName], imgSrc);
}
JSFX.fadeOut = function(imgName) {
  if(document.layers) {
    var img = JSFX.findImg(imgName, document);
    if(img.state != "FREEZE") img.src=img.offSrc;
  } else JSFX.imgFadeOut(document.images[imgName]);
}
JSFX.freeze = function(imgPre,imgPost,imgCount) {
  for(JSFX.imgIndex=1;JSFX.imgIndex<=imgCount;JSFX.imgIndex++) {
    imgName=imgPre + JSFX.imgIndex;
    var img;
    if(document.layers) {
      img = JSFX.findImg(imgName, document);
      if(imgName != imgPre + imgPost ) {
        img.src=img.offSrc;
        img.state = "OFF";
      } else {
        img.state = "FREEZE";
      }
    } else {
      if(imgName != imgPre + imgPost ) {
        document.images[imgName].state = "ON";
        JSFX.imgFadeOut(document.images[imgName]);
      } else {
        document.images[imgName].state = "FREEZE";
      }
    }
  }
}

