/*

Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux

Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/ 
preInit "Scheduler" idea by Cameron Adams aka The Man in Blue
http://www.themaninblue.com/writing/perspective/2004/09/29/ 

Tweaked to deal with empty nodes 19 Feb 2006

*/

var	scrShots; 
var currentImage;
var previousImage;
var scrshotImages;
var preInitTimer;

// Init scrShots
preInit();

function preInit() {

	if ((document.getElementById)&&(scrShots=document.getElementById('scrshots'))) {
		scrShots.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') 
			clearTimeout(preInitTimer);
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
	
}

function fader(imageNumber,opacity) {
	var obj=scrshotImages[imageNumber];
	if (obj.style) {
		if (obj.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			obj.style.MozOpacity = (opacity/100) - .001;
		} else if (obj.style.opacity!=null) {
			/* CSS3 compatible */
			obj.style.opacity = (opacity/100) - .001;
		} else if (obj.style.filter!=null) {
			/* IE's proprietary filter */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function fadeInit() {
	if (document.getElementById) {
		preInit(); 
		scrshotImages = new Array;
		var node = scrShots.firstChild;

		while (node) {
			if (node.nodeType==1) {
				scrshotImages.push(node);
			}
			node = node.nextSibling;
		}
		
		for(i=0;i<scrshotImages.length;i++) {
			scrshotImages[i].style.position='absolute';
			scrshotImages[i].style.top=0;
			scrshotImages[i].style.zIndex=0;
			fader(i,0);
		}
		
		scrShots.style.visibility = 'visible';

		opacity=100;
		currentImage=0;
		previousImage=scrshotImages.length-1;
		fader(currentImage,100);
	}

}

function show(imgIndex)
{
	if( imgIndex != currentImage ) {
		
		previousImage=currentImage;
		currentImage =imgIndex;

		scrshotImages[previousImage].style.zIndex = 0;
		scrshotImages[currentImage].style.zIndex = 100;

		opacity=0;
		window.setTimeout("crossfade("+opacity+")", 0);
		
	}
}

function crossfade(opacity) {
		if (opacity < 100) {
			fader(currentImage,opacity);
			opacity += 10;
			window.setTimeout("crossfade("+opacity+")", 30);
		} 
 		else {
			fader(previousImage,0);
		}		
}


// Initialize fader
addEvent(window,'load',fadeInit)


/* 3rd party helper functions */

/* addEvent handler for IE and other browsers */
function addEvent(elm, evType, fn, useCapture) 
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 