(function($) {
	
	// plugin definition	
	$.fn.banner_carousel = function (options) {
		var opts = $.extend({}, $.fn.banner_carousel.defaults, options);
		
		
		return this.each(function () {
			var auto_scrolling = true,
			
					$wrapper = $(this),
					$list = $wrapper.find('> ul'),
					$list_items = $list.find('> li'),
					$first_item = $list_items.filter(':first'),
					
					wrapper_height = $wrapper.innerHeight(),
					list_height = $list.outerHeight(),
					item_height = $first_item.outerHeight(), // includes padding

					nr_of_list_items = $list_items.length,
					visible_list_items = Math.ceil(wrapper_height / item_height),
					reset_position = 0;
					
				if (nr_of_list_items > visible_list_items) {
					add_clones();
					auto_scroll();
				}
				
				function add_clones () {
					if (opts.direction == ":up") {
						// clone the n first items to the end of the list
						$list_items.filter(":last").after($list_items.slice(0, visible_list_items	).clone().addClass('cloned'));
					} else {
						// clone the n last items to the beginning of the list
						$list_items.filter(":first").before($list_items.slice(- visible_list_items).clone().addClass('cloned'));
						// change the reset position for the down direction
						nr_of_list_items = $list.find('> li').length // length with clones
						reset_position = item_height * nr_of_list_items;
						// $wrapper.scrollTop(reset_position);
					}
				}
				
				function go_to_next () {
					var op, ext;
					if (opts.direction == ":up") {
						op ='+=';
						ext = nr_of_list_items * item_height;
					} else {
						op = '-=';
						ext = 0;
					}
			
					$wrapper.animate({scrollTop: op + item_height }, opts.speed, function() {
						if ($wrapper.scrollTop() == ext) { // reset...
							$wrapper.scrollTop(reset_position);
						}
					});
				}
				
				function auto_scroll () {
					setInterval(function () {
						if (auto_scrolling) {
							go_to_next();
						}
					}, opts.wait);
				}
				
				// set auto_scrolling variable to false on hover and true on out
				$wrapper.mouseover(function () { 
					auto_scrolling = false; 
				}).mouseout(function () { 
					auto_scrolling = true 
				});
		});	
	}

	
	// plugin defaults
	$.fn.banner_carousel.defaults = {
		direction: ':up',
		speed: 500,
		wait: 2000
	};
	
	
})(jQuery);
