// List teams in place order
var team = ['Brighouse', 'Halifax', 'Huddersfield', 'Belgrave', 'Hebden Bridge A', 'Hebden Bridge B'];

// List players in board order. Teams in place order
var player = [['R Broadbent', 'B Bendall', 'P Whitehouse', 'R Grandage', 'P Moss'],
	['R Cully', 'G Cash', 'DP Ellis', 'C Velosa', 'A Dawson'],
	['L Keely', 'D Keddie', 'M Parsons', 'A Aguirre', 'R Sutcliffe'],
	['D Patrick', 'D Tooley', 'L Johnson', 'A Gonzalez', 'D Milton'],
	['D Wedge', 'A Wright', 'N Sykes', 'M Wedge-Roberts', 'N Bamford'],
	['R Bardelang', 'P Rawlings', 'F Plumpton', 'B Fernley', 'J-P Ellis']];

// List in oder played. Opponents numbered from zero
var opponents = [[1,2,3,4,5], [0,4,5,3,2], [3,0,4,5,1], [2,5,0,1,4], [5,1,2,0,3], [4,3,2,1,0]];

// Player scores in each round. Players in board order, teams in place order
var points = [[[1,0.5,1,1,1], [1,1,0,1,1], [1,0,1,0,1], [0,0,1,0,1], [0.5,1,0,1,0.5]],
	[[0,0,1,1,0], [0,0,0,0,0.5], [0,0,1,0,0], [1,0,1,0,0.5], [0.5,0,1,1,0.5]],
	[[1,0.5,0,1,1], [0,0,1,1,0.5], [1,1,1,1,1], [1,1,1,1,0.5], [1,0,1,0,0.5]],
	[[0,1,0,0,0], [1,1,1,1,0], [0,1,0,1,0], [0,1,0,1,1], [0,1,1,0,0]],
	[[1,1,1,0,1], [0.5,1,0,0,1], [1,1,0,1,1], [1,1,0,1,0], [1,1,0,0,1]],
	[[0,0,0,0,0], [0.5,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,1,0.5]]];
	
var row = new Array();
var cell = new Array();
var teamResults;
var teamScore;
var indivScores;


function initialise() {
// Locate results table and window for score details
	teamResults = document.getElementById("teamResults");
	teamScore = document.getElementById("teamScore");
	indivScores = document.getElementById("indivScores");
// and the team rows
	row = teamResults.rows;
	rows = row.length;
// and the row for each team
	for (i=2;i<rows;i++) {
		cell = row[i].cells;
		cells = cell.length;
		for (j=1;j<cells;j++) {
			cell[j].onmouseover = function() {resultDetail(this.parentNode.rowIndex,this.cellIndex);}
			cell[j].onmouseout = function() {restore(this.parentNode.rowIndex,this.cellIndex);}
		}
	}
}

function resultDetail(row,column) {
	teamResults.rows[row].cells[column].style.backgroundColor='#F8F8E0';
	i = row-2; j = column-1;
	home = team[i]; away = team[opponents[i][j]]
	h_score = 0; a_score = 0;
	indiv_list = '(';
	for (k=0;k<5;k++) {
		h_score += points[i][k][j]; a_score += points[opponents[i][j]][k][j];
		indiv_list += player[i][k]+' '+tidy(points[i][k][j])+', ';
	}
	score = tidy(h_score)+'-'+tidy(a_score);
	indiv_list = indiv_list.slice(0,-2)+')';
	teamScore.firstChild.nodeValue = home+' '+score+' '+away;
	indivScores.firstChild.nodeValue = indiv_list;
}

function restore(row,column) {
	teamResults.rows[row].cells[column].style.backgroundColor='#FFC891';
	teamScore.firstChild.nodeValue = 'Point at a score for match details';
	indivScores.firstChild.nodeValue = '';
}

function tidy(value) {
	if (value==0) {return '0'}
	if (value==0.5) {return '½'}
	if (value==1) {return '1'}
	whole = Math.floor(value);
	if (whole==value) {return String(whole)}
	else {return String(whole)+'½'}
}
