
var useBSNns;

if (useBSNns)
{
	if (typeof(bsn) == "undefined")
		bsn = {}
	var _bsn = bsn;
}
else
{
	var _bsn = this;
}





_bsn.Crossfader = function (prefix, fadetime, delay )
{
	this.aDivs = new Array();
	
	var count = 1;  //items must be numbered 1..n
	var elementName;
	do {
		elementName=prefix+String(count);
		if (document.getElementById(elementName)!=null) {
			this.aDivs.push(elementName);
	
			//set initial state of the div to hidden.
			document.getElementById(elementName).style.opacity = 0;
			document.getElementById(elementName).style.position = "absolute";
			document.getElementById(elementName).style.filter = "alpha(opacity=0)";
			document.getElementById(elementName).style.visibility = "hidden";
		}
		count++;
	} while (document.getElementById(elementName)!=null);

	//for (var i=0;i<this.aDivs.length;i++){
	//	alert(this.aDivs[i]);
	//}

	//initialize variables, start the process.

	this.isPaused = false;
	this.isFading = false;
	this.nVisibleDivIndex = -1;
	this.nDur = fadetime;
	this.nDelay = delay;
	this._showNextDiv();
}



//funtion forces the transition to the next div tag.

_bsn.Crossfader.prototype._showNextDiv = function()
{
	if (this.nInterval)
		clearInterval(this.nInterval);
	
	//rotate through the indices, go back to 0 if we're out of range.
	this.nOldAct = this.nVisibleDivIndex;
	this.nVisibleDivIndex++;
	if (!this.aDivs[this.nVisibleDivIndex]) this.nVisibleDivIndex = 0;
	
	//if there's only one image, don't do anything (except the first time - we come in as -1)
	if (this.nVisibleDivIndex == this.nOldAct) return false;
	
	this._showIndex(this.nVisibleDivIndex);
}


//timed function is called from an "interval" aka timer,
// and fades this div when the time is right.

_bsn.Crossfader.prototype._fade = function()
{
	this.isFading = true;
	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[this.nVisibleDivIndex] ).style.opacity = op;
	document.getElementById( this.aDivs[this.nVisibleDivIndex] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (this.nOldAct > -1)
	{
		document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
		document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	
	if (this.nTime == this.nDur)
	{
		clearInterval( this.nID2 );
		
		if (this.nOldAct > -1)
		{
			document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";	
		}
		
		if (!this.isPaused)
		{
		  var p=this;
		  this.nInterval = setInterval(function() { p._showNextDiv() }, this.nDelay);
		}
	
		this.isFading = false;
	}
}

_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}

_bsn.Crossfader.prototype._play = function()
{
	if(!this.isPaused)
	{
		return;
	}
		this.isPaused = false;
		this._showNextDiv();
}

_bsn.Crossfader.prototype._pause = function()
{
		this.isPaused = true;
		clearInterval( this.nInterval );
}

_bsn.Crossfader.prototype._showDiv = function(index)
{
	if (this.isFading || this.nVisibleDivIndex == index)
	{
		return;
	}
		
	this._pause();
	this.nOldAct = this.nVisibleDivIndex;
	this.nVisibleDivIndex = index;
	
	this.isPaused = false;
	this._showIndex(index);
}

_bsn.Crossfader.prototype._showIndex = function(index)
{
	document.getElementById( this.aDivs[index] ).style.visibility = "visible";
	document.getElementById( 'rotpageimg' + (index + 1) ).className = "page_on";
	if (this.nOldAct > -1)
	{
		document.getElementById( 'rotpageimg' + (this.nOldAct + 1) ).className = "page_off";	
	}
	
	this.nInt = 50;
	this.nTime = 0;
	
	var p=this;
	this.nID2 = setInterval(function() { p._fade() }, this.nInt);
}
