/*----------------------------------------------------------------
西门吹雪
Email:sabesabe3312@163.com
MSN:sabesabe3312@163.com
QQ:149561420
Run At:FireFox,Opera,IE5.0,IE5.5,IE6.0
Please keep this Info.
----------------------------------------------------------------*/
function resizeObj(){
	this.obj	=null;
	this.grabX	=null;
	this.grabY	=null;
	this.width	=null;
	this.cursor	="col-resize";
}

function JGrid(pWidth,pHeight,pParent){
	width		=parseInt(pWidth)					|| "100%";
	height		=parseInt(pHeight)					|| "100%";
	body		=document.getElementById(pParent)	|| document.body;
	rowHeight			=23;//行高
	titleHeight			=26;//标题高度
	rowCountBlockWidth	=35;//左则显示行数的块的宽度

	resizeField	=null;
	//----------------------------------------------------------
	//加this是全局，在它之内，就不在用this来变引用类的变量了。

	//可以用parent来指示。

	//以下函数由外部调用。用由动态调整控件的大小。

	//----------------------------------------------------------
	this.setWidthHeight=function(pWidth,pHeight){
		parent.width=pWidth;
		parent.height=pHeight;
	}
	
	this.setRowHeight=function(pRowHeight){
		if(!isNaN(pRowHeight))
			parent.rowHeight=pRowHeight;
	}
	
	this.setTitleHeight=function(pTitleHeight){
		if(!isNaN(pTitleHeight))
			parent.titleHeight=pTitleHeight;
	}
	
	var oOutLine;
	createOutLine=function(){
		oOutLine=document.createElement("DIV");
		oOutLine.className="outLine";
		
		with(oOutLine.style){
			width	=(this.width=="100%") ? "100%" : this.width + "px";
			height	=(this.height=="100%") ? "100%" : this.height + "px";
			overflow="auto";
			position="relative";
		}
		this.body.appendChild(oOutLine);
		
		oOutLine.onselectstart=function(){
			return true;	
		}		
		
		oOutLine.onclick=function(){}
		oOutLine.onscroll=function(){
			//opera的DIV没有这个事件，奇怪。

			oTopLeftBlock.style.top		=oTitleBlock.style.top		=oOutLine.scrollTop+"px";
			oTopLeftBlock.style.left	=oRowCountBlock.style.left	=oOutLine.scrollLeft+"px";
		}
		
		oOutLine.onmouseup=function(){
			this.style.cursor="default";
		}
	}
	
	var oTopLeftBlock;
	createTopLeftBlock=function(){
		oTopLeftBlock=document.createElement("DIV");
		oTopLeftBlock.className="topLeftBlock";
		
		with(oTopLeftBlock.style){
			width	=rowCountBlockWidth+"px";
			height	=titleHeight+"px";
			position="relative";
			top="0px";
			left="0px";
			zIndex="100000";
		}
				
		oOutLine.appendChild(oTopLeftBlock);
	}
	
	var oRowCountBlock;
	createRowCountBlock=function(){
		oRowCountBlock=document.createElement("DIV");
		oRowCountBlock.className="rowCountBlock";
		
		with(oRowCountBlock.style){
			width	=rowCountBlockWidth+"px";
			height	="100%";
			position="absolute";
			left="0px";
			top=titleHeight+1+"px";
			//因为position的值为relative，所以。。。relative只对高度有效。

			//top=titleHeight+"px";
			zIndex=oTopLeftBlock.style.zIndex-1;
		}
		oOutLine.appendChild(oRowCountBlock);
	}
	
	var oTitleBlock;
	createTitleBlock=function(){
		oTitleBlock=document.createElement("DIV");
		oTitleBlock.className="titleBlock";
		with(oTitleBlock.style){
			left=rowCountBlockWidth+"px";
			height=titleHeight+"px";
			top="0px";
			position="absolute";
			zIndex=oTopLeftBlock.style.zIndex-2;
		}
		oOutLine.appendChild(oTitleBlock);
	}
	
	
	var oDataBlock;
	createDataBlock=function(){
		oDataBlock=document.createElement("DIV");
		oDataBlock.className="dataBlock";
		
		with(oDataBlock.style){
			left=rowCountBlockWidth+1+"px";
			top=titleHeight+1+"px";
			position="absolute";
			overflow="visible";
			zIndex=oTopLeftBlock.style.zIndex-3;
		}
	
		oOutLine.appendChild(oDataBlock);
	}
	
	this.createTitle=function(pTitleArray){
		//[caption,width,align];
		var i,tTitleItem;
		var tCaption,tWidth,tAlign;
		var tRealWidth=0;
		for(i=0;i<pTitleArray.length;i++){
			tCaption	=pTitleArray[i][0]				|| "未设置";
			tWidth		=parseInt(pTitleArray[i][1])	|| 100;
			tRealWidth	+=tWidth;
			tAlign		=pTitleArray[i][2]				||	"left";
			createTitleItem(tCaption,tWidth,tAlign,i);
		}
		oTitleBlock.style.width=tRealWidth+"px";
	}
	
	createTitleItem=function(pCaption,pWidth,pAlign,pIndex){
		var oTitleItem=document.createElement("DIV");
		oTitleItem.className="titleItemNormal";
		oTitleItem.innerHTML=pCaption;
	
		with(oTitleItem.style){
			try{//因为有一两个属性在fireFox下不能使用，所以，要try
				width=pWidth+"px";
				//善用height=100%:下面这个不能用。

				//height="100%";
				height=oTitleBlock.style.height;
				position="absolute";
				lineHeight=oTitleBlock.style.height;
				if(oTitleBlock.lastChild!=null)
					left=oTitleBlock.lastChild.offsetLeft+oTitleBlock.lastChild.offsetWidth+"Px";
				else
					left="0px";
				
				overflow="hidden";
				wordBreak="keep-all";
				textOverflow="ellipsis"
			}catch(e){}
		}
		
		oTitleItem.onselectstart=function(){
			return false;	
		}
		
		oTitleItem.onmouseover=function(){
			//alert(this.tagName);
			if(!JGrid.isMoving)
				this.className="titleItemActive"
		}
		
		oTitleItem.onmouseout=function(){
			if(!JGrid.isMoving)
				this.className="titleItemNormal";
		}

		oTitleItem.onmousedown=function(evt){
			JGrid.isMoving = true;
			if(!evt) evt=event;
			var obj=evt.srcElement || evt.target;
			if(obj.className!="splitBar") return false;
			
			//这里，应先让click事件失效，然后在mouseup里，click事件有效。

			
			resizeField=new resizeObj();
			resizeField.obj	=obj;
			with(resizeField){
				obj.style.cursor=cursor;
				obj.parentNode.cursor=cursor;
				grabX	=evt.clientX;
				width	=obj.parentNode.clientWidth;
				evt.returnValue = false;
				evt.cancelBubble = true;
			}				
		}
		
		oTitleItem.onmouseup=function(){
			if(resizeField){
				resizeField=null;
				oDataTable.style.width=this.parentNode.style.width=parseInt(this.parentNode.lastChild.style.left)+parseInt(this.parentNode.lastChild.style.width)+"px";
				//FireFox好像不支持cells[i,j]的格式。

				try{oDataTable.rows[0].cells[pIndex].style.width=this.style.width}catch(e){};
				oOutLine.onscroll();
			}
			JGrid.isMoving = false;
		}
		
		oTitleItem.onmousemove=function(evt){
			if(resizeField){			
				if(!evt) evt=event;
				var xMin=8;
				//resizeField.obj.parentNode.style.width=Math.max(xMin,resizeField.width + evt.clientX - resizeField.grabX)+"px";
				resizeField.obj.parentNode.style.width=resizeField.width + evt.clientX - resizeField.grabX+"px";
				//把下面这个鸡肋去掉，动作流畅一些。

				//oDataTable.style.width=this.parentNode.style.width=parseInt(this.parentNode.lastChild.style.left)+parseInt(this.parentNode.lastChild.style.width)+"px";
				//try{oDataTable.rows[0].cells[pIndex].style.width=this.style.width}catch(e){};
				resizeField.obj.parentNode.positionChange();
			}
			event.cancelBubble = true;
		}
				
		oTitleItem.positionChange=function(){
			try{
				this.nextSibling.style.left=parseInt(this.style.left)+parseInt(this.style.width)+"px";
				this.nextSibling.positionChange();
			}catch(e){}
		};
		
		oTitleItem.appendChild(createSplitBar());
		oTitleBlock.appendChild(oTitleItem);
	}
	
	createSplitBar=function(){
		var splitBar=document.createElement("DIV");
		splitBar.className="splitBar";
		with(splitBar.style){
			right="0px";
			position="absolute";
			overflow="visible";
		}
		
		var tmpDiv=document.createElement("DIV");
			tmpDiv.innerHTML="不让浏览器把本层做为浮动层";
			tmpDiv.style.display="none";
		
		splitBar.appendChild(tmpDiv);
		
		return splitBar;
	}
	
	var oDataTable,oLastClick;//最后一次点击的行。;
	this.tackData=function(pDataTable){
		if(!document.getElementById(pDataTable)){
			alert("没有找到数据表格："+pDataTable);
			return;
		}
			
		oDataTable=document.getElementById(pDataTable)
		oDataTable.style.tableLayout="fixed";
		try{
			oDataTable.border=0;
			oDataTable.cellPadding=0;
			oDataTable.cellSpacing=0;
			oDataBlock.appendChild(oDataTable);
		}catch(e){
			//
		}
		
		if(oDataTable.rows.length==0) return;
		oDataTable.style.width=oTitleBlock.clientWidth+"px";
		var i,tCell;
		for(i=0;tCell=oDataTable.rows[0].cells[i];i++){
			tCell.width=parseInt(oTitleBlock.childNodes[i].style.width);
		}
		
		
		oLastClick=null;
		oFlagRowCount=null;
		oDataTable.onclick=function(evt){
			//try{alert(oLastClick.rowIndex)}catch(e){}
			if(!evt) evt=event;
			if(evt.srcElement)
				tO=evt.srcElement;
			else
				tO=evt.target;
			
			if(tO.tagName=="TD"){
				try{
					oLastClick.className=oLastClick.getAttribute("baseStyle");
					
					oRowCountBlock.childNodes[oLastClick.rowIndex].innerHTML=oLastClick.rowIndex+1;
					try{oRowCountBlock.childNodes[oFlagRowCount].innerHTML=oFlagRowCount+1}catch(e){};
				}catch(e){}
				
				if(tO.parentNode!=oLastClick){
					tO.parentNode.className="rowClickStyle";
					oRowCountBlock.childNodes[tO.parentNode.rowIndex].innerHTML="<font color='#cc6600'>★<font>"	//鼠标点击后列首显示的图形，如果不想显示图形，则改成：<font color='#cc6600'>"+[tO.parentNode.rowIndex+1]+"<font>"
					try{oLastClick=tO.parentNode;}catch(e){}
				}else{
					oLastClick=null;
				}
			}
		}
		
		oDataTable.onmousemove=function(evt){
			if(!evt) evt=event;
			if(evt.srcElement)
				tO=evt.srcElement;
			else
				tO=evt.target;
				
			if(tO.tagName=="TD"){
				if(tO.parentNode.className!="rowClickStyle"){
					tO.parentNode.className="rowMoveStyle";
				}
			}
		}
		oDataTable.onmouseout=function(evt){
			if(!evt) evt=event;
			if(evt.srcElement)
				tO=evt.srcElement;
			else
				tO=evt.target;
				
			if(tO.tagName=="TD"){
				if(tO.parentNode.className=="rowMoveStyle"){
					tO.parentNode.className=tO.parentNode.getAttribute("baseStyle")
				}
			}
		}		
		
		createRowCount();
	}
	
	reTackData=function(){
		if(oDataTable.rows.length>0){
			var tRow,i;
			for(i=0;tRow=oDataTable.rows[i];i++){
				with(tRow){
					className=="rowClickStyle" ? className="rowClickStyle" : (i%2==0?className="rowStyle1" : className="rowStyle2");
					setAttribute("baseStyle",(i%2==0 ? "rowStyle1" : "rowStyle2"));
				}
			}
			
			//设定各表格的各列的宽度

			for(i=0;i<oDataTable.rows[0].cells.length;i++){
				if(oTitleBlock.childNodes.length>i)
					oDataTable.rows[0].cells[i].style.width=oTitleBlock.childNodes[i].style.width;
				else
					oDataBase.rows[0].cells[i].style.width="100px";
			}
		}
		window.status=(new Date-beginTime)
	}	
	
	var rowCount=0;
	createRowCount=function(){
		try{		
		var i,tRow;
		for(i=0;tRow=oDataTable.rows[i];i++){
			var oRowCount=document.createElement("DIV");
			oRowCount.className="rowCount"
			rowCount=i;
			oRowCount.innerHTML=i+1;
			
			(i%2==0) ? tRow.className="rowStyle1" : tRow.className="rowStyle2";
			tRow.setAttribute("baseStyle",tRow.className);
			
			tCell=tRow.cells[0];
			
			if(tCell.clientHeight>rowHeight){
				tCell.style.height=parseInt(tCell.clientHeight)+"px";
			}else{
				tCell.style.height=rowHeight+"px";
			}
			tCell.style.lineHeight=rowHeight+"px";

			with(oRowCount.style){
				position="relative";
				left="0px";
				width="100%";
				height=tCell.style.height
				//height=tRow.style.height;
				lineHeight=height;
			}
			oRowCountBlock.appendChild(oRowCount);
		}
		}catch(e){
			//
		}
	}
	
	this.create=function(){
		createOutLine();
		createTopLeftBlock();
		createRowCountBlock();		
		createTitleBlock();
		createDataBlock();
	}
}
JGrid.isMoving = false;