var players =[];
var table;
var style;
var arrows; // стрелки сортировочные

function showActive()
{
	document.getElementById("showActive").style.backgroundColor = "#fc0";
	document.getElementById("showAll").style.backgroundColor = "";
	style.deleteRule(style.cssRules.length - 1);
	style.insertRule('.nonActive{display:none}', style.cssRules.length);
	var j = 1;
	for (var i = 2, l = table.rows.length; i < l; i++)
		if (table.rows[i].className != "nonActive")
		{
			table.rows[i].cells[0].innerHTML = j;
			j++;
		}
}
function showAll()
{
	document.getElementById("showAll").style.backgroundColor = "#fc0";
	document.getElementById("showActive").style.backgroundColor = "";
	style.deleteRule(style.cssRules.length - 1);
	style.insertRule('.nonActive{background-color:#fffbeb;}', style.cssRules.length);
	for (var i = 2, l = table.rows.length; i < l; i++)
		table.rows[i].cells[0].innerHTML = i - 1;
}

function getB(obj)
{
	return obj.getElementsByTagName('b')[0].innerHTML;
}

function setArrows(number, isAscending)
{
	for(var i = 0, l = arrows.length; i < l; i++) arrows[i].innerHTML = (i == number) ? isAscending ? "&#9650;" : "&#9660;" : "";  
}

function isAsc(obj)
{
	return obj.getElementsByTagName('b')[0].innerHTML.charCodeAt(0) != 9650;
}

function sortByName(isAscending)
{
	setArrows(0, isAscending);
	players.sort(function(a, b) 
	{ 
		if (a[2] > b[2]) return isAscending ? 1 : -1; 
		if (a[2] < b[2]) return isAscending ? -1 : 1; 
		if (a[2] == b[2]) return 0; 
	})
}

function sortByCity(isAscending)
{
	setArrows(1, isAscending);
	players.sort(function(a, b) 
	{ 
		if (a[3] > b[3]) return isAscending ? 1 : -1; 
		if (a[2] < b[3]) return isAscending ? -1 : 1; 
		if (a[3] == b[3]) return 0; 
	})
}

function sortByAllGames(isAscending)
{
	setArrows(4, isAscending);
	players.sort(function(a, b) 
	{ 
		if (a[6].length > b[6].length) return isAscending ? 1 : -1; 
		if (a[6].length < b[6].length) return isAscending ? -1 : 1;
		if (a[6] > b[6])  return isAscending ? 1 : -1; 
		if (a[6] < b[6])  return isAscending ? -1 : 1;
		if (a[6] = b[6])  return 0; 
	})
}

function sortByThisYearGames(isAscending)
{
	setArrows(5, isAscending);
	players.sort(function(a, b) 
	{ 
		if (a[7] - 0 > b[7] - 0) return isAscending ? 1 : -1; 
		if (a[7] - 0 < b[7] - 0) return isAscending ? -1 : 1; 
		if (a[7] - 0 == b[7] - 0) return 0; 
	})
}

function sortByDate(isAscending)
{
	setArrows(3, isAscending);
	players.sort(function(a, b) 
	{ 
		var re = /(\d+)\.(\d+)\.(\d{4})/;
		var c = new Date(a[8].replace(re, "$2/$1/$3"));
		var d = new Date(b[8].replace(re, "$2/$1/$3"));
		if (c > d) return isAscending ? 1 : -1; 
		if (c < d) return isAscending ? -1 : 1; 
		if (c == d) return 0; 
	})
}

function sortByRating(isAscending)
{
	setArrows(2, isAscending);
	players.sort(function(a, b) 
	{ 
		if (a[5] - 0 > b[5] - 0) return isAscending ? 1 : -1; 
		if (a[5] - 0 < b[5] - 0) return isAscending ? -1 : 1; 
		if (a[5] - 0 == b[5] - 0) 
		{ 
			if (a[6].length > b[6].length) return isAscending ? 1 : -1;
			if (a[6].length < b[6].length) return isAscending ? -1 : 1;
			if (a[6] > b[6])  return isAscending ? 1 : -1; 
			if (a[6] < b[6])  return isAscending ? -1 : 1;
			if (a[6] = b[6])  return 0; 
		}
	})
}
	
function initTable()
{	
	arrows = document.getElementById("tblPlayers").getElementsByTagName("b");
	style = document.createElement("style");
	style.setAttribute("type", "text/css");
	document.getElementsByTagName("head")[0].appendChild(style);
	style = document.styleSheets[document.styleSheets.length - 1];
	style = StyleSheetMakeCompatible(style);
	style.insertRule('#showActive,#showAll{cursor:pointer;cursor:hand;padding:.2em .5em;}', style.cssRules.length);
	style.insertRule('.pseudolink{cursor:pointer;cursor:hand;}', style.cssRules.length);
	style.insertRule('.pseudolink b{font-size:70%;font-size:expression("100%");}', style.cssRules.length);
	style.insertRule('.nonActive{display:none}', style.cssRules.length);
	
	var tables = document.getElementById("content").getElementsByTagName("table");
	for (var e = 1; e < 3; e++)
	{
		for (var i = 0, l = tables[e].rows.length; i < l; i++)
		{
			var player = {0: e == 1}
			for (var j = 0, k = 0, cl = tables[e].rows[i].cells.length; j < cl; j++)
			{
				if (j != 2) // английское написание
				{	
					k++;
					if (j == 1 && tables[e].rows[i].cells[j + 1].innerHTML != "") 
						player[k] = "<!-- " + tables[e].rows[i].cells[j].innerHTML + " --><a href='http://www.shogi.net/fesa/index.php?mid=5&player=" + tables[e].rows[i].cells[j + 1].innerHTML + "'>" + tables[e].rows[i].cells[j].innerHTML + "</a>"	
					else
						player[k] = tables[e].rows[i].cells[j].innerHTML;
				}
			}
			players.push(player);
		}
	}
	
	tables[1].parentNode.removeChild(tables[1]);
	tables[1].parentNode.removeChild(tables[1]);
	
	// сортируем по рейтингу
	sortByRating(false);
	
	table = tables[0];
	renderTable();
	showActive();
}

function renderTable()
{
	var tbody = document.createElement("tbody");
	for (var i = 0, l = players.length; i < l; i++)
	{
		var tr = document.createElement("tr");
		for (var p in players[i])
		{
			var td = document.createElement("td");
			if (p == 0)
			{
				td.innerHTML = i + 1;
				if (!players[i][p]) tr.className = "nonActive";
			}
			else
				td.innerHTML = players[i][p];
            tr.appendChild(td);
		}
		tbody.appendChild(tr);
	}
	table.appendChild(tbody);
}

function fillTable()
{
	var j = 0;
	for (var i = 0, l = players.length; i < l; i++)
	{
		for (var p in players[i])
		{
			var tr = table.rows[i + 2];
			var td = tr.cells[p];
			if (p == 0)
			{
				if (players[i][p]) j++;
				td.innerHTML = (document.getElementById("showActive").style.backgroundColor != "") ? j : i + 1;
				tr.className = !players[i][p] ? "nonActive" : "";
			}
			else
				td.innerHTML = players[i][p];
		}
	}
	
}

function callNeededHandlers()
{
	initTable();
	fixLayout();
}

function StyleSheetMakeCompatible(style)
{
	// Gecko does not access to cssRules while these rules are loading
	try
	{
		style.cssRules;
	}
	catch (e)
	{
		return style;
	}

	// Create CSSStyleSheet.cssRules
	if (typeof style.cssRules == 'undefined' && typeof style.rules != 'undefined')
		style.cssRules = style.rules;

	// Create CSSStyleSheet.insertRule and CSSStyleSheet.deleteRule
	if (typeof style.insertRule == 'undefined' && typeof style.addRule != 'undefined')
		style.insertRule = StyleSheetInsertRule;
	if (typeof style.deleteRule == 'undefined' && typeof style.removeRule != 'undefined')
		style.deleteRule = style.removeRule;

	if (typeof style.cssRules == 'undefined' || typeof style.insertRule == 'undefined' || typeof style.deleteRule == 'undefined')
		return null;
	else
		return style;
}

function StyleSheetInsertRule(rule, index)
{
	if (rule.match(/^((?:"(?:\\.|[^\"])+"|'(?:\\.|[^\'])+'|[^\{])+)\{(.*)\}\s*$/))
	{
		var selectors = RegExp.$1;
		var rule = RegExp.$2;
		var r = new RegExp("[^,]+", "g");
		var m = r.exec(selectors);
		
		while (m)
		{
			this.addRule(m[0], rule, index);
			m = r.exec(selectors);
		}
		return index;
	}
}
