XN.namespace("ui");
XN.ui.pager=function(p){
var _2=this;
this._ID=XN.util.createObjID();
this.config=this.config||{};
$extend(this.config,{showPageCount:5,classOfCurrentPage:"current"});
$extend(this.config,p);
if(this.getConfig("container")){
this.frame=$(this.getConfig("container"));
}else{
var f=$element("ul");
this.frame=f;
}
XN.event.addEvent(this.frame,"click",function(e){
e=e||window.event;
_2._parsePageEvent(e);
});
};
XN.ui.pager.prototype=$extend({},XN.ui.element);
$extend(XN.ui.pager.prototype,{_pageCount:null,_currentPage:null,_parsePageEvent:function(e){
var el=XN.event.element(e);
var ac;
XN.event.stop(e);
if(el.getAttribute("page")){
this.setCurrentPage(el.getAttribute("page"));
}else{
if(ac=el.getAttribute("action")){
if(this[ac+"Page"]){
this[ac+"Page"]();
}
}
}
},pageCount:function(){
return this._pageCount;
},setPageCount:function(_8){
this._pageCount=_8;
this.renderUI();
this.fireEvent("countChange",_8);
return this;
},setCurrentPage:function(_9,_a,_b){
if(this._pageCount==0){
return;
}
_9=parseInt(_9);
if(!_a&&_9==this._currentPage){
return this;
}
if(_9<1){
_9=1;
}else{
if(_9>this._pageCount){
_9=this._pageCount;
}
}
var _c=this.getConfig("classOfCurrentPage");
if(this._currentPage&&this.getPageEl(this._currentPage)){
this.getPageEl(this._currentPage).delClass(_c);
}
this._currentPage=_9;
if(this.getConfig("showPageCount")){
this.renderUI();
if(this._currentPage==1){
this.getEl("firstPage").hide();
}else{
this.getEl("firstPage").show();
}
if(this._currentPage==this._pageCount){
this.getEl("lastPage").hide();
}else{
this.getEl("lastPage").show();
}
if(this._currentPage>1){
this.getEl("previousPage").show();
}else{
this.getEl("previousPage").hide();
}
if(this._currentPage<this._pageCount){
this.getEl("nextPage").show();
}else{
this.getEl("nextPage").hide();
}
}
this.getPageEl(_9).addClass(_c);
XN.log(this.getPageEl(_9));
if(!_b){
this.fireEvent("pageChange",_9);
}
if(!_b){
this.fireEvent("pageChangeForSys",_9);
}
return this;
},getConfig:function(_d){
return this.config[_d];
},getEl:function(id){
return $(this.getID(id));
},getPageEl:function(_f){
return $(this.getPageID(_f));
},getPageID:function(num){
return this.getID("page_"+num);
},getID:function(id){
return "ui_pager_"+this._ID+"_"+id;
},renderUI:function(){
if(this._pageCount===0){
this.frame.innerHTML="";
this.fireEvent("noPage");
return;
}
var _12=[];
if(this.getConfig("showPageCount")){
_12.push("<li class=\"first-page\"><a action=\"first\" href=\"#nogo\" id=\""+this.getID("firstPage")+"\">\u9996\u9875</a></li>");
_12.push("<li class=\"previous-page\"><a action=\"previous\" href=\"#nogo\" id=\""+this.getID("previousPage")+"\">\u4e0a\u4e00\u9875</a></li>");
}
var i,j;
if(this.getConfig("showPageCount")){
var _15=Math.floor(this.getConfig("showPageCount")/2);
i=Math.max(this._currentPage-_15,1);
j=Math.min(this._pageCount,i+this.getConfig("showPageCount")-1);
}else{
i=1;
j=this._pageCount;
}
while(i<=j){
_12.push(this.createPagerItem(i));
i++;
}
if(this.getConfig("showPageCount")){
_12.push("<li class=\"next-page\"><a action=\"next\" href=\"#nogo\" id=\""+this.getID("nextPage")+"\">\u4e0b\u4e00\u9875</a></li>");
_12.push("<li class=\"last-page\"><a action=\"last\" href=\"#nogo\" id=\""+this.getID("lastPage")+"\">\u5c3e\u9875</a></li>");
}
this.frame.innerHTML=_12.join("");
},firstPage:function(){
this.setCurrentPage(1);
return this;
},lastPage:function(){
this.setCurrentPage(this._pageCount);
return this;
},nextPage:function(){
this.setCurrentPage(this._currentPage+1);
return this;
},previousPage:function(){
this.setCurrentPage(this._currentPage-1);
return this;
},createPagerItem:function(num){
return ["<li id=\""+this.getPageID(num)+"\">","<a page=\""+num+"\" href=\"#nogo\">"+num+"</a>","</li>"].join("");
}});
XN.ui.pager.sync=function(a,b){
a.addEvent("pageChangeForSys",function(n){
b.setCurrentPage(n,true,true);
b.fireEvent("pageChange",n);
});
b.addEvent("pageChangeForSys",function(n){
a.setCurrentPage(n,true,true);
a.fireEvent("pageChange",n);
});
a.addEvent("countChange",function(c){
if(b.pageCount()==c){
return;
}
b.setPageCount(c);
});
b.addEvent("countChange",function(c){
if(a.pageCount()==c){
return;
}
a.setPageCount(c);
});
};
XN.ui.pager.prototype.gotoPage=XN.ui.pager.prototype.setCurrentPage;
XN.event.enableCustomEvent(XN.ui.pager.prototype);

