function CGrid(tbl, params)
{
	this.table = tbl;
	this.tbody = tbl.tBodies[0];
	this.tempty = tbl.tBodies[1];
	this.tfoot = tbl.tFoot;
	this.params = params;
	
	this.tbody_row = this.tbody.rows[0];	
	this.tbody_row.style.display = "none";

	if (this.tfoot)
	{
		this.tfoot_row = this.tfoot.rows[0];
		this.tfoot_row.style.display = "none";
	}
	
	if (this.tempty)
	{
		this.tempty.style.display = "none";
	}
	
	//paging
	this.page_current = 0;
	this.page_count = 0;
	this.page_size = params.page_size;
}


CGrid.prototype.fillRow = function(tt, tr, i, obj)
{
	for (var i = 0, num = tt.cells.length; i < num; i++)
	{
		var td = document.createElement("td");
		tr.appendChild(td);
		
		td.innerHTML = tt.cells[i].innerHTML.replace(/{(.*?)}/g, 
			function(text)
			{
				var p = text.substring(1, text.length - 1);
				return obj[p];
			}
		);
	}
}


CGrid.prototype.fill = function(adata)
{
	var i0, num, i;

	// delete all rows, except template
	for (i = 1, num = this.tbody.rows.length; i < num; i++)
	{
		this.tbody.removeChild(this.tbody.rows[1]);
	}
	
	this.table.style.display = "";

	if (adata.length == 0)
	{
		if (this.tempty) this.tempty.style.display = "";
		//if (this.table.tHead) this.table.tHead.style.display = "none";
		if (this.tfoot) this.tfoot.style.display = "none";
		this.tbody.style.display = "none";
		return 0;
	}

	// hide the empty text
	if (this.tempty) this.tempty.style.display = "none";
	if (this.table.tHead) this.table.tHead.style.display = "";
	this.tbody.style.display = "";

	// calculate start, end
	if (this.page_size > 0)
	{
		this.page_count = Math.ceil(adata.length / this.page_size);
		i0 = this.page_current * this.page_size;
		num = Math.min(i0 + this.page_size, adata.length);
	}
	else
	{
		i0 = 0;
		num = adata.length;
	}
	
	
	for (i = i0; i < num; i++)
	{
		var tr = document.createElement("tr");
		
		adata[i].cgrid_datarow_index = i;

		if (this.processDataRow)
		{
			this.processDataRow(adata[i]);
		}
		
		tr.cgrid_row_data = adata[i];
		this.tbody.appendChild(tr);
		
		this.fillRow(this.tbody_row, tr, i, adata[i]);
		
		if (this.processTableRow)
		{
			this.processTableRow(tr, adata[i]);
		}	
	}

	if (this.page_current >= this.page_count && this.page_current > 0)
		this.page_current = this.page_count - 1;
		
	this.page_number = this.page_current + 1;

	if (this.tfoot)
	{
		if (this.tfoot.rows.length == 2)
		{
			this.tfoot.removeChild(this.tfoot.rows[1]);
		}

		if (this.page_count > 1)
		{
			var tr = document.createElement("tr");
			this.tfoot.appendChild(tr);
			
			this.fillRow(this.tfoot_row, tr, 0, this);		

			this.tfoot.style.display = "";
		}
	}


	this.adata = adata;
}


CGrid.prototype.updateTableRow = function(datarow)
{
	var tblrow = this.tbody.rows[datarow.cgrid_datarow_index + 1];
	
	for (var i = 0, n = tblrow.cells.length; i < n; i++)
	{
		tblrow.removeChild(tblrow.cells[0]);
	}
	

	if (this.processDataRow)
	{
		this.processDataRow(datarow);
	}
	
	this.fillRow(this.tbody_row, tblrow, datarow.cgrid_datarow_index, datarow);
	
	if (this.processTableRow)
	{
		this.processTableRow(tblrow, datarow);
	}	
}


CGrid.getParentRow = function(el)
{
	while (el)
	{
		if (el.cgrid_row_data)
			return el;
			
		el = el.parentNode;
	}

	return null;
}


CGrid.getParentRowData = function(el)
{
	while (el)
	{
		if (el.cgrid_row_data)
			return el.cgrid_row_data;
			
		el = el.parentNode;
	}

	return null;
}


CGrid.prototype.showNextPage = function()
{
	this.page_current++;
	if (this.page_current + 1 > this.page_count)
	{
		this.page_current = 0;
	}

	this.fill(this.adata);
	
	window.scroll(0, this.table.offsetTop);
}


CGrid.prototype.showPrevPage = function()
{
	this.page_current--;
	if (this.page_current < 0)
	{
		this.page_current = this.page_count - 1;
	}

	this.fill(this.adata);

	window.scroll(0, this.table.offsetTop);
}





CGrid.create = function(tbl, params)
{
	if (typeof(tbl) == "string")  tbl = document.getElementById(tbl);
	
	return new CGrid(tbl, params || {});
}
