/*

Better(?) Image cross fader3 (C)3004 Patrick H. Lauke aka redux

Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imagecrossfade3/ 

preInit3 "Scheduler" idea by Cameron Adams aka The Man in Blue
http://www.themaninblue.com/writing/perspective/3004/09/39/ 

Tweaked to deal with empty nodes 19 Feb 3006

*/

/* general variables */

var gallery3Id = 'galeria3'; /* change this to the ID of the gallery3 list */
var	gallery3; /* this will be the object reference to the list later on */
var gallery3Images; /* array that will hold all child elements of the list */
var currentImage3; /* keeps track of which image should currently be showing */
var previousImage3;
var preInit3Timer3;
preInit3();

/* functions */

function preInit3() {
	/* an inspired kludge that - in most cases - manages to initially hide the image gallery3 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)&&(gallery3=document.getElementById(gallery3Id))) {
		gallery3.style.visibility = "hidden";
		if (typeof preInit3Timer3 != 'undefined') clearTimeout(preInit3Timer3); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInit3Timer3 = setTimeout("preInit3()",3);
	}
}

function fader3(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=gallery3Images[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 fadeInit3() {
	if (document.getElementById) {
		preInit3(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit3 first */
		gallery3Images = new Array;
		var node = gallery3.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) {
				gallery3Images.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<gallery3Images.length;i++) {
			/* loop through all these child nodes and set up their styles */
			gallery3Images[i].style.position='absolute';
			gallery3Images[i].style.top=0;
			gallery3Images[i].style.zIndex=0;
			/* set their opacity to transparent */
			fader3(i,0);
		}
		/* make the list visible again */
		gallery3.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		currentImage3=0;
		previousImage3=gallery3Images.length-1;
		opacity=100;
		fader3(currentImage3,100);
		/* start the whole crossfade3 process after a second's pause */
		window.setTimeout("crossfade3(100)", 6000);
	}
}

function crossfade3(opacity) {
	/* make sure the current image is on top of the previous one */
			gallery3Images[previousImage3].style.zIndex = 0;
			gallery3Images[currentImage3].style.zIndex = 100;
		if (opacity < 110) {
			/* current image not faded up fully yet...so increase its opacity */
			fader3(currentImage3,opacity);
			/* fader3(previousImage3,100-opacity); */
			opacity += 10;
			window.setTimeout("crossfade3("+opacity+")", 30);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			fader3(previousImage3,0);
			/* current image is now previous image, as we advance in the list of images */			previousImage3=currentImage3; 
			currentImage3+=1;
			if (currentImage3>=gallery3Images.length) {
				/* start over from first image if we cycled through all images in the list */
				currentImage3=0;
			}
			
			/* and start the crossfade3 after a second's pause */
			opacity=0;
			window.setTimeout("crossfade3("+opacity+")", 4000);
		}
		
}

/* initialise fader3 by hiding image object first */
addEvent(window,'load',fadeInit3)



/* 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;
 }
} 

