/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005
(C) MediaThread, October 2007

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

http://mtcrossmedia.com/
************************************************************************************************************/

// Configuration:: Images path
var imgArrowUp   = "imgs/arrow_up.gif";
var imgArrowDown = "imgs/arrow_down.gif";
var imgTransp    = "imgs/transparent.gif";

// Preload
var sortTableImg2=new Image(); sortTableImg2.src=imgArrowUp;
var sortTableImg1=new Image(); sortTableImg1.src=imgArrowDown;
var sortTableImg0=new Image(); sortTableImg0.src=imgTransp;

var tableWidget_okToSort = true;
var tableWidget_arraySort = new Array();
tableWidget_tableCounter = 1;
var activeColumn = new Array();
var currentColumn = false;

function sortNumeric(a,b) {
	a = a.replace(/,/,'.');
	b = b.replace(/,/,'.');
	a = a.replace(/[^\d\.\/]/g,'');
	b = b.replace(/[^\d\.\/]/g,'');
	if(a.indexOf('/')>=0)a = eval(a);
	if(b.indexOf('/')>=0)b = eval(b);
	return a/1 - b/1;
}


function sortString(a, b) {
  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
  return 0;
}
	
function getSortableContent(obj) {
	return obj.getElementsByTagName('SPAN')[0].innerHTML+'';
}

function sortTable() {
	if(!tableWidget_okToSort)return;
	tableWidget_okToSort = false;
	
	var trObj = this.parentNode;//.parentNode.parentNode;
	var imgsObj = trObj.getElementsByTagName('IMG');
	for (var i=0; i<imgsObj.length; i++) {
		imgsObj[i].src = imgTransp;
	}

	/* Getting index of current column */
	var obj = this;
	var indexThis = 0;
	while(obj.previousSibling){
		obj = obj.previousSibling;
		if(obj.tagName=='TD')indexThis++;		
	}
	
	if(this.getAttribute('direction') || this.direction){
		direction = this.getAttribute('direction');
		if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
		if(direction=='ascending'){
			direction = 'descending';
			this.setAttribute('direction','descending');
			this.direction = 'descending';
			this.getElementsByTagName('IMG')[0].src = imgArrowDown;
		}else{
			direction = 'ascending';
			this.setAttribute('direction','ascending');		
			this.direction = 'ascending';		
			this.getElementsByTagName('IMG')[0].src = imgArrowUp;
		}
	}else{
		direction = 'ascending';
		this.setAttribute('direction','ascending');
		this.direction = 'ascending';
		this.getElementsByTagName('IMG')[0].src = imgArrowUp;
	}
	
	var tableObj = this.parentNode.parentNode.parentNode;
	var tBody = tableObj.getElementsByTagName('TBODY')[0];
	
	var widgetIndex = tableObj.getAttribute('tableIndex');
	if(!widgetIndex)widgetIndex = tableObj.tableIndex;
	
	if(currentColumn)currentColumn.className='';
	document.getElementById('col' + widgetIndex + '_' + (indexThis+1)).className='highlightedColumn';
	currentColumn = document.getElementById('col' + widgetIndex + '_' + (indexThis+1));

		
	var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
	if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=this){
		if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');			
	}

	activeColumn[widgetIndex] = this;
	
	var cellArray = new Array();
	var cellObjArray = new Array();
	for(var no=1;no<tableObj.rows.length;no++){
		var content = tableObj.rows[no].cells[indexThis].innerHTML+'';
		var content = getSortableContent(tableObj.rows[no].cells[indexThis]);
		cellArray.push(content);
		cellObjArray.push(tableObj.rows[no].cells[indexThis]);
	}
	
	if(sortMethod=='N'){
		cellArray = cellArray.sort(sortNumeric);
	}else{
		cellArray = cellArray.sort(sortString);
	}
	
	if(direction=='descending'){
		for(var no=cellArray.length;no>=0;no--){
			for(var no2=0;no2<cellObjArray.length;no2++){
				if(getSortableContent(cellObjArray[no2]) == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
					cellObjArray[no2].setAttribute('allreadySorted','1');	
					tBody.appendChild(cellObjArray[no2].parentNode);				
				}				
			}			
		}
	}else{
		for(var no=0;no<cellArray.length;no++){
			for(var no2=0;no2<cellObjArray.length;no2++){
				if(getSortableContent(cellObjArray[no2]) == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
					cellObjArray[no2].setAttribute('allreadySorted','1');	
					tBody.appendChild(cellObjArray[no2].parentNode);				
				}				
			}			
		}				
	}
	
	for(var no2=0;no2<cellObjArray.length;no2++){
		cellObjArray[no2].removeAttribute('allreadySorted');		
	}

	// Re-Set the background color of the cells
	for (var cRows=0; cRows<tBody.rows.length; cRows++) {
		var className = (cRows%2)?'sortTableLightCell':'sortTableDarkCell';
		for (var cCells=0; cCells<tBody.rows[cRows].cells.length; cCells++) {
			tBody.rows[cRows].cells[cCells].className=className;
		}
	}

	tableWidget_okToSort = true;
	
	
}

function clearSelection() {
	// Removes the annoying(?) auto selection of word on double-click
	if( typeof document.selection !== 'undefined' ) {
	if(document.selection) { document.selection.empty(); }
	} else if(window.getSelection) {
		var sel = window.getSelection();
		if(sel.removeAllRanges) {
			window.getSelection().removeAllRanges();
		}
		sel = null;
	}
}

function initSortTable(objId,sortArray) {
	var obj = document.getElementById(objId);
	obj.setAttribute('tableIndex',tableWidget_tableCounter);
	obj.tableIndex = tableWidget_tableCounter;
	tableWidget_arraySort[tableWidget_tableCounter] = sortArray;
	var tHead = obj.getElementsByTagName('THEAD')[0];
	var cells = tHead.getElementsByTagName('TD');
	for(var no=0;no<cells.length;no++){
		if(sortArray[no]){
			cells[no].onclick = sortTable;
			cells[no].ondblclick = clearSelection;
		}else{
			cells[no].style.cursor = 'default';	
		}
	}
	// Insert arrow img placeholder
	var tHead = obj.getElementsByTagName('THEAD')[0];
	for (var cRows=0; cRows<tHead.rows.length; cRows++) {
		var className = (cRows%2)?'sortTableLightCell':'sortTableDarkCell';
		var objCellsCol = tHead.rows[cRows].cells;
		for (var cCells=0; cCells<objCellsCol.length; cCells++) {
			objCellsCol[cCells].innerHTML+= '<img src="'+imgTransp+'" />';
			if (cCells==0) {
				objCellsCol[cCells].setAttribute('direction','ascending');
				objCellsCol[cCells].direction = 'ascending';
				objCellsCol[cCells].getElementsByTagName('IMG')[0].src = imgArrowUp;
			}

		}
	}
/*	
	// Right align numeric cells
	for(var no2=0;no2<sortArray.length;no2++){	
	 			if(sortArray[no2] && sortArray[no2]=='N')obj.rows[0].cells[no2].style.textAlign='right';
	}
*/
	// Re-Set the background color of the cells
	var tBody = obj.getElementsByTagName('TBODY')[0];
	for (var cRows=0; cRows<tBody.rows.length; cRows++) {
		var className = (cRows%2)?'sortTableLightCell':'sortTableDarkCell';
		for (var cCells=0; cCells<tBody.rows[cRows].cells.length; cCells++) {
			tBody.rows[cRows].cells[cCells].className=className;
		}
	}
	
	tableWidget_tableCounter++;
}

