/*

Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux

Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/ 

preInitN "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 galleryNId = 'vetrinaN'; /* change this to the ID of the gallery list */
var	galleryN; /* this will be the object reference to the list later on */
var galleryNImages; /* array that will hold all child elements of the list */
var currentImageN; /* keeps track of which image should currently be showing */
var previousImageN;
var preInitNTimer;
preInitN();

/* functions */

function preInitN() {
	/* 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)&&(galleryN=document.getElementById(galleryNId))) {
		galleryN.style.visibility = "hidden";
		if (typeof preInitNTimer != 'undefined') clearTimeout(preInitNTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		preInitNTimer = setTimeout("preInitN()",2);
	}
}

function faderN(imageNumberN,opacity) {
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
	var objN=galleryNImages[imageNumberN];
	if (objN.style) {
		if (objN.style.MozOpacity!=null) {  
			/* Mozilla's pre-CSS3 proprietary rule */
			objN.style.MozOpacity = (opacity/100) - .001;
		} else if (objN.style.opacity!=null) {
			/* CSS3 compatible */
			objN.style.opacity = (opacity/100) - .001;
		} else if (objN.style.filter!=null) {
			/* IE's proprietary filter */
			objN.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function fadeInitN() {
	if (document.getElementById) {
		preInitN(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInitN first */
		galleryNImages = new Array;
		var nodeN = galleryN.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 (nodeN) {
			if (nodeN.nodeType==1) {
				galleryNImages.push(nodeN);
			}
			nodeN = nodeN.nextSibling;
		}
		for(i=0;i<galleryNImages.length;i++) {
			/* loop through all these child nodes and set up their styles */
			galleryNImages[i].style.position='absolute';
			galleryNImages[i].style.top=0;
			galleryNImages[i].style.zIndex=0;
			/* set their opacity to transparent */
			faderN(i,0);
		}
		/* make the list visible again */
		galleryN.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		currentImageN=0;
		previousImageN=galleryNImages.length-1;
		opacity=100;
		faderN(currentImageN,100);
		/* start the whole crossfade process after a second's pause */
		window.setTimeout("crossfadeN(100)", 5000); // Velocità crossfade
	}
}

function crossfadeN(opacity) {
		if (opacity < 100) {
			/* current image not faded up fully yet...so increase its opacity */
			faderN(currentImageN,opacity);
			/* faderN(previousImage,100-opacity); */
			opacity += 10;
			window.setTimeout("crossfadeN("+opacity+")", 30);
		} else {
			/* make the previous image - which is now covered by the current one fully - transparent */
			faderN(previousImageN,0);
			/* current image is now previous image, as we advance in the list of images */
			previousImageN=currentImageN;
			currentImageN+=1;
			if (currentImageN>=galleryNImages.length) {
				/* start over from first image if we cycled through all images in the list */
				currentImageN=0;
			}
			/* make sure the current image is on top of the previous one */
			galleryNImages[previousImageN].style.zIndex = 0;
			galleryNImages[currentImageN].style.zIndex = 100;
			/* and start the crossfade after a second's pause */
			opacity=0;
			window.setTimeout("crossfadeN("+opacity+")", 5000); // Velocità crossfade
		}
		
}

/* initialise fader by hiding image object first */
addEventN(window,'load',fadeInitN)



/* 3rd party helper functions */

/* addEventN handler for IE and other browsers */
function addEventN(elm, evType, fn, useCapture) 
// addEventN 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;
 }
} 
