
(function($){

	$(document).ready(function(){

		$('#content div.section').each(function(){

			// setup image gallery.
			$('div.image ul a', this)
				.each(function(){
					// preload image.
					var img = new Image();
					img.src = this.href;

					$(this)
						// add large photo to image container.
						.closest('div.image')
							.find('div.large')
								.append('<img src="'+ img.src +'" />')
							.end()
						.end()

						// thumbnail click handler.
						.click(function(){
							if (!$(this).hasClass('on')) {
								var thumbnailList = $(this).closest('ul');
								$('a.on', thumbnailList).removeClass('on');
	
								// get clicked index.
								var index = $('a', thumbnailList).index(this);
	
								// switch large photo.
								$(this)
									.addClass('on')
									.closest('div.image')
										.find('div.large img')
											.hide()
											.eq(index)
												.fadeIn()
											.end()
										.end()
									.end()
								;
							}
	
							return false;
						})
					;
				})
				// show first photo.
				.eq(0)
					.trigger('click')
				.end()
			;

			// next anchor handler.
			$('div.details p.next a', this).click(function(){
				$('#tab a[href=#'+ this.rel +']').trigger('click');
				return false;
			});
		});

	});

})(jQuery);

