Web.Components.Tables = function () {
	this.Objetos 	= Web.Library.Instances.create('Objects');
	this.Documents 	= Web.Components.Instances.create('Documents');
	this.fl_retorno = 1;
	this.rowCount   = 0;
	this.cellCount  = 0;
	this.maxCell    = 1;
	this.row_guard  = new Array();
	this.str_name   = "";
	this.obj_Rproperty = {};
	this.obj_Revents   = {};
	this.obj_Cproperty = {};
	this.obj_Cevents   = {};
	
	this.create = function (strObj,criarEm) {
		var objObj, criar, nome, TEvents;
		nome     = this.Objetos.getNonEmpty(this.getName(), strObj);
		objObj   = this.Objetos.getObj(strObj, 'TABLE');
		criar    = (typeof objObj == 'string') ? true : false;
		criarEm  = (criarEm != null && typeof this.Objetos.getObj(criarEm) == 'object') ? this.Objetos.getObj(criarEm) : criarEm;
		criarEm  = (criarEm != null) ? criarEm : this.Objetos.getBody();
		TEvents  = this.getTEvents();

		objObj = (typeof objObj == 'object') ? objObj : this.Documents.newTable(strObj, null, null);
		criarTB  = (objObj.getElementsByTagName('TBODY').length>0) ? false : true;
		objTBody = (criarTB) ? this.Documents.newTBody() : objObj.getElementsByTagName('TBODY')[0];
		
		if (criar) {
			objObj.setAttribute('id', nome);
			criarEm.appendChild(objObj);
		}
		
		objObj['objObj'] = this;
		
		if (typeof TEvents == 'object') {
			for (x in TEvents) {
				if (TEvents[x] == '______array' || TEvents[x] == '') continue;
				this.Objetos.register(objObj, TEvents[x][0], TEvents[x][1]);
			}
		}
		
		this.setTable(objObj);
		this.setTableID(strObj);
		this.setCreateAT(criarEm);
		this.setTBody(objTBody);
		
		return (this.getRetorno() == 2) ? this : objObj;
	}

	this.AddEvent = function (objObj, strEvent, strFunc) {
		this.Objetos.register(objObj, strEvent, strFunc);
	}
	
	this.RemoveEvent = function (objObj, strEvent, strFunc) {
		this.Objetos.unregister(objObj, strEvent, strFunc);
	}
	
	this.tableAttributes = function (argumentos) {
		this.Objetos.setAttributes(this.getTable(), argumentos);
	}
	
	this.Fill = function (objArray, rowArgs, cellArgs) {
		cellArgs = (cellArgs != null) ? cellArgs : new Object();
		
		for (i in objArray) {
			if (objArray[i] == '______array') continue;
			this.createRow(rowArgs);
			
			for (j in objArray[i]) {
				if (objArray[i][j] == '______array') continue;
				
				cellArgs['innerHTML'] = objArray[i][j];
				this.createCell(cellArgs);
			}
		}
	}
	
	this.RemoveAll = function () {
		var objTBody  = this.getTBody();
		var Elementos = objTBody.getElementsByTagName('TR');
		
		for (var i = 0; i < Elementos.length; i++) {
			objTBody.removeChild(Elementos[i]);
		}
	}

	this.createRow = function (argumentos) {
		var objTBody = this.getTBody();
		var newRow   = this.Documents.newTR(null, argumentos);
		var Properties  = this.getRProperty();
		var Eventos     = this.getREvents();
		newRow.setAttribute('posID', this.rowCount);

		this.maxCell   = (this.maxCell > this.cellCount) ? this.maxCell : this.cellCount;
		this.cellCount = 0;
		
		if (Properties[this.rowCount] != null) {
			this.Objetos.setAttributes(newRow, Properties[this.rowCount]);
		}
		
		if (typeof Eventos[this.rowCount] == 'object') {
			for (x in Eventos[this.rowCount]) {
				if (Eventos[this.rowCount][x] == '______array' || Eventos[this.rowCount][x] == "") continue;
				this.Objetos.register(newRow, Eventos[this.rowCount][x][0], Eventos[this.rowCount][x][1]);
			}
		}
		
		objTBody.appendChild(newRow);
		
		this.setRowGuard(newRow);
		this.rowCount++;
		
		return (this.getRetorno() == 2) ? this : newRow;
	}
	
	this.setRowAttributes = function (newRow, argumentos) {
		var Obj = (typeof newRow == 'number') ? this.getRowID(newRow) : newRow;
		this.Objetos.setAttributes(Obj, argumentos);
	}
		
	this.createCell = function (argumentos) {
		var objTBody = this.getTBody();
		var tbRows   = objTBody.getElementsByTagName("TR");
		var lastRow  = tbRows[tbRows.length-1];
		var Properties  = this.getCProperty();
		var Eventos     = this.getCEvents();
		
		if (Properties[this.rowCount-1] != null && Properties[this.rowCount-1][this.cellCount] != null && Properties[this.rowCount-1][this.cellCount]['TH'] != null) {
			var newCell = this.Documents.newTH(null, argumentos);
		} else {
			var newCell = this.Documents.newTD(null, argumentos);
		}

		if (Properties[this.rowCount-1] != null) {
			this.Objetos.setAttributes(newCell, Properties[this.rowCount-1][this.cellCount]);
		}
		
		if (typeof Eventos[this.rowCount-1] == 'object') {
			for (x in Eventos[this.rowCount-1][this.cellCount]) {
				if (Eventos[this.rowCount-1][this.cellCount][x] == '______array' || Eventos[this.rowCount-1][this.cellCount][x] == "") continue;
				this.Objetos.register(newCell, Eventos[this.rowCount-1][this.cellCount][x][0], Eventos[this.rowCount-1][this.cellCount][x][1]);
			}
		}
		
		newCell.setAttribute('posID', this.cellCount);
		lastRow.appendChild(newCell);
		
		this.cellCount++;
		
		if (this.rowCount == 1) {
			this.bindTable();
		}
		
		return (this.getRetorno() == 2) ? this : lastRow;
	}
	
	this.setCellAttributes = function (newCell, argumentos, rowPos) {
		var objTBody = this.getTBody();
		var tbRows   = objTBody.getElementsByTagName("TR");
		var lastRow  = tbRows[tbRows.length-1];
		var lastRow  = (rowPos != null) ? this.getRowID(rowPos) : lastRow;
		var Obj      = (typeof newCell == 'number') ? this.getCellID(lastRow, newCell) : newCell;
		
		this.Objetos.setAttributes(Obj, argumentos);
		if (typeof argumentos == 'object') {
			for (x in argumentos) {
				if (argumentos[x] == '______array') continue;
				switch(x.toLowerCase()) {
					case 'bold':
							Obj.style.fontWeight = 'bold';
							Obj.style.textAlign  = 'center';
						break;
						
					case 'auto_cp':
							Obj.colSpan = Math.max(this.maxCell,this.cellCount);
						break;
						
					case 'deletetext':
							var texto    = (typeof argumentos[x] == 'object') ? argumentos[x][1] : argumentos[x];
							var confirma = (typeof argumentos[x] == 'object') ? "if (confirm('"+argumentos[x][0]+"')) " : "";
							var chamada  = this.getClassName() +".getRowGuard("+(this.rowCount-1)+")";
							var addText  = "<a href='javascript:;' onclick=\"" + confirma +this.getClassName()+ ".deleteRow( "+chamada+" );\">" +texto+ "</a>";
							Obj.innerHTML += addText;
						break;
				}
			}
		}
	}
	
	this.bindTable = function () {
		var objTable = this.getTable();
		var objTBody = this.getTBody();
		
		objTable.appendChild(objTBody);
	}
	
	this.getRowID = function (indice) {
		var objTBody = this.getTBody();
		var Linhas   = objTBody.getElementsByTagName('TR');
		var objRow;
		
		for (var x = 0; x < Linhas.length; x++) {
			if (Linhas[x].getAttribute('posID') == indice) {
				return Linhas[x];
			}
		}
		
		return false;
	}
	
	this.getCellID = function (objRow, indice) {
		var Cells  = objRow.getElementsByTagName('TD');
		var objRow;
		
		for (var x = 0; x < Cells.length; x++) {
			if (Cells[x].getAttribute('posID') == indice) {
				return Cells[x];
			}
		}
		
		return false;
	}
	
	this.deleteRow = function (objRow) {
		var objTable = this.getTable();
		var objTBody = this.getTBody();
		var createAT = this.getCreateAT();
		var Linhas;
		
		this.row_guard[objRow.getAttribute('posID')] = null;
		objTBody.removeChild(objRow);
		
		Linhas = objTable.getElementsByTagName("TR");
		
		if (Linhas.length == 0) {
			this.getCreateAT().removeChild(objTable);
		}
		
		return true;
	}
	
	this.changeLineColor = function (Colors, isCss) {
		var objTBody = this.getTBody();
		var Linhas   = objTBody.getElementsByTagName('TR');
		
		for (var i = 0; i < Linhas.length; i++) {
			if (i%Colors.length==0) {
				if (isCss!=null) {
					Linhas[i].className   = Colors[0];
				} else {
					Linhas[i].style.backgroundColor = Colors[0];
				}
			} else {
				if (isCss!=null) {
					Linhas[i].className   = Colors[1];
				} else {
					Linhas[i].style.backgroundColor = Colors[1];
				}
			}
		}
	}
	
	this.getObjObj = function () { return this.obj_table; }
	this.setObjObj = function (valor) { this.obj_table = valor; }
	
	this.getName = function () { return this.str_name; }
	this.setName = function (valor) { this.str_name = valor; }
	
	this.getTable = function () { return this.obj_table; }
	this.setTable = function (valor) { this.obj_table = valor; }
	
	this.getTBody = function () { return this.obj_table_body; }
	this.setTBody = function (valor) { this.obj_table_body = valor; }
	
	this.getTableID = function () { return this.table_id; }
	this.setTableID = function (valor) { this.table_id = valor; }
	
	this.getCreateAT = function () { return this.create_at; }
	this.setCreateAT = function (valor) { this.create_at = valor; }
	
	this.getRowGuard = function (indice) { return this.row_guard[indice]; }
	this.setRowGuard = function (valor) { this.row_guard[this.rowCount] = valor; }
	
	this.getClassName = function () { return this.class_name; }
	this.setClassName = function (valor) { this.class_name = valor; }
	
	this.getRetorno = function () { return this.fl_retorno; }
	this.setRetorno = function (valor) { this.fl_retorno = valor; }
	
	this.getTEvents = function () { return this.obj_Tevents; }
	this.setTEvents = function (valor) { this.obj_Tevents = valor; }
	
	this.getRProperty = function () { return this.obj_Rproperty; }
	this.setRProperty = function (valor) { this.obj_Rproperty = valor; }
	
	this.getREvents = function () { return this.obj_Revents; }
	this.setREvents = function (valor) { this.obj_Revents = valor; }
	
	this.getCProperty = function () { return this.obj_Cproperty; }
	this.setCProperty = function (valor) { this.obj_Cproperty = valor; }
	
	this.getCEvents = function () { return this.obj_Cevents; }
	this.setCEvents = function (valor) { this.obj_Cevents = valor; }
}
