/**
 * Author: Lee Langley
 * Date Created: 05/05/2011 16:46
 */
$(window).load(function(){
	$('.imageBox').each(function(i){
		var fadeBox = this;

		// create the loop
		$(fadeBox).everyTime(6000, 'fade_box_' + i, function(){
			// this stores the current image
			var currentImage = null;

			// only bother with the fade if there is more than one image
			if($(fadeBox).children('img').length > 1){
				// we then loop through all the images to find the currently displayed image
				$(fadeBox).children('img').each(function(){
					// if the image is shown, save it
					if($(this).css('display') != 'none'){
						currentImage = this;
					}else{
						// otherwise ensure that it is hidden
						$(this).hide();
					}
				});

				// find the next image to display
				var nextImage = ($(currentImage).next('img').length > 0) ? $(currentImage).next('img') : $(fadeBox).children('img:first');
				// fade out the current image and fade in the next image
				$(currentImage).fadeOut(3000);
				$(nextImage).fadeIn(3000);
			}
		});
	});
});
