/**
 *  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;
}


var theDivs;
var theFadeTime;
var theDelay;


_bsn.Crossfader = function (altDivs, fadetime, delay )
{	
	nAct = -1;
	divs = altDivs;
	this.aDivs = divs;
	
	
	theDivs = divs;
	theFadeTime = fadetime;
	theDelay = delay;
	
	for (var i=0;i<divs.length;i++)
	{
	//console.log(divs[i]);
		document.getElementById(divs[i]).style.opacity = 0;
		document.getElementById(divs[i]).style.position = "absolute";
		document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
		document.getElementById(divs[i]).style.visibility = "hidden";
	}
	
	this.nDur = fadetime;
	this.nDelay = delay;
		
	this._newfade();
}

_bsn.Crossfader.prototype.indexHTML = function(max, index)
{
	var string = "";

	for(var i = 0; i < max; i++)
	{
		if(i == index || (i == 0 && index == max))
		{
			string += "<span style='padding: 1px; background-color: #ccc'>"+Math.round(i + 1)+"</span>&nbsp";
		}
		else
		{
			string += "<a href='javascript:_bsn.Crossfader.prototype.clickTo("+Math.round(i-1)+");' style='padding: 1px; background-color: #fff'>"+Math.round(i + 1)+"</a>&nbsp";
		}
	}

	return string;
}

_bsn.Crossfader.prototype.clickTo = function(index)
{
	nOldAct = nAct;
	nAct = index;
	this.aDivs = theDivs;
	
if (nID2)
		clearInterval(nID2);
		
	for (var i=0;i<divs.length;i++)
	{
	//console.log(divs[i]);
		document.getElementById(divs[i]).style.opacity = 0;
		document.getElementById(divs[i]).style.position = "absolute";
		document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
		document.getElementById(divs[i]).style.visibility = "hidden";
	}
	
	this.nDur = theFadeTime;
	this.nDelay = theDelay;
		
	this._newfade();
}
var nAct;
var nID1;
var nID2;
var nOldAct;
var divs;
_bsn.Crossfader.prototype._newfade = function()
{
	if (nID1)
		clearInterval(nID1);
	
	nOldAct = nAct;
	nAct++;
	document.getElementById("altNums").innerHTML = this.indexHTML(this.aDivs.length, nAct);
	if (!this.aDivs[nAct])	nAct = 0;
	
	if (nAct == nOldAct)
		return false;
	
	document.getElementById( this.aDivs[nAct] ).style.visibility = "visible";
	
	this.nInt = 50;
	this.nTime = 0;
	
	var p=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( this.aDivs[nAct] ).style.opacity = op;
	document.getElementById( this.aDivs[nAct] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (nOldAct > -1)
	{
		document.getElementById( this.aDivs[nOldAct] ).style.opacity = 1 - op;
		document.getElementById( this.aDivs[nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	
	if (this.nTime == this.nDur)
	{
		clearInterval( nID2 );
		
		if (nOldAct > -1)
			document.getElementById( this.aDivs[nOldAct] ).style.visibility = "hidden";	
		
		var p=this;
		if(playState)	
		{nID1 = setInterval(function() { p._newfade() }, this.nDelay);}
		else
		{this.pause();}
	}
}


var playState = true;

_bsn.Crossfader.prototype.pause = function()
{
	if (nID1)
		clearInterval(nID1);
		
	if (nID2)
		clearInterval(nID2);
		
		playState = false;
}
_bsn.Crossfader.prototype.play = function()
{
	this.clickTo(nAct);
	playState = true;
}


_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}
