jQuery(document).ready(function(){

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

});

var easingEffects = [
	'easeInQuad', 'easeOutQuad', 'easeInOutQuad',
	'easeInCubic', 'easeOutCubic', 'easeInOutCubic',
	'easeInQuart', 'easeOutQuart', 'easeInOutQuart',
	'easeInQuint', 'easeOutQuint', 'easeInOutQuint',
	'easeInSine', 'easeOutSine', 'easeInOutSine',
	'easeInExpo', 'easeOutExpo', 'easeInOutExpo',
	'easeInCirc', 'easeOutCirc', 'easeInOutCirc',
	'easeInElastic', 'easeOutElastic', 'easeInOutElastic',
	'easeInBack', 'easeOutBack', 'easeInOutBack',
	'easeInBounce', 'easeOutBounce', 'easeInOutBounce'	
	]

/**
 * Images
 */
function jGalleryImages(){

	jQuery('.jgallery-container').each(function(){
	
		var container = jQuery(this);
		var slider = container.find('.jgallery-slider');
		var images = container.find('.jgallery-image');
		var galleryID = jGalleryGetID(container);
		var imageSettings = jGalleryGetSettings(container);
		
		// Hide titles and captions
		slider.find('.jgallery-title-noscript').hide();
		slider.find('.jgallery-caption-noscript').hide();
		
		// Specifically declare dimensions for .jgallery-image in case links are enabled
		images.each(function(){
			if(jQuery(this).is('a')){
				jQuery(this).height(jQuery(this).find('img').outerHeight());
				jQuery(this).width(jQuery(this).find('img').outerWidth());
			}
		});
		
		var maxWidth = jGalleryMaxWidth(images);
		var maxHeight = jGalleryMaxHeight(images);
		
		// Set imageWidth and imageHeight to widest and tallest
		images.each(function(){
		
			if(jQuery(this).outerWidth() > maxWidth) maxWidth = jQuery(this).outerWidth();
			if(jQuery(this).outerHeight() > maxHeight) maxHeight = jQuery(this).outerHeight();
			
			// Position images according to transition type
			if(jQuery.inArray(imageSettings[0], easingEffects) != -1){
				jQuery(this).css({
					'position': 'absolute',
					'left': container.find('.jgallery-image').index(this) * maxWidth
				});
			} else {
				jQuery(this).css({
					'display': 'none',
					'position': 'absolute',
					'left': 0
				});
			}
		});
		
		// Set up container
		container.css({
			'position': 'relative',
			'width': container.find('.jgallery-image').width(),
			'height': maxHeight,
			'overflow': 'hidden'
		});
		
		// Set up slider
		slider.css({
			'position': 'relative',
			'width': maxWidth * slider.find('.jgallery-image').length,
			'height': maxHeight
		});
		
		// Make first image active
		container.find('.jgallery-image:first').addClass('active').show();
		
		// If slideshow is set to 'play', start slide show
		if(imageSettings[2] == 'play'){
			setInterval(function(){
				jGalleryLoader(galleryID, 'next');
			}, imageSettings[3]);
		}
	});
}

/**
 * Navigation
 */
function jGalleryNavigation(){

	jQuery('.jgallery-navigation').each(function(){
	
		var navigation = jQuery(this);
		var galleryID = jGalleryGetID(navigation);
		
		// Show navigation
		navigation.show();
		
		// Clicking 'next'
		navigation.find('.jgallery-next > a').click(function(event){
			jGalleryLoader(galleryID, 'next');
			event.preventDefault();
		});
		
		// Clicking 'previous'
		navigation.find('.jgallery-previous > a').click(function(event){
			jGalleryLoader(galleryID, 'previous');
			event.preventDefault();
		});
	});
}

/**
 * Titles
 */
function jGalleryTitles(){
	
	jQuery('.jgallery-titles').each(function(){
	
		var titles = jQuery(this);
		var galleryID = jGalleryGetID(titles);
		
		titles.height(jGalleryMaxHeight(titles.find('.jgallery-title')));
		
		// Display the first title
		titles.find('.jgallery-title:first').show();
		
	});
}

/**
 * Captions
 */
function jGalleryCaptions(){

	jQuery('.jgallery-captions').each(function(){
	
		var captions = jQuery(this);
		var galleryID = jGalleryGetID(captions);
		
		captions.height(jGalleryMaxHeight(captions.find('.jgallery-caption')));
		
		// Display the first caption and make active
		captions.find('.jgallery-caption:first').show();
	
	});
}

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

	jQuery('.jgallery-thumbnails').each(function(){
	
		// Show thumbnails
		var thumbnails = jQuery(this).show();
		var galleryID = jGalleryGetID(thumbnails);
		
		thumbnails.find('img').each(function(){
		
			var thumbnail = jQuery(this);
		
			// Fade thumbnails
			thumbnail.fadeTo(0, 0.3);
			
			// Make first thumbnail fully opaque
			if(thumbnail.is(':first-child')) thumbnail.fadeTo(0, 1).addClass('active');
			
			// Hovering over a thumbnail
			thumbnail.hover(
				function(){
					jQuery(this).fadeTo(100, 1);
				},
				function(){
					if(!jQuery(this).hasClass('active')){
						jQuery(this).fadeTo(100, 0.3);
					}
				}
			);
		
			// Clicking a thumbnail
			thumbnail.click(function(event){
			
				// Only if the thumbnail is not active
				if(!jQuery(this).hasClass('active')){
					
					// Get the thumbIndex
					var thumbIndex = thumbnails.find('img').index(this);
					
					// Run the loader					
					jGalleryLoader(galleryID, '', thumbIndex);
				}
				event.preventDefault();
			});
		});
	});
}

/**
 * Loads next, previous or selected items
 */
function jGalleryLoader(galleryID, direction, newImageIndex){

	var container = jQuery('.jgallery-container.jgallery-' + galleryID);
	var slider = container.find('.jgallery-slider');
	var images = container.find('.jgallery-image');
	var imageSettings = jGalleryGetSettings(container);
	var titles = jQuery('.jgallery-titles.jgallery-' + galleryID);
	var captions = jQuery('.jgallery-captions.jgallery-' + galleryID);
	var thumbnails = jQuery('.jgallery-thumbnails.jgallery-' + galleryID);
	
	var currentImage = container.find('.active');
	var currentImageIndex = '';
	
	var transition = imageSettings[0];
	var speed = parseInt(imageSettings[1]);
	
	currentImage.each(function(){
		currentImageIndex = container.find('.jgallery-image').index(this);
	});
	
	switch(direction){
		case 'next':
			if(currentImageIndex == images.length - 1){
				newImageIndex = 0;
			} else {
				newImageIndex = currentImageIndex + 1;
			}
			break;
		case 'previous':
			if(currentImageIndex == 0){
				newImageIndex = images.length - 1;
			} else {
				newImageIndex = currentImageIndex - 1;
			}
			break;
		default:
	}
	
	newImage = images.eq(newImageIndex);
	
	if(jQuery.inArray(transition, easingEffects) != -1){
		jQuery.easing.def = transition;
		slider.animate({
			'left': (newImageIndex * jGalleryMaxWidth(images)) * -1
		}, speed);	
	} else {
		switch(transition){
			case 'fade':
				currentImage.fadeOut(speed);
				newImage.fadeIn(speed);
				break;
			case 'flash':
				currentImage.hide();
				newImage.fadeIn(speed);
				break;
			default:
				currentImage.hide();
				newImage.show();
		}
	}
	
	if(jQuery.support.opacity){
		titles.find('.jgallery-title').eq(currentImageIndex).fadeOut(speed);
		titles.find('.jgallery-title').eq(newImageIndex).fadeIn(speed);
		captions.find('.jgallery-caption').eq(currentImageIndex).fadeOut(speed);
		captions.find('.jgallery-caption').eq(newImageIndex).fadeIn(speed);
	} else {
		titles.find('.jgallery-title').eq(currentImageIndex).hide();
		titles.find('.jgallery-title').eq(newImageIndex).show();
		captions.find('.jgallery-caption').eq(currentImageIndex).hide();
		captions.find('.jgallery-caption').eq(newImageIndex).show();
	}
	
	thumbnails.find('.jgallery-thumbnail').eq(currentImageIndex).removeClass('active').fadeTo(speed, 0.3);
	thumbnails.find('.jgallery-thumbnail').eq(newImageIndex).addClass('active').fadeTo(speed, 1);
	
	currentImage.removeClass('active');
	newImage.addClass('active');
}

/**
 * Return gallery ID
 */
function jGalleryGetID(item){
	return item.attr('class').split('jgallery-').slice(1, 2);
}

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

/**
 * Return the max width of passed set of items
 */
function jGalleryMaxWidth(items){
	var maxWidth = '';
	items.each(function(){
		if(jQuery(this).outerWidth() > maxWidth) maxWidth = jQuery(this).outerWidth();
	});
	return maxWidth;
}

/**
 * Return the max height of passed set of items
 */
function jGalleryMaxHeight(items){
	var maxHeight = '';
	items.each(function(){
		if(jQuery(this).outerHeight() > maxHeight) maxHeight = jQuery(this).outerHeight();
	});
	return maxHeight;
}