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


TRejilla._aInstancias = new Array ();

TRejilla.prototype.Depurar       = true;
TRejilla.prototype.onSeleccionar = null;

TRejilla.prototype._oCargando   = null;
TRejilla.prototype._oColumnas   = null;
TRejilla.prototype._iColumnas   = 0;
TRejilla.prototype._oContenedor = null;
TRejilla.prototype._oDatos      = null;
TRejilla.prototype._iFilas      = 0;
TRejilla.prototype._aFlags      = new Array ();
TRejilla.prototype._oInfo       = null;
TRejilla.prototype._oTabla      = null;
TRejilla.prototype._oTHead      = null;
TRejilla.prototype._aVars       = 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 ();

	for (var i = this._aVars.length - 1; i >= 0; i--)	Ajax.Vars (this._aVars [i][0], this._aVars [i][1]);
	this._aVars   = new Array ();
	Ajax._Rejilla = self;
	Ajax.onCargar = this._onCargar;
	this._Cargando ();
	Ajax.Cargar (sURL);
}


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._DIVMouseEvents = function (Elemento, Fil, Col)
{
	var self = this;

	if (! this._aFlags [Col -1] & 1)
	{	if (Elemento.addEventListener)
			Elemento.addEventListener ('click', function (event) { self._onSeleccionar (Fil, Col); }, false);
		else if (Elemento.attachEvent)
			Elemento.attachEvent ('onclick', function () { self._onSeleccionar (Fil, Col); });
	}
}


TRejilla.prototype.Fila = function (iFila)
{
	this._oDatos.RecNo (iFila);
}


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

	if (bVacia) tr.className = 'Vacia';
	else this._TRMouseEvents (tr);
	for (var i = 0; i < this._iColumnas; i++)
	{	if (! (this._aFlags [i] & 2))
		{	td  = document.createElement ('td');
			div = document.createElement ('div');
			div.className = 'Col' + (i + 1);
			div.innerHTML = bVacia ? '&nbsp;' : this._oDatos.ByPos (i);
			if (! bVacia) this._DIVMouseEvents (div, Fil, i + 1);
			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._oTHead = document.createElement ('thead');
	this._oColumnas.First ();
	while (! this._oColumnas.Eof ())
	{	this._aFlags [i] = this._oColumnas.ByName ('Flag');
		if (! (this._aFlags [i] & 2))
		{	th  = document.createElement ('th');
			div = document.createElement ('div');
			div.className = 'Col' + (i + 1);
			div.innerHTML = this._oColumnas.ByName ('Nombre');
			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._onCargar= function (Peticion)
{
	var Aux = DataSet (Peticion.asText (), 'COLUMNAS', 'DATOS', 'INFORMACION');
	var self = Peticion._Rejilla;

	self._oColumnas = Aux [0];
	self._oDatos    = Aux [1];
	self._oInfo     = Aux [2];
	self._iColumnas = self._oColumnas.RecordCount ();
	if (! self._iColumnas && self.Depurar) alert ('No se han definido columnas.');
	else self._CreateTable ();
	if (self._oCargando) self._oCargando.style.display = 'none';
}


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


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


TRejilla.prototype.Vars = function (Variable, Valor)
{
	this._aVars [this._aVars.length] = new Array (Variable, Valor);
}


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


function idRejilla (sIdentificador)
{
	if (! TRejilla._aInstancias [sIdentificador])
		TRejilla._aInstancias [sIdentificador] = new TRejilla (sIdentificador);
	return TRejilla._aInstancias [sIdentificador];
}

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


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;

	if (this._oTabla) this._oContenedor.removeChild (this._oTabla);
	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);
}


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', '3');
		td.innerHTML = this._oInfo.ByName ('Valor');
		tr.appendChild (td);
		tfoot.appendChild (tr);
		this._oTabla.appendChild (tfoot);
	}
}





