function PrizePool( gameContext )
{
	this.objTable = document.getElementById( 'prizePoolTable' );	
	this.gameContext = gameContext;
	this.cachedPlacePaidAmounts = null;
	
	// DOM objects - Table Header Row
	this.container = document.getElementById( 'prizePool' );
	this.objTable.appendChild( createHeaderRow( [ 'Position', 'Prize', 'Player' ] ) );
	this.objTotalPrizeAmountText = document.createTextNode( '' );	
	var totalPrizePoolAmountPlaceholder = document.getElementById( 'totalPrizePoolAmount' );
	totalPrizePoolAmountPlaceholder.appendChild( this.objTotalPrizeAmountText );

	this.getWinnerFavour = function()
	{
		var objWinnerFavour = document.getElementById( 'winnerFavour' );
		var winnerFavour = parseFloat( objWinnerFavour.value );
		objWinnerFavour.className = isNaN(winnerFavour) ? 'error' : '';
		return winnerFavour;
	}

	this.getPrizeDropOff = function()
	{
		var objPrizeDropOff = document.getElementById( 'prizeDropOff' );
		var prizeDropOff = parseFloat( objPrizeDropOff.value );
		objPrizeDropOff.className = isNaN( prizeDropOff ) ? 'error' : '';
		return prizeDropOff;
	}

	this.getPlacePaidAmounts = function(totalPrizePool)
	{		
		var buyInsRemaining = totalPrizePool / gameContext.buyIn;
		var placePaidAmounts = [];
		var prizeDropOffFactor = this.getPrizeDropOff();
		var winnerFavour = this.getWinnerFavour();
		if ( isNaN( prizeDropOffFactor ) || isNaN( winnerFavour ) )
		{
			return [];
		}
		var placeIndex = 0;
		do 
		{
			var placePaidBuyInCount = Math.round( buyInsRemaining * prizeDropOffFactor + winnerFavour );
			if ( placePaidBuyInCount == 0 )
			{
				placePaidBuyInCount = 1;
			}
			placePaidAmounts[placeIndex] = placePaidBuyInCount * gameContext.buyIn;
			buyInsRemaining -= placePaidBuyInCount;
			placeIndex++;
		} while ( buyInsRemaining > 0 && placeIndex < gameContext.players.length )
		placePaidAmounts[0] += buyInsRemaining * gameContext.buyIn;
		if ( placePaidAmounts[0] == 0 )
		{
			// Empty the array
			placePaidAmounts = [];
		}						
		this.cachedPlacePaidAmounts = placePaidAmounts;
		return placePaidAmounts;
	};
		
	this.update = function()
	{
		var totalPrizePool = this.gameContext.getTotalBuyIn();
		var placePaidAmounts = this.getPlacePaidAmounts( totalPrizePool );		
		var topPlayers = this.gameContext.getTopPlayers();
		// Update or delete existing table rows
		var positionIndex = 0;
		var childRow = this.objTable.firstChild.nextSibling;
		var rowsToDelete = [];
		while ( childRow != null )
		{
			if ( positionIndex > placePaidAmounts.length - 1 )
			{
				rowsToDelete.push( childRow );
			}
			else
			{
				var objPositionText = childRow.firstChild.firstChild;
				var objPrizeText = childRow.firstChild.nextSibling.firstChild;
				var objPlayerText = childRow.firstChild.nextSibling.nextSibling.firstChild;
				objPositionText.nodeValue = getPositionString( positionIndex + 1 );
				objPrizeText.nodeValue = this.gameContext.currency + placePaidAmounts[positionIndex];
				var player = topPlayers[positionIndex];
				objPlayerText.nodeValue = player ? player.name : '-';
			}
			childRow = childRow.nextSibling;
			positionIndex++;
		}		
		for ( var i = 0; i < rowsToDelete.length; i++ )
		{
			this.objTable.removeChild( rowsToDelete[i] );
		}		
		// Add table rows as required
		while ( positionIndex < placePaidAmounts.length )
		{
			var objNewPrizeRow = document.createElement( 'tr' );
			var objPositionCell = document.createElement( 'td' );
			var objPrizeCell = document.createElement( 'td' );
			var objPlayerCell = document.createElement( 'td' );
			var objPositionText = document.createTextNode( getPositionString( positionIndex + 1 ) );
			var objPrizeText = document.createTextNode( this.gameContext.currency + placePaidAmounts[ positionIndex ] );
			var player = topPlayers[positionIndex];
			var playerName = player ? player.name : '-';
			var objPlayerText = document.createTextNode( playerName );
			objPositionCell.appendChild( objPositionText );
			objPrizeCell.appendChild( objPrizeText );
			objPlayerCell.appendChild( objPlayerText );
			objNewPrizeRow.appendChild( objPositionCell );
			objNewPrizeRow.appendChild( objPrizeCell );
			objNewPrizeRow.appendChild( objPlayerCell );
			this.objTable.appendChild( objNewPrizeRow );
			positionIndex++;
		}		
		if ( totalPrizePool > 0 )
		{
			this.objTotalPrizeAmountText.nodeValue = this.gameContext.currency + totalPrizePool;
			this.container.style.display = 'block';
		}
		else
		{
			this.objTotalPrizeAmountText.nodeValue = '';
			this.container.style.display = 'none';
		}
		this.gameContext.onPrizePoolChange();
	};
	
	this.toXml = function( documentContext )
	{
		if ( !documentContext )
		{
			alert( "No documentContext passed to PrizePool.toXML()" );
			return null;
		}
		var prizePoolElement = documentContext.createElement( 'prizepool' );
		var prizeDropOffFactor = this.getPrizeDropOff();
		var winnerFavourAdjustment = this.getWinnerFavour();
		if ( !isNaN( prizeDropOffFactor ) )
		{
			prizePoolElement.setAttribute( 'prizedropoff', prizeDropOffFactor );
		}
		if ( !isNaN( winnerFavourAdjustment ) )
		{
			prizePoolElement.setAttribute( 'winnerfavour', winnerFavourAdjustment );
		}
		return prizePoolElement;
	};
	
	this.initFromXml = function( xml )
	{
		var objWinnerFavour = document.getElementById( 'winnerFavour' );
		var objPrizeDropOff = document.getElementById( 'prizeDropOff' );
		try
		{
			objWinnerFavour.value = parseFloat( xml.getAttribute( 'winnerfavour' ) );
			objPrizeDropOff.value = parseFloat( xml.getAttribute( 'prizedropoff' ) );
		}
		catch(err) 
		{
		}
	};
	
	this.toString = function()
	{
		return "PrizePool";
	};
}
