/*--------------------------------------------------------------*/
// HTML TABLE SORTER
// OBJECT ORIENTED JAVASCRIPT IMPLEMENTATION OF QUICKSORT
// @author	Terrill Dent 
// @source	http://www.terrill.ca
// @date	August 28th, 2006
/*--------------------------------------------------------------*/
Pas_trier_der_ligne=0; //pas trier dernière rangé avec le total

function TSorter(){
	
	var table = Object;
	var trs = Array;
	var ths = Array;
	var curSortCol = Object;
	var prevSortCol = '10';
	
	
	

	var sortType = Object;

	function get(){}

	function getCell(index){			
		return trs[index].cells[curSortCol] 
	}

	/*----------------------INIT------------------------------------*/
	// Initialize the variable
	// @param tableName - the name of the table to be sorted
	/*--------------------------------------------------------------*/
	this.init = function(tableName)
	{
		
		table = document.getElementById(tableName);
		ths = table.getElementsByTagName("th");
		prevSortCol=0;
		ths[0].className = 'ascend';
		for(var i = 0; i < ths.length ; i++)
		{	
			if (ths[i].abbr!="donotdo"){				
				ths[i].onclick = function()
				{
					sort(this);
				}
			}
		}
		
		$(".sortable tr:even").css("background-color", "#819dc7");
	    $(".sortable tr:odd").css("background-color", "#dcdcdc");
		$(".red").css("background-color", "red");
		$(".yellow").css("background-color", "yellow");

		return true;
	};
	
	/*----------------------SORT------------------------------------*/
	// Sorts a particular column. If it has been sorted then call reverse
	// if not, then use quicksort to get it sorted.
	// Sets the arrow direction in the headers.
	// @param oTH - the table header cell (<th>) object that is clicked
	/*--------------------------------------------------------------*/
	function sort(oTH)
	{
		
		document.body.style.cursor = "wait";

		curSortCol = oTH.cellIndex;
		sortType = oTH.abbr;
		trs = table.tBodies[0].getElementsByTagName("tr");		
		//set the get function
		
		if (sortType=="donotdo")
		{
			//ne pas rien faire
			return 0
		}
		setGet(sortType)

		// it would be nice to remove this to save time,
		// but we need to close any rows that have been expanded
		for(var j=0; j<trs.length; j++)
		{
			if(trs[j].className == 'detail_row')
			{
				closeDetails(j+2);
			}
		}
		
		// if already sorted just reverse
		if(prevSortCol == curSortCol)
		{
			oTH.className = (oTH.className != 'ascend' ? 'ascend' : 'descend' );
			reverseTable();
		}
		// not sorted - call quicksort
		else
		{			
			oTH.className = 'descend';			
			//if(ths[prevSortCol].className != 'exc_cell'){ths[prevSortCol].className = '';}
			try{
				ths[prevSortCol].className = '';
			}catch(err){
				//do nothing
			}
			
			quicksort(0, trs.length-Pas_trier_der_ligne);
		}
		prevSortCol = curSortCol;		
		$(".sortable tr:even").css("background-color", "#819dc7");
	    $(".sortable tr:odd").css("background-color", "#dcdcdc");
		$(".red").css("background-color", "red");
		$(".yellow").css("background-color", "yellow");
		
		document.body.style.cursor = "default";
		

	}
	
	/*--------------------------------------------------------------*/
	// Sets the GET function so that it doesnt need to be 
	// decided on each call to get() a value.
	// @param: colNum - the column number to be sorted
	/*--------------------------------------------------------------*/
	function setGet(sortType)
	{
		
		switch(sortType)
		{   			
			case "link":					
			get = function(index)
			{
				
				return  getCell(index).firstChild.firstChild.nodeValue;
			};
			break;

			case "linknumber":						
			get = function(index)
			{
				return  parseFloat(getCell(index).firstChild.firstChild.nodeValue.replace(",",""));
			};
			break;

			case "text_inputs":
			get = function(index)
			{					
				return getCell(index).firstChild.value;
			};
			break;
			case "missing_values":
			get = function(index){	
				var cell = getCell(index);
				if(cell.firstChild)
					return cell.firstChild.nodeValue;
				else
					return ' ';
			}
			break;		
			
			case "number":			
				
			get = function(index){	
				return parseFloat(getCell(index).firstChild.nodeValue.replace(",",""));
			};
			break;
			
			default:				
			get = function(index){	return getCell(index).firstChild.nodeValue;};
			
			break;

		};	
	}

	/*-----------------------EXCHANGE-------------------------------*/
	//  A complicated way of exchanging two rows in a table.
	//  Exchanges rows at index i and j
	/*--------------------------------------------------------------*/
	function exchange(i, j)
	{
		if(i == j+1) {
			table.tBodies[0].insertBefore(trs[i], trs[j]);
		} else if(j == i+1) {
			table.tBodies[0].insertBefore(trs[j], trs[i]);
		} else {
			var tmpNode = table.tBodies[0].replaceChild(trs[i], trs[j]);
			if(typeof(trs[i]) == "undefined") {
				table.appendChild(tmpNode);
			} else {
				table.tBodies[0].insertBefore(tmpNode, trs[i]);
			}
		}
	}
	
	/*----------------------REVERSE TABLE----------------------------*/
	//  Reverses a table ordering
	/*--------------------------------------------------------------*/
	function reverseTable()
	{
		for(var i = 1; i<trs.length-Pas_trier_der_ligne; i++)
		{
			table.tBodies[0].insertBefore(trs[i], trs[0]);
		}
	}

	/*----------------------QUICKSORT-------------------------------*/
	// This quicksort implementation is a modified version of this tutorial: 
	// http://www.the-art-of-web.com/javascript/quicksort/
	// @param: lo - the low index of the array to sort
	// @param: hi - the high index of the array to sort
	/*--------------------------------------------------------------*/
	function quicksort(lo, hi)
	{
		if(hi <= lo+1) return;
		 
		if((hi - lo) == 2) {
			if(get(hi-1) > get(lo)) exchange(hi-1, lo);
			return;
		}
		
		var i = lo + 1;
		var j = hi - 1;
		
		if(get(lo) > get(i)) exchange(i, lo);
		if(get(j) > get(lo)) exchange(lo, j);
		if(get(lo) > get(i)) exchange(i, lo);
		
		var pivot = get(lo);
		
		while(true) {
			j--;
			while(pivot > get(j)) j--;
			i++;
			while(get(i) > pivot) i++;
			if(j <= i) break;
			exchange(i, j);
		}
		exchange(lo, j);
		
		if((j-lo) < (hi-j)) {
			quicksort(lo, j);
			quicksort(j+1, hi);
		} else {
			quicksort(j+1, hi);
			quicksort(lo, j);
		}
	}
}

