/*
 * Javascript MineSweeper v0.2
 * http://labs.ugolandini.com/minesweeper
 *
 * Copyright (c) 2010 Ugo Landini
 * Dual licensed under the MIT and GPL licenses.
 * http://labs.ugolandini.com/license/GPL-LICENSE.txt
 * http://labs.ugolandini.com/license/MIT-LICENSE.txt
 *
 * Date:            2010-01-10
 * Last updated:    2010-01-10 
 */

function MineCell(field, x, y) {

    this.mineField = field;
    this.x = x;
    this.y = y;
    this.clear();

}

MineCell.prototype.clear = function() {

    this.bomb = 0;
    this.clearAttributes();

};

MineCell.prototype.clearAttributes = function() {

    this.shown = false;
    this.flag = false;

};

MineCell.prototype.nearColors = {
    1: '#414fbd',
    2: '#1f6b00',
    3: '#b20908',
    4: '#010084',
    5: '#7d0000',
    6: '#2c8890',
    7: '#ab0403',
    8: '#af0a0c'
};


MineCell.prototype.leftClicked = function() {


    var refCell = $(this).data("ref");

    if (!refCell.mineField.startedTimer) {
        refCell.mineField.startedTimer = true;
        MineTimer.startTimer();
    }
    if (refCell.mineField.firstClick) {
        refCell.mineField.initWithBombs(refCell.x, refCell.y);
        refCell.mineField.firstClick = false;
    }

    if ($(this).is(".flagged") || $(this).is(".shown")) {
        return false;
    }

    (refCell.mineField.field[refCell.x][refCell.y]).show();
    refCell.mineField.adjournGameNumbers();

};


MineCell.prototype.rightClicked = function() {

    var refCell = $(this).data("ref");

    if ($(this).is(".shown")) {
        return false;
    }

    (refCell.mineField.field[refCell.x][refCell.y]).switchCandidate();
    refCell.mineField.adjournGameNumbers();

    return false;
};

MineCell.prototype.td = function() {

    var td = $("<td>").attr({
        id: this.x + "-" + this.y
    });

    if (this.shown) {
        td.addClass("shown")
    }

    if (this.flag) {
        td.addClass("flagged")
    }

    td.data("ref", this);
    td.click(this.leftClicked);
    td.rightClick(this.rightClicked);
    td.html("&nbsp");

    return td;

};


MineCell.prototype.show = function() {

    if ((!this.shown) && (!this.flag) && (this.x > 0) && (this.y > 0) && (this.x < this.mineField.x - 1) && (this.y < this.mineField.y - 1)) {
        var td = $("#" + this.x + "-" + this.y);
        if (this.bomb == 1) {
            td.addClass("lastClicked");
            this.mineField.showBombs();
            MineTimer.stopTimer();
            Utilities.gameLost();
            MineTimer.resetTimer();
        } else {
            this.shown = true;
            td.addClass("shown");
            var near = this.mineField.near(this.x, this.y);
            if (near > 0) {
                td.text(near).css({
                    color: this.nearColors[near]
                });
            } else {
                for (var i = -1; i < 2; i++) {
                    for (var j = -1; j < 2; j++) {
                        if ((i != 0) || (j != 0))
                            (this.mineField.field[this.x + i][this.y + j]).show();
                    }
                }
            }
        }
    }

};


MineCell.prototype.switchCandidate = function() {

    this.flag = !this.flag;

    var td = $("#" + this.x + "-" + this.y);
    if (this.flag) {
        td.addClass('flagged');
    } else {
        td.removeClass('flagged');
    }

};


MineCell.prototype.appendToRow = function(trow) {

    this.td().appendTo(trow);

};