(function($){
	
	// Requires jquery.scrollTo.js

	$.fn.extend({
		kovlerVideoList:function(options) {

			var opts = $.extend({	
				scrollItems: 4,
				scrollDuration: 500,
				displayedItems: 4
			}, options);
		
			return this.each(function() {
				var videos = $(this);
				var list = videos.find("ul");
				
				var current = 0;
				var count = list.find("li").length ;
				
				var scroller = videos.find("#scroll")
				
				if (count <= opts.displayedItems) scroller.css({display:"none"});
		
				scroller.find(".up").click(function (){

					if (current - opts.scrollItems > 0) {
						current -= opts.scrollItems;
					} else {
						current = 0;
					}
	 				
					scrollToItem(current);
					return false;
				})

				scroller.find(".down").click(function (){

					if (current + opts.scrollItems <= count - opts.scrollItems) {
						current += opts.scrollItems;
					} else if (current + opts.displayedItems <= count) {
						current += count - (current + opts.displayedItems);
					}
					
					scrollToItem(current);	
					return false;
				})
				
				var scrollToItem = function (item) {
					list.scrollTo("li:eq("+item+")", opts.scrollDuration);
				}
				
				// Scroll to current video on page load
				list.scrollTo(".current", opts.scrollDuration);				
			});	
		}
	});

})(jQuery);
