/* A module for a simple model */
//Create my namespace
var net;
if(!net) {
	 net = {};
} else if (typeof net != "object") {
  throw new Error("net already exists and is not an object");
}

if(!net.gonote) {
		net.gonote = {};
} else if(typeof net.gonote != "object") {
  throw new Error("net.gonote already exists and is not an object");
}

if(net.gonote.model) {
	throw new Error("net.gonote.model already exists");
}

net.gonote.model = {};


net.gonote.model.BoardController = function(/*nodeName*/boardNode, /*int*/boardMod, /*String*/unPlayButton, /*String*/suppressNumberButton, /*String*/liberateNumberButton, /*String*/highlightLastMoveButton) {

	this.seqNum = 1;
	this.moveStack = [];
	this.color = net.gonote.move.Stone.BLACK;
	this.displayBoardNode = document.getElementById(boardNode);
	this.unPlayButtonNode = document.getElementById(unPlayButton);
	this.suppressNumberButtonNode = document.getElementById(suppressNumberButton);
	this.liberateNumberButtonNode = document.getElementById(liberateNumberButton);
	this.highlightLastMoveButtonNode = document.getElementById(hightlightLastMoveButton);

	this.displayBoard = new net.gonote.displayboard.GoDisplayBoard(node, boardMod);
	this.gameBoard = new net.gonote.board.GameBoard(boardMod);

	//register the event handler as a method of this object
	//and catch it coming down.
//	net.gonote.util.registerObjectEventHandler(myNode, "click", this, true);
	dojo.event.connect(this.displayBoardNode, "onClick", this, "boardClickHandler");
	dojo.event.connect(this.unPlayButtonNode, "onClick", this, "unPlayButtonHandler");
	dojo.event.connect(this.suppressNumberButtonNode, "onClick", this, "suppressNumberButtonHandler");
	dojo.event.connect(this.liberateNumberButtonNode, "onClick", this, "liberateNumberButtonHandler");
	dojo.event.connect(this.highlightLastMoveButtonNode, "onClick", this, "highlightLastMoveButtonHandler");
};		

net.gonote.model.BoardController.prototype.boardClickHandler = function(evt) {

	var moveX = this.displayBoard.getMoveX(evt);
	var moveY = this.displayBoard.getMoveY(evt);

	var name = this.seqNum.toString();
	net.gonote.util.log("Name: " + name);

	var move = new net.gonote.move.Move(moveXIndex, moveYIndex, name, name, this.color);

	//Remember that gameBoard.play() sets the captures on the move
	if(this.gameBoard.play(move) == net.gonote.board.GameBoard.PLAY_OK) {
		this.moveStack.push(move);
		this.displayBoard.addMove(move);
		this.incrementSeqNum();
		this.toggleColor();
		this.moveStack.push(move);
	} else {
		//Error messages should be displayed
	}

	//And we are finished with the event
	evt.stopPropagation();

};

net.gonote.model.BoardController.prototype.incrementSeqNum =  function() { this.seqNum += 1; };
net.gonote.model.BoardController.prototype.decrementSeqNum = function() { this.seqNum -= 1; };
net.gonote.model.BoardController.prototype.toggleColor: function() {
				if(this.color == net.gonote.move.Stone.BLACK) {
					this.color = net.gonote.move.Stone.WHITE;
				} else {
					this.color = net.gonote.move.Stone.BLACK;
				}
};
net.gonote.model.BoardController.prototype.unPlayButtonHandler = function(evt) { 

	this.board.decrementSeqNum();
	this.board.toggleColor();

	var move = this.moveStack.pop();
	this.gameBoard.erase(move);
	this.displayBoard.deleteMove(move);

	//And we are finished with the event
	evt.stopPropagation();


};
net.gonote.model.BoardController.prototype.suppressNumberButtonHandler = function(evt) { 
	this.displayBoard.suppressNumbers();
	//And we are finished with the event
	evt.stopPropagation();
};
net.gonote.model.BoardController.prototype.liberateNumberButtonHandler = function(evt) { 
	this.displayBoard.liberateNumbers();
	//And we are finished with the event
	evt.stopPropagation();
};
net.gonote.model.BoardController.prototype.highlightLastMoveButtonHandler = function(evt) { 
	var move = this.moveStack.pop();
	this.moveStack.push(move);
	this.displayBoard.highlightMove(move);
	//And we are finished with the event
	evt.stopPropagation();
};


