/*
*
*	Project: 		LoGallery
*
*	Version: 		1.1 (22th July 2009)
*
*	Author: 		Rolled by Losource (losource.net)
*
*	Description: 	In-place gallery, nice and small
*
*	License: 		GNU General Public License
*
*	Copyright:		2009 Andrew Flannery
*
*	This program is free software: you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*	GNU General Public License for more details.
*
*	You should have received a copy of the GNU General Public License
*	along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

(function($) {
	$.fn.loGallery = function(options) {
		var opts = $.extend({}, $.fn.loGallery.defaults, options);
		
		this.each(function(i, div) {
			var elImageContainer = $(div).find(opts.imageContainer);
			var elThumbsContainer = $(div).find('ul');
			var elCaptionContainer = $(div).find(opts.captionContainer);			
			if (!$(elCaptionContainer).length > 0) elCaptionContainer = $(div).nextAll(opts.captionContainer);
			var w = $(elImageContainer).width();
			var activeIndex;
			
			if (opts.adjustHeight) $(elImageContainer).height(opts.imageContainerInitialHeight + 'px');
			
			if (opts.useNextPreviousNavigation) {
				elPrevious = $('<a href="#" class="nav previous active" rel="previous"></a>');
				elNext = $('<a href="#" class="nav next active" rel="next"></a>');
				$(elImageContainer).append(elPrevious).append(elNext).find('a.nav').click(function() {
					if ($(this).hasClass('previous')) {
						$(elThumbsContainer).children().eq(activeIndex-1).find('a').click();
					}

					if ($(this).hasClass('next')) {
						$(elThumbsContainer).children().eq(activeIndex+1).find('a').click();
					}
					
					return false;
				});	
			}
			
			$(elThumbsContainer).find('a').click(function() {
				if ((opts.altBehaviorClass != '') && (opts.onAltBehavior != null) && ($(this).hasClass(opts.altBehaviorClass))) {
					return opts.onAltBehavior.call(this);
				} else {
					var link = $(this);
					var img = new Image;
					var oldImg = $(elImageContainer).find('img');
				
					$(elThumbsContainer).find('a.active').removeClass('active')
				
					$(this).addClass('active');
					activeIndex = $(elThumbsContainer).children().index($(this).parent());
				
					$(elImageContainer).addClass('loading');
				
					$(img).hide(); 
				
					$(oldImg).fadeOut('normal', function() {
						$(oldImg).remove();
					
						$(img).load(function() {
							if (opts.adjustHeight) $(elImageContainer).animate({ height: img.height });
							$(elCaptionContainer).append('<span>' + $(img).attr('alt') + '</span>');
							if (opts.forceCentre) $(this).css('margin-left', (w - img.width) / 2 + 'px');
							$(this).appendTo(elImageContainer).removeClass('loading').fadeIn();
						}).attr({
							src: $(link).attr('href'),
							alt: $(link).attr('title'),
							title: $(link).attr('title')
						});
					});
				
					if (opts.useNextPreviousNavigation) {
						if (activeIndex == 0) {	
							$(elPrevious).removeClass('active');
						} else {
							$(elPrevious).addClass('active');
						}
					
						if (activeIndex == ($(elThumbsContainer).children().length - 1)) {
							$(elNext).removeClass('active');
						} else {
							$(elNext).addClass('active');
						}
					}
					
					$(elCaptionContainer).children().hide().remove();
				
					return false;
				}											
			});
			
			$(elThumbsContainer).find('li a').not('.' + opts.altBehaviorClass).first().click();
		});
	};
	 
	$.fn.loGallery.defaults = {
		imageContainer: '.image',
		adjustHeight: true,
		imageContainerInitialHeight: 400,
		captionContainer: '.caption',
		useNextPreviousNavigation: true,
		forceCentre: true,
		altBehaviorClass: ''
	};
	
})(jQuery);