/** DynTable (c) Hugues Thi�baux - SIRTEM
*	v 0.1
 */
function GetParentTable( obj )
	{
		parent = obj;
		while ( parent !== null && !( parent  instanceof HTMLTableElement ) )
		{ parent = parent.parentNode; }
		return parent;
	}

function GetParentRow( obj )
	{
		var parent = obj;
		//while ( parent !== null && !( parent  instanceof HTMLTableRowElement ) )// marche pas sous IE
		while ( parent !== null && !( parent.nodeName == 'TR' ) ) // marche sous IE
		{ parent = parent.parentNode; }
		return parent;
	}

function ClearTable( tbl )
{
	var x= tbl.rows.length - 1;
	for ( var i = x; i >= 0 ; i-- )	
	{ 
		tbl.deleteRow( i
			/*tbl.rows[i]*/ 
			); 
	}
}

function findNodeByName( base, nodename )
{
	var base_childNodes = Element.immediateDescendants(base);
	for ( var i in base_childNodes /*base.childNodes*/ )
	{
		if ( base_childNodes[i].nodeName == nodename ) return base_childNodes[i];
	}
	return null;
}

function DynTable ( dom_table, dom_bt_new, dom_bt_sav, data_url )
{
	this.table		= dom_table;
	this.data_url	= data_url;
	this.datas 		= null;
	Element.addClassName( this.table, 'dyntable' )
	this.tbody 		= dom_table.appendChild( Builder.node('tbody'));
	var head 		= findNodeByName( dom_table, 'THEAD' );
	if ( head == null )  alert ("Pas de section THEAD dans la table");
	var tr			= findNodeByName( head, 'TR' );
	if ( tr == null )  alert ("Pas de section THEAD/TR dans la table");

	this.FieldNames = new Array();
	this.nbcells    = 0;
	var tr_childNodes= Element.immediateDescendants(tr); //tr.childNodes
	for ( var i in tr_childNodes )
	{
		if ( tr_childNodes[i].nodeName == 'TH')
			this.FieldNames[this.nbcells++] =tr_childNodes[i].getAttribute('FieldName');
	}

	this.doClick	= function ( tr ) {		/*alert ('doclick' + tr.dbid );*/		}

	this.onClick		= function ( event ) {		this.doClick( GetParentRow( Event.element (event) ));		};
	this.doAjaxRequest 	= function ( method, parameters )
	{
		var options = {
				asynchronous:	true,
				evalScripts:	false
				};
		if ( parameters != null ) 	{ options.parameters = parameters; }
		if ( method != null ) 		{ options.onComplete = method.bind(this); }
		new Ajax.Request( this.data_url, options );
	};

	this.SaveFromEvent = function  ( event )
	{
		this.Save();
	};

	this.doAddRowFromEvent = function ( event )
	{
		alert ('doAddVariableFromEvent � d�finir');
	};

	this.AddRowFromEvent = function ( event )
	{
		this.doAddRowFromEvent( event );
		//this.AddVariable( -1,  $('input_varname').value, $('input_varvalue').value );
	};

	if ( dom_bt_new != null )	Event.observe( dom_bt_new , 'click',	this.AddRowFromEvent.bind(this) );
	if ( dom_bt_sav != null ) 	Event.observe( dom_bt_sav,	'click',	this.SaveFromEvent.bind(this) );

	this.MakeEditButtons = function ( cell )
		{
			var nmod = Builder.node("Text",{}, ["Modifier"]);
			var nsup = Builder.node("Text",{}, ["Supprimer"]);
			cell.appendChild ( nmod );
			cell.appendChild ( nsup );
			Event.observe( nmod, 		'click', this.eventModif.bind(this));
			Event.observe( nsup, 		'click', this.eventRemove.bind(this));
		};


	this.AddRow = function ( datas )
		{
			var tr 	= this.tbody.insertRow( this.tbody.rows.length );
			tr.dbid 	= datas.Id;
			tr.removed	= -1;
			this.datas[datas.Id] = datas;

			for (  var i = 0; i < this.nbcells; i++ )
			{
				tr.insertCell( i );
				if ( this.FieldNames[i] != null )	tr.cells[i].innerHTML = datas[this.FieldNames[i]];
			}

			this.MakeEditButtons( tr.cells[this.FieldNames.length-1] );
			Event.observe( tr, 'click', this.onClick.bind(this) );
			return tr;
		};

	this.LoadComplete = function( request, json )
		{
			var responses = AjaxHelper.makeresponse( request, json );
			this.datas = {};
			for ( var i = 0; i < responses.length; i++ ) 	{ this.AddRow( responses[i] );}
	 	};

	this.LoadFromArray = function( ar )
		{
			ClearTable( this.tbody );
			this.datas = {};
			for ( var i = 0; i < ar.length; i++ ) 			{ this.AddRow( ar[i] );}
	 	};

	this.SaveComplete = function( request, json )
		{
			var responses = this.getAjaxResponse( request, json );
			this.Load();
	 	};

	this.Load	= function ()
		{
			ClearTable( this.tbody );
			AjaxHelper.request( this.data_url, this.LoadComplete.bid(this), {'do': 'load' } );
		};

	this.Save	= function ()
		{
			AjaxHelper.request( this.data_url, this.SaveComplete.bind(this), {'do': 'save', json: this.ToJson() } );
		};

	this.ToArray= function ()
	{
		var res = new Array();
	  	for ( var i = 0; i < this.tbody.rows.length ; i++ )
	  	{
	  		var tr = this.tbody.rows[i];
	  		res[i] = { Id: tr.dbid, Removed: tr.removed };
			for (  var j = 0; j < this.nbcells; j++ )
			{
				if ( this.FieldNames[j] != null )
				{
					res[i][this.FieldNames[j]] = tr.cells[j].innerHTML;
				}
			}
		}
		return res;
	}
	this.ToJson	= function ()
	{
		return JSON.stringify( this.ToArray() );
	};

	this.eventModif= function ( event )
	{
		this.TRTextToEdit( GetParentRow( Event.element (event))); // target est un TD meme si l'evemenent est sur le tr
	}

	this.eventEndModif= function ( event )
	{
		this.EditToTRText( GetParentRow( Event.element (event) ) ); // target est un TD meme si l'evemenent est sur le tr
	};

	this.eventRemove= function ( event )
	{
		GetParentRow( Event.element (event) ).removed = 1;
		GetParentRow( Event.element (event) ).style.display = 'none';
	};

	this.TRTextToEdit = function( tr ) {};
	this.EditToTRText = function( tr ) {};
}
