// In: string imgId: id of img object.
// In: string linkId: Id of anchor.
// In: array sources: An array of pairs [diagrem URL, image source name].
// In: int fadeTime: Number of milliseconds to display.
// In: int steps: Number of steps in fading. 
function Display(imgId, linkId, sources, fadeTime, steps)
{
	// Delay in milliseconds between each update.
	var Timeout = fadeTime/steps;
	var Img = document.getElementById(imgId);
	var Link = document.getElementById(linkId);
	var Sources = sources;
	var FadeTime = fadeTime;
	var Steps = steps;
	var Index = Math.max(0, Math.round(Math.random() * (sources.length - 1)));
	var RestTime = fadeTime;
	var Cache = new Image();
	var _this = this; // capture current context

	this.Start = 
		function ()
		{
			
			setTimeout (Fade, Timeout);
		};
		
		
		function Fade()
		{
			if (RestTime > 0)
			{
				Browser.SetOpacity(Img, RestTime > FadeTime/2 ? 2*(FadeTime - RestTime)/FadeTime : 2*RestTime/FadeTime);
				RestTime -= Timeout;
			}
			else
			{
				Link.href = Sources[Index][0];
				Img.src = Sources[Index][1];
				RestTime = FadeTime;
				Index--;
				if (Index < 0)
				{
					Index = Sources.length - 1;
				}
				Cache.src = Sources[Index][1];
			}
			_this.Start();
		}
}
