$(document).ready(function()
{
	if($('.gallery_container.gallery_overview a').length)
	{
		$('.gallery_container.gallery_overview a').fancybox({

			'titlePosition': "over",
			'overlayColor': '#000',
			'overlayOpacity': .5
		});
	}
	
	
	if($('.gallery_container.gallery_inline').length)
	{
		slideshow.init(gallery_slideshow_speed);
	}
	
	
})

var slideshow = {
	
	'imageCount': 0,
	'currentImage': 0,
	'speed': 3000,
	'transition': 1000,
	'showNextTimeout': false,
	
	'init': function(gallery_slideshow_speed)
	{
		// check if we have a slideshow on the page
		if($('ul.slideshow').length)
		{
			if(gallery_slideshow_speed)
				this.speed = gallery_slideshow_speed;
				
			// get image count
			this.imageCount = $('ul.slideshow li').length;
			
			// only start if we have more than one image
			if(this.imageCount > 1)
			{
				$('ul.slideshow li').css('overflow', 'hidden');
				$('ul.slideshow li').css('width', $('ul.slideshow').width());
				$('ul.slideshow li').css('position', 'absolute');

				// get slideshow height from first image
				var height = $('ul.slideshow li:first img').height();
				$('ul.slideshow').height(height);
				$('ul.slideshow li').height(height);

				// show next image on click
				$('ul.slideshow li').click(function()
				{
					slideshow.showNextImage();
				})

				// start slideshow
				this.showNextTimeout = setTimeout(function(){slideshow.showNextImage()}, slideshow.speed);
			}
		}
	},
	
	'showNextImage': function()
	{
		// clear timeout to prevent overlapping of click event and timeer
		clearTimeout(this.showNextTimeout);
		
		// calculate next image
		var next = (slideshow.currentImage + 1 < slideshow.imageCount) ? slideshow.currentImage + 1 : 0;
		
		// fade in/out images
		$('ul.slideshow li').eq(next).fadeIn(slideshow.transition);
		$('ul.slideshow li').eq(slideshow.currentImage).fadeOut(slideshow.transition);
		
		// set current image and fire next timeout
		slideshow.currentImage = next;
		this.showNextTimeout = setTimeout(function(){slideshow.showNextImage()}, slideshow.speed+slideshow.transition);
	}
}
