/*
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
//------------------------------------------------------
// Create images from the list of names rather than
// from LI child elements. R Wilson 7-18-2009
//------------------------------------------------------
 */

var galleryId = 'gallery'; /* change this to the ID of the gallery list */
var gallery; /* this will be the object reference to the list later on */
var galleryImages; /* array that will hold all child elements of the list */
var currentImage; /* keeps track of which image should currently be showing */
var previousImage;
var preInitTimer;
var stillTimeMS = 3000;
var fadeSliceMS = 60;
preInit();


var imgNames = new Array();
imgNames[0] = "rotate_images/ultimate001.jpg";
imgNames[1] = "rotate_images/ultimate002.jpg";
imgNames[2] = "rotate_images/ultimate003.jpg";
imgNames[3] = "rotate_images/ultimate004.jpg";
imgNames[4] = "rotate_images/ultimate005.jpg";
imgNames[5] = "rotate_images/ultimate006.jpg";
imgNames[6] = "rotate_images/ultimate007.jpg";
imgNames[7] = "rotate_images/ultimate008.jpg";
imgNames[8] = "rotate_images/ultimate009.jpg";

var creditNames = new Array();
creditNames[0] = "Andrew Morelli";
creditNames[1] = "Todd Fletcher";
creditNames[2] = "Chuck Stieg";
creditNames[3] = "Kristian Bauck-Nordeide";
creditNames[4] = "Paddy Steer";
creditNames[5] = "Wolfgang Schuster";
creditNames[6] = "Patrick Groulx";
creditNames[7] = "Todd Fletcher";
creditNames[8] = "Oskari Vuori";


function preInit()
{
	// 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) && (gallery = document.getElementById(galleryId)))
	{
		gallery.style.visibility = "hidden";
		if (typeof preInitTimer != 'undefined')
			clearTimeout(preInitTimer); /*
										 * thanks to Steve Clay
										 * http://mrclay.org/ for this small
										 * Opera fix
										 */
	}
	else
	{
		preInitTimer = setTimeout("preInit()", 2);
	}
}

function fader(imageNumber, opacity)
{
	/*
	 * helper function to deal specifically with images and the cross-browser
	 * differences in opacity handling
	 */
	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;
		
		//------------------------------------------------------
		// Create images from the list of names rather than
		// from LI child elements. R Wilson 7-18-2009
		//------------------------------------------------------
		for(var i=0;i<imgNames.length;++i)
		{
			var ref = document.createElement('img');
			ref.src = imgNames[i];
			gallery.appendChild(ref);
			galleryImages.push(ref);
		}
		
		for (i = 0; i < galleryImages.length; i++)
		{
			/* loop through all these child nodes and set up their styles */
			galleryImages[i].style.position = 'absolute';
			galleryImages[i].style.top = 0;
			galleryImages[i].style.zIndex = 0;
			/* set their opacity to transparent */
			fader(i, 0);
		}
		/* make the list visible again */
		gallery.style.visibility = 'visible';
		/* initialise a few parameters to get the cycle going */
		currentImage = 0;
		
		gallery.title = "Built by " + creditNames[currentImage];
		
		previousImage = galleryImages.length - 1;
		opacity = 100;
		fader(currentImage, 100);
		/* start the whole crossfade process after a second's pause */
		window.setTimeout("crossfade(100)", stillTimeMS);
	}
}

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 += 10;
		window.setTimeout("crossfade(" + opacity + ")", fadeSliceMS);
	}
	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;
		
		gallery.title = "Built by " + creditNames[previousImage];
		
		/* and start the crossfade after a second's pause */
		opacity = 0;
		window.setTimeout("crossfade(" + opacity + ")", stillTimeMS);
	}

}

/* 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;
	}
}

/* initialise fader by hiding image object first */
addEvent(window, 'load', fadeInit);




