

/*****************************************************************************
 *
 *  Define the HumanPlayer Object and Functions
 *
 *****************************************************************************/
function HumanPlayer(aName, playerIndex, theSkillLevel, theGame, theBag)
{
    this.init(aName, playerIndex, theSkillLevel, theGame, theBag);
}

/**
 *  Setup inheritance
 */
HumanPlayer.prototype = new ComputerPlayer();
HumanPlayer.prototype.constructor = HumanPlayer;
HumanPlayer.superclass = ComputerPlayer.prototype;

HumanPlayer.prototype.init = function(aName, playerIndex, theSkillLevel, theGame, theBag)
{
	var board = theGame.getBoard();
	this.dragControls = new Array();
	this.anchor = getElementPosition('anchor');

	this.dragControls.push(new DragControl('control1', board, 420+this.anchor.left, 345+this.anchor.top));
	this.dragControls.push(new DragControl('control2', board, 455+this.anchor.left, 345+this.anchor.top));
	this.dragControls.push(new DragControl('control3', board, 490+this.anchor.left, 345+this.anchor.top));
	this.dragControls.push(new DragControl('control4', board, 525+this.anchor.left, 345+this.anchor.top));
	this.dragControls.push(new DragControl('control5', board, 560+this.anchor.left, 345+this.anchor.top));
	this.dragControls.push(new DragControl('control6', board, 595+this.anchor.left, 345+this.anchor.top));

    HumanPlayer.superclass.init.call(this, aName, playerIndex, theSkillLevel, theGame, theBag);

}


HumanPlayer.prototype.isComputerPlayer = function()
{
    return false;
}

HumanPlayer.prototype.setDHTMLCallbacks = function()
{
    // establish the drag and drop components of the game with walter's library
    for (var i=1; i<=6; i++)
    {
		ADD_DHTML('control' + i);
	}

	DragControl.setDHTMLCallbacks('control1');
	DragControl.setDHTMLCallbacks('control2');
	DragControl.setDHTMLCallbacks('control3');
	DragControl.setDHTMLCallbacks('control4');
	DragControl.setDHTMLCallbacks('control5');
	DragControl.setDHTMLCallbacks('control6');

	for (var i=0; i<this.dragControls.length; i++)
	{
		this.dragControls[i].moveToAssignedLocation();
	}

}



HumanPlayer.prototype.activate = function()
{
	DragControl.activate();
	HumanPlayer.superclass.activate.call(this);
}


HumanPlayer.prototype.deactivate = function()
{
	DragControl.deactivate();
	HumanPlayer.superclass.deactivate.call(this);
}

HumanPlayer.prototype.getSelectedDragControl = function()
{
	for (var i=0; i<this.dragControls.length; i++)
	{
		if (this.dragControls[i].isOnSpace())
		{
			return this.dragControls[i];
		}
	}
	return null;
}


HumanPlayer.prototype.swapTiles = function(callback)
{
	if (this.scoreBoard.canSwapTiles(this.hand))
	{
		this.hand = this.bag.swap(this.hand, 6);
		for (var i=0; i<this.hand.length; i++)
		{
			var dragControl = this.dragControls[i];
			if (dragControl.getTile() != null)
			{
				dragControl.clearTile();
			}
			this.associateTileWithDragControl(this.hand[i]);
		}
	}
	else
	{
		this.refillHand();
	}
	callback();
}

HumanPlayer.prototype.canEndTurn = function()
{
	var dragControl = this.getSelectedDragControl();
	return (dragControl != null);
}


HumanPlayer.prototype.getSelectedTile = function()
{
	var dragControl = this.getSelectedDragControl();
	if (dragControl != null)
	{
		return dragControl.getTile();
	}
	return null;
}

HumanPlayer.prototype.getSelectedOrientation = function()
{
	var dragControl = this.getSelectedDragControl();
	if (dragControl != null)
	{
		return dragControl.getTile().getOrientation();
	}
	return null;
}


HumanPlayer.prototype.getSelectedSpace = function()
{
	var dragControl = this.getSelectedDragControl();
	if (dragControl != null)
	{
		return dragControl.getSelectedSpace();
	}
	return null;
}

HumanPlayer.prototype.takeTurn = function()
{
}

HumanPlayer.prototype.refillHand = function()
{
	while ((this.hand.length < 6) && this.bag.hasMoreTiles())
	{
		var newTile = this.bag.draw();
		this.hand.push(newTile);
		this.associateTileWithDragControl(newTile);
	}
}

HumanPlayer.prototype.associateTileWithDragControl = function(newTile)
{
	for (var i=0; i<this.dragControls.length; i++)
	{
		var dragControl = this.dragControls[i];
		if ((dragControl.getTile() == null) || dragControl.getTile().isPlaced())
		{
			dragControl.setTile(newTile);
			return;
		}
	}
}


HumanPlayer.prototype.scoreTilePlacement = function(tileScore)
{
	var ingeniousCnt = HumanPlayer.superclass.scoreTilePlacement.call(this, tileScore);

	var dragControl = this.getSelectedDragControl();
	if (dragControl != null)
	{
		dragControl.clearTile();
	}
	return ingeniousCnt;
}
