(function($) {
	'use strict';
	$.fn.gallery = function(options) {
		var defaults = {
			current: 0,
			classes: ''
		};
		options = $.extend(defaults, options);
		return this.each(function() {
			var $gallery = $(this),
				$galleryItems = $gallery.children();
			$gallery
				.addClass('gallery')
				.addClass(options.classes)
				.height($galleryItems.height())
				.attr('tabindex', 1);
			$galleryItems
				.addClass('gallery-item')
				.eq(options.current)
					.addClass('current');
			$gallery.on('keyup', function(event) {
				var $current = $('.current', $gallery);
				var index = $current.index(),
					newIndex;
				$current.removeClass('current');
				if (index === $galleryItems.length - 1 || index === 0) {
					$gallery.trigger('end-of-world');
				}
				switch (event.which) {
					case 39:
						newIndex = index === $galleryItems.length - 1 ? 0 : index + 1;
						break;
					case 37:
						newIndex = index === 0 ? $galleryItems.length - 1 : index - 1;
						break;
				}
				
				$galleryItems.eq(newIndex).addClass('current');
			});
		});
	}
})(jQuery);