// Browser Slide-Show script. With image cross fade effect for those browsers
// that support it.
// Script copyright (C) 2004-2011 www.cryer.co.uk.
// Script is free to use provided this copyright header is included.
var SlideShowWithLinks = {
	FadeDurationMS : 1000,
	slideCache : new Array(),
	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;
	},
	ChangeOpacity: function(id,msDuration,msStart,fromO,toO)
	{
		var element=document.getElementById(id);
		var msNow = (new Date()).getTime();
		var opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
		if (opacity>=100)
		{
			SlideShowWithLinks.SetOpacity(element,100);
			element.timer = undefined;
		}
		else if (opacity<=0)
		{
			SlideShowWithLinks.SetOpacity(element,0);
			element.timer = undefined;
		}
		else 
		{
			SlideShowWithLinks.SetOpacity(element,opacity);
			element.timer = window.setTimeout("SlideShowWithLinks.ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",10);
		}
	},
	FadeInImage: function(foregroundID,newImage,backgroundID)
	{
		var foreground=document.getElementById(foregroundID);
		if (foreground.timer) window.clearTimeout(foreground.timer);
	
		if (backgroundID)
		{
			var background=document.getElementById(backgroundID);
			if (background)
			{
				if (background.src)
				{
					foreground.src = background.src;	
					SlideShowWithLinks.SetOpacity(foreground,100);
				}
//				alert(foreground.width);
//				background.width = foreground.width;
//				background.height = foreground.height;
				background.src = newImage;
//				background.style.backgroundImage = 'url(' + newImage + ')';
//				background.style.backgroundRepeat = 'no-repeat';
				var startMS = (new Date()).getTime();
				foreground.timer = window.setTimeout("SlideShowWithLinks.ChangeOpacity('" + foregroundID + "',"
					+ SlideShowWithLinks.FadeDurationMS + "," + startMS + ",100,0)",10);
			}
		} else {
			foreground.src = newImage;
		}
	},
	NextImage: function(backgroundID,pictureID,linkID,imageLinks,displaySecs)
	{
		var separator = imageLinks.indexOf(";");
		if (separator==0)
		{
			// Probably an extra training semicolon, a common mistake.
			imageLinks = imageLinks.substring(1,imageLinks.length);
			separator = imageLinks.indexOf(";");
		}
		var nextImage = imageLinks.substring(0,separator);
		if (SlideShowWithLinks.slideCache[nextImage]
			&& SlideShowWithLinks.slideCache[nextImage].loaded)
		{
			SlideShowWithLinks.FadeInImage(pictureID,nextImage,backgroundID);
			var futureImages= imageLinks.substring(separator +1,imageLinks.length) + ';' + nextImage;
			separator = futureImages.indexOf(";");
			var nextLink = futureImages.substring(0,separator);
			futureImages= futureImages.substring(separator+1,imageLinks.length) + ';' + nextLink;
	
//			var picture=document.getElementById(pictureID);
//			if (picture.filters)
//			{
//				picture.style.filter="blendTrans(duration=2)";
//				picture.filters.blendTrans.Apply();
//			}
//			picture.src = nextImage;
			document.getElementById(linkID).href = nextLink;
//			if (picture.filters)
//			{
//				picture.filters.blendTrans.Play();
//			}
			setTimeout("SlideShowWithLinks.NextImage('"
				+backgroundID+"','"
				+pictureID+"','"+linkID+"','"+futureImages+"',"+displaySecs+")",
				displaySecs*1000);
			// Identify the next image to cache.
			separator = futureImages.indexOf(";");
			nextImage = futureImages.substring(0,separator);
		} else {
			setTimeout("SlideShowWithLinks.NextImage('"
				+backgroundID+"','"
				+pictureID+"','"+linkID+"','"+imageLinks+"',"+displaySecs+")",250);
		}
		// Cache the next image to improve performance.
		if (SlideShowWithLinks.slideCache[nextImage] == null) {
			SlideShowWithLinks.slideCache[nextImage] = new Image;
			SlideShowWithLinks.slideCache[nextImage].loaded = false;
			SlideShowWithLinks.slideCache[nextImage].onload = function(){this.loaded=true};
			SlideShowWithLinks.slideCache[nextImage].src = nextImage;
		}		
	},
	Run: function(divID,pictureID,linkID,imageLinks,displaySecs)
	{
// ABSOLUTE - Relative to first parent with position other than static.
// Span relative 0 0?

		// Insert a foreground image.
		var picture = document.getElementById(pictureID);
		var foreground = picture.cloneNode(true);
		foreground.ForegroundID=pictureID;
		foreground.id = pictureID + '.bgnd';
		picture.parentNode.insertBefore(foreground,picture);
	
		foreground.style.float = "none";
		foreground.style.position = "absolute";
		
		foreground.style.float = picture.style.float;

		foreground.style.zIndex = 1;
		foreground.style.filter = 'alpha(opacity=0)';
		foreground.style.MozOpacity = 0;
		foreground.style.opacity = 0;

		SlideShowWithLinks.NextImage(pictureID,foreground.id,linkID,imageLinks,displaySecs);
	}
};

