/**
 *  author:		Timothy Groves - http://www.brandspankingnew.net
 *	version:	1.3 - 2006-11-02
 *				1.2 - 2006-11-01
 *				1.1 - 2006-09-29
 *				1.0 - 2006-09-25
 *
 *	requires:	nothing
 *
 */

var useBSNns;

if (useBSNns)
{
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}
else
{
	var _bsn = this;
}




_bsn.Crossfader = function (divs, fadetime, delay, cf_width)
{
	this.nAct = 0;
	this.aDivs = divs;
	if (document.getElementById('cf_wrapper')) {
		document.getElementById('cf_wrapper').style.overflow = "visible";
		/*document.getElementById('cf_wrapper').style.margin = "0 -6px 0 6px";*/
	}

	for (var i=1;i<divs;i++)
	{
		document.getElementById("cf"+i).style.opacity = document.getElementById("cf"+i).style.MozOpacity = 0;
		document.getElementById("cf"+i).style.filter = "alpha(opacity=0)";
		document.getElementById("cf"+i).style.visibility = "hidden";
		document.getElementById("cf"+i).style.marginRight = -cf_width+"px";
		document.getElementById("cf"+i).style.cssFloat = document.getElementById("cf"+i).style.styleFloat = "left";
	}

	document.getElementById("cf"+i).style.opacity = document.getElementById("cf"+i).style.MozOpacity = 0;
	document.getElementById("cf"+i).style.filter = "alpha(opacity=0)";
	document.getElementById("cf"+i).style.visibility = "hidden";
	document.getElementById("cf"+i).style.cssFloat = document.getElementById("cf"+i).style.styleFloat = "left";

	this.nDur = fadetime;
	this.nDelay = delay;

	this._newfade();
}


_bsn.Crossfader.prototype._newfade = function()
{
	if (this.nID1)
		clearInterval(this.nID1);

	this.nOldAct = this.nAct;
	this.nAct++;
	if (this.nAct > this.aDivs)	this.nAct = 1;

	if (this.nAct == this.nOldAct)
		return false;

	document.getElementById("cf"+this.nAct).style.visibility = "visible";


	this.nInt = 50;
	this.nTime = 0;

	var p=this;
	this.nID2 = setInterval(function() { p._fade() }, this.nInt);
}


_bsn.Crossfader.prototype._fade = function()
{
	this.nTime += this.nInt;

	var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
	var op = ieop / 100;
	document.getElementById("cf"+this.nAct).style.opacity = document.getElementById("cf"+this.nAct).style.MozOpacity = op;
	document.getElementById("cf"+this.nAct).style.filter = "alpha(opacity="+ieop+")";

	if (this.nOldAct > 0)
	{
		document.getElementById("cf"+this.nOldAct).style.opacity = document.getElementById("cf"+this.nOldAct).style.MozOpacity = 1 - op;
		document.getElementById("cf"+this.nOldAct).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}

	if (this.nTime == this.nDur)
	{
		clearInterval( this.nID2 );

		if (this.nOldAct > 0) {
			document.getElementById("cf"+this.nOldAct).style.visibility = "hidden";
		}

		var p=this;
		this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
	}
}



_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}

