var leaveImgTimeOut, nextImgTimeOut;
$(document).ready(function()
{
	$('.galleryImageList li div ul.image li:not(.img)').hide();
	$('.galleryImageList li div:not(.selected)').fadeTo('fast', 0.5);
	getCurrentImageInfo();
	
	if($('.viewSelectedImage .caption').length && $('.viewSelectedImage .caption').find('li').html().length)
	{
		$('.viewSelectedImage .caption').fadeIn('slow', function()
		{
			leaveImgTimeOut = setTimeout(function() { $('.viewSelectedImage .caption').fadeOut('slow') }, 1000);
		});
	}
	
	$('.galleryImageList li div:not(selected,.disabled)').live('mouseenter', function()
	{
		$(this).fadeTo('fast', 1);
	});
	
	$('.galleryImageList li div:not(.selected)').live('mouseleave', function()
	{
		$(this).fadeTo('fast', 0.5);
	});
	
	$('.galleryImageList li div:not(.selected,.disabled)').live('click',function()
	{
		img = $(this).find('input:hidden').val();
		$('.galleryImageList .selected').removeClass('selected');
		$(this).addClass('selected');
		$('.galleryImageList li div').fadeTo('fast', 0.5);
		$('.galleryImageList li div.selected').fadeTo('fast', 1);
		getCurrentImageInfo();
		
		$('.viewSelectedImage img').fadeOut('fast', function()
		{
			$(this).attr('src', img).fadeIn('fast');

			if($('.viewSelectedImage .caption').find('li').html().length)
			{
				$('.viewSelectedImage .caption').fadeIn('slow', function()
				{
					leaveImgTimeOut = setTimeout(function() { $('.viewSelectedImage .caption').fadeOut('slow') }, 1000);
				});
			}
		});
	});
	
	$('.viewSelectedImage').bind('mouseenter, mousemove', function()
	{
		clearTimeout(leaveImgTimeOut);
		
		if($(this).children('.caption').find('li').html().length)
		{
			$(this).children('.caption').fadeIn('fast');
		}
	}).mouseleave(function()
	{
		$(this).children('.caption').fadeOut('fast');
	});
	
	$('.galleryImageList div.button').mouseover(function()
	{
		$(this).addClass('hover');
	}).mouseout(function()
	{
		$(this).removeClass('hover');
	}).click(function()
	{
		clearTimeout(nextImgTimeOut);
	});
	
	$('.galleryImageList div.button.left').click(function()
	{
		var list = $(this).parents('.galleryImageList');
		var selected = list.find('div.selected').parent();
		if(selected.prev().length)
		{
			var nextItem = selected.prev();
		}
		else
		{
			var nextItem = list.children('ul').children('li:last');
		}
		nextItem.children('div').click();
	});
	
	$('.galleryImageList div.button.right').click(function()
	{
		var list = $(this).parents('.galleryImageList');
		var selected = list.find('div.selected').parent();
		if(selected.next().length)
		{
			var nextItem = selected.next();
		}
		else
		{
			var nextItem = list.children('ul').children('li:first');
		}
		nextItem.children('div').click();
		
		if($('#home_galleryImageList').length)
		{
			nextImgTimeOut = setTimeout(function() { $('.galleryImageList div.button.right').click() }, 8000);
		}
	});
	
	if($('#home_galleryImageList').length)
	{
		nextImgTimeOut = setTimeout(function() { $('.galleryImageList div.button.right').click() }, 8000);
	}
});

function getCurrentImageInfo()
{
	html = '<ul>'
	
	$('.galleryImageList div.selected ul li:not(.img,.cat)').each(function()
	{
		html += '<li>'+$(this).html()+'</li>';
	});
	
	html += '</ul>';
	$('.viewSelectedImage .caption').html(html);
}
