/* jGallery javascript, version 0.5 */

jQuery(document).ready(function(){

	// Settings
	var imageFadeSpeed = 350; // One of three predefined speeds ("slow", "normal", or "fast") or a number of milliseconds (e.g. 1000). Use 0 for no fade.
	var captionFadeSpeed = imageFadeSpeed;
	var thumbFadeSpeed = 150;
	var thumbFadeOpacity = 0.3; // Opacity setting for inactive thumbnails (a number from 0 to 1).

	/**
	 * Images
	 */
	jQuery('.jgallery-images').each(function(){
	
		// Define image list and captions
		var imageList = jQuery(this);
		
		var tallestImageHeight = 0;
		
		// find the height of the tallest image in this set
		imageList.children('span').each(function(){
			if(jQuery(this).children('a').children('img').height() > tallestImageHeight){
				tallestImageHeight = jQuery(this).children('a').children('img').height();
			}
		});
		
		// Hide all images, set position as absolute
		imageList.children('span').hide().css({ position: 'absolute' });
		
		// Show first image, add active class
		var firstImage = imageList.children('span:first').show().addClass('active');
		
		imageList.css({ height: tallestImageHeight });
		
	});
	
	/**
	 * Captions
	 */
	jQuery('.jgallery-captions').each(function(){
	
		var captionList = jQuery(this);
		
		// Display first caption, make active
		captionList.children('p:first').show().addClass('active');
		
		// Set caption height
		captionList.css({ height: 
			captionList.children('p:first').height() +
			parseInt(captionList.children('p:first').css('marginTop').replace('px', '')) +
			parseInt(captionList.children('p:first').css('marginBottom').replace('px', ''))
		});
	
	});
	
	/**
	 * Thumbnails
	 */
	jQuery('.jgallery-thumbnails').each(function(){
	
		var thumbList = jQuery(this).show();
		var galleryID = thumbList.attr('class').split('-').slice(1, 2);
		var imageList = jQuery('.jgallery-' + galleryID + '-images');
		
		thumbList.children('span').each(function(){
		
			var container = jQuery(this);
			container.children('img').fadeTo(0, thumbFadeOpacity);
			
			// Make first thumbnail fully opaque
			if(container.is(':first-child')) container.addClass('active').children('img').fadeTo(0, 1);
			
			// Hovering over an thumbnail
			container.children('img').hover(
				function(){ jQuery(this).fadeTo(thumbFadeSpeed, 1); },
				function(){
					if(!container.hasClass('active')){
						jQuery(this).fadeTo(thumbFadeSpeed, thumbFadeOpacity);
					}
				}
			);
			
			// Clicking on an thumbnail
			container.children('img').click(function(){
			
				var imageID = jQuery(this).parent('span').attr('class').split('-').slice(-1);

				if(!jQuery(this).parent('span').hasClass('active')){
					loadImage(galleryID, imageID);
				}
			});
		});
	});
	
	/**
	 * Navigation
	 */
	jQuery('.jgallery-navigation').each(function(){
	
		// Define and show navigation
		var navigation = jQuery(this).show();
		var galleryID = navigation.attr('class').split('-').slice(1, 2);
		
		// Define previous and next links
		var previous = '<span class="jgallery-previous">&laquo; <a href="#">Previous</a></span>';
		var next = '<span class="jgallery-next"><a href="#">Next</a> &raquo;</span>';
		
		// Append to navigation
		navigation.prepend(previous, next);
		
		// Clicking "next"
		navigation.children('.jgallery-next').children('a').click(function(event){
			loadImage(galleryID, '', 'next');
			event.preventDefault();
		});
		
		// Clicking "previous"
		navigation.children('.jgallery-previous').children('a').click(function(event){
			loadImage(galleryID, '', 'previous');
			event.preventDefault();
		});
	});
	
	/**
	 * Image loader
	 * @galleryID = The ID of the gallery in focus
	 * @newImageID = The ID of the thumbnails and images to match
	 * @direction = optional: 'next' or 'previous'
	 */
	function loadImage(galleryID, newImageID, direction){
	
		var imageList = jQuery('.jgallery-' + galleryID + '-images');
		var captionList = jQuery('.jgallery-' + galleryID + '-captions');
		var thumbList = jQuery('.jgallery-' + galleryID + '-thumbnails');
		
		var currentImage = imageList.children('span.active');
		var currentCaption = captionList.children('p.active');
		var currentThumb = thumbList.children('.active').children('img');
		
		var activeImageID = parseInt(currentImage.attr('class').replace(' active', '').split('-').slice(2));	
		
		switch(direction){
			case 'next':
				if(activeImageID == imageList.children('span').length){
					var newImage = imageList.children('.jgallery-image-1');
					var newCaption = captionList.children('.jgallery-caption-1');
					var newThumb = currentThumb.parent('span').siblings('span:first').children('img');
				} else {
					var newImage = imageList.children('.jgallery-image-' + (activeImageID + 1));
					var newCaption = captionList.children('.jgallery-caption-' + (activeImageID + 1));
					var newThumb = currentThumb.parent('span').next('span').children('img');
				}
				break;
			case 'previous':
				if(activeImageID == 1){
					var newImage = imageList.children('.jgallery-image-' + imageList.children('span').length);
					var newCaption = captionList.children('.jgallery-caption-' + imageList.children('span').length);
					var newThumb = currentThumb.parent('span').siblings('span:last').children('img');
				} else {
					var newImage = imageList.children('.jgallery-image-' + (activeImageID + -1));
					var newCaption = captionList.children('.jgallery-caption-' + (activeImageID - 1));
					var newThumb = currentThumb.parent('span').prev('span').children('img');
				}
				break;
			default:
				var newImage = imageList.children('.jgallery-image-' + newImageID);
				var newCaption = captionList.children('.jgallery-caption-' + newImageID);
				var newThumb = thumbList.children('span.jgallery-thumbnail-' + newImageID).children('img');
		}
		
		currentImage.removeClass('active').fadeOut(imageFadeSpeed);
		currentCaption.fadeOut(captionFadeSpeed).removeClass('active');
		currentThumb.fadeTo(thumbFadeSpeed, thumbFadeOpacity).parent('span').removeClass('active');
		
		newImage.addClass('active').fadeIn(imageFadeSpeed);
		newCaption.fadeIn(captionFadeSpeed).addClass('active');
		newThumb.fadeTo(thumbFadeSpeed, 1).parent('span').addClass('active');
	}

});