/**
 *  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
 *
 */

function stopSlideshowAt(id) //IE hack
	{
	
	id=id.split("_");
	if (id.length!=3) {return false;}
	stop=eval(id[0]+".Crossfader._stopAt("+id[2]+")");
	}

function togglePlaySlideshow(id)
	{
	id=id.split("_");

	if (id.length=2 || id.length==3) 
		{
		stop=eval(id[0]+".Crossfader._togglePlay()");
		}
	else {return false;}
	}
		
var useBSNns;

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

var slideShow = {
	//new Crossfader( new Array('slide1','slide2','slide3','slide4','slide5'), 1500, 5000,"slideNavigation");
	// method to create global instance:
	create: function ( instanceName,divs, fadetime, delay,navdiv,prevId,nextId,onChange )
	{
		divArr=new Array();
		for(dc=0;dc<divs.length;dc++) {divArr.push("'"+divs[dc]+"'");}
		divStr="new Array("+divArr.join(",")+")";		
		eval( instanceName + " = new slideShow.object('" + instanceName + "',"+divStr+","+fadetime+","+delay+",'"+navdiv+"','"+prevId+"','"+nextId+"',onChange)" );
		//eval( instanceName + ".setOnChange('x');");
	},

	// this is the actual created global object
	object: function ( instanceName,divs, fadetime, delay,navdiv,prevId,nextId,onChange) 
	{
	
		this.Crossfader = new Crossfader( instanceName,divs, fadetime, delay,navdiv,prevId,nextId);
		this.Crossfader.setOnChange(onChange);
	}	
}





_bsn.Crossfader = function (instanceName,divs, fadetime, delay,navdiv,prevId,nextId )
{	
	this.nAct = -1;
	this.counter=0;
	this.aDivs = divs;
	this.instanceName = instanceName;
	this.playing=true;

	for (var i=0;i<divs.length;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;
	
	if (document.getElementById(navdiv)) {this.navdiv=document.getElementById(navdiv);this._createNav();}
	if (document.getElementById(prevId) && document.getElementById(nextId)) 
		{
		this.prevId=document.getElementById(prevId);
		this.nextId=document.getElementById(nextId);
		this.nextId.childNodes[0].onclick = function(){stopSlideshowAt(this.getAttribute("rel"));}; //ivm IE workaround
		this.prevId.childNodes[0].onclick = function(){stopSlideshowAt(this.getAttribute("rel"));}; //ivm IE workaround
		}

	
	
	//this.callBackFunction=function(x){document.title=String(x);}
	
	this._newfade();
}

_bsn.Crossfader.prototype.setOnChange = function(x) 
	{
	this.onChange=x;
	}

_bsn.Crossfader.prototype.toString2 = function()
	{
	return this.instanceName;
	}
	
_bsn.Crossfader.prototype._createNav = function()
	{
	for(co=0;co<this.aDivs.length;co++)
		{
		b=document.createElement("A");
		b.setAttribute("href","javascript:void(0);");

		b.setAttribute("id",String(this.toString2()+"_nav_"+String(co)));
		b.onclick = function(){stopSlideshowAt(this.getAttribute("id"));}; //ivm IE workaround
		b.onmouseout = function(){togglePlaySlideshow(this.getAttribute("id"));}; //ivm IE workaround
		
		txt=document.createTextNode("\u00a0");
		b.appendChild(txt);
		this.navdiv.appendChild(b);
		}

	//pause/play
	b=document.createElement("A");
	b.setAttribute("href","javascript:void(0);");

	b.setAttribute("id",String(this.toString2()+"_play"));
	b.setAttribute("class","pause");
	b.className="pause";
	b.onclick = function(){togglePlaySlideshow(this.getAttribute("id"));}; //ivm IE workaround

	
	txt=document.createTextNode("\u00a0");
	b.appendChild(txt);
	this.navdiv.appendChild(b);
		
	}
	
_bsn.Crossfader.prototype._togglePlay = function()
	{
	this.playing=!this.playing;

	playButton=document.getElementById(String(this.toString2())+"_play");

	this.nTime=0;
	clearInterval( this.nID1 );
	clearInterval( this.nID2 );
	
	if (this.playing==true)
		{
		
		
		if (playButton) {playButton.className="pause";}
		for (var i=0;i<this.aDivs.length;i++) //reset alle slides
			{
			if (i!=this.nAct)
				{
				document.getElementById(this.aDivs[i]).style.opacity = 0;
				document.getElementById(this.aDivs[i]).style.position = "absolute";
				document.getElementById(this.aDivs[i]).style.filter = "alpha(opacity=0)";
				document.getElementById(this.aDivs[i]).style.visibility = "hidden";
				}
			
			}
		
		var p=this;
		this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);

		}
	else
		{
		this._stopAt(this.nAct); //stop op huidige slide
		}
	}
	
_bsn.Crossfader.prototype._stopAt = function(toShow)
	{
	this.nTime=0;
	clearInterval( this.nID1 );
	clearInterval( this.nID2 );
	
	this.playing=false;
	
	this.nAct=toShow;
	this.nOldAct = this.nAct-1;

	if (!this.aDivs[this.nOldAct])	{this.nOldAct = 0;}
	
	playButton=document.getElementById(String(this.toString2())+"_play");

	if (playButton) {playButton.className="play";}
	
	if (this.prevId)
		{
		if (this.nAct==0) {this.prevId.style.visibility="hidden";}
		else 
			{
			this.prevId.childNodes[0].setAttribute("rel",this.toString2()+"_nav_"+String(this.nAct-1));
			this.prevId.style.visibility="visible";
			}
		if (this.nAct==(this.aDivs.length-1)) {this.nextId.style.visibility="hidden";}
		else 
			{
			this.nextId.childNodes[0].setAttribute("rel",this.toString2()+"_nav_"+String(this.nAct+1));
			this.nextId.style.visibility="visible";
			}
		}
		
		
	for(co=0;co<this.aDivs.length;co++)
		{
		if (co==toShow)
			{
			if (document.getElementById( this.aDivs[co] )) {document.getElementById( this.aDivs[co] ).style.visibility = "visible";}
			if (document.getElementById( this.toString2()+"_nav_"+String(co))) {document.getElementById( this.toString2()+"_nav_"+String(co)).className = "current";}
			}
		else
			{
			if (document.getElementById( this.aDivs[co] )) {document.getElementById( this.aDivs[co] ).style.visibility = "hidden";}
			if (document.getElementById( this.toString2()+"_nav_"+String(co))) {document.getElementById( this.toString2()+"_nav_"+String(co)).className = "";}
			}
		if (document.getElementById( this.aDivs[co] )) {document.getElementById( this.aDivs[co] ).style.opacity = 100;}
		if (document.getElementById( this.aDivs[co] )) {document.getElementById( this.aDivs[co] ).style.filter = "alpha(opacity=100)";}
		}	
		
	if(typeof(this.onChange)=="function") {this.onChange(toShow);}	
	}
	
_bsn.Crossfader.prototype._newfade = function(plus)
{
	if (this.nID1)
		{clearInterval(this.nID1);}
	if (this.nID2)
		{clearInterval(this.nID2);}
	this.nOldAct = this.nAct;
	this.nAct++;
	this.counter++;
	if (!this.aDivs[this.nAct])	{this.nAct = 0;}
	
	if (this.nAct == this.nOldAct)
		{
		return false;
		}
	
	document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";

	//callBackFunction
	if(typeof(this.onChange)=="function") {this.onChange(this.nAct);}
				
	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( this.aDivs[this.nAct] ).style.opacity = op;
	document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";
	
	if (document.getElementById( this.toString2()+"_nav_"+String(this.nAct))) {document.getElementById( this.toString2()+"_nav_"+String(this.nAct)).className = "current";}
	
	if (this.prevId)
		{
		if (this.nAct==0) {this.prevId.style.visibility="hidden";}
		else 
			{
			this.prevId.childNodes[0].setAttribute("rel",this.toString2()+"_nav_"+String(this.nAct-1));
			this.prevId.style.visibility="visible";
			}
		if (this.nAct==(this.aDivs.length-1)) {this.nextId.style.visibility="hidden";}
		else 
			{
			this.nextId.childNodes[0].setAttribute("rel",this.toString2()+"_nav_"+String(this.nAct+1));
			this.nextId.style.visibility="visible";
			}
		}
		
	if (this.nOldAct > -1)
	{
	
		if (document.getElementById( this.toString2()+"_nav_"+String(this.nOldAct))) {document.getElementById( this.toString2()+"_nav_"+String(this.nOldAct)).className = "";}
		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";	
		
		var p=this;
		this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
	}
}



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

