
	/* --------------------------------------------------
	Single.php
	-------------------------------------------------- */
	$(document).ready(function(){
	
		var animation, photoWidth, accordionContainer,
			mediumImage, thumbContainer, photoList, photos,
			prevButton, nextButton, activePhoto, fullSizeImage;
			
 		// Function that centers all the photos in each gallery
		var centerPhotos = function(){
		
			// Stop any existing animations
			if (animation) animation.stop();
		
			// Gather all the important meta data
			var link = $(photos[activePhoto]);
			var photo = link.children().first();
			var photo_width = link.attr('medium_w');
			var photo_height = link.attr('medium_h');
			
			// Change to the active photo
			animation = fullSizeImage.addClass('loading').find('img, p').animate({ opacity: 0, queue: false }, 350, function(){
				mediumImage
					.attr('src', '')
					.attr('title', link.attr('title'))
					.attr('alt', mediumImage.attr('title'))
					.attr('src', link.attr('medium_src'))
					.attr('width', photo_width)
					.attr('height', photo_height)
					.parent().attr('href', link.attr('href'))
					.parent().next().html('<strong>' + link.attr('artist') + '</strong>, ' + link.attr('title'));
			}).delay(2150).animate({ opacity: 1, queue: false }, 350, function(){
				fullSizeImage.removeClass('loading');
			});
			
			// Activate the current photo
			link.parent().addClass('current').siblings().removeClass('current');
			
			// Check the pagination
			if (activePhoto == 0) {
				prevButton.addClass('disabled').next().removeClass('disabled');
			} else if (activePhoto == photos.length - 1) {
				nextButton.addClass('disabled').prev().removeClass('disabled');
			} else {
				$('ul.gallery-nav li a').removeClass('disabled');
			}
		
			// Center the active photo
			if (activePhoto < 3) {
				photoList.animate({ left: 0 }, { duration: 500, queue: false });
			} else if (activePhoto >= 3 && activePhoto <= photos.length - 4) {
				photoList.animate({ left: (((activePhoto * photoWidth) - (thumbContainer.outerWidth() / 2) + (photoWidth / 2)) * -1) }, {
					duration: 500,
					queue: false
				});
			} else {
				photoList.animate({ left: ((((photos.length - 3) * photoWidth) - (thumbContainer.outerWidth() / 2) + (photoWidth / 2)) * -1) }, {
					duration: 500,
					queue: false
				});
			}
		};

		var prepImageGallery = function(){
			// Initialize all the global variables
			photoWidth = 105;
			accordionContainer = $('div.ui-accordion-content-active');
			fullSizeImage = accordionContainer.find('div.full-size-image');
			mediumImage = fullSizeImage.find('div.image img');
			thumbContainer = accordionContainer.find('div.thumbs');
			photoList = thumbContainer.find('ul');
			photos = photoList.find('li a');
			prevButton = accordionContainer.find('ul.gallery-nav li.previous a');
			nextButton = accordionContainer.find('ul.gallery-nav li.next a');
			activePhoto = photoList.find('li').index(photoList.find('li.current'));
			
			// Cancel the rest of the function if this isn't a gallery
			if (!accordionContainer.hasClass('gallery')) return;
			
			// Medium-Sized Image
			$('div.image a').fancybox({
				overlayOpacity: 0.75,
				overlayColor: '#000',
				autoScale: false,
				autoDimensions: false,
				cyclic: false,
				speedIn:	500,
				speedOut: 100,
				changeSpeed: 100,
				hideOnContentClick: true,
				padding: 7,
				opacity: true
			});
			
			// Thumbnail Photos
			photos.each(function(index, photo){
				$(photo).click(function(){
					activePhoto = index;
					centerPhotos();
					return false;
				});
			});
			
			// Previous Button
			prevButton.click(function(){
				if (!prevButton.hasClass('disabled')){
					activePhoto -= 1;
					centerPhotos();
					return false;
				}
			});
			
			// Next Button
			nextButton.click(function(){
				if (!nextButton.hasClass('disabled')){
					activePhoto += 1;
					centerPhotos();
					return false;
				}
			});
			
			// Center the Photos Once the Page Loads
			if (!accordionContainer.hasClass('loaded')) {
				accordionContainer.addClass('loaded');
				centerPhotos();
			}
		};
		
		// Turn on the accordion
		$("div#mainColumn").accordion({
			autoHeight: false,
			navigation: true,
			header: '.accordion-header',
			change: prepImageGallery
		});
		
		prepImageGallery();
		
		// Hook up the sidebar links
		$('div#sidebar ul#nav li a').each(function(index, link){
			$(link).click(function(){
				$(this).parent().addClass('current').siblings().removeClass('current');
				$("div#mainColumn").accordion('activate', index);
				return false;
			});
		});
		
		// Hook up the featured artist links
		$('div#sidebar ul#featuredArtists li a').click(function(){
		
			// Find the right artist from the list
			var artistImages = $('div#galleryImages div.thumbs').find('a[artist=' + $(this).html() + ']');
			activePhoto = $('div#galleryImages div.thumbs ul li a').index(artistImages.first());
			$('div#galleryImages div.thumbs ul li:eq(' + activePhoto + ')').addClass('current').siblings().removeClass('current');
			
			// Reset the accordion
			$("div#mainColumn").accordion('activate', 0);
			$("div#sidebar ul#nav li:eq(0)").addClass('current').siblings().removeClass('current');
			setTimeout(centerPhotos, 500);
			return false;
		});
	
	});
