var LOC = {};
LOC.SE_MORE_THAN = "Plus de";
LOC.SE_NO_RESULTS = "Pas de résultats";

//TODO: security (chiffrement)
//TODO: like clauses (type_varchar, type_text) à mettre en place si on enregistre les données en bdd avec le prevent
mt.component.SearchEngine = function(name,dg_name,page_name,infos_count,infos_limit,advanced) {
	var self = this;
	
	//var TYPE_VARCHAR = 1;
	//var TYPE_TEXT = 2;
	this.bUseHttpPrevent = true;//unused
	
	this.sLabelElement = "";
	this.bFemaleKindElement = false;

	this.vHeaders = [];

	/** SQL */
	this.sSelectPart;
	this.sMainTable;
	this.sMainAliasTable;
	this.vOtherTables = [];
	this.vOtherTablesWithLeftJoin = [];
	this.vClauseInvariant = [];
	this.vFilter = [];
	this.sGeneratedRequest = "";
	this.sGroupByClause;
	this.bSelectWithDistinct;//unused
	this.sIdInTable;

	/** OPTIONS */
	this.iCurrentPage = 0;
	this.useCurrentPageFromPagination = false;
	this.iMaxElementsPerPage = 20;
	this.iGroupPerPage = 10;
	this.vResults;
	this.iTotalCountCriterias = 0;
	this.bMaxElementsCountUsed = true;
	this.bMaxElementsCountReach = false;
	this.iMaxElementsCount = 1000;
	this.iCurrentPageCount = 0;
	
	/** STYLE */
	this.paginationLinkClassName = "se_pagination";
	this.paginationIconPrefix = "blue_";
	this.headerIconPrefix = "blue_";
	this.headerLinkClassName = "se_header";
	this.lineBackground = "";
	this.lineBackgroundBis = "";
	
	this.initDataGrid = function(){
		this.dg = new mt.component.DataGrid(dg_name);
		this.dg.addStyle("width", "100%");
		this.onLoadDataGrid();
	}
	this.onLoadDataGrid = function() {};
	
	this.addOtherTable = function(tableName,jointure){
		var table = new mt.component.SearchEngineTable(tableName,jointure);
		this.vOtherTables.push(table);
	}
	this.addOtherTableWithLeftJoin = function(tableName,jointure){
		var table = new mt.component.SearchEngineTable(tableName,jointure);
		this.vOtherTablesWithLeftJoin.push(table);
	}
	this.addClauseInvariant = function(str) {
		this.vClauseInvariant.push(str);
	}
	/**
	* fields = Array
	* liste des champ de la recherche
	* si plusieurs champ alors ils sont séparé par un OR
	
	* values = Array
	* liste de valeurs recherchees
	* si plusieurs valeurs alors utilisation d'un IN
	
	* optLike = Boolean
	* si optLike = true alors utilisation de LIKE %% sinon =
	*
	* compareFilter = String
	* valeurs : =, >=, <>, <= etc
	*/

	this.addFilter = function(fields,values,optLike,compareFilter){
		if(!isNotNull(compareFilter))
			compareFilter = "=";
		var filter = new mt.component.SearchEngineFilter(fields,values,optLike,compareFilter);
		this.vFilter.push(filter);
		return filter;
	}
	this.setGroupByClause = function(group){
		this.sGroupByClause = group;
	}

	this.addHeader = function(title,select,order,direction){
		var header = new mt.component.SearchEngineHeader(title,select,order);
		header.direction = direction;
		this.vHeaders.push(header);
		return header;
	}

	this.render = function() {
		if(isNotNull(dg_name)){
			this.dg = new mt.component.DataGrid(dg_name);
			this.dg.addStyle("width", "100%");
		}
		if(isNotNull(name))
			this.domNode = $(name);
		if(isNotNull(page_name))
			this.domPage = $(page_name);
		if(isNotNull(infos_count))
			this.domCount = $(infos_count);
		if(isNotNull(infos_limit))
			this.domLimit = $(infos_limit);
			Element.hide(this.domLimit);
		if(isNotNull(advanced)){
			this.domAdvanced = $(advanced);	
			if (debugMode) Element.show(this.domAdvanced);
		}
	
		var headers = new Array();
		this.initDataGrid();
		this.initAdvancedFunction();
		this.vHeaders.each(function(header,index){
			
			var direction = "";
			var headerElement = document.createElement("div");
			var headerRemove = document.createElement("a");
			headerRemove.className = self.headerLinkClassName;
			var headerAdd = document.createElement("a");
			//headerAdd.className = self.headerLinkClassName;
			
			if(isNotNull(header.direction)){
				direction = "";

				headerRemove.href = "javascript:void(0);";
				headerRemove.onclick = function() {
				      self.removeOrderBy(header);
				}
				headerRemove.innerHTML = "&nbsp;<img style=\"vertical-align : middle;\" src=\"" 
									+ rootPath + "images/icones/fleches/" + self.headerIconPrefix+header.direction+".gif" + "\" alt=\"supprimer le tri\" title=\"supprimer le tri\" />";
				
				if( header.direction == "desc"){
					direction = "asc";
				}else{
					direction = "desc";
				}
			}else{
				direction = "desc";
			}
			if(!isNotNull(header.order)){
				headerAdd = document.createElement("label");
				headerAdd.innerHTML = header.title;
			}else{
				headerAdd.href = "javascript:void(0);";
				headerAdd.onclick = function() {
					self.addOrderBy(header,direction);
				}
				headerAdd.innerHTML = header.title;
			}

			headerElement.appendChild(headerAdd);
			headerElement.appendChild(headerRemove);
			headers.push(headerElement);
		});
		this.dg.setHeader(headers);
	}
	
	this.onSelect = function() {};
	this.populate = function() {
		//$(dg_name).style.display = "block";
		this.dg.removeAll();
		
		
		
		this.vResults.each(function(item,index) {	

			if(item.iCurrentPageCount >= 0){
				self.iCurrentPageCount = item.iCurrentPageCount;
			}else if(item.bMaxElementsCountReach == true || item.bMaxElementsCountReach == false){
				self.bMaxElementsCountReach = item.bMaxElementsCountReach;
			}
			else if(isNotNull(item.sPagination)){
				self.paginate(item.sPagination.evalJSON());
			}else if(item.iTotalCountCriterias >= 0){
				self.iTotalCountCriterias = item.iTotalCountCriterias;
			}else if(isNotNull(item.sGeneratedRequest)){
				self.sGeneratedRequest = item.sGeneratedRequest;
				spanRequest.innerHTML = self.sGeneratedRequest;
			}
			else{
				
				var values = [];
				self.vHeaders.each(function(header, index2){
					var selectValues = [];
					if (header.selection) {
						header.selection.each(function(value){
							selectValues.push(item[value]);
						});
						values.push(header.onPopulate(selectValues,item[self.sIdInTable]));
					} else {
						values.push(header.onPopulate(item));
					}
				});
	
				var lineData = self.dg.addItem(values);
				var line = lineData.node;
				line.data = item[self.sIdInTable];
				
				var modulo = index%2;
				if(modulo==0 && isNotNull(self.lineBackground)){
					line.style.backgroundColor = self.lineBackground;			
				}
				if(modulo==1 && isNotNull(self.lineBackgroundBis)){
					line.style.backgroundColor = self.lineBackgroundBis;			
				}
				/*
				line.style.cursor = "pointer";
				line.onmouseover = function() {
					this.style.backgroundColor = "#FFF6C1";					
				}
				line.onmouseout = function() {
					this.style.backgroundColor = "#FAFAFA";
				}
				*/
				line.onclick = function() {
					self.onSelect();
				}
			}
		});
		
		if (!this.dg.bRendered) this.dg.render();
	}
	
	/** PAGINATION */
	this.onPaginate = function(){};
	this.onPaginateElement = function(item){
		var link = document.createElement("a");
		link.className = self.paginationLinkClassName;
		link.href = "javascript:void(0);";
		var bActiveLink = true;
		if(item.name == "current"){
			link.innerHTML = "["+(item.value+1)+"]";
			bActiveLink = false;
		}else if(item.name == "page"){
			link.innerHTML = item.value+1;
		}else{
			link.innerHTML = "&nbsp;<img style=\"vertical-align : middle;\" src=\"" + rootPath + "images/icones/fleches/"+self.paginationIconPrefix+item.name+".gif" + "\" alt=\""+item.name+"\" title=\""+item.name+"\" />";
		}
		
		if(bActiveLink){
			link.onclick = function() {
				self.iCurrentPage = item.value;
				self.useCurrentPageFromPagination = true;
				self.render();
				self.search();
			}
		}
		self.domPage.appendChild(link);
	}
	this.paginate = function(pagination){
		
		while (self.domPage.firstChild) {
		  self.domPage.removeChild(self.domPage.firstChild);
		}
		
		pagination.each(function(item,index) {
			if(isNotNull(item)){
				self.onPaginateElement(item);
			}
		});
		
		this.onPaginate();
	}
	
	/** COUNT */
	this.infoCount = function(){
		var sPlurial = ((this.iTotalCountCriterias>1)?"s":"");
		var sKind = ((this.bKindFemaleElement)?"e":"");
		
		var numResultInfo = "";
		if (this.iTotalCountCriterias>0) {
			if (this.iTotalCountCriterias==1000){
				numResultInfo = LOC.SE_MORE_THAN+" "+this.iTotalCountCriterias+" "+this.sLabelElement;
			} else {
				numResultInfo = this.iTotalCountCriterias+" "+this.sLabelElement;
			}
		} else {
			numResultInfo = LOC.SE_NO_RESULTS;
		}
		
		this.domCount.innerHTML = numResultInfo;
		
		while (self.domLimit.firstChild) {
		  self.domLimit.removeChild(self.domLimit.firstChild);
		}
		if(this.bMaxElementsCountUsed && this.bMaxElementsCountReach){
			if(this.iCurrentPageCount>0){
				var link = document.createElement("a");
				link.className = self.paginationLinkClassName;
				link.href = "javascript:void(0);";
				link.innerHTML = "précédente";
				
				link.onclick = function() {
					self.iCurrentPageCount--;
					self.useCurrentPageFromPagination = true;
					self.render();
					self.search();
				}
				self.domLimit.appendChild(link);
			}
			var span = document.createElement("span");
			span.innerHTML = " limite de recherche : "+(this.iCurrentPageCount*this.iMaxElementsCount)+" > "+((this.iCurrentPageCount+1)*this.iMaxElementsCount);
			self.domLimit.appendChild(span);
			
			var link = document.createElement("a");
			link.className = self.paginationLinkClassName;
			link.href = "javascript:void(0);";
			link.innerHTML = "suivante";
			
			link.onclick = function() {
				self.iCurrentPageCount++;
				self.useCurrentPageFromPagination = true;
				self.render();
				self.search();
			}
			self.domLimit.appendChild(link);
		}
	}
	
	/** ADVANCED FUNCTION */
	var inputCutCount;
	var chkLimit;
	var inputNbResults;
	var divAdvancedFunction;
	var spanRequest;
	this.hideAdvancedFunction = function(){
		try{Element.hide(divAdvancedFunction);}catch(e){}
	}
	this.showAdvancedFunction = function(){
		try{Element.toggle(divAdvancedFunction);}catch(e){}
	}
	this.applyAdvancedFunction = function(){
		try{
			this.bMaxElementsCountUsed = chkLimit.checked;
			this.iMaxElementsCount = inputCutCount.value;
			this.iMaxElementsPerPage = inputNbResults.value;
		}catch(e){alert(e);}
	}
	this.initAdvancedFunction = function(){
		if(isNotNull(this.domAdvanced)){
			while (self.domAdvanced.firstChild) {
			  self.domAdvanced.removeChild(self.domAdvanced.firstChild);
			}
			
			divAdvancedFunction = document.createElement("div");
			divAdvancedFunction.style.paddingTop = "5px";
			
			var divLimit = document.createElement("div");
			chkLimit = document.createElement("input");
			chkLimit.type = "checkbox";
			chkLimit.checked = this.bMaxElementsCountUsed;
			var span = document.createElement("span");
			span.style.paddingLeft = "5px";
			span.innerHTML = "Limiter les résultats";
			divLimit.appendChild(chkLimit);
			divLimit.appendChild(span);
			divAdvancedFunction.appendChild(divLimit);
			
			var divCutCount = document.createElement("div");
			divCutCount.style.marginTop = "5px";
			inputCutCount = document.createElement("input");
			inputCutCount.type = "text";
			inputCutCount.value = this.iMaxElementsCount;
			inputCutCount.maxLength = 4;
			inputCutCount.size = 4;
			inputCutCount.style.marginLeft = "5px";
			var spanCutCount = document.createElement("span");
			spanCutCount.innerHTML = "Limite";
			divCutCount.appendChild(spanCutCount);
			divCutCount.appendChild(inputCutCount);
			divAdvancedFunction.appendChild(divCutCount);
			
			var divNbResults = document.createElement("div");
			divNbResults.style.marginTop = "5px";
			inputNbResults = document.createElement("input");
			inputNbResults.type = "text";
			inputNbResults.value = this.iMaxElementsPerPage;
			inputNbResults.maxLength = 4;
			inputNbResults.size = 4;
			inputNbResults.style.marginLeft = "5px";
			var spanNbResults = document.createElement("span");
			spanNbResults.innerHTML = "Nombre de résultats par page";
			divNbResults.appendChild(spanNbResults);
			divNbResults.appendChild(inputNbResults);
			divAdvancedFunction.appendChild(divNbResults);
			
			spanRequest = document.createElement("span");
			spanRequest.innerHTML = self.sGeneratedRequest;
			divAdvancedFunction.appendChild(spanRequest);
			
			var link = document.createElement("a");
			link.href = "javascript:void(0);";
			link.innerHTML = "fonctions avancées";
			
			link.onclick = function() {
				self.showAdvancedFunction();
			}
			
			this.domAdvanced.appendChild(link);
			this.domAdvanced.appendChild(divAdvancedFunction);
			
			Element.hide(divAdvancedFunction);
			
		}
	}
	
			
	this.onLoad = function() {};
	this.onBeforeSearch = function() {};
	this.onSearch = function(searchData) {};
	this.search = function() {
		
		if (!this.domNode) this.render();
		
		this.onBeforeSearch();
		this.domCount.innerHTML = "";
		
		var searchData = new Object();
		searchData.sGroupByClause = this.sGroupByClause;
		searchData.vHeaders = this.vHeaders;
		searchData.sSelectPart = this.sSelectPart;
		searchData.sMainTable = this.sMainTable;
		searchData.sMainAliasTable = this.sMainAliasTable;
		searchData.vOtherTables = this.vOtherTables;
		searchData.vOtherTablesWithLeftJoin = this.vOtherTablesWithLeftJoin;
		searchData.vClauseInvariant = this.vClauseInvariant;
		searchData.vFilter = this.vFilter;
		this.vFilter = [];
		
		searchData.sIdInTable = this.sIdInTable;
		searchData.iGroupPerPage = this.iGroupPerPage;
		
		searchData.sSecureType = this.sSecureType;
		
		searchData.loadPaginated = (this.loadPaginated) ? true : false;
		
		if(isNotNull(this.domAdvanced)){
			this.applyAdvancedFunction();
		}
		searchData.bMaxElementsCountUsed = this.bMaxElementsCountUsed;
		searchData.iMaxElementsPerPage = this.iMaxElementsPerPage;
		searchData.iMaxElementsCount = this.iMaxElementsCount;
		
		if(this.useCurrentPageFromPagination)
			searchData.iCurrentPageCount = this.iCurrentPageCount;
		else
			searchData.iCurrentPageCount = 0;
		if(this.useCurrentPageFromPagination)
			searchData.iCurrentPage = this.iCurrentPage;
		else
			searchData.iCurrentPage = 0;
		this.useCurrentPageFromPagination = false;
		this.onSearch(searchData);
		
		function callback(results) {
			self.vResults = results.evalJSON();
			self.populate();
			if (!self.bHideCount) self.infoCount();
			self.hideAdvancedFunction();
			self.onLoad();
		}
		
		if (this.iIdEngine!=null) {
			AutoFormSearchEngine.getJSONArrayResultFromEngine(this.iIdEngine, Object.toJSON(searchData), callback);
		} else {
			AutoFormSearchEngine.getJSONArrayResult(Object.toJSON(searchData), callback);
		}

	}
	
	this.addOrderBy = function(header, direction){
		if (this.uniqueSort) {
			this.vHeaders.each(function(h){h.direction = ""});
		}
		header.direction = direction;
		this.render();
		this.search();
	}
	
	this.removeOrderBy = function(header){
		header.direction = "";
		this.render();
		this.search();
	}
}

mt.component.SearchEngineTable = function(tableName,jointure) {
	this.tableName = tableName;
	this.jointure = jointure;
}

/**
* title : string
* selection : Array<String>
* order : string
*/
mt.component.SearchEngineHeader = function(title,selection,order) {
	this.title = title;
	this.order = order;
	this.selection = selection;
	this.direction = "";
	this.separationValue = "<br/>";
	/**
	* values : Array : select values from selection array
	*/
	this.onPopulate = function(values,mainId) {
		var data = "";
		var separationValue = this.separationValue;
		values.each(function(value,index){
			data += value;
			if(index != (values.length-1))
				data += separationValue;
		});
		return data;
	};
}
mt.component.SearchEngineFilter = function(fields,values,optLike,compareFilter) {
	this.fields = fields;
	this.values = values;
	this.optLike = optLike;
	this.compareFilter = compareFilter;
}

mt.component.DataGrid = Class.create();
mt.component.DataGrid.prototype = {
	initialize:function(placeHolder) {
		this.bRendered = false;
		this.domNode = $(placeHolder);
		this.dataSet = [];
		this.columnStyles = [];
		this.createTable();
	},
	createTable:function() {
		this.table = document.createElement("TABLE");
		this.table.className = "dataGrid";
		this.table.cellSpacing = 1;
		this.table.jsObj = this;
		this.grid = document.createElement("TBODY");
		this.table.appendChild(this.grid);
	},	
	addStyle:function(name, value) {
		this.table.style[name] = value;
	},
	setHeader:function(values) {
		var self = this;
		var line = document.createElement("tr");
		line.className = "header";
		values.each(function(item, index){
			var cell = document.createElement("td");
			cell.className = "cell";
			for (i in self.columnStyles[index]) {
				if (i=="width" || i=="textAlign")
				cell.style[i] = self.columnStyles[index][i];
			}
			if(typeof(item)=='object') {
				cell.appendChild(item);
			} else {
				cell.innerHTML = item;
			}
			line.appendChild(cell);
		});
		if (this.moveOptionEnabled) {
			var cell = document.createElement("td");
			cell.className = "cell";
			line.appendChild(cell);
		}
		if (this.removeOptionEnabled) {
			var cell = document.createElement("td");
			cell.className = "cell";
			cell.style.width = "16px";
			if (this.removeOptionHeader)
				cell.appendChild(this.removeOptionHeader);
			line.appendChild(cell);
		}
		this.grid.insertBefore(line, this.grid.firstChild);
	},
	/*setGridHeight:function(h) {
		this.grid.style.overflow = "auto";
		this.grid.style.height = h+"px";
	},*/
	_addItem:function(index, values, options) {
		var self = this;
		var line = document.createElement("tr");
		line.className = "line";
		for (i in this.linesStyle) line.style[i] = this.linesStyle[i];
		line.index = index;
		var lineData = {node:line, cells:[]};
		if (options)
			lineData.removeOption = options.removeOption;
		values.each(function(item, index){
			if (item==null) item="";
		
			var cell = document.createElement("td");
			cell.className = "cell";
			for (i in self.columnStyles[index]) cell.style[i] = self.columnStyles[index][i];
			cell.getIndex = function() {
				return this.parentNode.index;
			}
			var content;
			
			if (item.editable) {
				cell.innerHTML = item.value;
				cell.data = item.data;
				cell.editor = new mt.component.InPlaceEditor(cell, false);
				cell.editor.onChange = function(value) {
					cell.data = value;
				}
			} else {
			
				if (item.nodeType==1) {
					cell.data = "";
					content = item;
					cell.appendChild(content);
				} else {
					if (!item.value) {
						cell.data = "";
						content = item;
					} else {
						cell.data = item.data;
						content = item.value;
					}
					if (content.nodeType==1) {
						cell.appendChild(content);
					} else {
						cell.innerHTML = content;
					}
				}
			
			}
			
			line.appendChild(cell);
			lineData.cells.push(cell);
		});
		
		if (this.moveOptionEnabled) {
			var imgUp = document.createElement("img");
			imgUp.src = rootPath+"images/icons/up.gif";
			imgUp.className = "pointer";
			imgUp.onclick = function() {
				self.moveItem(-1, this.parentNode.parentNode.index);
			}
			var imgDown = document.createElement("img");
			imgDown.src = rootPath+"images/icons/down.gif";
			imgDown.style.marginLeft = "2px";
			imgDown.className = "pointer";
			imgDown.style.marginLeft = "4px";
			imgDown.onclick = function() {
				self.moveItem(1, this.parentNode.parentNode.index);
			}
			var cell = document.createElement("td");
			cell.className = "cell";
			cell.getIndex = function() {
				return this.parentNode.index;
			}
			cell.appendChild(imgUp);
			cell.appendChild(imgDown);
			line.appendChild(cell);
			lineData.cells.push(cell);
		}
		
		if ((this.removeOptionEnabled && lineData.removeOption) || (this.removeOptionEnabled && lineData.removeOption==null)) {
			var imgDelete = document.createElement("img");
			imgDelete.src = rootPath+"images/delete.gif";
			imgDelete.className = "pointer";
			imgDelete.onclick = function() {
				var result = self.onBeforeRemove(this.parentNode.parentNode.index);
				if (result || result==null) {
					self.removeItem(this.parentNode.parentNode.index);
				}
			}
			var cell = document.createElement("td");
			cell.style.width = "16px";
			cell.className = "cell";
			cell.getIndex = function() {
				return this.parentNode.index;
			}
			cell.appendChild(imgDelete);
			line.appendChild(cell);
			lineData.cells.push(cell);
		}		
		
		if (index==this.dataSet.length) {
			this.grid.appendChild(line);
			this.dataSet.push(lineData);
		} else {
			this.grid.insertBefore(line, this.grid.childNodes[index]);
			this.dataSet.splice(index, 0, lineData);
		}
		
		return lineData;
	},
	addItem:function(values, options) {
		return this._addItem(this.dataSet.length, values, options);
	},
	insertItem:function(index, values, options) {
		var lineData = this._addItem(index, values, options);
		this._updateIndexes();
		return lineData;
	},
	_updateIndexes:function() {
		this.dataSet.each(function(line, index){
			line.node.index = index;
		});
	},
	setColumnStyle:function(index, styles) {
		this.columnStyles[index] = styles;
		this.dataSet.each(function(line){
			for (i in styles) {
				line.cells[index].style[i] = styles[i];
			}
		});
	},
	addRemoveOption:function(headerContent) {
		this.removeOptionHeader = headerContent;
		this.removeOptionEnabled = true;
		this.dataSet.each(function(line){
			line.removeOption = true;
		});
	},
	addMoveOption:function() {
		this.moveOptionEnabled = true;
	},
	removeItem:function(index, callEvent) {
		Element.remove(this.dataSet[index].node);
		this.dataSet.splice(index,1);
		this._updateIndexes();
		if (callEvent || callEvent==null) this.onRemove(index);
	},
	removeAll:function() {
		while(this.dataSet.length>0)
			this.removeItem(this.dataSet.length-1);
	},
	clear:function() {
		while(this.dataSet.length>0) {
			var index = this.dataSet.length-1;
			Element.remove(this.dataSet[index].node);
			this.dataSet.splice(index,1);
		}
		this._updateIndexes();
	},
	moveItem:function(direction, index) {
		if (direction==-1) {
			if (index>0) {
				var node = this.dataSet[index].node;
				var previousNode = this.dataSet[index-1].node;
				node.parentNode.insertBefore(node, previousNode);
				var previousSet = this.dataSet[index-1];
				this.dataSet[index-1] = this.dataSet[index];
				this.dataSet[index] = previousSet;
				this._updateIndexes();
				this.onMove(direction, index);
			}
		} else if (direction==1) {
			if (index<(this.dataSet.length-1)) {
				var node = this.dataSet[index].node;
				var nextNode = this.dataSet[index+1].node;
				node.parentNode.insertBefore(nextNode, node);
				var nextSet = this.dataSet[index+1];
				this.dataSet[index+1] = this.dataSet[index];
				this.dataSet[index] = nextSet;
				this._updateIndexes();
				this.onMove(direction, index);
			}
		}
	},
	onRemove:function() {},
	onBeforeRemove:function() {},
	onMove:function() {},
	_setItemDragEvents:function(line) {
		line.node.style.cursor = "move";
		var originalBackground = Element.getStyle(line.node, 'background-color');
		line.node.onmouseover = function() {
			this.style.backgroundColor = "lightyellow";
		}
		line.node.onmouseout = function() {
			this.style.backgroundColor = originalBackground;
		}
		line.node.onmousedown = function(e) {
			var e = e || window.event;
			var div = this.cloneNode(true);	
			div.style.position = "absolute";
			div.className = "dgDragItem";
			div.style.backgroundColor = "#FFF";
			div.style.border = "1px solid #C5D5FC";
			div.lineData = line;
			document.body.appendChild(div);
			Position.clone(this, div);
			var drag = new Draggable(div, {revert:true, endRevertEffect:function() {
				try{Element.remove(div);}catch(e){};
			}});
			drag.initDrag(e);
			drag.startDrag(e);
		}
	},
	enableItemDrag:function(enable) {
		var self = this;
		this.dataSet.each(function(line){
			if (enable) {
				self._setItemDragEvents(line);
			}			
		});
	},
	enableItemDrop:function(enable) {
		var self = this;
		this.dataSet.each(function(item, index){
			Droppables.add(item.node, {accept:'dgDragItem', onDrop:function(node){
				self.onItemDrop(node.onBoardData, {datagrid:self,itemIndex:index});
				Element.remove(node);
			},hoverclass:'dgItemOnDragOver', noPositionChange:true});
		});
	},	
	render:function() {
		this.bRendered = true;
		this.domNode.innerHTML = "";
		this.domNode.appendChild(this.table);
	}
};

function isNotNull(item) {
	if (item==null || item=="" || item=="undefined" || item=="null") return false;
	return true;
}
