// JavaScript Document
/**//**
* js分页类
* @param iAbsolute 每页显示记录数
* @param sTableId 分页表格属性ID值，为String
* @param sTBodyId 分页表格TBODY的属性ID值,为String,此项为要分页的主体内容
* @Version 1.0.0
* var __variable__; private
* function __method__(){};private
*/
function Page(iAbsolute,sTableId,sTBodyId)
{
//alert(iAbsolute);
this.absolute = iAbsolute; //每页最大记录数
this.tableId = sTableId;
this.tBodyId = sTBodyId;
this.rowCount = 0;//记录数
this.pageCount = 0;//页数
this.pageIndex = 0;//页索引
this.__oTable__ = null;//表格引用
this.__oTBody__ = null;//要分页内容
this.__dataRows__ = 0;//记录行引用
this.__oldTBody__ = null;
this.__init__(); //初始化;
};
/**//*
初始化
*/
Page.prototype.__init__ = function(){
this.__oTable__ = document.getElementById(this.tableId);//获取table引用
this.__oTBody__ = this.__oTable__.tBodies[this.tBodyId];//获取tBody引用
this.__dataRows__ = this.__oTBody__.rows;
this.rowCount = this.__dataRows__.length;
try{
this.absolute = (this.absolute <= 0) || (this.absolute>this.rowCount) ? this.rowCount : this.absolute; 
this.pageCount = parseInt(this.rowCount%this.absolute == 0 
? this.rowCount/this.absolute : this.rowCount/this.absolute+1);
}catch(exception){}

this.__updateTableRows__();
};

Page.prototype.getDataRows = function(){
	return this.__dataRows__;
}

/**//*
下一页
*/
Page.prototype.nextPage = function(){
if(this.pageIndex + 1 < this.pageCount){
this.pageIndex += 1;
this.__updateTableRows__();
}
};
/**//*
上一页
*/
Page.prototype.prePage = function(){
if(this.pageIndex >= 1){
this.pageIndex -= 1;
this.__updateTableRows__();
}
};
/**//*
首页
*/
Page.prototype.firstPage = function(){
if(this.pageIndex != 0){
this.pageIndex = 0;
this.__updateTableRows__();
} 
};
/**//*
尾页
*/
Page.prototype.lastPage = function(){
if(this.pageIndex+1 != this.pageCount){
this.pageIndex = this.pageCount - 1;
this.__updateTableRows__();
}
};
/**//*
页定位方法
*/
Page.prototype.aimPage = function(iPageIndex){
if(iPageIndex > this.pageCount-1){
this.pageIndex = this.pageCount - 1;
}else if(iPageIndex < 0){
this.pageIndex = 0;
}else{
this.pageIndex = iPageIndex;
}
this.__updateTableRows__();
};
/**//*
执行分页时，更新显示表格内容
*/
Page.prototype.__updateTableRows__ = function(){
var iCurrentRowCount = this.absolute * this.pageIndex;
var iMoreRow = this.absolute+iCurrentRowCount > this.rowCount ? this.absolute+iCurrentRowCount - this.rowCount : 0;
var tempRows = this.__cloneRows__();
//alert(tempRows === this.dataRows);
//alert(this.dataRows.length);
var removedTBody = this.__oTable__.removeChild(this.__oTBody__);
var newTBody = document.createElement("TBODY");
newTBody.setAttribute("id", this.tBodyId);

for(var i=iCurrentRowCount; i < this.absolute+iCurrentRowCount-iMoreRow; i++){
newTBody.appendChild(tempRows[i]);
}
this.__oTable__.appendChild(newTBody);
/**//*
this.dataRows为this.oTBody的一个引用，
移除this.oTBody那么this.dataRows引用将销失,
code:this.dataRows = tempRows;恢复原始操作行集合.
*/
this.__dataRows__ = tempRows;
this.__oTBody__ = newTBody;
//alert(this.dataRows.length);
//alert(this.absolute+iCurrentRowCount);
//alert("tempRows:"+tempRows.length);
this.__createPagerBar__();
};
/**//*
克隆原始操作行集合
*/
Page.prototype.__cloneRows__ = function(){
var tempRows = [];
for(var i=0; i<this.__dataRows__.length; i++){
/**//*
code:this.dataRows[i].cloneNode(param), 
param = 1 or true:复制以指定节点发展出去的所有节点,
param = 0 or false:只有指定的节点和它的属性被复制.
*/
tempRows[i] = this.__dataRows__[i].cloneNode(1);
}
return tempRows;
};

// 显示全部记录。
// 根据 __updateTableRows__ 方法改写
// add by yanrong 2009/11/2
Page.prototype.listAll = function() {

	var tempRows = this.__cloneRows__();
	var rowCount = tempRows.length;
	//alert(tempRows === this.dataRows);
	//alert(this.dataRows.length);
	
	
	if((rowCount % 5) == 0){
		cellCount = rowCount / 5;
	}else {
		cellCount = parseInt(rowCount / 5) + 1;
	}
	//alert(cellCount);return false;
	var removedTBody = this.__oTable__.removeChild(this.__oTBody__);
	var newTBody = document.createElement("TBODY");
	newTBody.setAttribute("id", this.tBodyId);
	
	$('cartSidebar').style.width = 223 * cellCount + "px";
	$('cartSide1').style.width = 223 * cellCount - 10 + "px";
	$('cartSide1').style.backgroundRepeat = "repeat";
	$('cartSide2').style.width = 223 * cellCount - 10 + "px";
	$('cartSide2').style.backgroundRepeat = "repeat";
	$('txtRemark').style.width = 223 * cellCount - 50 +"px";
	$('cartSide3').style.width = 223 * cellCount - 10 + "px";
	$('cartSide3').style.backgroundRepeat = "repeat";
	$('tblComponents').style.width=223 * cellCount - 10 + "px";
	$('cartSidebar').style.margin = "0 0 0 " + (725-100*cellCount) +"px";
	$('cellFlag').value = 1;
	this.__oTable__.appendChild(newTBody);
	for(var i = 0; tempRows != null && i < 5; i++) {
		newRow = newTBody.insertRow(newTBody.rows.length);
		for(var j = 0; j < cellCount; j++) { 
			if(tempRows[i+j*5]!=null) {
				newCell = newRow.insertCell(newRow.cells.length);
				//alert(tempRows[i+j].cells[0]);			
					newCell.innerHTML = get_tr_text(tempRows[i+j*5]);
					newCell.setAttribute("width", 50/cellCount);
				//newCell.style.width = "10px";
			}else{
				break;
			}			
		}
	}
	
	function get_tr_text(row) {
		var td = row.getElementsByTagName("td")[0];
		return td.innerHTML;
	}
	
	/**//*
	this.dataRows为this.oTBody的一个引用，
	移除this.oTBody那么this.dataRows引用将销失,
	code:this.dataRows = tempRows;恢复原始操作行集合.
	*/
	this.__dataRows__ = tempRows;
	this.__oTBody__ = newTBody;
	this.__createListAllPagerBar__();
};

Page.prototype.__createListAllPagerBar__ = function(){

	var container = document.getElementById("pagerBar");
	if(!container){
		return;
	}
	var bar = '<a href="#" onclick="page.backFirstPage();return false;">閉じる</a>';
	container.innerHTML = bar;
}

Page.prototype.backFirstPage = function(){
	page.pageIndex=1;
	page.firstPage();
	$('cartSidebar').style.width = "223px";
	$('cartSide1').style.width = "213px";
	$('cartSide1').style.backgroundRepeat = "no-repeat";
	$('cartSide2').style.width = "213px";
	$('cartSide2').style.backgroundRepeat = "repeat-y";
	$('txtRemark').style.width = "180px";
	$('cartSide3').style.width = "213px";
	$('cartSide3').style.backgroundRepeat = "no-repeat";
	$('tblComponents').style.width="190px";
	$('tblComponents').align="center";
	$('cartSidebar').style.margin = "0 0 0 725px";
	$('cellFlag').value = 0;
}


/* ページコントロールバーを作成する */
Page.prototype.__createPagerBar__ = function(){
	var container = document.getElementById("pagerBar");
	if(!container){
		return;
	}
	var prevLink,nextLink = '';
	
	if(this.pageIndex <= 0){
		prevLink = "前ページ";
	}else{
		prevLink = '<a href="#" onclick="page.prePage();return false;">前ページ</a>';
	}
	if((this.pageIndex >= this.pageCount - 1) || isNaN(this.pageCount)){
		nextLink = "次ページ";
	}else{
		nextLink = '<a href="#" onclick="page.nextPage();return false;">次ページ</a>';
	}
	var bar = prevLink + "&nbsp" + nextLink;
	if((this.pageIndex < this.pageCount - 1) || (this.pageCount > 1)) {
		bar += "<br/>" +
		'<a href="#" onclick="page.listAll();return false;">すべての構成を見る</a>';
	}
	
		container.innerHTML = bar;
	
}