/*

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

class structured, possible 2 cross fader object - by Marko Vrsnik (23.4.2006)
http://www.msplet.com

*/

/* functions */
function LoadGallery(gl, timer)
{
  var g = new Gal(gl);
  g.preInit();
  g.fadeInit(timer);
}

function Gal(gl)
{
   this.galleryId = gl; /* change this to the ID of the gallery list */
   this.gallery; /* this will be the object reference to the list later on */
   this.galleryImages; /* array that will hold all child elements of the list */
   this.currentImage; /* keeps track of which image should currently be showing */
   this.previousImage;
   this.preInitTimer;
   this.opacity;
   return this;
}


Gal.prototype.preInit= function() {
	/* 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)&&(this.gallery=document.getElementById(this.galleryId))) {
		this.gallery.style.visibility = "hidden";
		if (typeof this.preInitTimer != 'undefined') clearTimeout(this.preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	}
	else {
		var my = this; var f = function(){my.preInit();}
		this.preInitTimer = setTimeout(f,2);
	}
}

Gal.prototype.fader = function(imageNumber,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var obj=this.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+")";
		}
	}
}

Gal.prototype.fadeInit = function(timer) {		
	if (document.getElementById) {
	    //this.preInit; /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
		this.galleryImages = new Array;
		var node = this.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) {
				this.galleryImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<this.galleryImages.length;i++) {
			/* loop through all these child nodes and set up their styles */
			this.galleryImages[i].style.position='absolute';
			this.galleryImages[i].style.top=0;
			this.galleryImages[i].style.zIndex=0;
			/* set their opacity to transparent */
			this.fader(i,0);
		}
		/* make the list visible again */
		this.gallery.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		this.currentImage=0;
		this.previousImage=this.galleryImages.length-1;
		opacity=100;
		this.fader(this.currentImage,100);
		/* start the whole crossfade process after a second's pause */
		var my = this; var f = function(){my.crossfade(timer);}
		window.setTimeout(f, 2000);
		//window.setTimeout(this.crossfade2(), 2000);
	}
}

Gal.prototype.crossfade = function(timer) {
		if (this.opacity < 100) {
			/* current image not faded up fully yet...so increase its opacity */
			this.fader(this.currentImage,this.opacity);
			/* fader(previousImage,100-opacity); */
			this.opacity += 5;
			//window.setTimeout(this.crossfade(), 30);
			var my = this; var f = function(){my.crossfade(timer);}
			window.setTimeout(f, 40);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			this.fader(this.previousImage,0);
			/* current image is now previous image, as we advance in the list of images */
			this.previousImage=this.currentImage;
			this.currentImage+=1;
			if (this.currentImage>=this.galleryImages.length) {
				/* start over from first image if we cycled through all images in the list */
				this.currentImage=0;
			}
			/* make sure the current image is on top of the previous one */
			this.galleryImages[this.previousImage].style.zIndex = 0;
			this.galleryImages[this.currentImage].style.zIndex = 100;
			/* and start the crossfade after a second's pause */
			this.opacity=0;
			//window.setTimeout(this.crossfade(), 2000);
			var my = this; var f = function(){my.crossfade(timer);}
			if (timer!=null)
				window.setTimeout(f, 4000);
			else
				window.setTimeout(f, 4000);
		}

		
}
