//
//	Flags de columnas:
//		1.- NOCLICK, Desactivar onclick de celda
//		2.- INVISIBLE, No mostrar columna
//


TRejilla.prototype.Depurar       = true;
TRejilla.prototype.Vars          = null;
TRejilla.prototype.onActivarFila = null;
TRejilla.prototype.onCargarError = null;
TRejilla.prototype.onSeleccionar = null;
TRejilla.prototype.onPopUp       = null;
TRejilla.prototype.onShow        = null;

TRejilla.prototype._oCargando      = null;
TRejilla.prototype._aColsDef       = new Array ();
TRejilla.prototype._oColumnas      = null;
TRejilla.prototype._iColDatos      = 0;
TRejilla.prototype._iColRejilla    = 0;
TRejilla.prototype._oContenedor    = null;
TRejilla.prototype._oDatos         = null;
TRejilla.prototype._iFilaActiva    = 0;
TRejilla.prototype._iFilas         = 0;
TRejilla.prototype._sIdentificador = '';
TRejilla.prototype._oInfo          = null;
TRejilla.prototype._oTabla         = null;
TRejilla.prototype._oTHead         = null;


TRejilla.prototype.asBoolean = function (sCampo)
{
	return (this._oDatos.asBoolean (sCampo));
}


TRejilla.prototype.asFloat = function (sCampo)
{
	return (this._oDatos.asFloat (sCampo));
}


TRejilla.prototype.asInteger = function (sCampo)
{
	return (this._oDatos.asInteger (sCampo));
}


TRejilla.prototype.ByName = function (sCampo)
{
	return (this._oDatos.ByName (sCampo));
}


TRejilla.prototype._Cargando = function ()
{
	if (! this._oCargando)
	{	this._oCargando                = document.createElement ('div');
		this._oCargando.className      = 'Cargando';
		this._oCargando.style.display  = 'none';
		this._oCargando.style.position = 'absolute';
		this._oCargando.style.xIndex   = 100;
		this._oCargando.innerHTML      = 'Cargando ...';
		this._oContenedor.appendChild (this._oCargando);
	}
	this._CentrarCargando ();
}


TRejilla.prototype.Cargar = function (sURL)
{
	var self = this;
	var Ajax = new TAjax ();

	Ajax.Vars     = this.Vars;
	Ajax.onCargar = function (oPeticion)
									{
										try
										{
											var Aux = DataSet (oPeticion.asText (), 'COLUMNAS', 'DATOS', 'INFORMACION');
											
											self._oColumnas = Aux [0];
											self._oDatos    = Aux [1];
											self._oInfo     = Aux [2];
											self._iColDatos = self._oColumnas.RecordCount ();
											if (! self._iColDatos && self.Depurar) alert ('No se han definido columnas.');
											else self._CreateTable ();
											if (self._oCargando) self._oCargando.style.display = 'none';
										} catch (E)
										{	if (self._oCargando) self._oCargando.style.display = 'none';
											if (self.onCargarError) self.onCargarError (oPeticion);
											else alert (E);
										}
									}
	this._Cargando ();
	Ajax.Cargar (sURL);
	this.Vars = new Object ();
}


TRejilla.prototype._CentrarCargando = function ()
{
	var aPos  = null;
	var iLeft = 0;

	if (this._oTHead)
	{	aPos = this._FindPos (this._oTHead);
		this._oCargando.style.top     = (aPos [1] + this._oTabla.offsetHeight / 2 - this._oCargando.offsetHeight / 2)+ 'px';
		iLeft  = aPos [0] + this._oTHead.offsetWidth / 2;
	} else
	{	aPos = this._FindPos (this._oContenedor);
		this._oCargando.style.top  = this._oContenedor.offsetTop + 'px';
		iLeft = aPos [0] + this._oContenedor.offsetWidth / 2;
	}		
	this._oCargando.style.left    = iLeft + 'px';
	this._oCargando.style.display = 'block';
	this._oCargando.style.left    = (iLeft - this._oCargando.offsetWidth / 2) + 'px';
}


TRejilla.prototype._CreateTable = function ()
{
	var Filas = this._oInfo.Locate ('Variable', 'Filas') ? this._oInfo.ByName ('Valor') : 0;
	var tbody = document.createElement ('tbody');
	var td    = null;
	var Fil   = 1;

	this._oContenedor.innerHTML = '';
	this._oTabla = document.createElement ('table');
	this._oTabla.setAttribute ('border', '0');
	this._oTabla.setAttribute ('cellPadding', '0');
	this._oTabla.setAttribute ('cellSpacing', '0');
	
	this._Head ();
	this._Indice ();

	this._oDatos.First ();
	while (! this._oDatos.Eof ())
	{	this._FilaDatos (tbody, Fil++, false);
		this._oDatos.Next ();
	}
	while (Filas && Fil <= Filas) this._FilaDatos (tbody, Fil++, true);
	this._oTabla.appendChild (tbody);
	this._oContenedor.appendChild (this._oTabla);
	if (this.onShow) this.onShow (this);
	this._onClick (1, 0);
}


TRejilla.prototype._FilaActiva = function (iFila)
{
	var TR = null;
	
	if (this._iFilaActiva)
	{ TR = $ ('TR' + this._sIdentificador + this._iFilaActiva);
		if (TR)
		{	TR.setAttribute ('Activa', 'N');
			TR.className = '';
		}
	}
	this._iFilaActiva = iFila;
	this._oDatos.RecNo (iFila);
	TR = $ ('TR' + this._sIdentificador + this._iFilaActiva);
	if (TR)
	{	TR.setAttribute ('Activa', 'S');
		TR.className = 'Activa';
	}
}


TRejilla.prototype._FilaDatos = function (otbody, Fil, bVacia)
{
	var tr  = document.createElement ('tr');
	var td  = null;
	var div = null;

	tr.id = 'TR' + this._sIdentificador + Fil;
	if (bVacia) tr.className = 'Vacia';
	else this._TRMouseEvents (tr);
	for (var i = 0; i < this._iColDatos; i++)
	{	if (! (this._aColsDef [i][0] & 2))
		{	td  = document.createElement ('td');
			td.className = 'Col' + (i + 1);
			if (this._aColsDef [i][3]) td.style.textAlign = this._aColsDef [i][3];
			div = document.createElement ('div');
			div.className = this._aColsDef [i][1];
			div.innerHTML = bVacia ? '&nbsp;' : this._oDatos.ByPos (i);
			this._TDMouseEvents (td, Fil, i + 1, bVacia);
			td.appendChild (div);
			tr.appendChild (td);
		}
	}
	otbody.appendChild (tr);
}


TRejilla.prototype._FindPos = function (oElemento) 
{
	var iLeft = 0;
	var iTop  = 0;
	
	if (oElemento.offsetParent) 
	{	do 
		{	iLeft += oElemento.offsetLeft;
			iTop  += oElemento.offsetTop;
		} while (oElemento = oElemento.offsetParent);
	}
	return [iLeft, iTop];
}


TRejilla.prototype._Head = function ()
{
	var tr    = document.createElement ('tr');
	var th    = null;
	var div   = null;
	var i     = 0;

	this._iColRejilla = 0;
	this._oTHead = document.createElement ('thead');
	this._oColumnas.First ();
	while (! this._oColumnas.Eof ())
	{	
		this._aColsDef [i] = [this._oColumnas.ByName ('Flag'),  this._oColumnas.ByName ('Clase'), 
		                      this._oColumnas.ByName ('Ancho'), this._oColumnas.ByName ('Align')];
		if (! (this._aColsDef [i][0] & 2))
		{	this._iColRejilla++;
			th  = document.createElement ('th');
			th.className = 'Col' + (i + 1);
			if (this._aColsDef [i][2]) th.style.width = this._aColsDef [i][2] + 'px';
			div = document.createElement ('div');
			div.className = 'Col' + (i + 1);
			div.innerHTML = this._oColumnas.ByName ('Titulo');
			th.appendChild (div);

if (i == 0 && false)
{	div = document.createElement ('div');
	div.className = 'Ascendente';
	th.appendChild (div);
}
if (i == 1 && false)
{	div = document.createElement ('div');
	div.className = 'Descendente';
	th.appendChild (div);
}
			tr.appendChild (th);
		}
		this._oColumnas.Next ();
		i++;
	}
	this._oTHead.appendChild (tr);
	this._oTabla.appendChild (this._oTHead);
}


TRejilla.prototype._Indice = function ()
{	
	var tfoot = document.createElement ('tfoot');
	var tr    = document.createElement ('tr');
	var td    = document.createElement ('td');
	
	if (this._oInfo.Locate ('Variable', 'Indice'))
	{	td.setAttribute ('colSpan', this._iColRejilla);
		td.innerHTML = this._oInfo.ByName ('Valor');
		tr.appendChild (td);
		tfoot.appendChild (tr);
		this._oTabla.appendChild (tfoot);
	}
}


TRejilla.prototype._onClick = function (Fil, Col)
{
	this._FilaActiva (Fil);
	if (this.onActivarFila) this.onActivarFila (this, Fil, Col);
}


TRejilla.prototype._onDblClick = function (Fil, Col)
{
	if (this.onSeleccionar)
	{	this._FilaActiva (Fil);
		this.onSeleccionar (this, Fil, Col);
	}
}


TRejilla.prototype._TDMouseEvents = function (Elemento, Fil, Col, bVacia)
{
	var self = this;

	if (this._aColsDef [Col - 1][0] & 1) Elemento.style.cursor = 'default';
	else
	{	if (Elemento.addEventListener)
		{	if (! bVacia)
			{	Elemento.addEventListener ('click', function (event) { self._onClick (Fil, Col); }, false);
				Elemento.addEventListener ('dblclick', function (event) { self._onDblClick (Fil, Col); }, false);
			}
			Elemento.addEventListener ('contextmenu', function (event) { self._onPopUp (event, Fil, Col, false, bVacia); }, false);
		}	else if (Elemento.attachEvent)
		{	if (! bVacia)
			{	Elemento.attachEvent ('onclick', function () { self._onClick (Fil, Col); });
				Elemento.attachEvent ('ondblclick', function () { self._onDblClick (Fil, Col); });
			}
			Elemento.attachEvent ('oncontextmenu', function () { self._onPopUp (window.event, Fil, Col, true, bVacia); });
		}
	}
}


TRejilla.prototype._TRMouseEvents = function (Elemento)
{
	if (Elemento.addEventListener)
	{	Elemento.addEventListener ('mouseover', function (event) { Elemento.className = Elemento.getAttribute ('Activa') == 'S' ? 'ActivaOver' : 'Over'; }, false); 
		Elemento.addEventListener ('mouseout', function (event) { Elemento.className = Elemento.getAttribute ('Activa') == 'S' ? 'Activa' : ''; }, false);
	} else if (Elemento.attachEvent)
	{	Elemento.attachEvent ('onmouseover', function () { Elemento.className = Elemento.getAttribute ('Activa') == 'S' ? 'ActivaOver' : 'Over'; });
		Elemento.attachEvent ('onmouseout', function () { Elemento.className = Elemento.getAttribute ('Activa') == 'S' ? 'Activa' : ''; });
	}
}


function TRejilla (sIdentificador)
{
	this._aColumnas      = new Array ();
	this._oContenedor    = document.getElementById (sIdentificador);
	this._sIdentificador = sIdentificador;
	this.Vars            = new Object ();
	if (! this._oContenedor && this.Depurar) alert ('Identificador no encontrado');
}


/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

TRejilla.prototype._onPopUp = function (oEvent, Fil, Col, bIE, bVacia)
{
	if (bIE) oEvent.returnValue = false;
	else oEvent.preventDefault ();
	if (this.onPopUp)
	{	if (! bVacia) this._FilaActiva (Fil);
		this.onPopUp (this, [oEvent.clientX, oEvent.clientY], Fil, Col, bVacia);
	}
}

