$(document).ready(function()
{
	$('.accommodation_gallery').each(make_accomodation_gallery);
});

function make_accomodation_gallery()
{
	var item_count = $('.gallery_link', this).length;
	
	$('.gallery_link', this).fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		overlayShow: true,
		autoDimensions: false,
		titlePosition: 'inside',
		onComplete: function()
		{
			if ($('.flowplayer_fancybox', '#fancybox-inner').length == 1) {
				show_fancybox_flowplayer.apply($('.flowplayer_fancybox', '#fancybox-inner'));
			}
		},
		onStart: function(array, index)
		{
			var item = array[index];
			var gallery = $(item).closest('.accommodation_gallery');
			var gallery_item = $(item).closest('.gallery_item');

			var currentItem = parseInt($('.current_item', gallery).text());
			var newItem = $('.gallery_item', gallery).index(gallery_item);
			
			if (currentItem != newItem)
			{
				$('.gallery_item:eq('+currentItem+')', gallery).hide();
				$('.gallery_item:eq('+newItem+')', gallery).show();
				$('.current_item', gallery).text(newItem);
				
				$('.previous_link', gallery).toggle(newItem > 0);
				$('.next_link', gallery).toggle(newItem < item_count - 1);
			}
		}
	});
	
	$('.next_link, .previous_link', this).click(function()
	{
		var gallery = $(this).closest('.accommodation_gallery');
		if ($(gallery).hasClass('busy')) {
			return false;
		}
		$(gallery).addClass('busy');
	
		var step;
		if ($(this).hasClass('next_link')) {
			step = 1;
		}
		else {
			step = -1;
		}
	
		var current = parseInt($('.current_item', gallery).text());
		$('.gallery_item:eq('+current+')', gallery).fadeOut('fast', function()
		{
			current = current + step;
			$('.gallery_item:eq('+current+')', gallery).fadeIn('fast', function()
			{
				$('.current_item', gallery).text(current);
				$('.previous_link', gallery).toggle(current > 0);
				$('.next_link', gallery).toggle(current < item_count - 1);
				$(gallery).removeClass('busy');
			});
		});
		
		return false;
	});
};
