 /**
  * Fader-JavaScript.
  *
  * conf:
  *		'element': id of/element that this fader is for
  *		'steps': Steps to take fading
  *		'time': Time for one fade in ms
  *		'skipIE' : boolean if the fading will be disabled on ie.
  *
  * @author Steffen Friedrich (steffen.friedrich@pingping.ag, www.pingping.ag)
  ***/
function Fader(conf)
{	new CoreObject( this );

	this.defineMethods=_Fader_defineMethods;
	this.defineMethods();
	
	for(var d in conf)
		this[d]=conf[d];
	
	this.fading=0;
	this.nextFade=0;

	this.stepTime=this.time/this.steps;
	this.stepFade=100/this.steps;
}

function _Fader_defineMethods()
{	this.fadeIn=_Fader_in;
	this.finishIn=_Fader_finishIn;
	this.out=_Fader_out;
	this.finishOut=_Fader_finishOut;
	this.outChainIn=_Fader_outChainIn;
	this.checkForChain=_Fader_checkForChain;
	this.triggerChainOut=_Fader_triggerChainOut;
	this.to=_Fader_to;
}

function _Fader_to(perc,last)
{	this.element.style.opacity=perc/100;
	if(!this.skipIE)
		this.element.style.filter='alpha(opacity:'+Math.round(perc)+')';
	
if(last)
		if(perc==0) this.finishOut();
		else 
		if(perc==100) this.finishIn();
}

function _Fader_in(wait)
{	if(this.fading) 
	{ this.nextFade='fadeIn';
		this.nextFadeParameter=0;
		return;
	}	else this.fading=1;

	var t=0, f=0;

	this.to(0);
	this.element.style.display='block';
	
	wait = wait ? this.time : 0;

	for(var sklave=0;sklave<this.steps;sklave++)
	{	window.setTimeout(this.getInstanceName()+'.to('+f+')',Math.round(t)+wait);
		t+=this.stepTime;
		f+=this.stepFade;
	}

	window.setTimeout(this.getInstanceName()+'.to(100,1)',Math.round(t)+wait);
}

function _Fader_finishIn()
{	this.element.style.filter=null;
	this.fading=0;

	this.checkForChain();
}


function _Fader_out()
{	if(this.fading) 
	{ this.nextFade='out';
		this.nextFadeParameter=0;
		return;
	} else this.fading=1;

	var t=0, f=100;

	this.to(100);
	this.element.style.display='block';
	this.element.style.display='none';

	for(var sklave=0;sklave<this.steps;sklave++)
	{	window.setTimeout(this.getInstanceName()+'.to('+f+')',Math.round(t));
		t+=this.stepTime;
		f-=this.stepFade;
	}

	window.setTimeout(this.getInstanceName()+'.to(0,1)',Math.round(t));
}

function _Fader_finishOut()
{	this.element.style.display='none';
	this.fading=0;

	this.checkForChain();
}

function _Fader_outChainIn(ele)
{ if(this.fading) 
	{ this.nextFade='outChainIn';
		this.nextFadeParameter=ele;
		return;
	}

	this.nextFade='triggerChainOut';
	this.nextFadeParameter=ele;

	this.out();
}

function _Fader_triggerChainOut(ele)
{	ele.fadeIn();
}

function _Fader_checkForChain()
{	if(this.nextFade)
	{	if(this[this.nextFade])
			this[this.nextFade](this.nextFadeParameter);
		
		this.nextFade=0;
		this.nextFadeParameter=0;
	}		
}