$(document).ready(function() {		
	
	//Execute the smallboxR, set 4 seconds for each images
	smallboxR(8000);

});

function smallboxR(speed) {


	//append a LI item to the UL list for displaying caption
	$('ul.smallboxR').append('<li id="smallboxR-caption" class="caption"><div class="smallboxR-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.smallboxR li').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ul.smallboxR li:first').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	$('#smallboxR-caption h3').html($('ul.smallboxR a:first').find('img').attr('title'));
	$('#smallboxR-caption p').html($('ul.smallboxR a:first').find('img').attr('alt'));
		
	//Display the caption
	$('#smallboxR-caption').css({opacity: 0.7, bottom:0});
	
	//Call the gallery function to run the smallboxR	
	var timer = setInterval('smallRgallery()',speed);
	
	//pause the smallboxR on mouse over
	/*$('ul.smallboxR').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('biggallery()',speed);			
		}
	);*/
	
}

function smallRgallery() {


	//if no IMGs have the smallRshow class, grab the first image
	var current = ($('ul.smallboxR li.smallRshow')?  $('ul.smallboxR li.smallRshow') : $('#ul.smallboxR li:first'));

	//Get next image, if it reached the end of the smallboxR, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'smallboxR-caption')? $('ul.smallboxR li:first') :current.next()) : $('ul.smallboxR li:first'));
		
	//Get next image caption
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	

	//Set the fade in effect for the next image, smallRshow class has higher z-index
	next.css({opacity: 0.0}).addClass('smallRshow').animate({opacity: 1.0}, 2000);
	
	//Hide the caption first, and then set and display the caption
	$('#smallboxR-caption').slideToggle(300, function () { 
		$('#smallboxR-caption h3').html(title); 
		$('#smallboxR-caption p').html(desc); 
		$('#smallboxR-caption').slideToggle(1000); 
	});		

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

}
