<!--
/**
 * grid.js
 * @fileoverview 列表类，固定表头，支持排序、过滤、拖动
 * @author Lynk Li
 */

/**
 * @class Grid
 * 列表类
 * @constructor
 * @author Lynk Li
 * @param String grid 列表容器ID
 * @param String titleDiv 标题Div ID
 * @param String titleTb 标题table ID
 * @param String dataDiv 数据Div ID
 * @param String dataTb 数据table ID
 * @param String titleInner 标题内部Div ID
 * @param String scroller 滚动Div ID
 * @param String popDiv 弹出Div ID 
 */
function Grid(grid, titleDiv, titleTb, dataDiv, dataTb, titleInner, scroller, popDiv, gvarName, tplName) {
	this.oGrid = document.getElementById(grid);
	this.oTitleDiv = null;
	this.oTitleTb = null;
	this.oTitleInner = null;
	this.oDataDiv = null;
	this.oDataTb = null;
	this.oScroller = null;
	this.oPopDiv = document.getElementById(popDiv);
	this.currentColum = null;
	this.currentTH = null;
	this.currentX = 0;
	this.sortColum = 0;
	this.sortStart = 0;
	this.sortEnd = 0;
	this.title = [];
	this.data = [];
	this.filter = [];
	this.sortObj = null;
	this.imagesDir = _setting.imagesPath;
	this.gvarName = gvarName;
	this.tplName = tplName;
	this.sortFn = function(a, b) {
		if (a.cells[_mmgrid.sortColum].innerText > b.cells[_mmgrid.sortColum].innerText) {
			return 1;
		} else if (a.cells[_mmgrid.sortColum].innerText == b.cells[_mmgrid.sortColum].innerText) {
			return 0;
		}
		return -1;
	};
	this.sortFnNum = function(a, b) {
		if (_mmgrid.intval(a.cells[_mmgrid.sortColum].innerText) > _mmgrid.intval(b.cells[_mmgrid.sortColum].innerText)) {
			return 1;
		} else if (_mmgrid.intval(a.cells[_mmgrid.sortColum].innerText) == _mmgrid.intval(b.cells[_mmgrid.sortColum].innerText)) {
			return 0;
		}
		return -1;
	}
	this.intval = function(s) {
		if (!s || s == "") return 0;
		return parseInt(s);
	}
	this.insert = function(d) {
		this.data.push(d);
		var oTr = this.oDataTb.insertRow();
		for (var i = 0; i < this.title.length; i++) {
			var oTd = oTr.insertCell();
			eval("var value = d." + this.title[i].id + ";");
			oTd.innerHTML = value;
			oTd.style.display = this.title[i].display ? "" : "none";
		}
	};
	this.remove = function() {
	};
	this.resizeCell = function(col, width) {
		if (this.oDataTb == null) return;
		var cloumIndex = this.getColumIndex(col);
		if (cloumIndex == -1) return;
		for (var i = 0; i < this.oDataTb.rows.length; i++) {
			var dataDiv = this.oDataTb.rows[i].cells[cloumIndex].childNodes[0];
			dataDiv.style.width = parseInt(width) + 2;
		}
		this.oDataDiv.style.width = this.oTitleTb.clientWidth;
	};
	this.getColumIndex = function(col) {
		for (var i = 0; i < this.title.length; i++) {
			if (this.title[i].id == col) {
				return i;
			}
		}
		return -1;
	};
	this.resort	= function(col, obj) {
		this.sortColum = this.getColumIndex(col);
		if (this.sortColum == -1) return;
		var newRows = new Array();		
		for (var i = 0; i < this.oDataTb.rows.length; i++) {
			newRows[i] = this.oDataTb.rows[i];
		}
		
		if (col == "visitTimes") {
			newRows.sort(this.sortFnNum);
		} else {
			newRows.sort(this.sortFn);
		}
		
		if (this.sortObj != null) {
			this.sortObj.innerHTML = this.sortObj.innerText;
		}
		this.sortObj = obj;
		var sortdir = 0;
		if (obj.getAttribute('sortdir') && obj.getAttribute('sortdir') == "0") {
			obj.setAttribute('sortdir', '1');
			sortdir = 1;
			obj.innerHTML = obj.innerText + "<img src='" + this.imagesDir + "stat_xulie_down.gif' border='0' />";
		} else {
			obj.setAttribute('sortdir', '0');
			obj.innerHTML = obj.innerText + "<img src='" + this.imagesDir + "stat_xulie_up.gif' border='0' />";
		}
		if (sortdir == 1) newRows.reverse();
		for (var i = 0; i < newRows.length; i++) {
			this.oDataTb.tBodies[0].appendChild(newRows[i]);
		}
	};
	this.show = function() {
		if (!this.oGrid) return;
		var datas = [];
		for (var i = 0; i < this.data.length; i++) {
			var obj = [];
			for (var j = 0; j < this.title.length; j++) {
				var display = "";
				if (this.title[j].display) display = this.title[j].display;
				var align = ",align:'left'";
				if (this.title[j].align) align = ",align:'" + this.title[j].align + "'";
				eval("obj[j] = {value:this.data[i]." + this.title[j].id + ",width:" + this.title[j].width + align + ",display:'" + display + "'};");
			}
			datas.push(obj);
		}
		var data = {
			gvarName:gvarName,
			titles	: this.title,
			datas	: datas
			};
		var str = TrimPath.processDOMTemplate(tplName, data);
		this.oGrid.innerHTML = str;
		if (this.oTitleTb == null) this.oTitleTb = document.getElementById(titleTb);
		if (this.oDataTb == null) this.oDataTb = document.getElementById(dataTb);
		if (this.oTitleDiv == null) this.oTitleDiv = document.getElementById(titleDiv);
		if (this.oDataDiv == null) this.oDataDiv = document.getElementById(dataDiv);
		if (this.oTitleInner == null) this.oTitleInner = document.getElementById(titleInner);
		if (this.oScroller == null) this.oScroller = document.getElementById(scroller);
		this.oDataDiv.style.width = this.oTitleTb.clientWidth;
	};
	this.mouseDownEvent = function(col, ev) {
		if (this.currentTH != null) return;
		var obj = document.elementFromPoint(ev.x, ev.y);
		if (obj.tagName.toLowerCase() == "th") {
			var objL = document.elementFromPoint(ev.x - 1, ev.y);			
			if (objL.tagName.toLowerCase() == "th") {
				obj = objL;
			}
			this.currentX = ev.x;
			this.currentColum = col;
			this.currentTH = obj.childNodes[0];
			this.currentTH.setCapture();
		} else {			
			var objR = document.elementFromPoint(ev.x + 2, ev.y);
			if (objR.tagName.toLowerCase() == "th") {
				obj = objR;
				this.currentX = ev.x;
				this.currentColum = col;
				this.currentTH = obj.childNodes[0];
				this.currentTH.setCapture();
			}
		}
	};
	this.mouseUpEvent = function(col, ev) {
		if (this.currentTH != null) {
			this.currentTH.releaseCapture();
			this.currentTH = null;
			this.currentColum = null;
		}
	};
	this.mouseMoveEvent = function(col, ev) {
		if (this.currentTH != null) {
			if (this.currentColum != col) this.currentColum = col;
			var cw = parseInt(this.currentTH.clientWidth);
			//if (cw == 0) cw = 80;
			var width = Math.round(cw + ev.x - this.currentX);
			if (width < 10) width = 10;
			this.currentTH.style.width = width;
			this.resizeCell(this.currentColum, this.currentTH.style.width);
			this.currentX = ev.x;
		}
	};
	this.oldScrollLeft = 0;
	this.scrollEvent = function(ev) {
		this.oTitleDiv.style.display = "block";
		this.oTitleDiv.style.visibility = "visible";
		this.oTitleDiv.style.top = "127px";
		var contentLeft = document.getElementById("contentLeft");
		
		
		if (contentLeft.style.display == "none") {
			if (_common.ieVersionFlag == 6) {
				this.oTitleDiv.style.left = "7px";
			} else {
				this.oTitleDiv.style.left = "7px";
			}			
		} else {
			if (_common.ieVersionFlag == 6) {
				this.oTitleDiv.style.left = "121px";
			} else {
				this.oTitleDiv.style.left = "120px";
			}
		}
		this.oTitleInner.scrollLeft = this.oScroller.scrollLeft;
		var scrollLeft = this.oScroller.scrollLeft;
		if (this.oldScrollLeft == scrollLeft) return;
		this.oldScrollLeft = scrollLeft;
	};
	this.showFilter = function(col) {
		if (eval("this.filter." + col)) {
			var filter = eval("this.filter." + col);
			if (filter.length > 0) {
				var data = {
					images_path	: _setting.imagesPath,
					col		: col,
					filters : filter,
					confirmFn : gvarName + ".setFilter('" + col + "');"
					};
				var str = TrimPath.processDOMTemplate("monitor_filter_status_tpl", data);
				this.oPopDiv.innerHTML = str;
				this.oPopDiv.className = "";
				this.oPopDiv.style.position = "absolute";
				this.oPopDiv.style.left = event.x;
				this.oPopDiv.style.top = event.y;
				this.oPopDiv.style.display = "block";
			}
		}
		event.returnValue=false;
		return false;
	};
	this.setFilter = function(col) {
		this.oPopDiv.style.display = "none";
		var oChecks = document.getElementsByName("filter_checkbox_" + col + "[]");
		var fl = [];
		for (var i = 0; i < oChecks.length; i++) {
			var filt = oChecks[i].checked ? 0 : 1;
			var obj = {name:oChecks[i].value, filter:filt};
			fl.push(obj);
		}
		eval("this.filter." + col + " = fl;");
		this.doFilter(col);
	};
	this.doFilter = function(col) {
		var cloumIndex = this.getColumIndex(col);
		if (cloumIndex == -1) return;
		var filter = eval("this.filter." + col);
		var aShow = [];
		for (var i = 0; i < filter.length; i++) {
			var obj = filter[i];
			if (obj.filter == 0) aShow.push(obj.name);
		}
		for (var i = 0; i < this.oDataTb.rows.length; i++) {
			var oTr = this.oDataTb.rows[i];
			var value = oTr.cells[cloumIndex].childNodes[0].innerText;
			if (col == "snid") {
				if (aShow.length == 2) oTr.style.display = "block";
				else if (aShow.length == 0) oTr.style.display = "none";
				else {
					var snReg = /^[0-9]+$/g;
					if (aShow.has(_langPackage.monitor.filter.snid[0])) {
						if (snReg.test(value)) oTr.style.display = "none";
						else oTr.style.display = "block";
					} else {						
						if (snReg.test(value)) oTr.style.display = "block";
						else oTr.style.display = "none";
					}
				}
			} else {
				if (!aShow.has(value)) oTr.style.display = "none";
				else oTr.style.display = "block";
			}
		}
		_monitor.intervalColor();
	};
	this.setShowCols = function(checksName) {
		var oChecks = document.getElementsByName(checksName);
		for (var i = 0; i < oChecks.length; i++) {
			var cloumIndex = this.getColumIndex(oChecks[i].value);
			if (cloumIndex == -1) continue;
			if (oChecks[i].checked) this.title[cloumIndex].display = "block";
			else this.title[cloumIndex].display = "none";
		}
		
		for (var i = 0; i < this.title.length; i++) {
			var oTd = this.oTitleTb.rows[0].cells[i];
			oTd.style.display = this.title[i].display;
		}
		for (var i = 0; i < this.oDataTb.rows.length; i++) {
			for (var j = 0; j < this.title.length; j++) {
				var oTd = this.oDataTb.rows[i].cells[j];
				oTd.style.display = this.title[j].display;
			}
		}
		this.oDataDiv.style.width = parseInt(this.oGrid.clientWidth) - 17 + this.oGrid.scrollLeft + "px";
		this.oTitleDiv.style.display = "block";
		this.oTitleDiv.style.visibility = "visible";
		this.oTitleDiv.style.top = "127px";
		var contentLeft = document.getElementById("contentLeft");
		if (contentLeft.style.display == "none") {
			if (_common.ieVersionFlag == 6) {
				this.oTitleDiv.style.left = "7px";
			} else {
				this.oTitleDiv.style.left = "7px";
			}			
		} else {
			if (_common.ieVersionFlag == 6) {
				this.oTitleDiv.style.left = "121px";
			} else {
				this.oTitleDiv.style.left = "120px";
			}
		}
	};
}
//-->