function FaderSettings() {
	this.containerId=null;
	this.images=new Array();
	this.changeInterval=5; //change every 5 seconds
	this.changeSpeed=2; //transistion takes 2 seconds
	this.frameInterval=50; //20ms between two frames
	this.backgroundColor="#888";
	this.order="normal"; //normal|randomize|offset
	this.offset=0; //For use with order=offset
	this.randomizeStart=false; //start randomly between now and this.changeInterval
}


function Fader(settingsIn) {
	/* methods */
	this.setOpacity = function () {
		var opacityTruncated=Math.max(0,Math.min(1,this.opacity));
		
		this.currentImage.style.opacity = opacityTruncated;
		this.currentImage.style.filter = "alpha(opacity=" + parseInt(opacityTruncated*100) + ")";
	}
	
	this.nextSlide = function () {				
		//update styles and switch to next image
		this.currentImage.style.zIndex=0;
		this.opacity=1;
		this.setOpacity();		
		this.currentImage=this.images[this.nextImageIndex];
		this.nextImageIndex=(this.nextImageIndex+1)%this.images.length;
		this.currentImage.style.zIndex=2;
		this.images[this.nextImageIndex].style.zIndex=1;		
		
		//fade after timeout
		var callwrapper = new CCallWrapper(this, this.settings.changeInterval*1000, 'fadeOut');
		CCallWrapper.asyncExecute(callwrapper);	
	}
	
	this.fadeOut = function () {
		this.opacity-=this.decreaseStep;
		this.setOpacity();
				
		if (this.opacity>0) {
			var callwrapper = new CCallWrapper(this, this.settings.frameInterval, 'fadeOut');
			CCallWrapper.asyncExecute(callwrapper)		
		} else {
			this.nextSlide();
		}		
	}
	
	/* fields */
	this.settings=settingsIn; //FaderSettings object
	
	this.decreaseStep=1.0/(this.settings.changeSpeed*1000/this.settings.frameInterval);
	this.opacity=1;
	this.currentImage=null;
	this.nextImageIndex=1;
	this.images=new Array();
	this.container=document.getElementById(this.settings.containerId);
	
	this.imageNames=this.settings.images;
	switch (this.settings.order) {
		case 'randomize':
			this.imageNames.sort(function (){return (Math.round(Math.random())-0.5); });
			break;
		case 'offset':
		
			for (var i=0; i<this.settings.offset; i++) {
				this.imageNames.push(this.imageNames.shift());
			}
			break;
	}
	
	/* init */	
	//create inner divs, which contain the actual images as background	
	for(var i=0;i<this.settings.images.length;i++) {
		var img=document.createElement('div');
				
		img.style.height=this.container.offsetHeight+"px";
		img.style.width=this.container.offsetWidth+"px";
		img.style.position="absolute";		
		img.style.backgroundColor=this.settings.backgroundColor;
		img.style.backgroundImage="url('"+this.imageNames[i]+"')";
		img.style.backgroundRepeat="no-repeat";
		img.style.backgroundPosition="center center";		
		img.style.zIndex=0;
		img.style.opacity=1;
		img.style.filter="alpha(opacity=100)";
		
		this.container.appendChild(img);
		
		this.images.push(img);
	}
	
	this.images[0].style.zIndex=2;
	this.images[1].style.zIndex=1;

	this.currentImage=this.images[0];
		
	/* Javascript is teh sukc!!!1 */
	var callwrapper;
	if (this.settings.randomizeStart) {
		callwrapper = new CCallWrapper(this, parseInt(this.settings.changeInterval*1000*Math.random()), 'fadeOut');
	} else {
		callwrapper = new CCallWrapper(this, this.settings.changeInterval*1000, 'fadeOut');
	}
	CCallWrapper.asyncExecute(callwrapper);
}