var currentRow = 0;

$(document).ready(function() {
	// Enables scrollBar in #content 
	$('#scrollBar').jScrollPane({scrollbarWidth:12, scrollbarMargin:20, showArrows:true, verticalDragMaxHeight:80});
	
	// Set up caption overlays for large thumbnails when hovering
	$('#overlayCaptions img').each( function(index, thumb) {
		$(thumb).after('<div class="thumbCaption">'+$(thumb).attr('alt')+'</div>');
	});
	$('#overlayCaptions .thumbWrap').hover(
		function() {
			$(this).find('.thumbCaption').fadeIn(200);
		},
		function() {
			$(this).find('.thumbCaption').fadeOut(200);
		}
	);
	
	// Set up caption overlays for small subNav thumbnails when hovering
	$('.subNavRow img').wrap('<span class="thumbWrap">');
	$('.subNavRow img').each( function(index, thumb) {
		$(thumb).after('<div class="thumbCaption">'+$(thumb).attr('alt')+'</div>');
	});
	$('.subNavRow .thumbWrap').hover(
		function() {
			$(this).find('.thumbCaption').fadeIn(200);
		},
		function() {
			$(this).find('.thumbCaption').fadeOut(200);
		}
	);
	
	// Show the down arrow and hide all but the first row of thumbnails
	if( $('.subNavRow').length > 1 )
	{
		currentRow = $('.showMe').index();
		//alert( $('.showMe').index() );
		if( currentRow > 0 )
			$('#arrowUp').show();
		if( currentRow == $('.subNavRow').length-1 )
			$('#arrowUp').css('right','16px');
		if( currentRow < $('.subNavRow').length-1 )
			$('#arrowDown').show();
	}
	
	$('.subNavRow').css({'position' : 'absolute', 'top' : '40px'});
	$('.subNavRow').not('.showMe').hide();
	
	// Scroll thumbnails down when the down arrow is clicked
	$('#arrowDown').click( function() {
		current = $('.subNavRow').get(currentRow);
		$(current).hide('slide', {direction: 'up'}, 300);
		currentRow++;
		current = $('.subNavRow').get(currentRow);
		$(current).show('slide', {direction: 'down'}, 300);
		$('#arrowUp').show();
		
		if( currentRow == $('.subNavRow').length-1 ) {
			$('#arrowDown').hide();
			$('#arrowUp').css('right','16px');
		}
	});
	
	// Scroll thumbnails up when the up arrow is clicked
	$('#arrowUp').click( function() {
		current = $('.subNavRow').get(currentRow);
		$(current).hide('slide', {direction: 'down'}, 300);
		currentRow--;
		current = $('.subNavRow').get(currentRow);
		$(current).show('slide', {direction: 'up'}, 300);
		$('#arrowDown').show();
		$('#arrowUp').css('right','46px');
		
		if( currentRow == 0 ) {
			$('#arrowUp').hide();
		}
	});
	
	
	if( $('.insertShadowbox img').length > 0 )
	{
		var currentPiece;
		
		$('.insertShadowbox img').css('cursor','pointer');
		$('body').append('<div id="csb-container"><div id="csb-overlay"></div><div id="csb-wrapper"><div id="csb-body"></div><div id="csb-title"></div><div id="csb-nav"></div><div id="csb-next"></div><div id="csb-prev"></div><div id="csb-exit"></div></div>');
		$('#csb-overlay').css('opacity','0.8');
		
		// Create nav links
		$('.insertShadowbox img').each( function(index) { $('#csb-nav').append( '<span class="csb-nav-entry">'+(index+1)+'</span>'); });
		// Make nav links clickable
		$('#csb-nav .csb-nav-entry').click( function(event) {
			currentPiece = $('#csb-nav .csb-nav-entry').index(this);
			$('#csb-body').fadeOut(150, function() {
				switchImage();
			});
		});
		
		$('.insertShadowbox img').click( function(event) {
			$('#csb-body').hide();
			currentPiece = $('.insertShadowbox img').index(this);
			switchImage();
			$('#csb-container').fadeIn(150);
		});
		
		$('#csb-overlay').click( function(event) {
			$('#csb-container').fadeOut(150);
		});
		
		$('#csb-exit').click( function(event) {
			$('#csb-container').fadeOut(150);
		});
		
		$('#csb-next').click( function(event) {
			$('#csb-body').fadeOut(150, function() {
				currentPiece++;
				switchImage();
			});
		});
		
		$('#csb-prev').click( function(event) {
			$('#csb-body').fadeOut(150, function() {
				currentPiece--;
				switchImage();
			});
		});
		
		$('#clickToEnlarge').click( function(event) {
			$('#csb-body').hide();
			currentPiece = 0;
			switchImage();
			$('#csb-container').fadeIn(150);
		});
		
		function switchImage()
		{
			nextImage = $('.insertShadowbox img').get(currentPiece);
			var filename = '../images/enlarge/' + $(nextImage).attr('src').replace(/^.*[\/\\]/g, '');
			$('#csb-title').html( $(nextImage).attr('alt') );
			
			updateNav();
			loadAnImage(filename);
		}
		
		function loadAnImage(filename)
		{
			var anImage = new Image();
			
			$(anImage).load( function() {
				$(this).hide();
				$('#csb-body').css('background','url("'+filename+'") no-repeat center');
				$('#csb-body').fadeIn(150);
			}).attr('src',filename);
		}
		
		function updateNav()
		{
			if( currentPiece == 0 )
				$('#csb-prev').hide();
			else
				$('#csb-prev').show();
			
			if( currentPiece == ($('.insertShadowbox img').length - 1) )
				$('#csb-next').hide();
			else
				$('#csb-next').show();
			
			$('#csb-nav .csb-nav-entry').css('text-decoration','none');
			$('#csb-nav .csb-nav-entry').eq(currentPiece).css('text-decoration','underline');
		}
	}
	
});

$(window).load(function() {
	if( $('#scrollBar').length > 0 )
		$('#scrollBar').data('jsp').reinitialise();
});
