// http://flowplayer.org/tools/scrollable.html
(function($) {
	// plugin initialization
	$.fn.extend({
		scrollable: function(arg1, arg2, arg3) { 
			return this.each(function() {
				if (typeof arg1 == "string") {
					var el = $.data(this, "scrollable");
					el[arg1].apply(el, [arg2, arg3]);
					
				} else { 
					new $.scrollable(this, arg1, arg2);
				}
			});
		}		
	}); 
	$.scrollable = function(el, opts) {   
		$.data(el, "scrollable", this); 
		this.init(el, opts); 
	};
	$.extend($.scrollable.prototype, { 
		init: function(el, config)  {
			var self = this;  
			var opts = {								
				size: 1,
				horizontal:false,				
				activeClass:'active',
				speed: 500,
				onSeek: null,
				// jquery selectors
				items: '.items',
				prev:'.prev',
				next:'.next',
				navi:'.navi',
				naviItem:'span'
			}; 
	
			this.opts = $.extend(opts, config); 			
			var root = this.root = $(el);			
			var itemRoot = $(opts.items, root);			
			
			if (!itemRoot.length) itemRoot = root;			
			itemRoot.css({position:'relative', overflow:'hidden', visibility:'visible'});
			itemRoot.children().wrapAll('<div class="__scrollable" style="position:absolute;direction:rtl;right:0px;"/>');
			this.wrap = itemRoot.children(":first");
			this.wrap.css("width", "2000000px").after('<br clear="all"/>');
			this.items = this.wrap.children();
			this.index = 0;
            
		    itemRoot.width((opts.size * (this.items.eq(1).offset().left - this.items.eq(0).offset().left) -2));
			
			this.activeIndex = 0;
			
			$(opts.prev, root).click(function() {self.prev() });
			
            $(".doScroll").each(function(i){
               
	            var item = $(this); 
	            item.click(function() {
                    item.removeClass(opts.activeClass);
		            item.addClass(opts.activeClass);
		            self.setPage(item.attr("page"));
	            });   
	        });
		},
		
		seekTo: function(index, time) {
            if (index < 0) index = 0;			
			index = Math.min(index, this.items.length - this.opts.size);
			var item = this.items.eq(index);			
			if (item.size() == 0) return false; 			
			this.index = index;
			var a = 966 * index * -1;
			this.wrap.animate({right: a}, time);//time || this.opts.speed
			if ($.isFunction(this.opts.onSeek)) {
				this.opts.onSeek.call(this.getStatus());
			}
			return true; 
		},
		move: function(offset, time) {
			this.seekTo(this.index + offset, time);
		},
		next: function(time) {
			this.move(1, this.opts.speed);	
		},
		
		prev: function(time) {
			this.move(-1, this.opts.speed);	
		},
		setPage: function(index, time) {
			this.seekTo(this.opts.size * index, time);
		},
		begin: function(time) {
			this.seekTo(0, time);	
		},
		
		end: function(time) {
			this.seekTo(this.items.size() - this.opts.size, time);	
		}
	});  
	
})(jQuery);



