/*

ECB Optimised  Image cross fader  by Paolo Ferrari

Inspired by Patrick H. Lauke (http://www.splintered.co.uk/)


*/

/* general variables */

var galleryId = 'gallery'; 
var	gallery; 
var galleryImages; 
var currentImage; 
var previousImage;
var preInitTimer;
preInit();

/* functions */

function preInit() {

	if ((document.getElementById)&&(gallery=document.getElementById(galleryId))) {
		gallery.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); 
	} else {
		preInitTimer = setTimeout("preInit()",2);
	}
}

function fader(imageNumber,opacity) {

	var obj=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 fadeInit() {
	if (document.getElementById) {
		preInit(); 
		galleryImages = new Array;
		var node = gallery.firstChild;
		while (node) {
			if (node.nodeType==1) {
				galleryImages.push(node);
			}
			node = node.nextSibling;
		}
		for(i=0;i<galleryImages.length;i++) {
			galleryImages[i].style.display='block';
			galleryImages[i].style.position='absolute';
			//galleryImages[i].style.top=0;
			galleryImages[i].style.zIndex=0;
			fader(i,0);
		}
		gallery.style.visibility = 'visible';
		currentImage=0;
		previousImage=galleryImages.length-1;
		opacity=100;
		fader(currentImage,100);
		//window.setTimeout("crossfade(100)", 1000);
		crossfade(100);
	}
}

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

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;
 }
}
