// The global faderObject is needed for the timeout functions
var faderObject;

function Fader(images)
{
	this.currentImageIndex = 0;
	this.images = images;
	faderObject = this;
	
	this.changeOpac = function(opacity, id)
	{
		var object = document.getElementById(id).style; 
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = 'alpha(opacity=' + opacity + ')'; 
	}
	
	this.blendimage = function(divid, imageid, imagefile, millisec)
	{
		var speed = Math.round(millisec / 100);
		var timer = 0;
		
		document.getElementById(divid).style.backgroundImage = 'url(' + document.getElementById(imageid).src + ')';

		this.changeOpac(0, imageid);
		
		document.getElementById(imageid).src = imagefile;
		
		for(i = 0; i <= 100; i++)
		{
			setTimeout(function(opacity, id) { faderObject.changeOpac(opacity, id); }, (timer * speed), i, imageid);
			timer++;
		}
	}
	
	this.startSlideShow = function()
	{
		if(this.currentImageIndex + 1 != this.images.length)
		{
			imageObj = new Image();
			imageObj.src = this.images[this.currentImageIndex + 1];
		}
		
		setTimeout(function() { faderObject.newImage(); }, 3500);
	}
	
	this.newImage = function()
	{
		this.currentImageIndex++;
		
		if(this.currentImageIndex == this.images.length)
		{
			this.currentImageIndex = 0;
		}
		
		this.blendimage('blenddiv','blendimage', this.images[this.currentImageIndex], 2000);
		
		if(this.currentImageIndex + 1 != this.images.length)
		{
			imageObj = new Image();
			imageObj.src = this.images[this.currentImageIndex + 1];
		}
		
		setTimeout(function() { faderObject.newImage(); }, 6500);
	}
}


