/************
/* Pavel's image fader doohickey
/*
/* Easiest script to use that I've ever written.
/*
/* Just slap some images into a div, give that div an id,
/* and run pl_startFade with with the div's id and the appropriate timing/smoothness variables

/* ToDo: add a 'pause' function that would pause on the current image.

/* Strongly adapted from xfade2.js, whoever made it.
/* Kudos to you, kind and brave gentlemen, who have led me down this path.

/*************/


if (!window.pl_startFade) {
	function pl_startFade(divid, fadeSpeed, viewSpeed, smooth, delay) {
		// console.log("pl_startFade(" + divid + ", " + fadeSpeed + ", " + viewSpeed + "," + smooth + ")");

		// urls - an array of image urls
		// fadeSpeed - how long to take between images
		// viewSpeed - how long an image should stay up at 100% opacity
		// smooth - from 1 - 100, how smooth you want the transition to be. The lower the number, the smoother.
		// delay - how long to wait before starting the fade animation for the first time.

		// load all images in div
		var div = document.getElementById(divid);
		if (!div) { return; }

		div.style.position = 'relative';

		var imgs = div.getElementsByTagName('img');
		if (imgs.length < 2) { return; }

		// set all but first to invisible
		imgs[0].style.opacity = 100;
		imgs[0].style.MozOpacity = 100;
		imgs[0].style.filter = "alpha(opacity=100)";
		imgs[0].style.position = "absolute";
		imgs[0].style.top = "0px";
		imgs[0].style.left = "0px";
		for(var i=1; i<imgs.length; i++) {
			imgs[i].style.opacity = 0;
			imgs[i].style.MozOpacity = 0;
			imgs[i].style.filter = "alpha(opacity=0)";
			imgs[i].style.position = "absolute";
			imgs[i].style.top = "0px";
			imgs[i].style.left = "0px";

		}

		setTimeout("pl_nextFade('" + divid + "', " + fadeSpeed + ", " + viewSpeed + ", " + 0 + ", " + smooth + ")", viewSpeed + delay);
	}
}

if (!window.pl_nextFade) {
	function pl_nextFade(divid, fadeSpeed, viewSpeed, index, smooth) {
		// console.log("pl_nextFade(" + divid + ", " + fadeSpeed + ", " + viewSpeed + ", " + index + ", " + smooth + ")");

		// load all images in div
		var div = document.getElementById(divid);
		if (!div) { return; }

		var imgs = div.getElementsByTagName('img');
		if (imgs.length < 2) { return; }

		// start fading out images[index] and fading in images[(index + 1) % images.length]
		for(var i=0; i<=100; i += smooth) {
			setTimeout("pl_doFade('" + divid + "', " + index + ", " + i + ");", fadeSpeed * i / 100);
		}
		setTimeout("pl_nextFade('" + divid + "', " + fadeSpeed + ", " + viewSpeed + ", " + ((index+1)%imgs.length) + ", " + smooth + ")", viewSpeed + fadeSpeed * i / 100);
	}
}

if (!window.pl_doFade) {
	function pl_doFade(divid, index, percent) {
		// console.log("pl_doFade(" + divid + ", " + index + ", " + percent + ")");

		// load all images in div
		var div = document.getElementById(divid);
		if (!div) { return; }

		var imgs = div.getElementsByTagName('img');
		if (imgs.length < 2) { return; }

		imgs[index].style.opacity = (1 - percent / 100);
		imgs[index].style.MozOpacity = (1 - percent / 100);
		imgs[index].style.filter = "alpha(opacity=" + (100 - percent) + ")";

		imgs[(index+1)%imgs.length].style.opacity = (percent / 100);
		imgs[(index+1)%imgs.length].style.MozOpacity = (percent / 100);
		imgs[(index+1)%imgs.length].style.filter = "alpha(opacity=" + (percent) + ")";
	}
}