/**********  简单的分页脚本  ************/

Pagination.defaultInstanceName = "default";

function Pagination() {
	
	this.id              = Pagination.defaultInstanceName;
	
	this.currentPage     = 0;               // 当前页号
	this.pageSize        = 0;               // 总页数
	this.total           = 0;               // 总记录条数
	this.arrPageContent  = [];              // 内容数组, 里面的内容应该一条条记录, 比如 arrPageContent[0] = '<tr><td>标题[2006-10-01]</td></tr>'
	this.outerId         = "pagelist";      // 显示内容的节点id
	this.pageId          = "pageindex";     // 显示页号的节点id
	
}

	//20070925 wangzhe add
	function setgray(flag){
		var totalPage = pagination.pageSize;
		var currentNum = pagination.currentPage;

		var obj01 = document.getElementById("p0t");
		var obj02 = document.getElementById("p0f");
		var obj11 = document.getElementById("p1t");
		var obj12 = document.getElementById("p1f");
		var obj21 = document.getElementById("p2t");
		var obj22 = document.getElementById("p2f");
		var obj31 = document.getElementById("p3t");
		var obj32 = document.getElementById("p3f");

		if(flag == 0){
			obj01.style.display = "none";
			obj02.style.display = "";
			obj11.style.display = "none";
			obj12.style.display = "";
			if(totalPage > 1){
				obj31.style.display = "";
				obj32.style.display = "none";
				obj21.style.display = "";
				obj22.style.display = "none";
			}
		}else if(flag == 1){
			if(currentNum == 1){
				obj01.style.display = "none";
				obj02.style.display = "";
				obj11.style.display = "none";
				obj12.style.display = "";
			}if(totalPage > 1){
				obj31.style.display = "";
				obj32.style.display = "none";
				obj21.style.display = "";
				obj22.style.display = "none";
			}
		}else if(flag ==2){
			if(currentNum == totalPage){
				obj31.style.display = "none";
				obj32.style.display = "";
				obj21.style.display = "none";
				obj22.style.display = "";
			}if(totalPage > 1){
				obj01.style.display = "";
				obj02.style.display = "none";
				obj11.style.display = "";
				obj12.style.display = "none";
			}
		}else if(flag ==3){
			obj31.style.display = "none";
			obj32.style.display = "";
			obj21.style.display = "none";
			obj22.style.display = "";
			if(totalPage > 1){
				obj01.style.display = "";
				obj02.style.display = "none";
				obj11.style.display = "";
				obj12.style.display = "none";
			}
		}else{
			if(currentNum == 1){
				obj01.style.display = "none";
				obj02.style.display = "";
				obj11.style.display = "none";
				obj12.style.display = "";
				
				obj21.style.display = "";
				obj22.style.display = "none";
				obj31.style.display = "";
				obj32.style.display = "none";
			}else if(currentNum == totalPage){
				obj01.style.display = "";
				obj02.style.display = "none";
				obj11.style.display = "";
				obj12.style.display = "none";
				
				obj21.style.display = "none";
				obj22.style.display = "";
				obj31.style.display = "none";
				obj32.style.display = "";
			}else{
				obj01.style.display = "";
				obj02.style.display = "none";
				obj11.style.display = "";
				obj12.style.display = "none";
				
				obj21.style.display = "";
				obj22.style.display = "none";
				obj31.style.display = "";
				obj32.style.display = "none";
			}
		}
	}
	
	//20070925 wangzhe add
	function changeIndex(flag){
		if(flag == 0){
		     pageSelect.selectedIndex=0;setgray(0);
		     return ;
		}
		if(flag == 1){
		 if(pageSelect.selectedIndex != 0){
		     pageSelect.selectedIndex=pageSelect.selectedIndex -1;setgray(1);
		     return ;
		  }
		}
		if(flag == 2){
		    if(pageSelect.selectedIndex != pageSelect.options.length -1){
			   pageSelect.selectedIndex=pageSelect.selectedIndex +1;setgray(2);
			    return;
		 	}
		}
		if(flag == 3){
		     pageSelect.selectedIndex=pageSelect.options.length - 1;setgray();setgray(3);
		     return;
		}
	}




/**
* Binds this instance to window object using "Pagination."+this.id as a key.
*/
Pagination.prototype.bindById = function () {
    var key = "Pagination." + this.id;
    window[key] = this;
};

/**
* Finds an instance by id.
*/
Pagination.findInstance = function(id) {
    var key = "Pagination." + id;
    return window[key];
};

Pagination.prototype.setPageContent = function(content) {
	if(typeof(content) == "object" && content.length) {
		this.arrPageContent = content;
		this.currentPage = 1;
		this.pageSize = content.length;
		
		// 如果是理想情况, 每一条即在一个<tr>标签中, 则求出总记录条数:
		/*if(content[0].indexOf("<tr>") > 0) {
			for(i = 0; i < content.length; i++) {
				this.total += ;
			}
		}*/
	}
};

Pagination.prototype.getCurrentPage = function() {
	if(this.currentPage > 0 && this.pageSize > 0) {
		return this.arrPageContent[this.currentPage-1];
	}
};

Pagination.prototype.hasNextPage = function() {
	if(this.pageSize > 1 && this.currentPage != this.pageSize) {
		return true;
	}return false;
};

Pagination.prototype.hasPreviousPage = function() {
	if(this.pageSize > 1 && this.currentPage > 1) {
		return true;
	}return false;
};

Pagination.prototype.getNextPage = function() {
	if(this.hasNextPage()) {
		this.currentPage += 1;
		return this.getCurrentPage();
	}
};

Pagination.prototype.getPreviousPage = function() {
	if(this.hasPreviousPage()) {
		this.currentPage -= 1;
		return this.getCurrentPage();
	}
};

Pagination.prototype.getFirstPage = function() {
	if(this.hasPreviousPage()) {
		this.currentPage = 1;
		return this.getCurrentPage();
	}
};

Pagination.prototype.getLastPage = function() {
	if(this.hasNextPage()) {
		this.currentPage = this.pageSize;
		return this.getCurrentPage();
	}
};

Pagination.prototype.getIndicatePage = function(index) {
	index = parseInt(index);
	if(index > 0 && index <= this.pageSize && index != this.currentPage) {
		this.currentPage = index;
		return this.getCurrentPage();
	}
};

/**
 * 功能: 在页面上显示内容
 * 第一个参数 0:显示首页, 1:显示上一页, 2:显示下一页, 3:显示尾页
 * 第二个参数 显示该参数指定的页
 * 成功返回true, 失败返回false
 */
Pagination.prototype.display = function() {
	var outerElem = document.getElementById(this.outerId); // 取得显示内容的div节点对象
	if(outerElem === null) { outerElem = document.getElementsByName(this.outerId)[0]; }
	//outerElem = outerElem.firstChild; // 如果outerElem是table节点对象, 因为 TBODY 元素会为全部表格自动定义, 就算表格没有显式定义 TBODY 元素, 所以要取到这个tbody
	var prefix = '<table cellpadding=\"2\" cellspacing=\"1\" align=\"left\" border=\"0\" width=\"100%\">';
	var suffix = '</table>';
	if(arguments.length > 0) {
		if(arguments[0] === 0 && this.hasPreviousPage()) { outerElem.innerHTML = prefix + this.getFirstPage() + suffix; }
		if(arguments[0] == 1 && this.hasPreviousPage()) { outerElem.innerHTML = prefix + this.getPreviousPage() + suffix; }
		if(arguments[0] == 2 && this.hasNextPage()) { outerElem.innerHTML = prefix + this.getNextPage() + suffix; }
		if(arguments[0] == 3 && this.hasNextPage()) { outerElem.innerHTML = prefix + this.getLastPage() + suffix; }
		if(arguments[0] == 4 && this.currentPage != arguments[1]) { outerElem.innerHTML = prefix + this.getIndicatePage(arguments[1]) + suffix; }
	}
	//if(typeof(this.pageId) == "string" && this.pageId.length > 0) {
		//var indexElem = document.getElementById(this.pageId);
		//if(indexElem !== null) { indexElem.innerText = this.currentPage + "/" + this.pageSize; }
	//}
};

// default instance
pagination = new Pagination();
pagination.bindById();