
	(function($) {
	
		$.fn.verticalSlideshow = function(options) {
		
			settings = $.extend({
				interval : 5,
		       	resumesAfterClick : false,
				entrySelector : "div.entry",
				dataStorage : "verticalSlideshow",
				upButtonSelector: "#up-button",
				downButtonSelector : "#down-button",
				animationTime : 1000,
				animationFunction : "swing"
		    }, options);


			return this.each(function() {
				
				$.data(this, settings.dataStorage, {
					images : $(this).find(settings.entrySelector), 
					index : -1,
					timer : null
				});
				
				_startSlideshow(this);
								
			});
			
			
			function _get_data(container) {
				return $.data(container, settings.dataStorage);
			}
			
			function _set_data(container, data) {
				$.data(container, settings.dataStorage, data);
			}
		
			function _goToItem(container, index, direction) {

				var data = _get_data(container);

				if ( index >= data.images.length ) {
					index = 0;							// First Item
				} else if ( index < 0 ) {
					index = data.images.length - 1;		// Last Item
				}
				
				_showItem(container, index, direction);

			}

			function _showItem(container, index, direction) {
				
				// console.info("showItem("+index+","+direction+")");
			
				var data = _get_data(container);
				data.animating = true;
				
				// Image to show now
				var _new = $(data.images[index]);
				if ( data.index >= 0 ) {
					var _old = $(data.images[data.index]);
					_old.css("z-index", 1);
				}
				
				
				var height = $(container).height();
				var _oldCSS;
				if ( direction == "up" ) {
					
					_new.css({
						top : height+"px",
						zIndex : 100
					});
					
					_oldCSS = "-"+height+"px";
					
				} else {
				
					_new.css({
						top : "-"+height+"px",
						zIndex : 100
					});
					
					_oldCSS = height+"px";
					
				}
				
			
				// Perform the animation
				if ( _old )	_old.animate({top:_oldCSS}, {duration:settings.animationTime, queue:false}, settings.animationFunction);
				
				_new.animate({top:0}, settings.animationTime, settings.animationFunction, function() {
				
					if ( _old ) {
						_old.css("top", "200px");
					}
					
					data.animating = false;
					data.index = index;
					_set_data(container, data);
					
				});

			}


			function _nextItem(container) {
				var data = _get_data(container);
				_goToItem(container, data.index+1, "up");
			}
			
			
			function _previousItem(container) {
				var data = _get_data(container);
				_goToItem(container, data.index-1, "down");
			}


			function _startTimer(container) {
				
				var slideshowTimer = setInterval(function() {
					_nextItem(container);
				}, settings.interval*1000);	
			
				// Store timer for this container
				var data = _get_data(container);
				data.timer = slideshowTimer;
				_set_data(container, data);
				
			}


			function _setupButtons(container) {
				
				// Up Button
				$(container).find(settings.upButtonSelector).click(function(e) {
					
					e.preventDefault();
					
					var data = _get_data(container);					
					if ( !data.animating ) {
					
						clearInterval(data.timer);
						if ( settings.resumesAfterClick == true ) {
							_startTimer(container);
						}
				
						_nextItem(container);						
						
					}
					
				});


				// Down Button
				$(container).find(settings.downButtonSelector).click(function(e) {
					
					e.preventDefault();
				
					var data = _get_data(container);
					if ( !data.animating ) {
										
						clearInterval(data.timer);
						if ( settings.resumesAfterClick == true ) {
							_startTimer(container);
						}
					
						_previousItem(container);
						
					}
					
				});

			}


			function _startSlideshow(container) {
				_setupButtons(container);
				_showItem(container,0,"up");
				_startTimer(container);
			}
		
		}

	})(jQuery);
