(function ($) {
	$.fn.gallerySlide = function () {
		var gallery = $(this);
		var height = $(this).find('li:first').height();
		$(this).find('li').hover(function () {
			$(this).find('img:first').stop(true).animate({'top': '0px'}, 500);
		}, function () {
			$(this).find('img:first').stop(true).animate({'top': '-' + height + 'px'}, 500);
		}).click(function () {
			gallery.find('li').removeClass('current');
			// set up a clean new stage
			$('#gallerySlideOverlay').remove();
			$('body').append('<div id="gallerySlideOverlay"></div>');
			$("#gallerySlideOverlay").css({'opacity': 0.8, 'height': $(document).height() + 'px'}).fadeIn('fast');
			$("body").append('<div id="gallerySlideContainer"></div>');
			var container = $("#gallerySlideContainer");
			$(this).addClass('current');
			$(this).find('li:first').addClass('current');
			
			// find first image and load it up
			var firstImg = $(this).find('li:first img:first');
			container.html('<img src="' + firstImg.attr('src') + '" />');
			
			// position it based on new height
			var top = ($(window).height()/2) - (container.height()/2);
			var left = ($(window).width()/2) - (container.width()/2);
			container.css({'top': top + 'px', 'left': left + 'px'});
			container.fadeIn('fast');
			
			// close button
			container.append('<a href="#" id="gallerySlideClose">x</a>');
			
			// content
			container.append('<div id="gallerySlideTitle">' + firstImg.attr('alt') + '</div><div id="gallerySlideContent">' + $(this).find('li:first').text() + '</div>');
			$("#gallerySlideTitle, #gallerySlideContent").css({'width': (container.innerWidth() - 30) + 'px'});
			
			// previous & next
			container.append('<div id="gallerySlidePrevious"></div><div id="gallerySlideNext"></div>');
			$("#gallerySlidePrevious").css({'top': (container.find('img:first').height()/2) + 'px', 'left': '2px'});
			$("#gallerySlideNext").css({'top': (container.find('img:first').height()/2) + 'px', 'right': '2px'});
			
			// disable #
			return false;
		});
		
		// Next Previous buttons
		$("#gallerySlideNext").live('click', function () {
			var swap = gallery.find('li.current li.current').next();
			if(swap.length == 0) { swap = gallery.find('li.current li:first'); }
			
			swapImage(swap);
		});
		
		$("#gallerySlidePrevious").live('click', function () {
			var swap = gallery.find('li.current li.current').prev();
			if(swap.length == 0) { swap = gallery.find('li.current li:last'); }
			
			swapImage(swap);
		});
		
		// close gallery
		$("#gallerySlideOverlay, #gallerySlideClose").live('click', function () {
			$("#gallerySlideOverlay, #gallerySlideContainer").fadeOut('fast', function () {
				$("#gallerySlideOverlay, #gallerySlideContainer").remove();
			});
			
			return false;
		});
		
		function swapImage(swap) {
			// update image and content
			var swapImg = swap.find('img:first');
			$("#gallerySlideContainer img").attr('src', swapImg.attr('src'));
			$("#gallerySlideTitle").html(swapImg.attr('alt'));
			$("#gallerySlideContent").html(swap.text());
			
			gallery.find('li.current li').removeClass('current');
			swap.addClass('current');
			
			// update position height etc...
			$("#gallerySlideTitle, #gallerySlideContent").css({'width': ($("#gallerySlideContainer img").width() - 30) + 'px'});
		}
	};
	
})(jQuery);
