/*
tasks
1.	receive table column names and types (num / alpha)
	-	javascript object
2.	receive table rows
3.	on request
	- 	delete old html table
	-	display 'sorting' message
	-	create new html table where the rows have
		been sorted in requested order and 
		on requested column
	-	display new html table

*/

function k_table(array_column_types, array_column_titles, array_2d_rows) {

	this.tbl_column_types = array_column_types;
	this.tbl_column_titles = array_column_titles;
	this.rows = array_2d_rows.reverse(); 
	this.own_name = '';
	this.page_url = '';
	
	this.sort_by_col = 0;
		
	this.set_own_name = function (name_to_set) {
		this.own_name = name_to_set;
	}

	this.set_page_url = function (url_to_set) {
		this.page_url = url_to_set;
	}
	
	this.quick_sort = function (array_to_sort, sort_by_col, start_point, end_point) {

		var k = array_to_sort[Math.round((start_point + end_point)/2)][sort_by_col];
		var i = start_point;
		var j = end_point;
		
		while  (j > i) {
			if (this.tbl_column_types[sort_by_col] == 'num') {
				while (array_to_sort[i][sort_by_col] < k) ++i;			
				while (k < array_to_sort[j][sort_by_col]) j = j - 1;
			} else {
				while (array_to_sort[i][sort_by_col].toLowerCase() < k.toLowerCase()) ++i;			
				while (k.toLowerCase() < array_to_sort[j][sort_by_col].toLowerCase()) j = j - 1;
			}
			if (i <= j) {
				var d = array_to_sort[i];
				array_to_sort[i] = array_to_sort[j];
				array_to_sort[j] = d;
				++i;
				j = j - 1;
			}
		}
		
		if (start_point < j) this.quick_sort(array_to_sort, sort_by_col, start_point, j);
		if (i < end_point) this.quick_sort(array_to_sort, sort_by_col, i, end_point);
	}
			
	this.display_table = function(parent_element_name,sort_by) {

		// if col is clicked again, reverse the table, else sort it
		if (sort_by == this.sort_by_col) this.rows.reverse();
		else this.quick_sort(this.rows, sort_by, 0, this.rows.length - 1);
		this.sort_by_col = sort_by;

		// create table for sorted array
		var parent_element = document.getElementById(parent_element_name);
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		
		// create title row
		var tr = document.createElement("tr");
		for (var i=0; i < this.tbl_column_titles.length; i++) if (this.tbl_column_titles[i] != 'id'){ 
			var td = document.createElement("td");
			td.className = i == sort_by ? 'menuSel':'menu';
			var a =  document.createElement("a");
			var txt = document.createTextNode(this.tbl_column_titles[i]);
			a.className = i == sort_by ? 'menuSel':'menu';
			a.setAttribute('href',"javascript:"+this.own_name+".display_table('"+parent_element_name+"','"+i+"');");
	//		a.onclick = "alert('click');tst_table.sort_table("+i+");tst_table.display_table('arr_here')";
	//		a.setAttribute('onclick',
	//			"alert('click');tst_table.sort_table("+i+");tst_table.display_table('arr_here')");
			a.appendChild(txt);
			td.appendChild(a);
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
		
		// create data rows
		for (var i=0; i < this.rows.length; i++) {
			var tr = document.createElement("tr");
			for (var j=0; j < this.tbl_column_titles.length; j++) if (this.tbl_column_titles[j] != 'id'){//
				var td = document.createElement("td");
				var a =  document.createElement("a");
				var txt = document.createTextNode(this.rows[i][j])
				a.setAttribute('href',this.page_url+this.rows[i][0]);
				a.className = 'sortable_list';
				a.appendChild(txt);
				td.appendChild(a);
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);    
		table.className = 'content';
		while (parent_element.hasChildNodes()) parent_element.removeChild(parent_element.firstChild);
		parent_element.appendChild(table)
	}
} 


