
var tableSort = function (element, tagName, upStyle, downStyle) {
	this.table = (typeof (element) == "string") ? TAGDom.fromId(element) : element;
	this.td = this.table.getElementsByTagName("td");
	this.hName = (tagName.length < 1) ? "tag" : tagName;
	this.rows = this.table.rows;
	this.byUp = (upStyle == undefined) ? "DescUp" : upStyle;
	this.byDown = (downStyle == undefined) ? "DescDown" : downStyle;
	var totalTd = [];
	var totalNr = [];
	var totalTag = [];
	this.tempCellIndex;
	this.tempStatus = {};
	var setAllTag = function () {
		for (var i = 0; i < this.td.length; i++) {
			if (this.td[i].className === this.hName) {
				totalTag.push(this.td[i]);
			}
		}
	}.bind(this);
	var getStatus = function (tagName) {
		this.tempStatus[tagName] = (this.tempStatus[tagName] == true) ? false : true;
		for (tags in this.tempStatus) {
			if (tags == tagName) {
				return this.tempStatus[tags];
			} else {
				delete this.tempStatus[tags];
			}
		}
	}.bind(this);
	var changeShow = function (cellIn, status) {
		for (var i = 0; i < totalTag.length; i++) {
			if (totalTag[i].cellIndex == cellIn) {
				if (status == true) {
					totalTag[i].className = this.byUp;
				} else {
					if (status == false) {
						totalTag[i].className = this.byDown;
					}
				}
			} else {
				totalTag[i].className = this.hName;
			}
		}
	}.bind(this);
	var startSort = function (status) {
		for (var i = totalNr[this.tempCellIndex].length - 1; i > 0; i--) {
			for (var j = 0; j < i; ++j) {//alert(totalNr[this.tempCellIndex][j].replace(/<.*?>/g,"").substring(0,22));
				if ((status == true) ? totalNr[this.tempCellIndex][j].replace(/<.*?>/g,"").substring(0,22) > totalNr[this.tempCellIndex][j + 1].replace(/<.*?>/g,"").substring(0,22) : totalNr[this.tempCellIndex][j].replace(/<.*?>/g,"").substring(0,22) < totalNr[this.tempCellIndex][j + 1].replace(/<.*?>/g,"").substring(0,22)) {
					var temp = totalNr[this.tempCellIndex][j + 1];
					totalNr[this.tempCellIndex][j + 1] = totalNr[this.tempCellIndex][j];
					totalNr[this.tempCellIndex][j] = temp;
					for (var n = 0; n < totalNr.length; n++) {
						if (n != this.tempCellIndex) {
							var temps = totalNr[n][j + 1];
							totalNr[n][j + 1] = totalNr[n][j];
							totalNr[n][j] = temps;
						}
					}
				}
			}
		}
		InsertContect();
	}.bind(this);
	var InsertContect = function () {
		for (var i = 1; i < totalTd.length; i++) {//原来i的起始值是0现在改为1
			for (var j = 0; j < totalTd[i].length; j++) {
				totalTd[i][j].innerHTML = totalNr[i][j];
			}
		}
		totalTd.length = 0;
		totalNr.length = 0;
	};
	this.init = function (status) {
		for (var i = 0; i < (this.td.length / this.rows.length); i++) {
			totalTd[i] = new Array();
			totalNr[i] = new Array();
			for (var j = 0; j < this.rows.length; j++) {
				if (this.rows[j].cells[i].className !== this.hName) {
					totalTd[i][j - 1] = this.rows[j].cells[i];
					totalNr[i][j - 1] = this.rows[j].cells[i].innerHTML;
				}
			}
		}
		startSort(status);
	};
	this.toTagStart = function () {
		setAllTag();
		for (var i = 0; i < totalTag.length; i++) {
			totalTag[i].sIndex = function (cIndex) {
				this.tempCellIndex = cIndex;
			}.bind(this);	//设置列index
			totalTag[i].gStatus = function (values) {
				return getStatus(values);
			};				//获取与设置状态
			totalTag[i].ceShow = function (i, s) {
				changeShow(i, s);
			};							//改变显示的状态(排序状态的箭头)
			totalTag[i].stSort = function (status) {
				this.init(status);
			}.bind(this);			//初始化
			totalTag[i].onselectstart = function () {
				return false;
			};							//点击不选中文字
			totalTag[i].onclick = function () {
				this.sIndex(this.cellIndex);
				this.status = this.gStatus(this.firstChild.nodeValue);
				this.ceShow(this.cellIndex, this.status);
				this.stSort(this.status);
			};
		}
	};
};

