var changingImageMutex = 0;
function handleThumb( Event )
{
	var galleryId = $(this).parents( "div.pgMacro" )[0].id.slice( 7 );
	// get the index of 'this' within the list of thumbs
	var newIndex = $( "div#pgMacro" + galleryId + " p.pgThumbHolder a img" ).index( $(this)[0] );
	changeImage( galleryId, currentImage[galleryId], newIndex );
	return false;
}
function handleGalleryArrow( Event )
{
	var dir = Event.target.id.slice( 0, 4 );
	var galleryId = parseInt( Event.target.id.slice( 4 ) );
	var index = currentImage[galleryId];

	if( dir == "next" )
	{	
		changeImage( galleryId, index, index+1 );
	}
	else if( dir == "prev" )
	{
		changeImage( galleryId, index, index-1 );
	}
	return false;
}
function handleMainPhoto( Event )
{
	var galleryId = $(this).parents( "div.pgMacro" )[0].id.slice( 7 );
	var index = currentImage[galleryId];
	if( (index + 1) < imageCount[galleryId] )
	{
		changeImage( galleryId, index, index+1 );
	}
}
function changeImage( galleryId, currentIndex, newIndex )
{
	if( currentIndex == newIndex )
	{
		return;
	}
	// Make sure we didn't get another click during animation
	if( changingImageMutex == 1 )
	{
		return;
	}
	else
	{
		changingImageMutex = 1;
	}
	var galleryString = "div#pgMacro" + galleryId;
	
	// hide the current image and caption, show the new one
	$(galleryString + " div.pgContainer div:eq(" + currentIndex + "), " + galleryString + " div.pgCaption div:eq(" + currentIndex + ")").
		fadeOut( 
				"fast", 
				function() {
					// highlight/dehighlight the appropriate thumbnails
					changeThumbHighlight( galleryId, currentIndex, newIndex );
					// now fade in
					$(galleryString + " div.pgContainer div:eq(" + newIndex + "), " + galleryString + " div.pgCaption div:eq(" + newIndex + ")").fadeIn( "fast", function() {changingImageMutex=0;} );
				}
		);				
	// enable/disable Next/Prev links as needed
	if( newIndex == 0 )
	{
		$(galleryString + " span.pgPrev a").hide();
	}
	else
	{
		$(galleryString + " span.pgPrev a").show();
	}
	if( (newIndex + 1) == imageCount[galleryId] )
	{
		$(galleryString + " span.pgNext a").hide();
	}
	else 
	{
		$(galleryString + " span.pgNext a").show();
	}
	currentImage[galleryId] = newIndex;
	setCountText( galleryString, galleryId );
}
function changeThumbHighlight( galleryId, currentIndex, newIndex )
{
	var thumbs = "div#pgMacro" + galleryId + " p.pgThumbHolder a img";
	// Clear highlight on existing
	$(thumbs + ":eq(" + currentIndex + ")").removeClass( "pgThumbHighlight" ).addClass( "pgThumb" );
	// Highlight the new one
	$(thumbs + ":eq(" + newIndex + ")").removeClass( "pgThumb" ).addClass( "pgThumbHighlight" );
}
function setCountText( galleryString, galleryId )
{
	$(galleryString + " p.pgPhotocount").html( (currentImage[galleryId] + 1) + " of " + (imageCount[galleryId]) + " photos&nbsp;" );		
}
function handleThumbArrow( Event )
{
	var dir = Event.target.id.slice( 0, 4 );
	var galleryId = parseInt( Event.target.id.slice( 9 ) );	

	if( dir == "next" )
	{
		scrollThumbs( galleryId, "+=360px" );
	}
	else if( dir == "prev" )
	{
		scrollThumbs( galleryId, "-=360px" );
	}
	return false;
}
function scrollThumbs( galleryId, offset )
{
	$( "div#pgMacro" + galleryId + " p.pgThumbHolder" ).scrollTo( offset, 1000, { axis:"x" } );
}
$(function(){
	$("span.pgPrev a, span.pgNext a").click( handleGalleryArrow );	
	$("img.pgArrows").click( handleThumbArrow );
	$(".pgThumbHolder img").click( handleThumb );
	$(".pgContainer img").click( handleMainPhoto );	
});