$(document).ready(function() {

	autoSize();
  	if ($('#thumbs').length) { setupThumbs('#thumbs'); }
	setupVideoPlayer('video_player');

});
 
// Once the entire document is loaded, resize everything to fit
$(window).load(function () {
 	resizeImage();
});


// Automatically set the size of the main image to fit inside the browser
autoSize = function () {

  	var isResizing = false;
	
	resizeImage = function() {
		if (!isResizing) {
			isResizing = true;
			
			$('#content_inner').height($('#content_wrapper').height());
			
			if ($('#main_image').length) {
				// Get the aspect ratio of the main image
				var img_aspect = $('img', '#main_image .current').attr('width') / $('img', '#main_image .current').attr('height');
				// Store the current height of the available space
				var space_height = $('#content_inner').height() - 60;
				// Get the aspect ratio of the space available for the image
				var space_aspect = $('#main_image').width() / space_height;
				
				// Resize the image to fit in the space
				if (space_aspect > img_aspect) {
					$('img', '#main_image .current').height(space_height).width('auto').css('padding-top', 0);
				} else {
					$('img', '#main_image .current').width($('#main_image').width()).height('auto');
					$('img', '#main_image .current').css('padding-top', ((space_height - $('img', '#main_image .current').height()) / 2));
				}
										
				// Move the caption over to line up with the photo
				$('#main_image .current .caption').width($('#main_image .current img').width());
			}
			if ($('#main_video').length) {
				// Store the current height of the available space
				var space_height = $('#content_inner').height() - 40;
				// Get the aspect ratio of the space available for the image
				var video_height = $('#main_video #video_player').height();
				
				// Resize the image to fit in the space
				if (space_height > video_height) {
					var offset = (space_height - video_height) / 2;
					$('#main_video').css('padding-top', offset);
				} else {
					$('#main_video').css('padding-top', 0);
				}
			}
			if ($('.scroll').length) {
				// Store the current height of the available space
				var space_height = $('#content_inner').height() - 60;
				
				// Resize the thumbnail container
				$('.jScrollPaneContainer').height(space_height).width('auto');
				$('.scroll').height(space_height).width('auto');
				$('.scroll').jScrollPane({maintainPosition: true});		
			}
		
			isResizing = false;
		}
	};
	
	$('.scroll').jScrollPane({reinitialiseOnImageLoad: true});
	
	// Resize again once the image is fully loaded. Fixes Safari problem
	$('#main_image .current img').load(function() { resizeImage(); });
	$(window).resize(function() { resizeImage() });
	resizeImage();

}


// Various functionality for the thumbnails
setupThumbs = function (element) {
	
	// Store the scrolling sidebar element
	var scroller = $('.scroll');
	  	
	// Fade the whole section in and out if it's a sidebar
	if ( $(element).hasClass('sidebar') ) {
		// Fade them out initially
		$(element).parent().fadeTo("medium", 0.3);
		
		// Fade the entire section in on mouseover
		$(element).parent().hover(
			function() { $(this).stop().fadeTo(400, 1); },
			function() { $(this).stop().fadeTo(1200, 0.25); }
		);
	}
	
	// Fade individual thumbnails on mouseover
	$('img', element).hover(
		function() { $(this).fadeTo(250, 0.75); },
		function() { $(this).fadeTo(350, 1.0); }
	);
	
	// Show a specified image in the #main_image container
	displayImage = function (link) {
		// Hide all images
		$('li', '#main_image').hide().removeClass('current');
		
		// Show the selected image
		$(link.attr('href'), '#main_image').fadeIn(1000).addClass('current');
		
	  	resizeImage();
	  	
	  	// Scroll the sidebar to the current item
	  	if (scroller.height() > scroller.parent().height()) {
			scroller[0].scrollTo(link.position().top - 110);
		}	
	}
	
	// Show first image
	displayImage($('a:has(img):first', '#sidebar_wrapper'));
	
	// Change the thumnail links to load in place
	$('li > a', '#sidebar_wrapper').click(function() {
		displayImage($(this));
	});
	
}


// Set up the media player
setupVideoPlayer = function (element) {
	if ($('#'+element).length) {
		flowplayer(element, "http://www.williamnoland.com/scripts/flowplayer-3.0.5.swf", { 
			clip: {
				autoPlay: true,
				autoBuffering: true
			},
			plugins: { 
			    controls: { 
			        url: 'flowplayer.controls-3.0.3.swf', 
			         
			        // display properties 
			        backgroundColor: '#222222', 
			        backgroundGradient: 'none',
			        progressColor: '#666666',
			        bufferColor: '#444444',
			        sliderColor: '#333333',
			        buttonColor: '#666666',
			        buttonOverColor: '#888888',
			         
			        // controlbar specific configuration  
			        fontColor: '#efefef',
			        timeColor: '#bbbbbb',  
			        autoHide: 'never', 
			        
			        fullscreen:false
			    } 
			}
		});
	}
}
