/*
  File        : data.js
  Author      : DS
  Date        : 2005-04-06
  Description : JavaScript for data table
*/

/************************************************************

    Necessary files
    ---------------
         auto.js
         data.js

    State diagram
    -------------

       +-------+
       |       |
       | Ready |
       |       |
       +-------+


    Description
    -----------

         The following HTML code shows a typical use of this module. Note that the DHTML function
initDatas() is called once the page is loaded in order to create and attach the Data
objects.

<html>
	<head>
		<script src="dhtml.js"></script>
		<script src="auto.js"></script>
		<script src="data.js"></script>
		<style>
			.even {background-color:gray;}
			.odd  {background-color:white;}
		</style>
	</head>
	<body onload="InitDhtml()">
		<table id="data1" class="data" class1="even" class2="odd">
			<tr><td>row 1</td></tr>
			<tr><td>row 2</td></tr>
			<tr><td>row 3</td></tr>
			<tr><td>row 4</td></tr>
			<tr><td>row 5</td></tr>
		</table>
	</body>
</html>

************************************************************/

RegisterInitFunction(InitDatas);

/*** DHTML function to initialize data ***/
function InitDatas()
{
	/* Two-step initialization */
	var datas = new Array();
	/* Create Data controls */
	var tables = document.getElementsByTagName('table');
	for (var i=0; i<tables.length; i++)
	{
		var cl = tables[i].className;
		if (cl != null && cl == 'data')
		{
			var data     = tables[i];
			data.control = new Data(data);
			datas.push(data.control);
		}
	}
	/* Initialize in reverse order */
	while (datas.length)
	{
		var data = datas.shift();
		data.event("CONTROL_INIT");
	}
}

/*** Create a new data ***/
function Data(element)
{
	/* Call base class constructor */
	Auto.call(this);
	this.id        = element.id;
	this.element   = element;
	this.class1    = element.getAttribute("class1");
	this.class2    = element.getAttribute("class2");
}

/* Inherit from Auto */
Data.prototype = new Auto();

/*** Define transition table for class ***/
var Data_StateCounter  = 0;
var Data_StateReady    = Data_StateCounter++;
var Data_nb            = Data_StateCounter;
var Data_trans         = new Array(Data_nb);
/* State Ready */
Data_trans[Data_StateReady] = new Array
	(
		new Array("CONTROL_INIT"     , "this.init()"   , Data_StateReady)
	);

/*** Transition definitions ***/
function Data_init()
{
	var rows = this.element.rows;
	for (var i=0; i<rows.length; i++)
	{
		rows[i].className = Math.floor(i/2) == (i/2) ? this.class1 : this.class2;
	}
}

/* Over-ride the base class transition table Auto.trans */
Data.prototype.trans = Data_trans;
Data.prototype.init  = Data_init;

