// $VERSION: table.js 1.3 (07-11-2006)
/*
	Gestion des tableaux
*/
 Event.onDOMReady(function() { 
 	$$('table.tableau').each(function(element) {
		element.swapRow  = table_swapRow;
		element.sort = table_sort;
		element.compare = table_compare;
		element.getValue = table_getvalue;
		
	});
	
	var classname = 'impaire';
	$$('table.tableau tbody tr').each(function(element) {
		element.onmouseover= function(){tronmouseover(element) };
		element.onmouseout = function(){tronmouseout(element) };
		Event.observe(element, 'click', function(){tronmouseclick(element) }, false);
		classname = (classname == 'paire') ? 'impaire' : 'paire';
		element.className = classname;
	
	});
	
	$$('table.tableau thead td').each(function(element) {
		element.order = 1;
		element.type = 1;
		Event.observe(element, 'mouseover', function(){theadtdonmouseover(element) }, false);
		Event.observe(element, 'mouseout', function(){theadtdonmouseout(element) }, false);
		Event.observe(element, 'click', function(){theadtdonclick(element) }, false);
		//Event.observe(element, 'click', function(){enableTooltips("tab_docs")}, false);
	});
		//function(){enableTooltips("tab_docs")};
});
function table_init() { 
 	$$('table.tableau').each(function(element) {
		element.swapRow  = table_swapRow;
		element.sort = table_sort;
		element.compare = table_compare;
		element.getValue = table_getvalue;
		
	});
	
	var classname = 'impaire';
	$$('table.tableau tbody tr').each(function(element) {
		element.onmouseover= function(){tronmouseover(element) };
		element.onmouseout = function(){tronmouseout(element) };
		Event.observe(element, 'click', function(){tronmouseclick(element) }, false);
		classname = (classname == 'paire') ? 'impaire' : 'paire';
		element.className = classname;
	
	});
	
	$$('table.tableau thead td').each(function(element) {
		element.order = 1;
		element.type = 1;
		Event.observe(element, 'mouseover', function(){theadtdonmouseover(element) }, false);
		Event.observe(element, 'mouseout', function(){theadtdonmouseout(element) }, false);
		Event.observe(element, 'click', function(){theadtdonclick(element) }, false);
		//Event.observe(element, 'click', function(){enableTooltips("tab_docs")}, false);
	});
		
}

function tronmouseclick(tr){
	if (!tr.parentNode)return;
	if (tr.parentNode.nodeSelected)
		tr.parentNode.nodeSelected.className = (tr.parentNode.nodeSelected.getAttribute("oldFocusClassName")||'');
	tr.parentNode.nodeSelected = tr;
	tr.className =  'selected';
	
}

function tronmouseout(tr){
	if(tr.parentNode.nodeSelected && tr.parentNode.nodeSelected == tr) return;
    tr.className =  (tr.getAttribute("oldFocusClassName")||'');
}

function tronmouseover(tr){
	if(tr.parentNode.nodeSelected && tr.parentNode.nodeSelected == tr) return;
	tr.setAttribute("oldFocusClassName", tr.className);
	tr.className = 'focus';
}



function theadtdonmouseover(td){
	if (td.style)  				
		td.style.cursor = 'pointer';
	td.setAttribute("oldFocusClassName", td.className);
	td.className = 'focus';
	
}

function theadtdonmouseout(td){
	if (td.style)  				
		td.style.cursor = '';
	
    td.className =  (td.getAttribute("oldFocusClassName")||'');

}
function theadtdonclick(td){

	table = td.parentNode.parentNode.parentNode;
	if (!table.coltri) table.coltri = 0;
	table.rows[0].cells[table.coltri].style.background ='';
	
	table.coltri = td.cellIndex;	
	td.order = td.order * -1;
	if (td.order==1)
		td.style.background=' url(images/tableau/tri_decroissant.gif) no-repeat  top right'	
	else
		td.style.background='  url(images/tableau/tri_croissant.gif) no-repeat  top right'
	
	table.order = td.order;
	if (!td.getAttribute("datatype")){
		regT = new Array(
        /^(0[1-9]|[1-3]0|[1-3]1|[1-2][0-9])\D?(0[1-9]|1[0-2])\D?(0|[1-9]|\d{2}|\d{3}|\d{4})$/,
        /^(0[1-9]|[1-3]0|[1-3]1|[1-2][0-9])\D?([1-9]|0[1-9]|1[0-2])\D?(0|[1-9]|\d{2}|\d{3}|\d{4})$/,
        /^([1-9]|0[1-9]|[1-3]0|[1-3]1|[1-2][0-9])\D?([1-9]|0[1-9]|1[0-2])\D?(0|[1-9]|\d{2}|\d{3}|\d{4})$/
        )
        datatype = 2;
		for (irow = 1; irow < table.rows.length; irow++){
			if (table.getValue(irow, td.cellIndex)=='') {
		   		table.rows[irow].cells[td.cellIndex].setAttribute("tmporderby","");
				continue;
			}
	        var testdate = false;
			for(i=0; i<regT.length; i++){
			   if(regT[i].test(table.getValue(irow, td.cellIndex))) {
			   		testdate = true;
			   		table.rows[irow].cells[td.cellIndex].setAttribute("tmporderby",RegExp.$3+RegExp.$2+RegExp.$1);
			   }
			}
			if (!testdate){
				datatype = 1;
				break;
			}
		}
		if (datatype == 2){
			for (irow = 1; irow < table.rows.length; irow++){
				table.rows[irow].cells[td.cellIndex].setAttribute("orderby",table.rows[irow].cells[td.cellIndex].getAttribute("tmporderby"));
			}
		}
		td.setAttribute("datatype", datatype);
		td.datatype = datatype;  	
	}
	table.datatype = td.datatype;
	table.sort(1, table.rows.length-1);
	var classname = 'impaire';
	$$('table.tableau tbody tr').each(function(element) {
		element.onmouseover= function(){tronmouseover(element) };
		element.onmouseout = function(){tronmouseout(element) };
		element.onclick = function(){tronmouseclick(element) };
		classname = (classname == 'paire') ? 'impaire' : 'paire';
		element.className = classname;
	});
	

	
}

function table_swapRow (row1, row2) { 
	var node = this.rows[row1].cloneNode(true);
	var node2 = this.rows[row2].cloneNode(true);
	this.rows[row1].parentNode.replaceChild(node2, this.rows[row1]);
	this.rows[row2].parentNode.replaceChild(node, this.rows[row2]);
} 

function table_compare(row1, row2){
		if (this.order > 0)
			return (this.getValue(row1, this.coltri) < this.getValue(row2, this.coltri));
		else
			return (this.getValue(row1, this.coltri) > this.getValue(row2, this.coltri));
		
}

function table_getvalue(row, col) {
	if (!this.rows[row].cells[col].getAttribute("orderby"))	{
		var child = this.rows[row].cells[col];
		var value = child.nodeValue;
		while (child && child.nodetype != 3) {
			value = child.nodeValue
			child = child.firstChild;
		}
		this.rows[row].cells[col].setAttribute("orderby", value);
	}
	return this.rows[row].cells[col].getAttribute("orderby");
}


/* Attention :on prefere un algorithme qui minimise le nombre de swap de ligne  a celui ci qui minimise les iterations*/
function table_quicksort(min, max)
{
	var i = 0;
	var j = 0;
	var RandIndex = 0;
	
	if (min < max)
	{
		if (max - min == 1)
		{
			if (this.compare1(min, max))
				this.swapRow(min, max);
		}
		else
		{
			RandIndex = Math.round(Math.random() * (max - min) + min);
			this.swapRow(max, RandIndex);
			do
			{
				i = min; 
				j = max;
				while ((i < j) && (this.compare(max, i ))) i++;
				while ((j > i) && (this.compare(j, max))) j--;
				if (i < j) this.swapRow(i, j);
			}
			while (i < j)
			this.swapRow(i, max);
			if ((i - min) < (max - i))
			{
				this.sort(min, i - 1);
				this.sort(i + 1, max);
			}
			else
			{
				this.sort(i + 1, max);
				this.sort(min, i - 1);
			}
		}
	} 
/* function(){enableTooltips("tab_docs")};	 */
}
/* on prefere un algorythme qui minimise le nombre de swap de ligne */
function table_sort()
{
	var i = 0;
	var j = 0;
	var max = this.rows.length;
	var indice = 0;
	for (i = 1; i < max; i++) {
		indice = i;
		for( var j= i+1; j < max; j++)	{
			if (this.compare(indice,j))
				indice = j;
		}
		if (indice != i)
			this.swapRow(i, indice);
	}



 };


