// JavaScript Document

/**
 * Class sortTable
 *
 * @param string table_id
 */
function sortTable(table_id) {
	this.table = document.getElementById(table_id);
	this.ordering=new Array();
	this.classname = 'dif';
	this.getHeader();
		
} 

/**
 * Method to define the table header as the data fields 
 *
 *
 */
sortTable.prototype.getHeader=function() {
	var thead =this.table.tHead.rows;
	var header = thead.length;
	
	var th_cells;
	var cells_len = 0;
	
	th_cells = thead[header - 1].cells;	
	cells_len = th_cells.length;
	
	for (var i=0; i < cells_len; i++) {
		this.ordering[i] = "uo"; //"uo"|"asc"|"desc"
		this.attSort(th_cells[i]);
	}
	
	this.alterRowColor();
	
}

/**
 * Method to attach the method 'sortByCol' to the header's table 
 *
 * @param object tag - html dom
 */
sortTable.prototype.attSort=function(tag) {
	var instanceOfBox=this;
	var tag = tag;
	tag.onclick=function(){
		instanceOfBox.sortByCol(this.cellIndex);	
	}	
	tag.onmouseover=function() {
			this.style.cursor="hand";
	}	
}

sortTable.prototype.alterRowColor=function() {
	var tbody = this.table.tBodies[0];
	var tb_rows = tbody.rows;
	var rows_len = tb_rows.length;
	for (var n=0; n < rows_len; n++) {
		if ((n%2) == 1) {
			// Alter the line colors of the table
			tb_rows[n].className = this.classname;
		}
	}
}

/**
 * Method to sort the data table by field 
 *
 * @param int col_index - table column index
 */
sortTable.prototype.sortByCol=function(col_index) {
	var instanceOfBox=this;
	
	var tbody = this.table.tBodies[0];

	var tb_rows = tbody.rows;
	var values = new Array();
	var order = new Array();
		
		for (var i=0; i < tb_rows.length; i++) {
			values[i] = tb_rows[i].cells[col_index].innerHTML;
			order[i] = i;			
		}
	
	var ordering; //asc=true|desc=false
	
	switch(instanceOfBox.ordering[col_index])
	{
		case "asc":
			instanceOfBox.ordering[col_index] = "desc";
			ordering = false;
			break;
		case "desc":
			instanceOfBox.ordering[col_index] = "asc";
			ordering = true;
			break;
		default:
			instanceOfBox.ordering[col_index] = "asc";
			ordering = true;
	}
	
	var test;
	//ordena
	 var i, j;
  		for (i = values.length - 1; i >= 0; i--)
  		{
    		for (j = 0; j <= i; j++)
    		{
				test = (ordering)? values[j+1] < values[j]:values[j+1] > values[j];	
      			if (test)
      			{
					var temp = values[j];
					values[j] = values[j+1];
					values[j+1] = temp;
					
					temp = order[j];
					order[j] = order[j+1];
					order[j+1] = temp;
			     }
    		}
  		}
		
		
		var rows_len = values.length;
		var col_len = tb_rows[0].cells.length;
		var new_row;
		for (var n=0; n < rows_len; n++) {
			new_row = tbody.insertRow(tb_rows.length);
			if ((n%2) == 1) {
				// Alter the line colors of the table
				new_row.className = instanceOfBox.classname;
			}
			for (var m=0; m <col_len; m++) {
				new_cell = new_row.insertCell(m);
				new_cell.innerHTML = tb_rows[order[n]].cells[m].innerHTML;
			}
		}
		for (n=0; n < rows_len; n++) {		
			new_row = tbody.deleteRow(0);
		}
		
}