/* jGallery javascript, version 0.6 */

jQuery(document).ready(function(){

	// Prepare components
	jGalleryImages();
	jGalleryTitles();
	jGalleryCaptions();
	jGalleryNavigation();
	jGalleryThumbnails();

});

/**
	 * Master settings
	 */
var imageFadeOut = 350; // One of three predefined speeds ("slow", "normal", or "fast") or a number of milliseconds (e.g. 1000). Use 0 for no fade.
var imageFadeIn = imageFadeOut;
var titleFadeOut = imageFadeOut;
var titleFadeIn = imageFadeIn;
var captionFadeOut = titleFadeOut;
var captionFadeIn = titleFadeIn;
var thumbFadeOut = 150;
var thumbFadeIn = thumbFadeOut;
var thumbFadeOpacity = 0.3; // Opacity setting for inactive thumbnails (a number from 0 to 1).

/**
 * Images
 */
function jGalleryImages(){
	
	jQuery('.jgallery-images').each(function(){
	
		// Define image list and gallery id
		var imageList = jQuery(this);
		var galleryID = jGalleryID(imageList);
		
		// Get settings passed from jgallery.php
		var imageSettings = jGalleryGetSettings(imageList);
		
		// Hide titles and captions (they should only appear if javascript is disabled)
		imageList.find(jQuery('.jgallery-title').hide());
		imageList.find(jQuery('.jgallery-caption').hide());
		
		// Hide images, set position to absolute
		imageList.find('img').hide().css('position', 'absolute');
		
		// If height is set to variable
		if(imageSettings[0] == 'variable'){
		
			// Set imageList height to height of first image
			imageList.height(imageList.find('img:first').height());
		
		// Otherwise, height should be fixed at tallest image height
		} else {
			
			// Initialize tallest image height
			var tallestImageHeight = 0;
			
			// Find the height of the tallest image in this set
			imageList.find('img').each(function(){
				if(jQuery(this).height() > tallestImageHeight){
					tallestImageHeight = jQuery(this).height();
				}
			});
			
			// Set imageList height to the tallest image height
			imageList.height(tallestImageHeight);
		}
		
		// Show first image, add 'active' class
		imageList.find('img:first').show().addClass('active');
	
		// If slideshow is set to 'play', start slide show
		if(imageSettings[1] == 'play'){
			setInterval(function(){
				jGalleryLoader(galleryID, '', 'next', imageSettings);
			}, imageSettings[2]);
		}
	});
}

/**
 * Titles
 */
function jGalleryTitles(){

	jQuery('.jgallery-titles').each(function(){
	
		// Define title list and gallery id
		var titleList = jQuery(this);
		var galleryID = jGalleryID(titleList);
		
		// Initialize tallest title height
		var tallestTitleHeight = 0;
		
		// Find the heigh tof the tallest title
		titleList.children().each(function(){
			if(jQuery(this).height() > tallestTitleHeight) tallestTitleHeight = jQuery(this).height();
			
			// Display the first title, make active
			if(jQuery(this).is(':first-child')) jQuery(this).addClass('active').show();
		});
		
		// Set the title list height to the tallest title height
		titleList.height(tallestTitleHeight);
	});
}

/**
 * Captions
 */
function jGalleryCaptions(){
	
	jQuery('.jgallery-captions').each(function(){
	
		// Define caption list and gallery id
		var captionList = jQuery(this);
		var galleryID = jGalleryID(captionList);
			
		// Initialize tallest caption height
		var tallestCaptionHeight = 0;
		
		// Find the height of the tallest caption
		captionList.find('p').each(function(){
			if(jQuery(this).height() > tallestCaptionHeight) tallestCaptionHeight = jQuery(this).height();
		});
		
		// Set the captionList height to tallest caption height
		captionList.height(tallestCaptionHeight);
		
		// Display first caption and make active
		captionList.find('p:first').show().addClass('active');
	});
}

/**
 * Navigation
 */
function jGalleryNavigation(){
	
	jQuery('.jgallery-navigation').each(function(){
	
		// Define and show navigation
		var navigation = jQuery(this).show();
		var galleryID = jGalleryID(navigation);
		
		// Get imageList settings to pass to jGalleryLoader
		var imageSettings = jGalleryGetSettings(jQuery('.jgallery-images.jgallery-' + galleryID));
		
		// Clicking 'next'
		navigation.find('.jgallery-next > a').click(function(event){
			jGalleryLoader(galleryID, '', 'next', imageSettings);
			event.preventDefault();
		});
		
		// Clicking 'previous'
		navigation.find('.jgallery-previous > a').click(function(event){
			jGalleryLoader(galleryID, '', 'previous', imageSettings);
			event.preventDefault();
		});
	});
}

/**
 * Thumbnails
 */
function jGalleryThumbnails(){

	jQuery('.jgallery-thumbnails').each(function(){
	
		// Define and show thumbnails
		var thumbnailList = jQuery(this).show();
		var galleryID = jGalleryID(thumbnailList);
		
		// Get imageList settings to pass to jGalleryLoader
		var imageSettings = jGalleryGetSettings(jQuery('.jgallery-images.jgallery-' + galleryID));
		
		thumbnailList.find('img').each(function(){
		
			var thumbnail = jQuery(this);
		
			// Fade thumbnails
			thumbnail.fadeTo(0, thumbFadeOpacity);
			
			// Make first thumbnail fully opaque
			if(thumbnail.is(':first-child')) thumbnail.addClass('active').fadeTo(0, 1);
			
			// Hovering over a thumbnail
			thumbnail.hover(
				function(){ jQuery(this).fadeTo(thumbFadeIn, 1); },
				function(){
					if(!thumbnail.hasClass('active')){
						jQuery(this).fadeTo(thumbFadeOut, thumbFadeOpacity);
					}
				}
			);
			
			// Clicking on a thumbnail
			thumbnail.click(function(event){
			
				// Only if the thumbnail is not active
				if(!jQuery(this).hasClass('active')){
				
					// Find the image ID
					var imageID = jQuery(this).attr('class').split('-').slice(-1);
				
					// Pass to jGallery Loader
					jGalleryLoader(galleryID, imageID, '', imageSettings);
				}
				event.preventDefault();
			});
		});
	});
}

/**
 * Loads next or previous items
 */
function jGalleryLoader(galleryID, newImageID, direction, imageSettings){

	var imageList = jQuery('.jgallery-images.jgallery-' + galleryID);
	var titleList = jQuery('.jgallery-titles.jgallery-' + galleryID);
	var captionList = jQuery('.jgallery-captions.jgallery-' + galleryID);
	var thumbnailList = jQuery('.jgallery-thumbnails.jgallery-' + galleryID);
	
	var currentImage = imageList.find('.active');
	var currentTitle = titleList.find('.active');
	var currentCaption = captionList.find('.active');
	var currentThumbnail = thumbnailList.find('.active');
	
	var imageListLength = imageList.find('img').length;
	var activeImageID = parseInt(currentImage.attr('class').replace(' active', '').split('-').slice(2));
	
	var height = imageSettings[0];

	switch(direction){
		case 'next':
			if(activeImageID == imageListLength){
				var newImage = imageList.find('.jgallery-image-1');
				var newTitle = titleList.find('.jgallery-title-1');
				var newCaption = captionList.find('.jgallery-caption-1');
				var newThumbnail = thumbnailList.find('.jgallery-thumbnail-1');
			} else {
				var newImage = imageList.find('.jgallery-image-' + (activeImageID + 1));
				var newTitle = titleList.find('.jgallery-title-' + (activeImageID + 1));
				var newCaption = captionList.find('.jgallery-caption-' + (activeImageID + 1));
				var newThumbnail = thumbnailList.find('.jgallery-thumbnail-' + (activeImageID + 1));
			}
			break;
		case 'previous':
			if(activeImageID == 1){
				var newImage = imageList.find('.jgallery-image-' + imageListLength);
				var newTitle = titleList.find('.jgallery-title-' + imageListLength);
				var newCaption = captionList.find('.jgallery-caption-' + imageListLength);
				var newThumbnail = thumbnailList.find('.jgallery-thumbnail-' + imageListLength);
			} else {
				var newImage = imageList.find('.jgallery-image-' + (activeImageID + -1));
				var newTitle = titleList.find('.jgallery-title-' + (activeImageID + -1));
				var newCaption = captionList.find('.jgallery-caption-' + (activeImageID + -1));
				var newThumbnail = thumbnailList.find('.jgallery-thumbnail-' + (activeImageID + -1));
			}
			break;
		default:
			var newImage = imageList.find('.jgallery-image-' + newImageID);
			var newTitle = titleList.find('.jgallery-title-' + newImageID);
			var newCaption = captionList.find('.jgallery-caption-' + newImageID);
			var newThumbnail = thumbnailList.find('.jgallery-thumbnail-' + newImageID);
	}
	
	//alert(imageSettings[3]);
	
	// Fade out current items, remove 'active' class
	currentImage.removeClass('active').fadeOut(parseInt(imageSettings[3]));
	currentTitle.removeClass('active').fadeOut(titleFadeOut);
	currentCaption.removeClass('active').fadeOut(captionFadeOut);
	currentThumbnail.removeClass('active').fadeTo(thumbFadeOut, thumbFadeOpacity);
	
	// Fade in new items, add 'active' class
	newImage.addClass('active').fadeIn(parseInt(imageSettings[3]));
	newTitle.addClass('active').fadeIn(titleFadeIn);
	newCaption.addClass('active').fadeIn(captionFadeIn);
	newThumbnail.addClass('active').fadeTo(thumbFadeIn, 1);
	
	// If variableHeight is set, adjust imageList height to newImage height
	if(height == 'variable'){
		imageList.height(newImage.height());
	}
}

/**
 * Returns ID from item's class (jgallery-ID)
 */
function jGalleryID(item){
	return item.attr('class').split('jgallery-').slice(1, 2);
}

/**
 * Returns settings passed from jgallery.php in imageList class
 */
function jGalleryGetSettings(item){
	var settings_string = item.attr('class').split(' ').slice(2,3);
	return settings_string[0].split('-');
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	