(function($) {
	
	var options = {
		thumbList: "ul.thumbs"
	}

	var methods = {
		init : function () {
		
			return this.each(function () {
				
				var $this = $(this),
					$stage = null,
					img_src = $( options.thumbList ).find("img").first().attr("src");
					$this.append('<div class="stage">Stage</div>');
					$(options.thumbList).find("img").first().parents("li").addClass("active")
			
					$stage = $this.find('div.stage');
			
					// Populate with first item

					$stage.html('<img src="' + img_src + '" alt="" class="active" style="opacity:1"/>');
		
				// Append Stage Area
				// Populate with first item
				// Bind click population
			
				$( options.thumbList  + ' img').bind("click", function() {
					
					var $img = $(this),
						src = $img.attr("src"),
						html = null;
					
					// If image matches current
					// image then don't do anything!
					if ( $stage.find("img.active").attr("src") === src ) {
					
						return false;
					}
					
					
					$(options.thumbList).find("li").removeClass("active");
					$(this).parents( "li" ).addClass( "active" );
						
					html = '<img src="' + src +'" alt="" class="active" />';
					
					/*
					// If there is an active image then
					// let's rearrange.
					*/
					if ($stage.find("img.active").length) {
						$stage.find("img.active").removeClass("active").addClass("last");
					}
					
					/*
					// Append our new content
					*/
					$stage.find("img.last").after( html );
					
					/*
					// Manage transitions
					*/
					setTimeout(function() {
						$stage.find("img.last").fadeOut(function() {
							$(this).remove();
						});
						$stage.find("img.active").hide().css({"opacity" : 1}).fadeIn();
					}, 100);		
					
				});
								
			});
		}
	};

	$.fn.galleryLA = function ( method ) {
	
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1));
		}
		else if ( typeof method === 'object' || !method ) {
			return methods.init.apply( this, arguments);
		}
		else {
			$.error("Method " + method + ' does not exist');		
		}
	
	}

})(jQuery);


