/*
* author:MaoFangyun
* createTime:2015-01-15
* description:分页控件
* notice:需引用jquery
*/
function Pager(id) {
    id = "#" + id;
    this.pageIndex = 1;
    this.pageSize = 10;
    this.pageCount = 0;  //总页数
    this.recordCount = 0;//总记录数
    this.control = $(id);
    this.search = null;
    this.span = $(id).find("span");
    this.buttons = $(id).find("a");
    this.disableClass = "pgEmpty";//禁用时的样式
    this.init();
}
Pager.prototype = {
    init: function () {
        this.buttons.addClass(this.disableClass);
        this.span.text("共0条记录");
        this.bindEvents();
    },
    bindEvents: function () {
        var self = this;
		function bind(index, fun) {
            self.control.find("a:eq(" + index + ")").click(function () {
                if (!$(this).hasClass(self.disableClass)) {
					fun();
				}    
            });
        }
        bind(0, function () { self.first(); });
        bind(1, function () { self.previous(); });
        bind(2, function () { self.next(); });
        bind(3, function () { self.last(); });
    },
    setPager: function () {
        this.pageCount = Math.ceil(this.recordCount / this.pageSize);
        var info = this.recordCount == 0 ? "共0条记录" : "共" + this.recordCount + "条记录,当前" + this.pageIndex + "/" + this.pageCount + "页";
        this.span.text(info);
        if (this.pageIndex == 1) {
            this.control.find("a:lt(2)").addClass(this.disableClass);
        } else {
            this.control.find("a:lt(2)").removeClass(this.disableClass);
        }
        if (this.pageIndex >= this.pageCount) {
            this.control.find("a:gt(1)").addClass(this.disableClass);
        } else {
            this.control.find("a:gt(1)").removeClass(this.disableClass);
        }
    },
    first: function () {
        this.pageIndex = 1;
        this.search();
    },
    previous: function () {
        this.pageIndex--;
        if (this.pageIndex <= 1) {
            this.pageIndex = 1;
        }
        this.search();
    },
    next: function () {
        this.pageIndex++;
        if (this.pageIndex >= this.pageCount) {
            this.pageIndex = this.pageCount;
        }
        this.search();
    },
    last: function () {
        this.pageIndex = this.pageCount;
        this.search();
    }
};