(function($) {

	// ALTERED: getItems, getSize
		
	$.fn.circular = function()  {
	
		return this.each(function() {			
 
			var api = $(this).scrollable(), 
				 items = api.getItems(), 
				 conf = api.getConf(), 
				 wrap = api.getItemWrap(), 
				 index = 0;				
				
			// too few items. no need for this plugin.
			if (items.length < conf.size) { return false; }
			

			// clone first visible elements and append them to the end			
			items.slice(0, conf.size).each(function(i) {
  				$(this).clone().appendTo(wrap).click(function()  {
					api.click(items.length + i);		
				});			
			});			
			
			// clone last set of elements to the beginning in reversed order
			var tail = $.makeArray(items.slice(-conf.size)).reverse();
			
  			$(tail).each(function(i) {
				$(this).clone().prependTo(wrap).click(function()  {
					api.click(-i -1);			
				});				
			});
			
			// skip the clones at the beginning
			api.seekTo(conf.size, 0);	


			// overridden scrollable API methods
			$.extend(api, {
				move: function(offset, time, fn) {
					
					var to = index + offset + conf.size;				
					var exceed = to > api.getSize() - conf.size;
					
					if (to < 0 || exceed) {
						var fix = index + conf.size + (exceed ? -items.length : items.length); 
						api.seekTo(fix, 0);					
						to = fix + offset;					  
					} 
								
					return api.seekTo(to, time, fn);
				},			
				
				begin: function(time, fn) {
					return this.seekTo(conf.size, time, fn);	
				},
				
				end: function(time, fn) {				
					return this.seekTo(items.length, time, fn);	
				},
	
				click: function(i, time, fn) {				
					var to = i - index;				
					to -= Math.floor(conf.size / 2);
					return this.move(to, time, fn);
				},
	
				getIndex: function() {
					if (index < 0) { return items.length + index; } 
					if (index >= items.length) { return 0; }
					return index;	
				},
	
				getPageAmount: function()  {
					return Math.ceil(items.length / conf.size);		
				},
				
				getPageIndex: function()  {
					var i = api.getIndex();
					return Math.ceil((i - i % conf.size) / conf.size);		
				}	
				
			});  
			
			// navi buttons are never disabled 
			api.onSeek(function(i) {		
				index = i - conf.size;
				
				// navi buttons are never disabled
				api.getNaviButtons().removeClass(conf.disabledClass);
			});	
			
				
		});
		
	}

		
})(jQuery);
		
