$(document).ready(function(){
	initGallery("#header-slideshow", true);
});
$(document).ready(function(){
	initGallery("#rightcol-slideshow", true);
});
/* - not required until left column needs to be slideshow'd
$(document).ready(function(){
	initGallery("#leftcol-slideshow", true);
});
*/
	
function initGallery(holder, _autoplay)
{
	var duration = 4500; // ms
	var gallery;
	var timer;
	var autoplay = _autoplay;
	var order = true;
	gallery = $(holder).find(".box-visual-image div");
	var caption = $(holder).find(".box-visual-caption ul li");
	gallery.number = $(gallery).length;
	
	// turn the remaining images on now the page load is complete
	
	if(gallery.number > 1)
	{
		gallery.current = 0;
		
		$(gallery).css({"opacity": 0, "display": "none"});
		$(gallery).eq(0).css({"opacity": 1, "display": "block"});
		
		if(autoplay)
		{
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
	function play()
	{
			var temp = gallery.current;
			var caption = $(holder).find(".box-visual-caption ul li");
			
			$(caption).eq(temp).toggleClass("active");
			$(gallery).eq(temp).animate({"opacity": 0}, 700, function() {  $(gallery).eq(temp).css({"display": "none"});  });
			if (order) 	{
				
				if(++gallery.current >= gallery.number)
					gallery.current = 0;
				} else {

					if(--gallery.current < 0)
						gallery.current = gallery.number-1;
				}
					
				$(caption).eq(gallery.current).toggleClass("active");
				$(gallery).eq(gallery.current).css({"display": "block"}).animate({"opacity": 1}, 700);
					
				clearTimeout(timer); 
	}
	function rotate()
	{
		if(autoplay)
		{
			play();
			timer = setTimeout(function()
			{
				rotate();
			}
			, duration);
		}
	}
}
