//Author    :   狼Robot
//Contact   :   robot@k2046.cn
//Date      :   2008-04-19
//Explain   :   使Table可以点击排序.

/*使用说明  :
方法1:
    new TableSorter("tb1");
效果:
    id为tb1的table的第一行任意单元格都可以点击进行排序.

方法2:
    new TableSorter("tb1", 0, 1, 3);
效果:
    id为tb1的table的第一行0,1,3单元格可以进行点击排序.
*/
var aDisSortColumns = -1;
function TableSorter(table)
{	
    this.Table = this.$(table);
    if(this.Table.rows.length <= 1)
    {
        return;
    }
    var args = [];
    if(arguments.length > 1)
    {
        for(var x = 1; x < arguments.length; x++)
        {
            args.push(arguments[x]);
        }
    }
    this.Init(args);
}
function checkIsSort()
{
	
	return true;
}
function findNodeTextObj(pNode,nodeName)
{
	var res = findNodeText(pNode,nodeName);
	if(res == null)
		return pNode.innerText;
	return res;
}
function findNodeText(pNode,nName)
{
	nName = nName.toUpperCase();
	for(var i = 0; i < pNode.children.length; i++)
	{
		var cName = (pNode.children[i]).nodeName;
		cName = cName.toUpperCase();
		if(cName == "INPUT" && nName == "INPUT")	
		{
				if((pNode.children[i].type).toUpperCase() == "TEXT")
					return pNode.children[i].value;
		}
		else if(cName == "DIV" && nName == "DIV")	
				return pNode.children[i].innerText;
		else
		{
					var res = findInputText(pNode.children[i]);
					if (res != null)
						return res; 
		}
	}
	return null;
}

function checkEditRow()
{
	var editTab = document.getElementById(TABLE_ROW_EDIT_ID);
	if(editTab == null)
			return false;
	if(confirm(getLanTextByKey("T_SORT_FOR_EDIT_TIPS")))
	{
		(editTab.parentNode).removeChild(editTab);
		return false;
	}
	else
		return true;
}
function setHeaderSortStatus(th,ths)
{
	for(var i = 0;i<ths.length;i++)
	{
		var sp = getFirstChildObjByTag(ths[i],"span");
		if(ths[i].id != th.id && sp.nodeName == "SPAN")
			setHeaderSpClassNameByTh(ths[i],true,sp);
	}
	var sp = getFirstChildObjByTag(th,"span");
	setHeaderSpClassNameByTh(th,false,sp);	
}
function setHeaderSpClassNameByTh(th,isInitStatus,sp)
{
	setHeaderSpClassNameBySp(sp,isInitStatus);return;
	var thCs = th.childNodes;
	
	for(var i = 0;i<thCs.length;i++)
		setHeaderSpClassNameBySp(sp,isInitStatus);
}
function setHeaderSpClassNameBySp(sp,isInitStatus)
{
	if(isInitStatus)
	{
		sp.className = "icon_sort_list";
		sp.style.color = "#ccc";
		return;	
	}
	switch(sp.className)
	{
		case "icon_sort_list":			
			sp.className = "icon_sort_desc_list";
			sp.style.color = "#000";
			break;
		case "icon_sort_asc_list":
			sp.className = "icon_sort_desc_list";
			sp.style.color = "#000";
			break;
		case "icon_sort_desc_list":
			sp.className = "icon_sort_asc_list";
			sp.style.color = "#000";
			break;
		default:
			break;
	}
}
TableSorter.prototype = {
    $ : function(element)//简写document.getElementById
    {
        return document.getElementById(element);
    },
    Init : function(args)//初始化表格的信息和操作
    {
        this.Rows = [];
        this.Header = [];
        this.ViewState = [];
        this.LastSorted = null;
        this.NormalCss = "NormalCss";//正常的样式
        this.SortAscCss = "SortAscCss";//升序排列样式1
        this.SortDescCss = "SortDescCss";//降序排列样式2
        for(var x = 0; x < this.Table.rows.length; x++)
        {
            this.Rows.push(this.Table.rows[x]);
        }
        this.Header = this.Rows.shift().cells;
        for(var x = 0; x < (args.length ? args.length : this.Header.length); x++)
        {
            var rowIndex = args.length ? args[x] : x;
            if(rowIndex >= this.Header.length)
            {
                continue;
            }
            this.ViewState[rowIndex] = false;
            this.Header[rowIndex].style.cursor = "pointer";
            this.Header[rowIndex].onclick = this.GetFunction(this, "Sort", rowIndex);
        }
    },
    GetFunction : function(variable,method,param)//取得指定对象的指定方法.
    {
        return function()
        {
						setHeaderSortStatus((variable.Header)[param],variable.Header);
						
            variable[method](param);
        }
    },
    Sort : function(column)//执行排序.
    {
		//if(column==0)return;//第一列是否需要排序
		
				if(checkEditRow())
					return false;
        if(this.LastSorted)
        {
            this.LastSorted.className = this.NormalCss;
        }
        var SortAsNumber = true;
        for(var x = 0; x < this.Rows.length && SortAsNumber; x++)
        {
            SortAsNumber = findNodeTextObj(this.Rows[x].cells[column],"div");//this.IsNumeric(this.Rows[x].cells[column].innerHTML);
						SortAsNumber = this.IsNumeric(SortAsNumber);
        }
        this.Rows.sort(
        function(row1, row2)
        {
						
            var result;
            var value1,value2;
            value1 = findNodeTextObj(row1.cells[column],"div");//row1.cells[column].innerHTML;//
            value2 = findNodeTextObj(row2.cells[column],"div");//row2.cells[column].innerHTML;//
						//console.log("v1="+value1+",v2="+value2);
            if(value1 == value2)
            {
                return 0;
            }
            if(SortAsNumber)
            {
                result = parseFloat(value1) > parseFloat(value2);
            }
            else
            {
                result = value1 > value2;
            }
            result = result ? 1 : -1;
            return result;
        })
        if(this.ViewState[column])
        {
            this.Rows.reverse();
            this.ViewState[column] = false;
            this.Header[column].className = this.SortDescCss;
        }
        else
        {
            this.ViewState[column] = true;
            this.Header[column].className = this.SortAscCss;
        }
        this.LastSorted = this.Header[column];
        var frag = document.createDocumentFragment();
        for(var x = 0; x < this.Rows.length; x++)
        {
            frag.appendChild(this.Rows[x]);
        }
        this.Table.tBodies[0].appendChild(frag);
        this.OnSorted(this.Header[column], this.ViewState[column]);
    },
    IsNumeric : function(num)//验证是否是数字类型.
    {
        return /^\d+(\.\d+)?$/.test(num);
    },
    OnSorted : function(cell, IsAsc)//排序完后执行的方法.cell:执行排序列的表头单元格,IsAsc:是否为升序排序.
    {
        return;
    }
}