/**
 * Copyright (c) 2009 AIC Shenzhen. Technical Support Taiji Software CO.,LTD
 * Shenzhen. All rights reserved.
 * 
 * 实现对分页的各种操作。
 * 
 * @version 1.0
 * @author Wangkang, Huang Jianhu.
 */

/** 定义分页的相关属性。 */

/* 当前页数 */
Pagination.prototype.curPage = 1;
/* 每页记录数 */
Pagination.prototype.pageSize = 10;
/* 最大页数 */
Pagination.prototype.maxPage = 0;
/* 总记录数 */
Pagination.prototype.countEmp = 0;
/* 分页条件对象 */
Pagination.prototype.condition = null;
/* 分页使用的业务类 */
Pagination.prototype.dwrService = null;
/* 需要插入分页组件的位置ID */
Pagination.prototype.positionID = "";
/* 放置分页工具条的位置ID */
Pagination.prototype.toolsID = "";
/* 放置分页内容的位置ID */
Pagination.prototype.tabPageID = "tabPage";
/* 需要显示的记录列字段 */
Pagination.prototype.columnField = [];
/* 函数数组 */
Pagination.prototype.cellFuncs = [];

/**
 * 构造函数。
 * 
 * @param {Object}
 *            serviceCls DWR处理请求的js类。
 * @param {String}
 *            posID 需要插入分页组件的位置ID。
 * @param {String}
 *            tID 放置分页工具条的位置ID。
 */
function Pagination(serviceCls, posID, tID, tabpageid) {
	if(null != serviceCls && '' != serviceCls && undefined != serviceCls) this.dwrService = serviceCls;
	if(null != posID && '' != posID && undefined != posID) this.positionID = posID;
	if(null != tID && '' != tID && undefined != tID) this.toolsID = tID;
    if(null != tabpageid && '' != tabpageid && undefined != tabpageid) this.tabPageID = tabpageid;
}

/**
 * 设置分页查询需要的条件参数。
 * 
 * @param {Object}
 *            cdn 参数对象。
 */
Pagination.prototype.setCondition = function(cdn) {
	this.condition = cdn;
};

/**
 * 设置DWR服务类
 * 
 * @param {Object}
 *            serviceCls DWR中所对应的JAVA服务类。
 */
Pagination.prototype.setService = function(serviceCls) {
	this.dwrService = serviceCls;
};

/**
 * 设置需要显示的记录列字段
 * 
 * @param {Array}
 *            colField 数组形式的字段集。
 */
Pagination.prototype.setColumnField = function(colField) {
	this.columnField = colField;
	var me = this;
	for (var index = 0; index < this.columnField.length; index++) {
		// var eval(column + "index") = "" + this.columnField[index];
		// column = this.columnField[index];
		// eval("column" + index + " = this.columnField[" + index + "]");
		// this.cellFuncs[index] = function (cellObj) {
		// return cellObj["" + column];
		// };
	}
};

/**
 * 设置内容显示操作函数。
 * 
 * @param {Array}
 *            cfs 操作函数数组。
 */
Pagination.prototype.setCellFuncs = function(cfs) {
	this.cellFuncs = cfs;
};

/**
 * 初始化分页列表。
 */
Pagination.prototype.initData = function() {
	this.pageCountContorl();
	this.pageContorl();
};

/**
 * 分页访问控制器。
 */
Pagination.prototype.pageContorl = function() {
	var me = this;
	if (null == this.condition || undefined == this.condition) {
		this.dwrService.findPage(this.curPage, this.pageSize, function(result) {
					me.callBack(result);
				});
	} else {
		this.dwrService.findPageCDN(this.curPage, this.pageSize, this.condition,
				function(result) {
					me.callBack(result);
				});
	}
};

/**
 * 分页记录总数查询控制器。
 */
Pagination.prototype.pageCountContorl = function() {
	var me = this;
	if (null == this.condition || undefined == this.condition) {
		this.dwrService.findRecordCount(function(result) {
					me.callBackCount(result);
				});
	} else {
		this.dwrService.findRecordCountCDN(this.condition, function(result) {
					me.callBackCount(result);
				});
	}
};

/**
 * 回调函数。
 * 
 * @param {Object}
 *            resultList 是由DWR调用服务类后返回的数据集，它是一个Object的数组。
 */
Pagination.prototype.callBack = function(resultList) {
	DWRUtil.removeAllRows(this.tabPageID);
	DWRUtil.addRows(this.tabPageID, resultList, this.cellFuncs, {
        rowCreator:function(options){
            var row = document.createElement("tr");
            row.setAttribute("id",options.rowData.id);
            row.style.collapse = "separate";
            var flag = options.rowIndex % 2;
            switch (flag) {
                case 0: jQuery(row).addClass('alternate_line1');break;
                case 1: jQuery(row).addClass('alternate_line2');break;
            }
            row.onmouseover = function(){
                if (onColor != undefined)
                onColor(row);
            };
            row.onmouseout = function(){
                if (offColor != undefined)
                offColor(row);
            };
            return row;
        }
    });
	$(this.positionID).innerHTML = this.pageToos();
	this.registerHandle();
	$(this.toolsID).innerHTML = "[当前第" + this.curPage + "页(共" + this.maxPage
			+ "页)  显示" + resultList.length + "条(共" + this.countEmp + "条记录)]";
};

/**
 * 查找列表总数回调函数，通过总数以及每页显示的记录数(pageSize)计算出相关的数据。
 * 
 * @param {Object}
 *            count 列表总记录数。
 */
Pagination.prototype.callBackCount = function(count) {
	this.countEmp = count;
	this.maxPage = parseInt((this.countEmp + this.pageSize - 1) / this.pageSize);
};

/**
 * 由于delete为js的关键字，则删除方法由"delete"改为"deletePO" 删除数据库PO对象操作。
 * 
 * @param {Object} objPO
 *            需要删除的对象。
 */
Pagination.prototype.deletePO = function(objPO) {
	// 要一次提交，不然报错
	dwr.engine.beginBatch();
	this.dwrService.deletePO(objPO);
	this.countEmp = this.countEmp - 1;
	if (this.curPage == 1) {
		// 如果为当前页为1时 则跳至首页
		this.goHomePage();
	} else if (this.countEmp % this.pageSize === 0) {
		// 如果总数整除页数，则跳至上页
		this.goPreviousPage();
	}
	dwr.engine.endBatch();
};

/**
 * 分页工具栏信息。
 */
Pagination.prototype.pageToos = function() {
	var toolsStr = "<img src='images/tab/first.gif' style='cursor:pointer;' width='37' height='15' id='firstPage'/>";
	toolsStr += "<img src='images/tab/back.gif' style='cursor:pointer;' width='43' height='15' id='previousPage'/>";
	toolsStr += "<img src='images/tab/next.gif' style='cursor:pointer;' width='43' height='15' id='nextPage'/>";
	toolsStr += "<img src='images/tab/last.gif' style='cursor:pointer;' width='37' height='15' id='lastPage'/>";
	return toolsStr;
};

/**
 * 回到首页。
 */
Pagination.prototype.goHomePage = function() {
	this.curPage = 1;
	this.pageContorl();
};

/**
 * 下一页。
 */
Pagination.prototype.goNextPage = function() {
	if (this.curPage < this.maxPage) {
		this.curPage = this.curPage + 1;
		this.pageContorl();
	}
};

/**
 * 上一页。
 */
Pagination.prototype.goPreviousPage = function() {
	if (this.curPage > 1) {
		this.curPage = this.curPage - 1;
		this.pageContorl();
	}
};

/**
 * 到末页。
 */
Pagination.prototype.goLastPage = function() {
	this.curPage = this.maxPage;
	this.pageContorl();
};

/**
 * 为分页操作按钮添加事件监听。
 */
Pagination.prototype.registerHandle = function() {
	var me = this;
	$("firstPage").onclick = function() {
		me.goHomePage();
	};
	$("previousPage").onclick = function() {
		me.goPreviousPage();
	};
	$("nextPage").onclick = function() {
		me.goNextPage();
	};
	$("lastPage").onclick = function() {
		me.goLastPage();
	};
};
