/*

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

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

aa_bb_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 aa_bb_galleryId = 'gallery2'; /* change this to the ID of the aa_bb_gallery list */
var	aa_bb_gallery; /* this will be the object reference to the list later on */
var aa_bb_galleryImages; /* array that will hold all child elements of the list */
var aa_bb_currentImage; /* keeps track of which image should currently be showing */
var aa_bb_previousImage;
var aa_bb_preInitTimer;
aa_bb_preInit();

/* functions */

function aa_bb_preInit() {
	/* an inspired kludge that - in most cases - manages to initially hide the image aa_bb_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)&&(aa_bb_gallery=document.getElementById(aa_bb_galleryId))) {
		aa_bb_gallery.style.visibility = "hidden";
		if (typeof aa_bb_preInitTimer != 'undefined') clearTimeout(aa_bb_preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		aa_bb_preInitTimer = setTimeout("aa_bb_preInit()",2);
	}
}

function aa_bb_fader(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=aa_bb_galleryImages[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 bb_fadeInit() {
	if (document.getElementById) {
		aa_bb_preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger bb_fadeInit first */
		aa_bb_galleryImages = new Array;
		var node = aa_bb_gallery.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) {
				aa_bb_galleryImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<aa_bb_galleryImages.length;i++) {
			/* loop through all these child nodes and set up their styles */
			aa_bb_galleryImages[i].style.position='absolute';
			aa_bb_galleryImages[i].style.top=0;
			aa_bb_galleryImages[i].style.zIndex=0;
			/* set their opacity to transparent */
			aa_bb_fader(i,0);
		}
		/* make the list visible again */
		aa_bb_gallery.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		aa_bb_currentImage=0;
		aa_bb_previousImage=aa_bb_galleryImages.length-1;
		opacity=100;
		aa_bb_fader(aa_bb_currentImage,100);
		/* start the whole aa_bb_crossfade process after a second's pause */
		window.setTimeout("aa_bb_crossfade(100)", 1000);
	}
}

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

/* initialise aa_bb_fader by hiding image object first */
addEvent(window,'load',bb_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;
 }
} 

