
function mouseDown(e) {
	if (interactive) {
		currentCellCoords = whichCell(e);
		if (currentCellCoords != null) {
			z = currentCellCoords.substr(0,1);
			x = currentCellCoords.substr(1,1);
			y = currentCellCoords.substr(2,1);
			currentCell = cell[z][x][y];
			if (currentCell!=null && currentCell.piece) showMoves(z,x,y);
		}
		currentPiece= whichPiece(e);
		if (currentPiece != null) {
			dragPiece = div[currentPiece];
			setZIndex(dragPiece,9);
			origPosX = getLeft('div'+currentPiece);
			origPosY = getTop('div'+currentPiece);
			dragPieceActive = true;
			x = eventXcoord(e);
			y = eventYcoord(e);
			dragClickX = x - getLeft('div'+currentPiece);
			dragClickY = y - getTop('div'+currentPiece);
			return false;
		}
	}
}

function mouseMove(e) {
	if (interactive) {
		if (dragPieceActive) {
			x = eventXcoord(e);
			y = eventYcoord(e);
			dragPiece.xpos = x-dragClickX;
			dragPiece.ypos = y-dragClickY;
			shiftTo('div'+currentPiece,dragPiece.xpos,dragPiece.ypos);
			return false;
		} else {
			return true;
		}
	}
}

function mouseUp(e) {
	if (interactive) {
		x = eventXcoord(e);
		y = eventYcoord(e);
	
		if (dragPieceActive) {
			if (onTarget(e)) {
				document.images['img'+currentPiece].style.height = imgheights[parseInt(whichCell(e).substr(2,1))];
				
				myboard = 'board'+whichBoard(e);
				mycell = 'cell'+whichCell(e);
				myranknum = whichCell(e).substr(2,1);
				
				shiftTo(
					'div'+currentPiece,
					getLeft('boards')+getLeft('board'+whichBoard(e))+getLeft('cell'+whichCell(e))+getWidth('cell'+whichCell(e))/2-parseInt(document.images['img'+currentPiece].style.height)/2,
					getTop('boards')+getTop(myboard)+getTop(mycell)-(myranknum-2)
				);

				setZIndex(dragPiece,(5-whichBoard(e))*2+1);

				p = eval(position[currentPiece].substr(0,1));
				q = eval(position[currentPiece].substr(1,1));
				r = eval(position[currentPiece].substr(2,1));
				cell[p][q][r].piece = "Empty";

				position[currentPiece] = ""+whichCell(e);
				p = eval(position[currentPiece].substr(0,1));
				q = eval(position[currentPiece].substr(1,1));
				r = eval(position[currentPiece].substr(2,1));
				cell[p][q][r].piece = piecename[currentPiece];

				checkCapture(currentPiece);
				saveGame("Default");
			} else {
				shiftTo('div'+currentPiece,origPosX,origPosY);
			}
		}
		dragPieceActive = false;
		dragBoardActive = false;
		return true;
	}
}

imgheights = new Array(6);
//imgheights[1] = 23;
//imgheights[2] = 21;
//imgheights[3] = 19;
//imgheights[4] = 17;
//imgheights[5] = 15;
imgheights[1] = 21;
imgheights[2] = 20;
imgheights[3] = 19;
imgheights[4] = 18;
imgheights[5] = 17;
