

/*****************************************************************************
 *
 *  Define the ScoreBoard Object and Functions
 *
 *****************************************************************************/
function ScoreBoard(playerIndex)
{
    this.init(playerIndex);
}

ScoreBoard.prototype.init = function(playerIndex)
{
	this.anchorLeft = 440;
	this.anchorTop = 20;
	this.left = 4;
	this.top = 9;
	this.xfactor = 9.1;
	this.anchor = getElementPosition('anchor');

	this.playerIndex = playerIndex;
	this.anchorLeft += this.anchor.left;
	this.anchorTop += ((this.playerIndex-1) * 80) + this.anchor.top;

	if (this.playerIndex == 0)
	{
		this.anchorTop = 290 + this.anchor.top;
	}

	var symbols = Symbol.getAsArray();
	for (var i=0; i<symbols.length; i++)
	{
		this[symbols[i].getType()] = 0;
	}
	this.layout();
	this.render();

	this.scores = new Array();
    while (symbols.length > 0)
    {
        var rand = Math.floor(Math.random()*1573) % symbols.length;
        if (symbols.length == 1)
        {
            rand = 0;
        }
        this.scores.push((symbols.splice(rand, 1))[0]);
    }

	this.pointSpread = this.getPointSpread();
}

ScoreBoard.prototype.getX = function()
{
	return this.anchorLeft;
}

ScoreBoard.prototype.getY = function()
{
	return this.anchorTop;
}

ScoreBoard.prototype.getScore = function(symbol)
{
	return this[symbol.getType()];
}

ScoreBoard.prototype.getPlayerIndex = function()
{
	return this.playerIndex;
}

ScoreBoard.prototype.isInstantWinner = function()
{
	var maxCnt = 0;
	for (var i=0; i<this.scores.length; i++)
	{
		var symbol = this.scores[i];
		if (this[symbol.getType()] == 18)
		{
			maxCnt++;
		}
	}

	return (maxCnt == this.scores.length);
}

ScoreBoard.prototype.getIngeniousCubes = function()
{
	var cubes = new Array();
	for (var i=0; i<this.scores.length; i++)
	{
		var symbol = this.scores[i];
		if (this[symbol.getType()] == 18)
		{
			cubes.push(symbol);
		}
	}

	return cubes;
}


ScoreBoard.prototype.scoreTilePlacement = function(tileScore)
{
	var ingeniousCnt = 0;

	var pivotType = tileScore.tile.pivot.getType();
	var floatType = tileScore.tile.floater.getType();

	var newValue = this[pivotType] + tileScore.pivotScore;
	var origScore = this[pivotType];
	this[pivotType] = (newValue > 18) ? 18 : newValue;
	if ((origScore < 18) && (this[pivotType] >= 18))
	{
		ingeniousCnt++;
	}

	newValue = this[floatType] + tileScore.floatScore;
	origScore = this[floatType];
	this[floatType] = (newValue > 18) ? 18 : newValue;
	if ((origScore < 18) && (this[floatType] >= 18))
	{
		ingeniousCnt++;
	}

	this.scores.sort(this.getSortFunction());
	this.pointSpread = this.getPointSpread();

	this.render();

	return ingeniousCnt;
}

ScoreBoard.prototype.render = function()
{
	var symbols = Symbol.getAsArray();

	for (var i=0; i<symbols.length; i++)
	{
		var score = this[symbols[i].getType()];
		var cube = document.getElementById(symbols[i].getType() + this.playerIndex);
		Utility.moveTo
		(
			cube,
			Math.round((score * this.xfactor) + this.left),
			(i * 11) + this.top
		);
	}
}

ScoreBoard.prototype.layout = function()
{
	var div = document.getElementById('scoreboard' + (this.playerIndex + 1));
	Utility.moveTo(div, this.anchorLeft, this.anchorTop);
	var symbols = Symbol.getAsArray();
	for (var i=0; i<symbols.length; i++)
	{
		var cubeId = symbols[i].getType() + this.playerIndex;
		var cubeDiv = document.createElement('div');
		cubeDiv.className = 'positionable';
		cubeDiv.id = cubeId;
		cubeDiv.innerHTML = '<img width="8" height="8" src="' + symbols[i].getCubeImage() + '"/>';
		div.appendChild(cubeDiv);
	}
	showDiv(div);
}

ScoreBoard.prototype.getSymbolDesireWeight = function(s)
{
	var weight = 0;

	var score = this[s.getType()];
	if (score >= 18)
	{
		weight = -8;
	}

	for (var i=0; i<this.scores.length; i++)
	{
		if (this.scores[i].getType() == s.getType())
		{
			weight += ((6-i) % 3);
			if (this.pointSpread.lowestSymbol.getType() == s.getType())
			{
				weight += 6;
			}
			if (this.pointSpread.highestSymbol.getType() == s.getType())
			{
				weight -= 3;
			}
			break;
		}
	}

	return weight;
}


ScoreBoard.prototype.getSortFunction = function()
{
	var _this = this;

	var f = function(s1, s2)
	{
		if (s1 == null)
		{
			return 1;
		}

		if (s2 == null)
		{
			return -1;
		}

		if (_this[s1.getType()] > _this[s2.getType()])
		{
			return 1;
		}

		if (_this[s1.getType()] < _this[s2.getType()])
		{
			return -1;
		}
    	return 0;
	}
	return f;
}

ScoreBoard.prototype.getPointSpread = function()
{
	var lowestSymbol = null;
	var lowest = 19;
	var highest = -1;
	var symbols = this.scores;
	for (var i=0; i<symbols.length; i++)
	{
		var score = this[symbols[i].getType()];
		if (score < lowest)
		{
			lowest = score;
			lowestSymbol = symbols[i];
		}
		if (score > highest)
		{
			highestSymbol = symbols[i];
			highest = score;
		}
	}
	return { spread: highest - lowest, lowestSymbol: lowestSymbol, highestSymbol: highestSymbol };
}

ScoreBoard.prototype.emphasize = function()
{
}

ScoreBoard.prototype.deemphasize = function()
{
}


ScoreBoard.prototype.canSwapTiles = function(hand, ignoreTile)
{
	var symbols = new Array();
	symbols.push(this.scores[0]);
	var lowScore = this[this.scores[0].getType()];
	for (var i=1; i<this.scores.length; i++)
	{
		var symbol = this.scores[i];
		var score = this[symbol.getType()];
		if (score > lowScore)
		{
			break;
		}
		else
		{
			symbols.push(symbol);
		}
	}

	for (var i=0; i<symbols.length; i++)
	{
		var symbol = symbols[i];
		for (var j=0; j<hand.length; j++)
		{
			var tile = hand[j];
			if (tile != ignoreTile)
			{
				if (tile.pivot.getType() == symbol.getType())
				{
					return false;
				}
				if (tile.floater.getType() == symbol.getType())
				{
					return false;
				}
			}
		}
	}
	return true;
}

ScoreBoard.prototype.getIngeniousDisplayCubesHtml = function()
{
	var cubes = this.getIngeniousCubes();

	var html = '<center>';

	for (var i=0; i<cubes.length; i++)
	{
		for (var j=0; j<3; j++)
		{
			html += '<img src="' + cubes[i].getCubeImage() + '"/> ';
		}
	}

	html += '</center>';

	return html;
}


ScoreBoard.sortBoards = function(r1, r2)
{
    if (r1 == null)
    {
        return 1;
    }

    if (r2 == null)
    {
        return -1;
    }

    for (var i=0; i<r1.scores.length; i++)
    {
		if (r1[r1.scores[i].getType()] < r2[r2.scores[i].getType()])
		{
			return 1;
		}
		if (r1[r1.scores[i].getType()] > r2[r2.scores[i].getType()])
		{
			return -1;
		}
	}

    return 0;
}