var oTRs;		// TRオブジェクトリスト
var nColId;		// ソート対象のカラムID
var sType;		// ソート方式（n:数値、s:文字列、d:日時）
var A_COL = "1";
var BACK_COLOR  = "#fff"; //#f5ffff

function SortTableInit() {

	// DOMをサポートしていなければ終了
	if (!document.getElementById) { return; }

	nColId = "0";
	sType = "d";
	
	var i, nRow;
	var oTABLE;
	var oTDs;
	var category;

	// TR要素のリストを取り出す
	oTABLE = document.getElementById("contentList");
	oTRs = oTABLE.getElementsByTagName("TR");
	
	for (nRow = 0; nRow < oTRs.length; nRow++) {

		// 洋画、邦画、劇場用アニメを初期表示
		oTDs = oTRs[nRow].getElementsByTagName("TD");
		category =oTDs[0].firstChild.nodeValue;
		if (category.indexOf("C1E")>-1 || category.indexOf("C10E")>-1 || category.indexOf("C12E")>-1) {
			oTRs[nRow].style.display=""
		}
		else {
			oTRs[nRow].style.display="none"
		}
		
		/*
		// 投稿日降順がデフォルトソート順なので背景色設定
		oTDs[oTDs.length-1].style.backgroundColor = BACK_COLOR;
		*/

	}
}

// idで指定されるテーブルの colId番目のカラムをソートする
// typeには n（数値）か s（文字列）d（日付）のいずれかを指定する
function SortTable(id, colId, type) {

	window.status = "ソート中…";

	// DOMをサポートしていなければ終了
	if (!document.getElementById) { return; }
	
	var i, nRow, nCol, nRow_sort;
	var oTABLE, oTDs;
	var aCells = new Array(); // temp
	var nRowIDs = new Array(); // ソート対象行リスト
	var nRowIDs_old = new Array(); // ソート前状態保持用（これをソートしたもので上書きする）

	// TR要素のリストを取り出す
	oTABLE = document.getElementById(id);
	oTRs = oTABLE.getElementsByTagName("TR");

	// テーブルの内容を覚えておく
	for (nRow = 0, nRow_sort = 0; nRow < oTRs.length; nRow++) {
	
		// 非表示項目は無視
		if(oTRs[nRow].style.display == "none") {
			continue;
		}
		
		nRowIDs[nRow_sort] = nRow;
		nRowIDs_old[nRow_sort] = nRow;
		oTDs = oTRs[nRow].getElementsByTagName("TD");
		
		aCells[nRow] = new Array();
		for (nCol = 0; nCol < oTDs.length; nCol++) {
		
			if(nCol == A_COL) {
				aCells[nRow][nCol] = oTDs[nCol].innerHTML;
			}
			else {
				aCells[nRow][nCol] = oTDs[nCol].firstChild.nodeValue;
			}
		}
		nRow_sort++;
	}
		
	// 同じ項目が再度ソートされた場合、昇順、降順入れ替え
	if(nColId == colId) {
		nRowIDs.reverse();
	}
	
	// ソートする カラム番号とソート方式を外部変数に覚えておく
	else {
		nColId = colId;
		sType = type;
		nRowIDs.sort(CompareRows);
	}
	
	// 覚えていた内容をテーブルに書き戻す
	for (nRow = 0; nRow < nRowIDs.length; nRow++) {
		oTDs = oTRs[nRowIDs_old[nRow]].getElementsByTagName("TD");
		
		for (nCol = 0; nCol < oTDs.length; nCol++) {
		
			if(nCol == A_COL) {
				oTDs[nCol].innerHTML = aCells[nRowIDs[nRow]][nCol];
			}
			else {
				oTDs[nCol].firstChild.nodeValue = aCells[nRowIDs[nRow]][nCol];
			}

			/*
			if(nCol == nColId) {
				oTDs[nCol].style.backgroundColor = BACK_COLOR;
			}
			else {
				oTDs[nCol].style.backgroundColor = "#ffffff";
			}
			*/
		}
	}
	
	window.status = "";
}

// テーブルソートのための比較関数
function CompareRows(a, b) {

	var sA;
	var sB;
	
	if(nColId == A_COL) {
		sA = oTRs[a].getElementsByTagName("A")[0].firstChild.nodeValue;
		sB = oTRs[b].getElementsByTagName("A")[0].firstChild.nodeValue;
	}
	else {
		sA = oTRs[a].getElementsByTagName("TD")[nColId].firstChild.nodeValue;
		sB = oTRs[b].getElementsByTagName("TD")[nColId].firstChild.nodeValue;
	}

	
	// 数字
	if (sType == "n") {
		sA = parseInt(sA);
		sB = parseInt(sB);
	}
	
	// 日付
	else if(sType == "d") {
		var d;
		d = new Date(sA);
		sA = d.getTime();
		
		d = new Date(sB);
		sB = d.getTime();
	}

	
	if (sA == sB) {
		return 0;
	} else if (sA > sB) {
		return 1;
	} else {
		return -1;
	}
}