function getImageFader(foregroundID, backgroundID)
{
	return new ImageFader(foregroundID, backgroundID);
}

function ImageFader(foregroundID, backgroundID) 
{
	this.fadeDuration = 2000; //milliseconds
	this.fadeIncrement = 10;  //milliseconds
	this.startOpacity = 0; //percent
	this.endOpacity = 100; //percent

	this.foregroundID = foregroundID;
	this.backgroundID = backgroundID;
	this.foreground = null;
	this.background = null;
	this.fadeTimer = null;
	this.fadeStartTime = 0;

	this.FadeInImage = function(newImage)
	{
		if (this.background == null)
		{
			try {
				this.background = document.getElementById(backgroundID);
			} catch(e) {
				alert("Cannot find div with ID " + backgroundID);
			}
		}	
		if (this.foreground == null)
		{
			try {
				this.foreground = document.getElementById(foregroundID);
			} catch(e) {
				alert("Cannot find div with ID " + foregroundID);
			}
		}	

		this.background.style.backgroundImage = 'url(' + this.foreground.src + ')';
		this.background.style.backgroundRepeat = 'no-repeat';

		this.SetOpacity(this.foreground, 0);

		this.foreground.src = newImage;
		if (this.fadeTimer)
		{
			clearTimeout(this.fadeTimer);
		}
		this.fadeStartTime = (new Date()).getTime();
		var self = this;
		this.fadeTimer = setTimeout(function(){ 
			self.ChangeOpacity(); 
		}, this.fadeIncrement);
	}

	this.SetOpacity = function(object,opacityPct)
	{
	  // IE.
	  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
	  // Old mozilla and firefox
	  object.style.MozOpacity = opacityPct/100;
	  // Everything else.
	  object.style.opacity = opacityPct/100;
	}

	this.ChangeOpacity = function()
	{
		var opacity = this.foreground.style.opacity * 100;
		trace(opacity);
		var nowMillis = (new Date()).getTime();
		opacity = this.startOpacity + (this.endOpacity - this.startOpacity) * (nowMillis - this.fadeStartTime) / this.fadeDuration;
		if (opacity < 0)
		{
			this.SetOpacity(this.foreground, 0);
		} else if (opacity > 100) {
			this.SetOpacity(this.foreground, 100);
		} else {
			this.SetOpacity(this.foreground, opacity);
			var self = this;
			this.fadeTimer = setTimeout(function(){ 
				self.ChangeOpacity(); 
			}, this.fadeIncrement);
		}
	}
}