﻿/*
 James Q: Todo: better fadeIn and fadeOut effect. 
 1. absolute position in <li>
 2. detect total height in <li>
 3. use function to set height for <ul> (which was set as relative position)
 4. be mindful of additional padding for top and bottom in <ul>
 5. remove .hide() and .show() for the <li> to animate
 6. remove display: none in the css
*/

function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	var check = $('div.rotator ul li')
	var randomNo = Math.floor(Math.random() * check.length);
	var randomList = $(check[randomNo]);
	randomList.addClass('show');
	randomList.css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 8000 = change to next image after 6 seconds
	
	setInterval('rotate()',8000);	
}

function rotate() {	
	
	//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, rotate 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'));
	
	//Un-comment the 3 lines below to get the images in random order	
	//var sibs = current.siblings();
	//var rndNum = Math.floor(Math.random() * sibs.length );
	//var next = $( sibs[ rndNum ] );
			
	//Set the fade in effect for the next image, the show class has higher z-index
	next.addClass('show')
	.css({opacity: 0.0})
	.animate({opacity: 1.0}, 1000)	
	
	//Hide the current image
	current.removeClass('show')
	.animate({opacity: 0.0}, 1000)
	
};

$(document).ready(function() {	
	
	var maxHeight;
	
	//Get the whole li height
	var listHeight = $('div.rotator ul li')
	listHeight.each(function(index) {
	
		if (index == 0){
			maxHeight = $(this).height();
		}
		else{
			var checkHeight = $(this).height();
			
			if(maxHeight < checkHeight){
				maxHeight = checkHeight;
			}
		}
		
	});
	
	$('div.rotator ul').css('height', maxHeight);
	
	//Load the slideshow
	theRotator();
	$('div.rotator').fadeIn(1000);
		$('div.rotator ul li').fadeIn(1000); // tweek for IE
});

