$(document).ready(function() {
	$('div.contentBoxGallery').each(function() { registerGallery($(this)); });
});
var galleryAnimate=0;
function registerGallery(containerObj)
{
	if( $('a.galleryItemHidden',containerObj).length == 0 )
	{
		$('a.galleryNavButton',containerObj).hide();
	}
	else
	{
		$('a.galleryNavButtonLeft',containerObj).click(function() { galleryMove(containerObj,0); }).fadeTo(0,0.2);
		$('a.galleryNavButtonRight',containerObj).click(function() { galleryMove(containerObj,1); });
	}
}

function galleryMove(containerObj,dir)
{
	var visibleImages=$('a.galleryItem',containerObj).not('.galleryItemHidden');
	if(dir==0 && !galleryAnimate)
	{
		var nextCount=visibleImages.filter(':first').prevAll('.galleryItemHidden').length;
		if(nextCount > 0)
		{
			galleryAnimate=1;
			visibleImages.filter(':first').prev().css('margin-left','-128px').removeClass('galleryItemHidden').animate({ marginLeft:'0px' },300,null,function() { visibleImages.filter(':last').addClass('galleryItemHidden'); galleryAnimate=0; });
		}
		if(nextCount == 1)
		{
			$('a.galleryNavButtonLeft',containerObj).fadeTo(100,0.2);
		}
		$('a.galleryNavButtonRight',containerObj).fadeTo(100,1);
	}
	else if(dir==1 && !galleryAnimate)
	{
		var nextCount=visibleImages.filter(':last').nextAll('.galleryItemHidden').length;
		if(nextCount > 0)
		{
			galleryAnimate=1;
			visibleImages.filter(':last').next().removeClass('galleryItemHidden');
			visibleImages.filter(':first').animate({ marginLeft:'-128px' },300,null,function() { $(this).addClass('galleryItemHidden'); galleryAnimate=0; });
		}
		if(nextCount == 1)
		{
			$('a.galleryNavButtonRight',containerObj).fadeTo(100,0.2);
		}
		$('a.galleryNavButtonLeft',containerObj).fadeTo(100,1);
	}
}
