var TURN_YELLOW = 1;
var TURN_GREEN = 0;

var DIRECTION_YELLOW = -1;
var DIRECTION_GREEN = 1;
var DIRECTION_UP = -1;
var DIRECTION_DOWN = 1;
var DIRECTION_RIGHT = 1;
var DIRECTION_LEFT = -1;

function Board(){
	var that = this;
	
	this.reverse = false;
	
	this.grid = null;
	
	this.selected = null;
	
	this.turn = TURN_YELLOW;
	this.direction = DIRECTION_YELLOW;
	
	this.gameover = false;
	this.movable = true;
	this.jumpable = false;
	
	reset (-1, 3,-1, 3,-1, 3,-1, 3,
		    3,-1, 3,-1, 3,-1, 3,-1,
		   -1, 3,-1, 3,-1, 3,-1, 3,
		    0,-1, 0,-1, 0,-1, 0,-1,
		   -1, 0,-1, 0,-1, 0,-1, 0,
		    1,-1, 1,-1, 1,-1, 1,-1,
		   -1, 1,-1, 1,-1, 1,-1, 1,
		    1,-1, 1,-1, 1,-1, 1,-1);
	
	
	scan();
	//private functions

	function reset(){
		that.grid = new Array(8);
		for( var x=0; x< 8; x++ )
				{
							that.grid[x] = new Array(8);
												
							for( var y=0; y < 8; y++ )
							{
									that.grid[x][y] = new Cell(arguments[8*y+x]);
									that.getCell(x,y).setX(x);
									that.getCell(x,y).setY(y);
							}
				}
	}
	
	function scan(){
		
		that.movable = false;
		that.jumpable = false;
		that.selected = null;
		for( var x=0; x<8; x++){
			for( var y=0; y<8; y++){
				if(isMovable(x,y)&&(!that.jumpable)){
					that.movable = true;
					that.getCell(x,y).setMovable(true);
				}
				else{
					that.getCell(x,y).setMovable(false);
				}
				
				if(isJumpable(x,y)){
					that.movable = false;
					that.jumpable = true;
					that.getCell(x,y).setJumpable(true);
					that.getCell(x,y).setMovable(false);
				}
				else{
					that.getCell(x,y).setJumpable(false);
				}
			}
		}
		if(!(that.movable||that.jumpable)){
			that.gameover = true;
		}
	}
	
	function isFriend(x,y){
		if((that.getCell(x,y).yellow)&&(that.turn == TURN_YELLOW)||(that.getCell(x,y).green)&&(that.turn == TURN_GREEN)){
			return true;
		}
		else{
			return false;
		}
	}
	
	function isEnemy(x,y){
		if((that.getCell(x,y).green)&&(that.turn == TURN_YELLOW)||(that.getCell(x,y).yellow)&&(that.turn == TURN_GREEN)){
			return true;
		}
		else{
			return false;
		}
	}
	
	function isMovable(x,y){
		if(isMoveAllowed(x,y,DIRECTION_LEFT,DIRECTION_UP)||isMoveAllowed(x,y,DIRECTION_RIGHT,DIRECTION_UP)||isMoveAllowed(x,y,DIRECTION_LEFT,DIRECTION_DOWN)||isMoveAllowed(x,y,DIRECTION_RIGHT,DIRECTION_DOWN)){
			return true;
		}
		return false;
	}
	
	function isJumpable(x,y){
		if(isJumpAllowed(x,y,DIRECTION_LEFT,DIRECTION_UP)||isJumpAllowed(x,y,DIRECTION_RIGHT,DIRECTION_UP)||isJumpAllowed(x,y,DIRECTION_LEFT,DIRECTION_DOWN)||isJumpAllowed(x,y,DIRECTION_RIGHT,DIRECTION_DOWN)){
			return true;
		}
		return false;
	}
	
	function isMoveAllowed(x,y,direction_x,direction_y){
		
		if((!that.getCell(x,y).king)&&(direction_y!=that.direction)){
			return false;
		}
		
		var to_x = x+direction_x;
		var to_y = y+direction_y;
		
		if(outOfEdge(to_x,to_y)){
			return false;
		}
		
		if((isFriend(x,y))&&(!that.jumpable)){
			if(that.getCell(to_x,to_y).empty){
				return true;
			}
		}
		return false;
	}
	
	function isJumpAllowed(x,y,direction_x,direction_y){
		if((!that.getCell(x,y).king)&&(direction_y!=that.direction)){
			return false;
		}
		var to_x = x+2*direction_x;
		var to_y = y+2*direction_y;
		var through_x = x+direction_x;
		var through_y = y+direction_y;
		
		if(outOfEdge(to_x,to_y)||outOfEdge(through_x,through_y)){
			return false;
		}
		
		if(isFriend(x,y)){
			if(isEnemy(through_x,through_y)&&that.getCell(to_x,to_y).empty){
				return true;
			}
		}
		return false;
	}
	
	function outOfEdge(x,y){
		if((x<0)||(x>7)||(y<0)||(y>7)){
			return true;
		}
		else{
			return false;
		}
	}
	
	this.switchTurn = function(){
		if(that.turn == TURN_YELLOW){
			that.turn = TURN_GREEN;
			that.direction = DIRECTION_GREEN;
			if(!that.reverse){
			$("#container").removeClass("container_bottom");
			$("#container").addClass("container_top");
			}
			else{
			$("#container").removeClass("container_top");
			$("#container").addClass("container_bottom");
			}

		}
		else{
			that.turn = TURN_YELLOW;
			that.direction = DIRECTION_YELLOW;
			if(!that.reverse){
			$("#container").removeClass("container_top");
			$("#container").addClass("container_bottom");
			}
			else{
			$("#container").removeClass("container_bottom");
			$("#container").addClass("container_top");
			}
		}
		scan();
	}
	
	this.prepareMultiJump = function(){
		scan();
	}
	
	
	this.move = function(from_x,from_y,to_x,to_y){
		var move = new Array();
		var cell = that.selected;
		
		if(cell==null) {
			return false;
		}
		
		if(from_x!=cell.x||from_y!=cell.y){
			return false;
		}
		
		var direction_x = (to_x-from_x)/Math.abs(to_x-from_x);
		var direction_y = (to_y-from_y)/Math.abs(to_y-from_y);
		
		if(cell.movable){
			to_x = from_x+direction_x;
			to_y = from_y+direction_y;
		}
		
		if(cell.jumpable){
			to_x = from_x+2*direction_x;
			to_y = from_y+2*direction_y;
		}
		
		if(!(isMoveAllowed(from_x,from_y,direction_x,direction_y)||isJumpAllowed(from_x,from_y,direction_x,direction_y))||!direction_x||!direction_y){
			return false;
		}
		
		if(cell.selected){
			if(cell.movable){
				that.getCell(to_x,to_y).copyFrom(cell);
				that.getCell(to_x,to_y).effectAppear();
				cell.clear();
				cell.updateView();
			}
			if(cell.jumpable){
				that.getCell(to_x,to_y).copyFrom(cell);
				that.getCell(to_x,to_y).effectAppear();
				that.getCell(to_x-direction_x,to_y-direction_y).effectBoom();
				that.getCell(to_x-direction_x,to_y-direction_y).clear();
				cell.clear();
				cell.updateView();
			}
		}
		move.push(from_x);
		move.push(from_y);
		move.push(to_x);
		move.push(to_y);
		return move;
	}
	
	this.checkMultiJump = function(x,y){
		if(!isJumpable(x,y)||!that.jumpable){
			return false;
		}
		else{
			for(var i=0; i<8; i++){
				for(var j=0; j<8; j++){
					that.getCell(i,j).setJumpable(false);
				}
			}
			that.getCell(x,y).setJumpable(true);
			return true;
		}
	}
}


Board.prototype.getCell = function(x,y){
	return this.grid[x][y];
}


Board.prototype.select = function(x,y){
	if((this.movable&&this.getCell(x,y).movable)||this.getCell(x,y).jumpable){
		if(this.selected!==null){
			this.selected.setSelected(false);
			this.selected.updateView();
		}
		this.selected = this.getCell(x,y);
		this.selected.setSelected(true);
		this.selected.updateView();
		return true;
	}
	else{
		return false;
	}
}

Board.prototype.renderView = function(){
	var gridTable = document.getElementById('grid');
	while (gridTable.hasChildNodes()){
		gridTable.removeChild(gridTable.firstChild);
	}
	for( var y=0; y<8; y++){
		var gridRow = document.createElement('tr');
		for( var x=0; x<8; x++){
			var gridColumn = document.createElement('td');
			gridColumn.appendChild(this.getCell(x,y).renderView());
			gridRow.appendChild(gridColumn);
		}
		gridTable.appendChild(gridRow);
	}
}

Board.prototype.renderViewReverse = function(){
	this.reverse = true;
	var gridTable = document.getElementById('grid');
	while (gridTable.hasChildNodes()){
		gridTable.removeChild(gridTable.firstChild);
	}
	for( var y=7; y>=0; y--){
		var gridRow = document.createElement('tr');
		for( var x=7; x>=0; x--){
			var gridColumn = document.createElement('td');
			gridColumn.appendChild(this.getCell(x,y).renderView());
			gridRow.appendChild(gridColumn);
		}
		gridTable.appendChild(gridRow);
	}
}

Board.prototype.updateView = function(){
	for(var x=0; x<8; x++){
		for(var y=0; y<8; y++){
			this.board.getCell(x,y).updateView();
		}
	}
}

Board.prototype.clear = function(){
	var gridTable = document.getElementById('grid');
	while (gridTable.hasChildNodes()){
		gridTable.removeChild(gridTable.firstChild);
	}
}