(function(a){a.fn.extend({scrollable:function(d,c,b){return this.each(function(){if(typeof d=="string"){var e=a.data(this,"scrollable");e[d].apply(e,[c,b])}else{new a.scrollable(this,d,c)}})}});a.scrollable=function(b,c){a.data(b,"scrollable",this);this.init(b,c)};a.extend(a.scrollable.prototype,{init:function(f,d){var c=this;var g={size:5,horizontal:false,activeClass:"active",speed:300,onSeek:null,items:".items",prev:".prev",next:".next",navi:".navi",naviItem:"span"};this.opts=a.extend(g,d);var b=this.root=a(f);var e=a(g.items,b);if(!e.length){e=b}e.css({position:"relative",overflow:"hidden",visibility:"visible"});e.children().wrapAll('<div class="__scrollable" style="position:absolute"/>');this.wrap=e.children(":first");this.wrap.css(g.horizontal?"width":"height","200000em").after('<br clear="all"/>');this.items=this.wrap.children();this.index=0;if(g.horizontal){e.width(g.size*(this.items.eq(1).offset().left-this.items.eq(0).offset().left)-2)}else{e.height(g.size*(this.items.eq(1).offset().top-this.items.eq(0).offset().top)-2)}if(a.isFunction(a.fn.mousewheel)){b.bind("mousewheel.scrollable",function(h,i){c.move(-i,50);return false})}a(window).bind("keypress.scrollable",function(h){if(a(h.target).parents(".__scrollable").length){if(g.horizontal&&(h.keyCode==37||h.keyCode==39)){c.move(h.keyCode==37?-1:1);return false}if(!g.horizontal&&(h.keyCode==38||h.keyCode==40)){c.move(h.keyCode==38?-1:1);return false}}return true});this.items.each(function(i,h){a(this).bind("click.scrollable",function(){c.click(i)})});this.activeIndex=0;a(g.prev,b).click(function(){c.prev()});a(g.next,b).click(function(){c.next()});a(g.navi,b).each(function(){var l=a(this);var h=c.getStatus();if(l.is(":empty")){for(var j=0;j<h.pages;j++){var k=a("<"+g.naviItem+"/>").attr("page",j).click(function(){var i=a(this);i.parent().children().removeClass(g.activeClass);i.addClass(g.activeClass);c.setPage(i.attr("page"))});if(j==0){k.addClass(g.activeClass)}l.append(k)}}else{l.children().each(function(m){var n=a(this);n.attr("page",m);if(m==0){n.addClass(g.activeClass)}n.click(function(){n.parent().children().removeClass(g.activeClass);n.addClass(g.activeClass);c.setPage(n.attr("page"))})})}})},click:function(c){var e=this.items.eq(c);var b=this.opts.activeClass;if(!e.hasClass(b)&&(c>=0||c<this.items.size())){var d=this.items.eq(this.activeIndex).removeClass(b);e.addClass(b);this.seekTo(c-Math.floor(this.opts.size/2));this.activeIndex=c}},getStatus:function(){var b=this.items.size();var c={length:b,index:this.index,size:this.opts.size,pages:Math.floor(b/this.opts.size),page:Math.floor(this.index/this.opts.size)};return c},seekTo:function(c,i){if(c<0){c=0}c=Math.min(c,this.items.length-this.opts.size);var e=this.items.eq(c);if(e.size()==0){return false}this.index=c;if(this.opts.horizontal){var h=this.wrap.offset().left-e.offset().left;this.wrap.animate({left:h},i||this.opts.speed)}else{var g=this.wrap.offset().top-e.offset().top;this.wrap.animate({top:g},i||this.opts.speed)}if(a.isFunction(this.opts.onSeek)){this.opts.onSeek.call(this.getStatus())}var d=a(this.opts.navi,this.root);if(d.length){var b=this.opts.activeClass;var f=Math.round(c/this.opts.size);d.children().removeClass(b).eq(f).addClass(b)}return true},move:function(c,b){this.seekTo(this.index+c,b)},next:function(b){this.move(1,b)},prev:function(b){this.move(-1,b)},movePage:function(c,b){this.move(this.opts.size*c,b)},setPage:function(b,c){this.seekTo(this.opts.size*b,c)},prevPage:function(c){var b=Math.floor(this.index/this.opts.size);this.seekTo(this.opts.size*(b-1),c)},nextPage:function(c){var b=Math.floor(this.index/this.opts.size);this.seekTo(this.opts.size*(b+1),c)},begin:function(b){this.seekTo(0,b)},end:function(b){this.seekTo(this.items.size()-this.opts.size,b)}})})(jQuery);
