var Listscroll=Class.create({initialize:function(a,b){this.elements=b.elements?$$(a)[0].select(b.elements):$$(a)[0].select(".element");this.perpage=b.perpage||10;this.jumpto=b.jump||0;this.containerwidth=$$(a)[0].getWidth();this.container=$$(a)[0].setStyle({overflow:"hidden",width:this.containerwidth+"px",position:"relative"});this.autoupdate=b.autoupdate||false;this.troley=b.troley?$$(a)[0].down(b.troley):$$(a)[0].down(".troley");this.troley.setStyle({position:"relative"});var c=[],e=this.elements[0].select("[class^=sort]").pluck("className").grep(/sort_(.*)/,
function(d){return d.match(/sort_(.*)/)[1]}).uniq();a=(new Date).getTime();this.elements.each(function(d){for(var h={},g=0;g<e.length;g++)h[e[g]]=$(d).down(".sort_"+e[g]).innerHTML;h.element=$(d);c.push(h)});this.noanimation=(new Date).getTime()-a>200?true:false;if(this.elements.length>100)this.noanimation=true;this.sortfelder=e;this.data=c;if(b.pages){this.pages=$$(b.pages);this.showpages()}if(b.sort){this.sort=$$(b.sort);this.drop=new Element("select");this.sortOptions=this.elements[0].select("[class^=sort_]").pluck("className").grep(/sort_(.*)/);
for(a=0;a<this.sortOptions.length;a++){var f=this.sortOptions[a].match(/sort_(.*)/)[1];this.drop.insert((new Element("option",{selected:f==b.sortDefault,value:f})).update(this.elements[0].down("."+this.sortOptions[a]).readAttribute("rel")))}for(a=0;a<this.sort.length;a++)this.sort[a].insert(this.drop.cloneNode(true).observe("change",this.reorder.bind(this)));b.sortDefault&&this.sortaction(b.sortDefault)}this.positionize();this.jump(this.jumpto);b.btnLeft&&$$(b.btnLeft).invoke("observe","click",this.arrow.bind(this,
-1));b.btnRight&&$$(b.btnRight).invoke("observe","click",this.arrow.bind(this,1));this.autoupdate&&this.autoup()},showpages:function(){var a=Math.ceil(this.data.length/this.perpage),b=this.jumpto-3<0?0:this.jumpto-3,c=this.jumpto+(this.jumpto<4?6:4);if(c>a)c=a;for(var e=this.jumpto-7<0?0:this.jumpto-7,f=this.jumpto+7>=a-1?a:this.jumpto+7,d=0;d<this.pages.length;d++){this.pages[d].update();b>0&&this.pages[d].insert((new Element("span")).update("...").observe("click",this.jump.bind(this,e)));for(var h=
b;h<c;h++){var g=(new Element("span",h==this.jumpto?{className:"active"}:{})).update(h+1);this.pages[d].insert(g);this.jumpto==h&&g.addClassName("active");g.observe("click",this.jump.bind(this,h))}c<a&&this.pages[d].insert((new Element("span")).update("...").observe("click",this.jump.bind(this,f-1)))}},autoup:function(){this.autotimer=setInterval(this.positionize.bind(this,1),500)},changeContent:function(){this.positionize(1);this.jump(this.jumpto,1)},positionize:function(a){if(this.noanimation)a=
0;var b=this.data,c=this.perpage,e=this.containerwidth,f=0;(function(){var d=Math.floor(f/c),h=e*d;d=$R(d*c-1,d*c+f%c-1).collect(function(g){return g<0||g%c==c-1?0:b[g].element.getHeight()}).inject(0,function(g,i){return g+i});a?b[f].element.morph({left:h+"px",top:d+"px"}):b[f].element.absolutize().setStyle({left:h+"px",top:d+"px",height:"auto"});f++;f<b.length&&setTimeout(arguments.callee,5)})()},sortaction:function(a){function b(c,e,f){return parseInt(e[c])>0?c.indexOf("desc")>-1?parseFloat(e[c])>
parseFloat(f[c])?-1:1:parseFloat(e[c])>parseFloat(f[c])?1:-1:e[c].toLowerCase()>f[c].toLowerCase()?1:-1}this.data.sort(b.curry(a))},jump:function(a,b){if(this.noanimation)b=0;var c=-1*this.containerwidth*(a||0);if(b){this.troley.morph({left:c+"px"});$(this.container).morph({height:this.getPageHeight(a)+"px"})}else{this.troley.setStyle({left:c+"px"});$(this.container).setStyle({height:this.getPageHeight(a)+"px"})}this.jumpto=parseInt(a);window.location.hash=a;this.pages&&this.showpages()},getPageHeight:function(a){var b=
this.data;return $R(a*this.perpage,a*this.perpage+this.perpage-1).collect(function(c){if(c>b.length-1)return 0;return b[c].element.getHeight()}).inject(0,function(c,e){return c+e})},reorder:function(a){this.sortaction($F(a.element()));this.positionize(1)},arrow:function(a,b){b.stop();a=this.jumpto+a;a>Math.ceil(this.data.length/this.perpage)-1||a<0||this.jump(a,1)}});
