/* Object representing each tile on the gameboard. */
function Hex(gameboard, target, x, y, value) {
    this.gameboard = gameboard;
    this.target = target;
    this.x = x;
    this.y = y;
    this.value = value;
    
    // Accounts for the staggering of the pieces.
    if (y % 2 == 0)
        this.target.setValue("canvas.Left",  x * 96);
    else
        this.target.setValue("canvas.Left",  x * 96 + 50);
        
    this.target.setValue("canvas.Top", y * 76);
    
    this.shapeContent = this.target.children.getItem(0).children.getItem(this.target.children.getItem(0).children.count - 1);
    this.valueDisplay = this.target.children.getItem(1);
    this.defaultFill = this.shapeContent.fill;
    
    setCallback(this.target, "mouseLeftButtonDown", delegate(this, this.handleMouseDown));
    setCallback(this.target, "mouseEnter", delegate(this, this.handleMouseEnter));
    setCallback(this.target, "mouseLeave", delegate(this, this.handleMouseLeave));
    
    ++this.gameboard.openSpaces;
}

Hex.prototype.mode = "Deselected";
Hex.prototype.occupier = null;

Hex.prototype.handleMouseDown = function() {
    this.gameboard.tryMoveTo(this);
}

Hex.prototype.handleMouseEnter = function() {
    this.gameboard.trySelectTo(this);
}

Hex.prototype.handleMouseLeave = function() {
    this.gameboard.clearSemiSelect();
}

Hex.prototype.setMode = function(mode) {
    this.mode = mode;
    
    if (mode == "Selected")
        this.shapeContent.opacity = 1;
    else if (mode == "Deselected") {
        this.shapeContent.opacity = 0;
        this.valueDisplay.opacity = 0;
    }
    else if (mode == "SemiSelected")
        this.shapeContent.opacity = 1;
}

Hex.prototype.showScore = function() {
    this.valueDisplay.opacity = 1;
}

Hex.prototype.setFill = function(fill) {
    var fill = this.gameboard.control.createFromXaml(fill);
    this.target.children.removeAt(0);
    this.target.children.add(fill);
    
    --this.gameboard.openSpaces;
}
