
function MSW_Fader(list, obj, link, baseUrl)
{
	this.list = list;
	this.obj = $(obj);
	this.link = $(link);
	this.baseUrl = baseUrl;
	this.currentIndex = 0;
	if (this.baseUrl == null)
	{
		var loc = window.location.href;
		var lpos = loc.lastIndexOf('/');
		this.baseUrl = loc.substring(0, lpos+1);
	}
	this.start();
	for (x = 0; x < list.length; x++)
		this.cache[x] = null;
	this.cacheImage(0);
};
MSW_Fader.prototype.list = null;
MSW_Fader.prototype.obj = null;
MSW_Fader.prototype.link = null;
MSW_Fader.prototype.baseUrl = "";
MSW_Fader.prototype.timer = null;
MSW_Fader.prototype.currentIndex = 0;
MSW_Fader.prototype.cache = new Array();


MSW_Fader.prototype.start = function()
{
	this.continueTimer();
}

MSW_Fader.prototype.pauseTimer = function()
{
	clearTimeout(this.timer);
};


MSW_Fader.prototype.continueTimer = function()
{
	var thisObj = this;
	name = this.obj.src;
	name = name.substring(this.baseUrl.length, name.length);
	for (x = 0; x < this.list.length; x++)
		if (this.list[x].image == name)
			break;
	clearTimeout(this.timer);
	// TODO: this needs to point to the object method rotateImage
	//this.timer = setTimeout("rotate_image(" + (x+1) + ")", 2 * 1000);
	this.timer = setTimeout(function(){thisObj.rotateImage(x+1);}, 2 * 1000);


};

MSW_Fader.prototype.cacheImage = function (index)
{
	if (this.cache[index] == null && index < this.list.length)
	{
		this.cache[index] = new Image; 
		this.cache[index].src = this.imageSrc(index);
	}
}

MSW_Fader.prototype.imageSrc = function(index)
{
	if (index < this.list.length)
		return this.baseUrl + this.list[index].image;
	else
		return "";
}
MSW_Fader.prototype.rotateImage = function(index)
{
	var thisObj = this;
	if ((index >= this.list.length) || (index < 0) || this.list[index].image == '')
		index = 0;
	// start fading away
	Effect.Fade(this.obj, 
	{afterFinishInternal: function(effect)
		{
			// after faded away - now update image, and link
			effect.element.src = thisObj.baseUrl + thisObj.list[index].image;
			if (thisObj.list[index].link.length > 0)
				thisObj.link.href = thisObj.list[index].link;
			else
				thisObj.link.href = 'javascript:void(true);';

			// now bring the image back
			new Effect.Appear(effect.element, 
			{afterFinishInternal: function (effect)
				{
					// finished - set the timeout for the next image
					clearTimeout(thisObj.timer);
					// TODO: this needs to point to the object method rotateImage
					//this.timer = setTimeout("rotate_image(" + (index+1) + ")", rimg[index].time * 1000);
					thisObj.timer = setTimeout(function(){thisObj.rotateImage(index+1);}, thisObj.list[index].time * 1000);
					// cache the next image
					thisObj.cacheImage(index+1);
				}
			});
		}
	});
}



