/*

Better(?) Image cross faderX (C)2004 Patrick H. Lauke aka redux

Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imagecrossfadeX/

preInitX "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

*/

/* general variables */

var galleryXId = 'fade_sculptureindoor'; /* change this to the ID of the galleryX list */
var	galleryX; /* this will be the object reference to the list later on */
var galleryXImages; /* array that will hold all child elements of the list */
var currentImageX; /* keeps track of which image should currently be showing */
var previousImageX;
var preInitXTimer;
preInitX();

/* functions */

function preInitX() {
	/* an inspired kludge that - in most cases - manages to initially hide the image galleryX list
	   before even onload is triggered (at which point it's normally too late, and the whole list already
	   appeared to the user before being remolded) */
	if ((document.getElementById)&&(galleryX=document.getElementById(galleryXId))) {
		galleryX.style.visibility = "hidden";
		if (typeof preInitXTimer != 'undefined') clearTimeout(preInitXTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitXTimer = setTimeout("preInitX()",2);
	}
}

function faderX(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=galleryXImages[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) {
		preInitX(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
		galleryXImages = new Array;
		var node = galleryX.firstChild;
		/* instead of using childNodes (which also gets empty nodes and messes up the script later)
		we do it the old-fashioned way and loop through the first child and its siblings */
		while (node) {
			if (node.nodeType==1) {
				galleryXImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<galleryXImages.length;i++) {
			/* loop through all these child nodes and set up their styles */
			galleryXImages[i].style.position='absolute';
//			galleryXImages[i].style.top=0;
			galleryXImages[i].style.zIndex=0;
			/* set their opacity to transparent */
			faderX(i,0);
		}
		/* make the list visible again */
		galleryX.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		currentImageX=0;
		previousImageX=galleryXImages.length-1;
		opacity=100;
		faderX(currentImageX,100);
		/* start the whole crossfadeX process after a second's pause */
		window.setTimeout("crossfadeX(100)", 2000);                              // Gary - time before first fade
	}
}

function crossfadeX(opacity) {
		if (opacity < 100) {
			/* current image not faded up fully yet...so increase its opacity */
			faderX(currentImageX,opacity);
			/* faderX(previousImageX,100-opacity); */
			opacity += 10;
			window.setTimeout("crossfadeX("+opacity+")", 60);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			faderX(previousImageX,0);
			/* current image is now previous image, as we advance in the list of images */
			previousImageX=currentImageX;
			currentImageX+=1;
			if (currentImageX>=galleryXImages.length) {
				/* start over from first image if we cycled through all images in the list */
				currentImageX=0;
			}
			/* make sure the current image is on top of the previous one */
			galleryXImages[previousImageX].style.zIndex = 0;
			galleryXImages[currentImageX].style.zIndex = 100;
			/* and start the crossfadeX after a second's pause */
			opacity=0;
			window.setTimeout("crossfadeX("+opacity+")", 2650);                  // Gary - Time between fades
		}

}

/* initialise faderX by hiding image object first */
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;
 }
}
