
function Page(pageSize,container,instanceName){
	
	this.name = instanceName;
	this.pageSize = pageSize;
	this.container = container;
	this.cols = [];
	this.current = 1;
	this.dataList =null;
	this.all=0;
	if(this.instanceListenner){
		this.instanceListenner();
	}
	this.init();
	
}

/*
 * 
 */
Page.prototype.init = function(){
	this.conObj = document.getElementById(this.container);
	if(!this.conObj){
		alert("can not find container");
		return false;
	}
	
	this.table = document.createElement("table");
	this.table.setAttribute("id","gridTable",0);
	this.table.setAttribute("width","96%",0);
	this.table.setAttribute("align","center",0);
	this.table.className="dpTable";
	this.conObj.innerHTML="";
	this.conObj.appendChild(this.table);
	
};

Page.prototype.setWaiter=function(){
	
	var tbody = this.table.tBodies[0];
	if(tbody){
		while(tbody.rows[0]){
			if(tbody.rows[0].removeNode){
				tbody.rows[0].removeNode(true);
			}else{
				tbody.rows[0].parentNode.removeChild(tbody.rows[0]) 
			}
		}
		var cell = tbody.insertRow(-1).insertCell(-1);
		cell.colSpan=this.cols.length;
		cell.innerHTML="<div class='page_waiter'></div>";
	}
};

Page.prototype.getNextPage = function(){
	return this.current+1;
};

Page.prototype.getNextPageBtn = function(){
	if(this.current <this.getPageCount()){
		return "<a href='#' onclick='"+this.name+".next()'>下一页</a>";
	}else{
		return "下一页";
	}
};

Page.prototype.getPrePage = function(){
	return this.current-1;
};
Page.prototype.getPrePageBtn = function(){
	if(this.current>1){
		return "<a href='#' onclick='"+this.name+".previous()'>上一页</a>";
	}else{
		return "上一页";
	}
};

Page.prototype.getFirstPage = function(){
	return 1;
};

Page.prototype.getFirstPageBtn = function(){
	if(this.current>1){
		return "<a href='#' onclick='"+this.name+".first()'>首页</a>";
	}else{
		return "首页";
	}
};

Page.prototype.getLastPage = function(){
	return this.getPageCount();
};

Page.prototype.getLastPageBtn = function(){
	
	if(this.current<this.getPageCount()){
		return "<a href='#' onclick='"+this.name+".last()'>最末页</a>";
	}else{
		return "最末页";
	}
	
};

Page.prototype.getPageCount = function(){
	if(this.all<this.pageSize){
		return 1;
	}
	if(this.all%this.pageSize === 0){
		return parseInt(this.all/this.pageSize);
	}
	if(this.all%this.pageSize>0){
		return parseInt(this.all/this.pageSize)+1;
	}
};

Page.prototype.next = function(){
	
	
	if(this.current < this.getPageCount()){
		this.current =  this.current +1;
		this.setWaiter();
		this.load(this.current,this.pageSize);
		
	}
	
	//this.createGrid();
};

Page.prototype.previous = function(){
	if(this.current>1){
		this.current = this.current - 1;
		this.setWaiter();
		this.load(this.current,this.pageSize);
		
	}
	
};

Page.prototype.first = function(){
	this.current =1;
	this.setWaiter();
	this.load(this.current,this.pageSize);
	
};

Page.prototype.last = function(){
	this.current = this.getPageCount();
	this.setWaiter();
	this.load(this.current,this.pageSize);
	
};

Page.prototype.createTHead = function(){
	//debugger;
	if(this.beforeCreateTHead){
		this.beforeCreateTHead();
	}
	
	if(this.cols){
		var thead = this.table.createTHead();
		
		var row = thead.insertRow(-1);
		
		for(var i=0;i<this.cols.length;i++){
			
			var cell = row.insertCell(-1);
			var obj = this;
			
			cell.innerHTML=this.cols[i].title;
			if(this.cols[i].isSort){
				cell.setAttribute("sortCol",this.cols[i].col);
				cell.setAttribute("index",this.cols[i].objectIndex);
				cell.onclick = function(){
					obj.sort(this);
				};
			}
		}
	}
	
	this.setWaiter();
	if(this.afterCreateTHead){
		this.afterCreateTHead();
	}
};

Page.prototype.createGrid =  function(){
	
	var me = this;
	if(this.beforeCreateGrid){
		this.beforeCreateGrid();
	}
	var tbody = this.table.tBodies[0];
	
	if(!tbody){
		tbody = document.createElement("tbody");
		this.table.appendChild(tbody);
	}
	
	if(tbody){
		
		while(tbody.rows[0]){
			if(tbody.rows[0].removeNode){
				tbody.rows[0].removeNode(true);
			}else{
				tbody.rows[0].parentNode.removeChild(tbody.rows[0]);
			}
		}
		
		var pageDataList = this.dataList;
		if(pageDataList.length==0){
			var row = tbody.insertRow(-1);
			var cell = row.insertCell(-1);
			cell.colSpan = me.cols.length;
			cell.align = "center";
			cell.innerHTML = "暂无相关信息";
		}
		
		var className = "odd";
		for(var j=0;j<pageDataList.length;j++){
			var row = tbody.insertRow(-1);
			row.className=className;
			if(className=="odd"){
				className="even";
			}else{
				className="odd";
			}
			for(var t =0;t<this.cols.length;t++){
				cell = row.insertCell(-1);
				
				cell.innerHTML = this.cols[t].getCol(pageDataList[j]);
				
			}
			//扩展 onclick 事件
			
			if(row.attachEvent){
				row.attachEvent("onclick",
					function(){
						if(me.onRowClick){
							me.onRowClick(event);
						}
					}
				);
			}else if(row.addEventListener){
				
				row.addEventListener("click",
					function(){
						if(me.onRowClick){
							me.onRowClick(event);
						}
					}
					,false
				);
			}
			
			
			if(me.onCreateRow){
				me.onCreateRow(row,pageDataList[j]);
			}
			
		}
	}
	
	
	if(this.afterCreateGrid){
		this.afterCreateGrid();
	}
	
};

Page.prototype.loadReply = function(data){
	this.dataList = data.list;
	
	this.all = data.all;
	this.createGrid();
    this.createNumBar();
    if(this.afterLoad){
		
    	this.afterLoad();
    }
//    if(this.dataList && this.dataList.length>0){
//	    //this.conObj.innerHTML="";
//		//this.conObj.appendChild(this.table);
//    }else{
//    	if(!this.empty){
//    		this.empty = "<div align='center'>没有数据！</div>";
//    	}
//    	this.conObj.innerHTML=this.empty;
//    	if(this.numBar){
//    		document.getElementById(this.numBar).innerHTML="";
//    	}
//    }
};

Page.prototype.gotoPage = function(n){
	this.current = n;
	this.load(this.current,this.pageSize);
	this.setWaiter();
};

Page.prototype.sortDESC = function(index,prop){
	var dataList = this.dataList;
	//debugger;
	for(var j=0;j<dataList.length;j++){
		
		for(var i=0;i<(dataList.length-j);i++){
			if(i>0){
				var data1;
				var data0;
				if(dataList[i].length){
					data1 = dataList[i][index];
					data0 = dataList[i-1][index];
				}else{
					data1 = dataList[i];
					data0 = dataList[i-1];
				}
				if(data1[prop]>data0[prop]){
					var tempData = dataList[i-1];
					dataList[i-1]=dataList[i];
					dataList[i]=tempData;
				}
			}
		}
	}
	this.data = dataList;
	this.createGrid();
};

Page.prototype.sortASC = function(index,prop){
	var dataList = this.dataList;
	
	//debugger;
	for(var j=0;j<dataList.length;j++){
		
		for(var i=0;i<(dataList.length-j);i++){
			if(i>0){
				var data1;
				var data0;
				if(dataList[i].length){
					data1 = dataList[i][index];
					data0 = dataList[i-1][index];
				}else{
					data1 = dataList[i];
					data0 = dataList[i-1];
				}
				if(data1[prop]<data0[prop]){
					var tempData = dataList[i-1];
					dataList[i-1]=dataList[i];
					dataList[i]=tempData;
				}
			}
		}
	}
	
	this.data = dataList;
	this.createGrid();
};

Page.prototype.sort = function(obj){
	//debugger;
	if(window.event){
		obj = event.srcElement;
	}
	
	if(obj.tagName == "IMG" ){
		obj = obj.parentElement;
	}
	
	if(this.currentSortImg){
		this.currentSortImg.style.display="none";
	}
	
	var prop = obj.getAttribute("sortCol");
	var notes = obj.childNodes;
	
	var img;
	
	for(var i=0;i<notes.length;i++){
		if(notes[i].tagName == "IMG" && notes[i].name=="sort"){
			img = notes[i];
		}
	}
	
	//debugger;
	if(img){
		this.currentSortImg = img;
		img.style.display="";
		if(img.getAttribute("sortType") =="asc"){
			
			this.sortDESC(obj.getAttribute("index"),prop);
			img.setAttribute("sortType","desc",0);
			img.src="images/desc.gif";
		}else if(img.getAttribute("sortType") == "desc"){
			
			this.sortASC(obj.getAttribute("index"),prop);
			img.setAttribute("sortType","asc",0);
			img.src="images/asc.gif";
		}
	}else{
		
		this.sortDESC(obj.getAttribute("index"),prop);
		var image = new Image("","sort","images/desc.gif");
		var imgObj = image.newImage();
		imgObj.setAttribute("sortType","desc",0);
		obj.appendChild(imgObj);
		this.currentSortImg = imgObj;
	}
	
	
	//alert(obj.outerHTML);
};

Page.prototype.pageAll = function(objId){
	var obj = document.getElementById(objId);
	if(obj){
		obj.innerHTML = this.all;
	}
};

Page.prototype.setNumBar = function(barCon){
	this.numBar = barCon;
};

Page.prototype.createNumBar = function(){
	
	var barConObj;
	if(this.numBar){
		barConObj = document.getElementById(this.numBar);
	}
	if(barConObj){
		var start = this.current - 5;
		var end = this.current + 5;
		var barStr ="";
		barStr="共"+this.getPageCount()+"页/第"+this.current+"页";
		for(var i=start;i<=end;i++){
			if(i>0 && i<=this.getPageCount()){
				barStr+=" <label class=\"page_num\" style=\"cursor:pointer;\" onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\" onclick=\""+this.name+".gotoPage("+i+")\">"
				barStr+=(i==this.current)?"<b style='color:red'>"+i+"</b>":i;
				barStr+="</label> ";
			}
		}
		barStr += "    "+this.getFirstPageBtn() +" "+ this.getPrePageBtn() +" "+ this.getNextPageBtn() +" "+ this.getLastPageBtn();
		barConObj.align="right";
		barConObj.style.width="96%";
		barConObj.innerHTML=barStr;
	}
};

Page.prototype.appendCol = function(col){
	this.cols.push(col);
};


Page.prototype.create = function(){
	
	if(this.beforeCreate){
		this.beforeCreate();
	}
	
	this.createTHead();
	if(this.beforeLoad){
		this.beforeLoad();
	}
	
	//debugger;
	this.load(1,this.pageSize);
	
};

Page.prototype.reload = function(currentPage){
	if(currentPage){
		this.current = currentPage;
	}
	this.load(this.current,this.pageSize);
}
function SimpleCol(title,index,col,isSort){
	this.title = title;
	this.objectIndex = index;
	this.col = col;
	this.isSort = isSort;
	this.getCol = function(data){
		
		try {
			if (typeof data[index][this.col] == "undefined") {
				return data[this.col]?data[this.col]:"";
			}
			return data[index][this.col]?data[index][this.col]:"";
		} catch (Ex) {
			return data[this.col]?data[this.col]:"";
		}
	};
}
function ArrayCol(title,col,isSort){
	this.title = title;
	this.col = col;
	this.isSort = isSort;
	this.getCol = function(data){
		return data[this.col];
	}
	
	
}
function Image(id,name,src){
	this.id = id;
	this.name = name;
	this.src = src;
};

Image.prototype.newImage = function(){
	var img = document.createElement("IMG");
	img.setAttribute("name",this.name,0);
	img.setAttribute("id",this.id,0);
	img.setAttribute("src",this.src,0);
	return img;	
};

/**
 *需要Date的拓展
 */
function DateCol(title,index,col,isSort,format){
	this.title = title;
	this.objectIndex = index;
	this.col = col;
	this.isSort = isSort;
	this.getCol = function(data){
		var value = null;
		if(data.length){
			value = data[index][this.col];
		}else{
			value = data[this.col];
		}
		if(value)
			return value.format(format);
		return "-";
	};
}


/**
 * 行号列
 */
function RowNumberCol(title,pageInstance){
	var fun = pageInstance.beforeCreateGrid;
	var me = this;
	pageInstance.beforeCreateGrid = function(){
		me.num = 0;
		if(fun){
			fun();
		} 
	}
	this.title = title;
	this.num = 0;
	this.isSort = false;
	this.getCol = function(data){
		this.num++;
		return this.num;
	};
}
