/* JS Fader Script */

// times are in milliseconds
var faderDefaultRotateInterval = 4000;
var faderTransitionInterval = 600;

function initFader()
{
	//Set the opacity of all images (exect the first) to 0
	$('div#rotator ul li:not(:first)').css({opacity: 0.0});

	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});

	//get duration for the first item in the list
        var rotateDelay = getTransitionDuration($('div#rotator ul li:first'));
        if (rotateDelay <= 0)
                rotateDelay = faderDefaultRotateInterval;

	//Call the fade function to run the slideshow (note: fade() will repeatedly call itself via setTimeout)
	setTimeout('fade()', rotateDelay);
}

function fade()
{
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, fade it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, faderTransitionInterval);

	//Hide the current image
	current.animate({opacity: 0.0}, faderTransitionInterval)
	.removeClass('show');

	// get duration for this list item
	var rotateDelay = getTransitionDuration(next);
	if (rotateDelay <= 0)
		rotateDelay = faderDefaultRotateInterval;

	// pseudo-recursively call fade()
	setTimeout('fade()', rotateDelay);
}

function getTransitionDuration(elem)
{
	// elem: should be a reference to an element (li maybe) that has a class of durationX

	//parse duration
	if (elem.hasClass('duration1'))
		return 1000;

	else if (elem.hasClass('duration2'))
		return 2000;

	else if (elem.hasClass('duration3'))
		return 3000;

	else if (elem.hasClass('duration4'))
		return 4000;

	else if (elem.hasClass('duration5'))
		return 5000;

	else if (elem.hasClass('duration6'))
		return 6000;

	else if (elem.hasClass('duration7'))
		return 7000;

	else if (elem.hasClass('duration8'))
		return 8000;

	else if (elem.hasClass('duration9'))
		return 9000;

	else if (elem.hasClass('duration10'))
		return 10000;

	else
		return 0;
}
