/*

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

*/

/* general variables */

var gallery2Id = 'gallery2'; /* change this to the ID of the gallery list */
var nsgallery2Id = 'nsgallery2';
var	gallery2; /* this will be the object reference to the list later on */
var gallery2Images; /* array that will hold all child elements of the list */
var current2Image; /* keeps track of which image should currently be showing */
var previous2Image;
var pre2InitTimer;
var dwell2=2400;
var cross=80;
var fps=30;
pre2Init();

/* functions */

function pre2Init() {
	/* an inspired kludge that - in most cases - manages to initially hide the image gallery 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)&&(nsgallery2=document.getElementById(nsgallery2Id))) {
        nsgallery2.style.visibility = "hidden";
    }
	if ((document.getElementById)&&(gallery2=document.getElementById(gallery2Id))) {
		gallery2.style.visibility = "hidden";
		if (typeof pre2InitTimer != 'undefined') clearTimeout(pre2InitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		pre2InitTimer = setTimeout("pre2Init()",2);
	}
}

function fader2(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=gallery2Images[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 fade2Init() {
	if (document.getElementById) {
		pre2Init(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fade2Init first */
		gallery2Images = new Array;
		var node = gallery2.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) {
				gallery2Images.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<gallery2Images.length;i++) {
			/* loop through all these child nodes and set up their styles */
			gallery2Images[i].style.position='absolute';
			gallery2Images[i].style.top=0;
			gallery2Images[i].style.zIndex=0;
			/* set their opacity to transparent */
			fader2(i,0);
		}
		/* make the list visible again */
		gallery2.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		current2Image=0;
		previous2Image=gallery2Images.length-1;
		opacity=100;
		fader2(current2Image,100);
		/* start the whole cross2fade process after a second's pause */
		window.setTimeout("cross2fade(100)", dwell2);
	}
}

function cross2fade(opacity) {
		if (opacity < 100) {
			/* current image not faded up fully yet...so increase its opacity */
			fader2(current2Image,opacity);
			/* fader2(previous2Image,100-opacity); */
			opacity += cross/fps;
			window.setTimeout("cross2fade("+opacity+")", fps);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			fader2(previous2Image,0);
			/* current image is now previous image, as we advance in the list of images */
			previous2Image=current2Image;
			current2Image+=1;
			if (current2Image>=gallery2Images.length) {
				/* start over from first image if we cycled through all images in the list */
				current2Image=0;
			}
			/* make sure the current image is on top of the previous one */
			gallery2Images[previous2Image].style.zIndex = 0;
			gallery2Images[current2Image].style.zIndex = 100;
			/* and start the cross2fade after a second's pause */
			opacity=0;
			window.setTimeout("cross2fade("+opacity+")", dwell2);
		}
		
}

/* initialise fader by hiding image object first */
add2Event(window,'load',fade2Init)



/* 3rd party helper functions */

/* add2Event handler for IE and other browsers */
function add2Event(elm, evType, fn, useCapture) 
// add2Event 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;
 }
} 

