
/////////////////////////////////////////////
/////////// My Globals //////////////////////
/////////////////////////////////////////////
/*
	'board' holds objects that represent the pieces. 
	board[row][col]
	board[0][0] = top left
	board[0][7] = top right
	board[7][0] = bottom left
	board[7][7] = bottom right
*/
var board = new Array();
board[0] = new Array(8);
board[1] = new Array(8);
board[2] = new Array(8);
board[3] = new Array(8);
board[4] = new Array(8);
board[5] = new Array(8);
board[6] = new Array(8);
board[7] = new Array(8);

var SQUARE_DIM = 34;  // width & height of a chessboard square in pixels
var BOARD_X = 0;	  // X coordinate of the top left corner of the chessboard
var BOARD_Y = 0;	  // Y coordinate of the top left corner of the chessboard


/////////////////////////////////////////////
/////////// My Methods //////////////////////
/////////////////////////////////////////////
function init()
{
	initBoard();
}

function initBoard()
{
	for(i = 0; i < 8; i++)
		for(j = 0; j < 8; j++)
			board[i][j] = null;

	BOARD_X = dd.elements["s00"].x;
	BOARD_Y = dd.elements["s00"].y;
	//alert("Board coords : " + BOARD_X + " " + BOARD_Y);
}



function placePieceRC(newPieceName, row, col)
{
	dd.elements[newPieceName].copy();
	numCopies = dd.elements[newPieceName].copies.length;
	newPiece = dd.elements[newPieceName].copies[numCopies-1];
	targetName = "s" + row + col;
	placePiece(newPiece, dd.elements[targetName]);
}

function placePiece(newPiece, dropTarget)
{
	// remove newPiece from previous position
	removePieceFromBoard(newPiece);

	// Get row and column
	x_coord = dd.elements[dropTarget.name].x;
	y_coord = dd.elements[dropTarget.name].y;

	rcArray = getBoardRowCol(x_coord, y_coord);

	if(rcArray == null)
	{
		// Piece is dropped off of board.  Delete it and move on.
		newPiece.del();
		return;
	}

	row = rcArray[0];
	col = rcArray[1];
	//alert(row + "  " + col);
	
	// Remove previous piece
	if(board[row][col] != newPiece)
		removePiece(row, col);

	// Get X and Y coords of new square
	xyArray = getBoardXY(row, col);
	
	new_x = xyArray[0];
	new_y = xyArray[1];

	pieceW = newPiece.w;
	pieceH = newPiece.h;

	new_x = new_x + (SQUARE_DIM - pieceW)/2;
	new_y = new_y + (SQUARE_DIM - pieceH)/2;

	// Place new piece
	board[row][col] = newPiece;
	newPiece.moveTo(new_x, new_y);

	//printBoard();
}

function removePiece(row, col)
{
	//alert(row + " " + col);
	previousPiece = board[row][col];
	if(previousPiece != null)
	{
		//alert("Removing " + previousPiece.name);
		previousPiece.del();
		board[row][col] = null;
	}
}

function removePieceFromBoard(newPiece)
{
	for(i = 0; i < 8; i++)
		for(j = 0; j < 8; j++)
		{
			if(board[i][j] == newPiece)
			{
				board[i][j] = null;
				return;
			}
		}
}

/*
	Input: X and Y coordinates of a pixel
	Output: An array representing the square on the chessboard containing this pixel, where:
	
			arr[0] = row (0-7)
			arr[1] = col (0-7)

			or 
			
			null
*/
function getBoardRowCol(pixelX, pixelY)
{
	col = Math.floor((pixelX - BOARD_X)/SQUARE_DIM);
	row = Math.floor((pixelY - BOARD_Y)/SQUARE_DIM);

	if(  (0 <= row && row <= 7) &&
		 (0 <= col && col <= 7))
	{
		var arr = new Array(row, col);
		return arr;
	}
	
	return null;
}

/*
	Input: Row and column of a square on the chessboard
	Output: An array representing the (x,y) coords of this square:
	
			arr[0] = x 
			arr[1] = y 

			or 
			
			null
*/
function getBoardXY(row, col)
{
	if(  (0 <= row && row <= 7) &&
		 (0 <= col && col <= 7))
	{
		
		var arr = new Array(2);
		arr[0] = BOARD_X + col*SQUARE_DIM;
		arr[1] = BOARD_Y + row*SQUARE_DIM;
		return arr;
	}

	return null;
}

function clearBoard()
{
	for(i = 0; i < 8; i++)
		for(j = 0; j < 8; j++)
	{
		tmp = board[i][j];
		if(tmp != null)
		{
			tmp.del();
			board[i][j] = null;
		}
	}
}

function setupBoard()
{
	// clear board
	clearBoard();
	placePieceRC("w_rook", 7, 0);
	placePieceRC("w_rook", 7, 7);
	placePieceRC("w_knight", 7, 1);
	placePieceRC("w_knight", 7, 6);
	placePieceRC("w_bishop", 7, 2);
	placePieceRC("w_bishop", 7, 5);
	placePieceRC("w_queen", 7, 3);
	placePieceRC("w_king", 7, 4);

	placePieceRC("w_pawn", 6, 0);
	placePieceRC("w_pawn", 6, 1);
	placePieceRC("w_pawn", 6, 2);
	placePieceRC("w_pawn", 6, 3);
	placePieceRC("w_pawn", 6, 4);
	placePieceRC("w_pawn", 6, 5);
	placePieceRC("w_pawn", 6, 6);
	placePieceRC("w_pawn", 6, 7);

	placePieceRC("b_rook", 0, 0);
	placePieceRC("b_rook", 0, 7);
	placePieceRC("b_knight", 0, 1);
	placePieceRC("b_knight", 0, 6);
	placePieceRC("b_bishop", 0, 2);
	placePieceRC("b_bishop", 0, 5);
	placePieceRC("b_queen", 0, 3);
	placePieceRC("b_king", 0, 4);

	placePieceRC("b_pawn", 1, 0);
	placePieceRC("b_pawn", 1, 1);
	placePieceRC("b_pawn", 1, 2);
	placePieceRC("b_pawn", 1, 3);
	placePieceRC("b_pawn", 1, 4);
	placePieceRC("b_pawn", 1, 5);
	placePieceRC("b_pawn", 1, 6);
	placePieceRC("b_pawn", 1, 7);
}

function printBoard()
{
	s = "";
	for(i = 0; i < 8; i++)
	{
		for(j = 0; j < 8; j++)
		{
			s = s + board[i][j] + "\t";
		}
		s = s + "\n";
	}

	alert(s);
}

function changeBoardStyle()
{
	val = document.getElementById("boardSelect").value;

	darkColor = "#D18B47";
	lightColor = "#FFCE9E";

	if(val == "1")
	{
	}
	else if(val == "2")
	{
		
	darkColor = "#7086B7";
	lightColor = "#E9FDF9";

	}
	else if(val == "3")
	{
		darkColor = "#E5571F";
		lightColor = "#FBE35B";
	}



	for(i = 1; i < 9; i++)
		for(j = 1; j < 9; j++)
		{
			id = "s" + i + "" + j;
			color = darkColor;
			if( (i%2 == 0 && j%2 == 0) ||
				(i%2 != 0 && j%2 != 0))
			{
				color = lightColor;
			}

				document.getElementById(id).style.backgroundColor = color;
		
		}

}


function printArray(origName)
{
	//alert(dd.elements[origName].copies);
	s = "+++ " + origName + " " + dd.elements[origName].copies.length + "+++";
	for(i = 0; i < dd.elements[origName].copies.length; i++)
	{
		s += "\n" + dd.elements[origName].copies[i].name;
	}

	alert(s);

}


