﻿/**
 *	based on the Crossfader by Timothy Groves
 *  by Thomas Theilig
 */

this.Fader = function (inDivs, inDuration, inDelay)
{
	var i;
	
	this.divs = inDivs;
	this.duration = inDuration;
	this.delay = inDelay;
	this.interval = 25;
 
	this.isAnim = (inDuration > 0) ? true : false;
	this.isAuto = (inDelay > 0) ? true : false;

	for (i = 0; i < this.divs.length; i++)
	{
		this._iHide(i);
	}
	
	this.act = -1;
	this.old = -1;
}

this.Fader.prototype._show = function(inTarget, isSwitch)
{
	var j;
	if (inTarget == null)
	{
		this._change(-1);
		return;
	}
	for (j = 0; j < this.divs.length; j++)
	{
		if (inTarget == this.divs[j])
		{
			if ((this.act == j) && isSwitch != null) return;
			this._change(j);
			return;
		}
	}
	// functionality: if the next line is uncommented, this function will
	// hide anything if called with an illegal argument. like this, it will
	// do nothing
	// this._change(-1);
}

this.Fader.prototype._hide = function()
{
	var that = this;
	if (this.isAnim)
	{
		this._killfade();
		this.old = this.act;
		this.act = -1;
		this.fadeTime = 0;
		this.intFade = setInterval(function() {that._fade(); }, this.interval);
	}
	else
	{
		this._iHide(this.act);
		this.old = -1;
		this.act = -1;
	}
}

this.Fader.prototype._change = function(inNew)
{
	var that = this;
	if (this.isAnim) { this._killfade(); }
	this.old = this.act;
	this.act = (inNew >= 0) ? inNew : this.act + 1;
	if (!this.divs[this.act]) { this.act = 0; }
	if (this.old == this.act)
	{
		if (this.isAuto)
		{
			this.intDelay = setInterval(function() {that._change(-1); }, this.delay);
		}
		else
		{
			this._hide();
		}
		return;
	}
	if (this.isAnim)
	{
		document.getElementById(this.divs[this.act]).style.visibility = "visible";
		this.fadeTime = 0;
		this.intFade = setInterval(function() {that._fade(); }, this.interval);
	}
	else
	{
		if (this.old > -1) { this._iHide(this.old); }
		if (this.act > -1) { this._iShow(this.act); }
		if (this.isAuto)
		{
			this.intDelay = setInterval(function() {that._change(-1); }, this.delay);
		}
	}
}

this.Fader.prototype._fade = function()
{
	this.fadeTime += this.interval;
	var opacityP = Math.round(this._ease(this.fadeTime, this.duration));
	var opacity = opacityP / 100;

	if (this.act > -1)
	{
	document.getElementById(this.divs[this.act]).style.opacity = opacity;
	document.getElementById(this.divs[this.act]).style.filter = "alpha(opacity="+opacityP+")";
	}
	
	if (this.old > -1)
	{
		document.getElementById(this.divs[this.old]).style.opacity = 1 - opacity;
		document.getElementById(this.divs[this.old]).style.filter = "alpha(opacity="+(100 - opacityP)+")";
	}
	
	if (this.fadeTime >= this.duration)
	{
		this._killfade();
		if (this.isAuto)
		{
			var that = this;
			this.intDelay = setInterval(function() {that._change(-1); }, this.delay);
		}
	}
}

this.Fader.prototype._killfade = function()
{
	if (this.intDelay) { clearInterval(this.intDelay); }
	if (this.intFade) {	clearInterval(this.intFade); }
	if (this.old > -1) { this._iHide(this.old); }
	if (this.act > -1) { this._iShow(this.act); }
}


this.Fader.prototype._iHide = function(inI)
{
	if (document.getElementById(this.divs[inI]))
	{
		document.getElementById(this.divs[inI]).style.opacity = 0;
		document.getElementById(this.divs[inI]).style.filter = "alpha(opacity=0)";
		document.getElementById(this.divs[inI]).style.visibility = "hidden";
	}
}

this.Fader.prototype._iShow = function(inI)
{
	if (document.getElementById(this.divs[inI]))
	{
		document.getElementById(this.divs[inI]).style.visibility = "visible";
		document.getElementById(this.divs[inI]).style.opacity = 1;
		document.getElementById(this.divs[inI]).style.filter = "";
	}
}

this.Fader.prototype._ease = function(inCurrent, inTotal)
{
	return (Math.cos(Math.PI * inCurrent / inTotal) / -2 + 0.5) * 100;
}
