/*****

Some code from:
----------
Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
---------

Substantially rewritten to allow params, and fail differently for javascript.
Note that while it allows multiple faders, if they're fading at the same rate
we should probably unify to one timer for efficiency.

*****/

var Fader = Class.create();
Fader.prototype = {
	initialize: function(imageContainerTagName)
	{
		var imgCont = $(imageContainerTagName);
		if(!imgCont) return;
		imgCont.Fader = this;
		this.path = imageContainerTagName;
		this.imgs = imgCont.getElementsByTagName("img");
		for(i=1;i<this.imgs.length;i++) this.imgs[i].xOpacity = 0;
		this.imgs[0].style.display = "block";
		this.imgs[0].xOpacity = .99;

		this.pauseLen = imgCont.getAttribute('pauseLen') ? imgCont.getAttribute('pauseLen') : 1000;
		var fadeLen = imgCont.getAttribute('fadeLen') ? imgCont.getAttribute('fadeLen') : 1000;
		// each tick is 50 millis.  fadeLen/50 = numTicks.  inc = 1/numTicks.
		// inc*fadeLen = 50.  inc = 50/fadeLen.
		this.fadeInc = 50/fadeLen;
		this.current = 0;
	
		//setTimeout(this.xfade,this.pauseLen);
		setTimeout("$('"+imageContainerTagName+"').Fader.xfade()", this.pauseLen);
	},

	xfade: function()
	{
		var cOpacity = this.imgs[this.current].xOpacity;
		var nIndex = this.imgs[this.current+1]?this.current+1:0;
		var nOpacity = this.imgs[nIndex].xOpacity;
	
		cOpacity-=this.fadeInc;
		nOpacity+=this.fadeInc;
	
		this.imgs[nIndex].style.display = "block";
		this.imgs[this.current].xOpacity = cOpacity;
		this.imgs[nIndex].xOpacity = nOpacity;
	
		this.setOpacity(this.imgs[this.current]); 
		this.setOpacity(this.imgs[nIndex]);
	
		if(cOpacity<=0) 
		{
			this.imgs[this.current].style.display = "none";
			this.current = nIndex;
			setTimeout("$('"+this.path+"').Fader.xfade()", this.pauseLen);
		} 
		else 
		{
			setTimeout("$('"+this.path+"').Fader.xfade()", 50);
		}
	},
	
	setOpacity: function(obj) 
	{
		if(obj.xOpacity>.99) 
		{
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
};


