$(document).ready(function(){
	
	if ( $("ul.sf-menu").length > 0 ) $("ul.sf-menu").superfish(); 
	$('#document h1:first-child, #document h2:first-child, #document h3:first-child, #document p:first-child, ').css('margin-top', 0);

	if (history.length < 1) {
		$('.buttonBack').hide();
	} else {
		$('.buttonBack').bind( 'click', function(e) {
			e.preventDefault();
			history.back();
		})
	}

	$('.titleBlock, .recordImage').hoverIntent( function() {
		fadeAndScroll( $(this).parent().find('.areaAffected'), 'in');
	}, function() {
		fadeAndScroll( $(this).parent().find('.areaAffected'), 'out');
	});
	
/*
	
	if($(".gallery").length>0 && $("#wrapper.index").length<1) { //gallery page - set up lightbox
		
		Shadowbox.init({
	    	// skip the automatic setup again, we do this later manually
	    	skipSetup: true
		});
		
		Shadowbox.setup("#document .gallery a", {
        	player: "img",
        	gallery: "RTP",
        	continuous: true
    	});
    	
	}
	
*/

	$("#document img, .gallery_wrapper img").each( function () {
		if ( $(this).parent('a').parent().attr('class') != 'gallery' ) {
		
			var thisImage = $(this);
			var url = thisImage.attr('src');
			var urlChunks = url.split('/');
			var altText = thisImage.attr( 'alt' );
			
			var fileName = urlChunks[ urlChunks.length -1 ];
			var galleryDir;
			
			if (urlChunks[ urlChunks.length -2 ] == 'thumbnails') galleryDir = urlChunks[ urlChunks.length -3 ];
			else galleryDir = urlChunks[ urlChunks.length -2 ];
			
			if ( imageCredits[galleryDir] != undefined && imageCredits[galleryDir][fileName] != undefined ) {
				if (altText != '') altText += ' - ';
				altText += imageCredits[galleryDir][fileName];
			}
			
			if (altText != '') thisImage.tipTip({content: altText});
		}
	});
	

	$("#document .gallery a").lightBox();
	resolveQuickLinks();
	
});

function fadeAndScroll(fadeItem, fadeDirection) {

	if (fadeDirection == 'in') {
		 fadeItem.animate({
		    opacity: 1,
		    height: 'toggle'
		  }, 500 );
	} else {
		 fadeItem.animate({
		    opacity: 0,
		    height: 'toggle'
		  }, 500 );
	}

}

function resolveQuickLinks() {
	
	$('#quickLinks ul ul').hide();
	$('#quickLinks li.selected').parents('ul').show();
	$('#quickLinks li.selected').children('ul').show();

}
