//////////////////////////////////////////////
// Italcom Spa
// Browser: IE 6.0, Mozilla 1.7
// Library: CRepeater
// Version 1.0
// Require: 
//          
//          
// Objects:
//////////////////////////////////////////////


// Costructor
function CRepeater( id, cols, rows) 
{ 
	this.cols = cols;
	this.rows = rows;
	this.Table = null;
	this.id = id;
	this.cells = [];
	this.createTable();
	this.LayOutInitialized = false;
}

CRepeater.prototype.OnDefineLayOut = function (sender, col, row) {return true;}
CRepeater.prototype.OnFillCell = null;
CRepeater.prototype.OnSetLayOut = function(sender, row, col,argument){};
CRepeater.prototype.ObjectKey = null;

CRepeater.prototype.onSetLayout = function(sender, row, col, obj) {if (this.OnSetLayOut != null) this.OnSetLayOut(sender, row, col, obj); }

CRepeater.prototype.createTable 
= // ===================================
function ()
{
	this.Table = document.createElement('TABLE');
	this.Table.id = "Table_" + this.id;
	this.Table.repeaterObj = this;
}

CRepeater.prototype.InvalidateLayout
= // ================================
function ()
{
	// cicla tutte le celle
	for (var i = 0; i< this.cells.length;i++)
	{
		if (this.cells[i].cellRepeater != null)
		{
			this.cells[i].cellRepeater.InvalidateLayout();
		}
	}
	if ((this.Table != null) && (this.Table.parentNode != null))
	{
		this.Table.parentNode.removeChild (this.Table);
	}
	
	this.cells = [];
	this.createTable();
	this.LayOutInitialized = false;
	
}


CRepeater.prototype.RenderLayout 
= // ================================
function(objectParent)
{

	// Se non e' ancora stato definito il layout della tabella , invochiamo il metodo per definire il layout
	if (this.LayOutInitialized == false)
	{
		//
		// creiamo la tabella.
		// 
	
		for (var row=0;row<this.rows;row++)
		{
			var tableRow = this.Table.insertRow(-1);
			//this.objectCreated(this, row, -1, tableRow);
			
			var colidx=0;
			for (var col=0;col<this.cols;col++)
			{
				if ( this.OnDefineLayOut(row, col) == true )
				{
					// Create TD
					var cell = tableRow.insertCell(colidx);
					//this.objectCreated(this, row, col, cell);
					this.cells[this.cells.length] = {cellObject:cell, cellCol: colidx, cellRow: row };
					colidx++;
				}
			}
				
		}
		

		this.onSetLayout(this, -1, -1, this.Table);
		objectParent.appendChild(this.Table); 		
		// richiede di definire l' impostazione della tabella.
		this.LayOutInitialized = true;
	}
	else // richiede di definire l' impostazione della tabella.
		this.onSetLayout(this, -1, -1, this.Table);
	
	
	// richiede di definire l' impostazione delle righe che compongono la tabella
	
	for (var i=0;i<this.Table.rows.length;i++)
	{
		this.onSetLayout(this, i, -1, this.Table.rows[i]);
	}
	
	// Invochiamo il metodo per definire il contenuto delle celle.
	
	for (var i=0;i<this.cells.length;i++)
	{
		// richiede di definire l' impostazione delle celle che compongono la tabella.
		this.onSetLayout(this, this.cells[i].cellRow, this.cells[i].cellCol, this.cells[i].cellObject);

		if (this.OnFillCell != null)
		{
			// richiede di definire il contenuto.
			var obj = this.OnFillCell ( this.cells[i].cellObject, this.cells[i].cellCol, this.cells[i].cellRow );
			if ( obj != null )
			{
					if (typeof(obj) == "string")
					{
						this.cells[i].cellObject.innerHTML = obj;
					}
					else
					{
						if (obj instanceof CRepeater) // se l' oggetto ritornato e' un CRepeater
						{
							this.cells[i].cellObject.innerHTML = "";
							// aggiungere il nodo figlio dell' oggetto table dell' oggetto di classe CRepeater ritornato.
							obj.RenderLayout(cell);
							var tableobj = obj.Table;
							this.cells[i].cellObject.appendChild(tableobj);
							this.cells[i].cellRepeater = obj;
							// appendere il nodo figlio (cioe' l' oggetto table) al TD corrente.
						}
						else 
						{
						
							/* 
								Disgraziatamente HTMLElement e' undefined in IE: per testare se il nodo passato 
								e' effettivamente un' istanza di un elemento del DOM l' unica possibilita' e' tentare di agganciarlo
								se va in eccezione non e' l' oggetto corretto.
								In FireFox invece e' sufficiente testare l' istanza di base dell' oggetto.
							*/
							if (typeof (HTMLElement) != "undefined")
							{	
								// FireFox
								if (obj instanceof HTMLElement)
									this.cells[i].cellObject.appendChild(obj);
							}
							else
							{
								// IE
								try
								{
									this.cells[i].cellObject.appendChild(obj);
								} catch (e) {}
							}
						}
					}
			}
		}
	}
	
}



