/* -----------------------------------------------------------------------
   fader.js
   simon@intelligentweb.co.uk
   ____________

   ----------------------------------------------------------------------- */

var cssId = "block-block-31";	/* This needs to be the id of the UL containing the images */
var faderBlock;			/* This is a reference to the UL referenced by cssId */
var listElements;		/* Array of list items */
var currentListElement;		/* Pointer into listElements */
var thisElement;		/* Element we're currently processing */
var nextElement;		/* Next Element in line */
var opacity;			/* current opacity */
var interElementPause = 3000;	/* Pause between images */
var faderPause = 100;		/* Pause between fade change */



function startFader() {
	if ( (document.getElementById) && (faderBlock = document.getElementById(cssId))) {	// Check we have DOM and then grab the block
//		faderBlock.style.position = "relative";
		initElements(faderBlock);
		
		if (listElements.length > 1) {
			currentListElement = 0;

			// Set initial opacity

			thisElement = listElements[currentListElement];
			nextElement = listElements[currentListElement + 1];
			opacity = 0;
			window.setTimeout("fadeOut()", interElementPause);
		}
	} else {
		// Either our DOM isn't working or we have some other problem
	}
}


/*
 * initElements() - create array of list items and hide all but first image
 *
 */
function initElements(block) {
	var docNode = block.firstChild;

	listElements = new Array;

	/* Start dealing with the list items */

	while (docNode) {
		if (docNode.nodeName == "UL") {
			docNode = docNode.firstChild;
		}

		if (docNode.nodeName == "LI") {
			listElements.push(docNode);
			if (listElements.length > 1) {
				docNode.style.position	= "absolute";
				docNode.style.top 	= 0;
				setOpacity(docNode, 0);
			}
		}
		docNode = docNode.nextSibling;
	}

}



/*
 * fadeOut() - Manage the changes in opacity for the desired two images
 *
 */
function fadeOut() {
	thisElement.style.display = "list-item";
	nextElement.style.display = "list-item";

	setOpacity(thisElement, 100 - opacity);
	setOpacity(nextElement, opacity);
	opacity	= opacity += 5;

	if (opacity <= 100)
		window.setTimeout("fadeOut()", faderPause);
	else {						/* If we're at 100% opacity, time to look at the next element */
		currentListElement++;
		if (currentListElement < listElements.length - 1) {
			thisElement = listElements[currentListElement];
			nextElement = listElements[currentListElement + 1];
		} else { 
			thisElement = listElements[currentListElement];
			nextElement = listElements[0];
			currentListElement = -1;
		}
		opacity = 0;
		window.setTimeout("fadeOut()", interElementPause);
	}
}



/* 
 * setOpacity() - set opacity of element (0 = transparent, 100 = normal
 *
 */
function setOpacity(node,opacity) {
	if (node.style) {
		if (node.style.MozOpacity!=null) {  			/* Mozilla/Firefox */
			node.style.MozOpacity = (opacity/100) - .001;
		} else if (node.style.opacity!=null) {			/* CSS3 */
			node.style.opacity = (opacity/100) - .001;
		} else if (node.style.filter!=null) {			/* IE */
			node.style.filter = "alpha(opacity="+opacity+")";
		}
	}

}

/* ---------- General setup below ---------- */


/* initialise fader by hiding image object first */
addEvent(window,'load',startFader); 



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


