(function(a){a.fn.jTPS=function(d){a(this).data("tableSettings",a.extend({perPages:[5,10,20,50,"Все"],perPageText:"На странице: ",perPageDelim:'<span style="color:#ccc;">|</span>',perPageSeperator:".."},d));var f=a(this),j=j||1,m=a(this).data("tableSettings").perPages,o=o||m[0],n=a("tbody tr",this).length;a(this).addClass("jTPS");a(".stubCell",this).remove();var p=0,l=Math.max(a("thead:first tr:last th",this).length,parseInt(a("thead:first tr:last th").attr("colspan")||0)),c=o-(a("tbody tr",this).length%o),k=a("tbody tr:first td:first",this).css("height");for(;p<c&&c!=o;p++){a("tbody tr:last",this).after('<tr class="stubCell"><td colspan="'+l+'" style="height:'+k+';">&nbsp;</td></tr>')}if(n>o&&o!=0){a("tbody tr:gt("+(o-1)+")",this).addClass("hideTR")}if(o!=0){a("thead th[sort]",this).each(function(){a(this).addClass("sortableHeader").unbind("click").bind("click",function(){var s=a(f).find("thead").index(a(this).parent().parent()),r=a(f).find("thead:eq("+s+") tr:last th").index(a(this)),u=a(f).find("thead:eq("+s+") th:eq("+r+")").hasClass("sortAsc")?true:false;g(f,r,u,s);var t=parseInt(a(f).find(".hilightPageSelector:first").html())||1;a(f).find("tbody:eq("+s+") tr").removeClass("hideTR").filter(":gt("+((o-1)*t)+")").addClass("hideTR");a(f).find("tbody:eq("+s+") tr:lt("+((o-1)*(t-1))+")").addClass("hideTR");a(f).find(".pageSelector:first").click();a(f).find("thead:eq("+s+") .sortDesc, thead:eq("+s+") .sortAsc").removeClass("sortDesc").removeClass("sortAsc");a(f).find("thead:eq("+s+") tr:last th:eq("+r+")").addClass(u?"sortDesc":"sortAsc")})})}a("tfoot .selectPerPage",this).empty();var e=m.length;while(e--){a("tfoot .selectPerPage",this).prepend(((e>0)?a(this).data("tableSettings").perPageDelim:"")+'<span class="perPageSelector">'+m[e]+"</span>")}b(this,j||1);a("tfoot .selectPerPage",this).prepend(a(this).data("tableSettings").perPageText).find(".perPageSelector").each(function(){if((parseInt(a(this).html())||n)==o||m.length==1){a(this).addClass("perPageSelected")}a(this).bind("mouseover mouseout",function(r){r.type=="mouseover"?a(this).addClass("perPageHilight"):a(this).removeClass("perPageHilight")});a(this).bind("click",function(){o=parseInt(a(this).html())||n;if(o>n){o=n}a(".stubCell",this).remove();var t=0,v=a(f).find("thead th").length,w=(o-(a(f).find("tbody tr").length%o)),s=a(f).find("tbody tr:first td:first").css("height");for(;t<w&&w!=o;t++){a(f).find("tbody tr:last").after('<tr class="stubCell"><td colspan="'+v+'" style="height: '+s+';">&nbsp;</td></tr>')}a(f).find("tbody tr").removeClass("hideTR").filter(":gt("+((o-1)*j)+")").addClass("hideTR");a(f).find("tbody tr:lt("+((o-1)*(j-1))+")").addClass("hideTR");a(f).find(".pageSelector:first").click();a(this).siblings(".perPageSelected").removeClass("perPageSelected");a(this).addClass("perPageSelected");b(f,1);var u=a("tbody tr:not(.hideTR):first",f).prevAll().length,r=a("tbody tr:not(.hideTR):not(.stubCell)",f).length;a("tfoot .status",f).html("Показано "+(u+1)+"-"+(u+r)+" из "+n+"")})});var q=a("tbody tr:not(.hideTR):first",this).prevAll().length,h=a("tbody tr:not(.hideTR):not(.stubCell)",this).length;a("tfoot .status",this).html("Показано "+(q+1)+"-"+(q+h)+" из "+n);function b(v,u){a("tfoot .pagination",v).empty();var r=(o>=n||o==0)?0:Math.ceil(n/o),t=r;while(r--){a("tfoot .pagination",v).prepend('<div class="pageSelector">'+(r+1)+"</div>")}var s=a("tfoot .pageSelector",v).length;a("tfoot .pageSelector.hidePageSelector",v).removeClass("hidePageSelector");a("tfoot .pageSelector.hilightPageSelector",v).removeClass("hilightPageSelector");a("tfoot .pageSelectorSeperator",v).remove();a("tfoot .pageSelector:lt("+((u>(s-4))?(s-5):(u-2))+"):not(:first)",v).addClass("hidePageSelector").eq(0).after('<div class="pageSelectorSeperator">'+a(v).data("tableSettings").perPageSeperator+"</div>");a("tfoot .pageSelector:gt("+((u<4)?4:u)+"):not(:last)",v).addClass("hidePageSelector").eq(0).after('<div class="pageSelectorSeperator">'+a(v).data("tableSettings").perPageSeperator+"</div>");a("tfoot .pageSelector:eq("+(u-1)+")",v).addClass("hilightPageSelector");a("tfoot .pagination .pageSelector",f).each(function(){a(this).click(function(){a("#slide").hide();a(this).parent().find(".hilightPageSelector").removeClass("hilightPageSelector");a(this).addClass("hilightPageSelector");var z=parseInt(a(this).html());var x=(z-1)*o,y=x+o;a("tbody tr",f).removeClass("hideTR").addClass("hideTR");if(z==1){a("tbody tr:first",f).andSelf().removeClass("hideTR");a("tbody tr:gt("+x+"):lt("+(o-1)+")",f).andSelf().removeClass("hideTR")}else{if(z==t&&t>1){a("tbody tr:gt("+(x-1)+"):lt("+(n-o*(t-1))+")",f).andSelf().removeClass("hideTR")}else{a("tbody tr:gt("+(x-1)+"):lt("+o+")",f).andSelf().removeClass("hideTR")}}var A=a("tbody tr:not(.hideTR):first",f).prevAll().length,w=a("tbody tr:not(.hideTR):not(.stubCell)",f).length;a("tfoot .status",f).html("Показано "+(A+1)+"-"+(A+w)+" из "+n+"");if(y>n){y=(n-1)}b(v,z)})});a("#slide").show()}function g(v,D,u,y){var w=a("thead:eq("+y+") th:eq("+D+")",v).hasClass("sortAsc")||a("thead:eq("+y+") th:eq("+D+")",v).hasClass("sortDesc")||false,E=String.fromCharCode(0),C=/([-]{0,1}[0-9.]{1,})/g,F=a("tbody:eq("+y+") tr:not(.stubCell)",v).get(),t=[];a(F).each(function(I,N){var G=(a("td:eq("+D+")",N).html()||"").toString().toLowerCase()||"",L=G.replace(C,E+"$1"+E).split(E),M=(new Date(G)).getTime(),K=L.length;if(M){t.push(M+","+(t.length))}else{var J=[];for(var H=0;H<K;H++){J.push((new Date(L[H])).getTime()||parseFloat(L[H])||L[H])}t.push(J.join(",")+","+(t.length))}});if(!w){i(t,0,(F.length-1));if(!u){t.reverse()}}a("tbody:eq("+y+")",v).attr("temp","true").before("<tbody></tbody>");var A=t.length,s=a("tbody:eq("+y+")",v)[0];if(w){while(A--){s.appendChild(F[A])}}else{while(A--){s.appendChild(F[parseInt(t[A].split(",").pop())])}}a("tbody[temp=true]",v).remove();var B=0,z=a("thead:eq("+y+") tr:last th",v).length,r=(o-(a("tbody:eq("+y+") tr",v).length%o)),x=a("tbody:eq("+y+") tr:first td:first",v).css("height");for(;B<r&&r!=o;B++){a("tbody:eq("+y+") tr:last",v).after('<tr class="stubCell"><td colspan="'+z+'" style="height:'+x+';">&nbsp;</td></tr>')}a("tbody tr").removeClass("even").removeClass("odd");if(n&1){a("#listing tbody tr:even").addClass("even");a("#listing tbody tr:odd").addClass("odd")}else{a("#listing tbody tr:even").addClass("odd");a("#listing tbody tr:odd").addClass("even")}}function i(s,w,u){var x=w,t=u,r=s[w];var v=function(C,A){var B=C.split(",").slice(0,(C.length-1)),z=A.split(",").slice(0,(C.length-1));for(var D=0,y=Math.min(B.length,z.length),E=Math.max(B.length,z.length);D<E;D++){if((parseFloat(B[D])||B[D])<(parseFloat(z[D])||z[D])){return -1}if((parseFloat(B[D])||B[D])>(parseFloat(z[D])||z[D])){return 1}if(D>y&&D<=E){return 0}}return 0};while(w<u){while((v(s[u],r)>=0)&&(w<u)){u--}if(w!=u){s[w]=s[u];w++}while((v(s[w],r)<=0)&&(w<u)){w++}if(w!=u){s[u]=s[w];u--}}s[w]=r;r=w;w=x;u=t;if(w<r){i(s,w,(r-1))}if(u>r){i(s,(r+1),u)}}return this}})(jQuery);