//Shortcut
function element(id) {
	return document.getElementById(id);
}
//X and Y to Tile No.
function coord2Tile(x, y) {
	output = (y*(mapLimit+1)) + x;
	return output;
}
//Keyboard handler
function keyCheck(e) {
	if (e.keyCode) {
		keycode = e.keyCode;
	}
	else {
		keycode = e.which;
	}
	if (keycode == 37 || keycode == 65) {
		moveChar('left');
	}
	if (keycode == 38 || keycode == 87) {
		moveChar('up');
	}
	if (keycode == 39 || keycode == 68) {
		moveChar('right');
	}
	if (keycode == 40 || keycode == 83) {
		moveChar('down');
	}
}
//Get current X, Y, Top and Left
function getValues() {
	//Shorten it
	top = element('dchar').style.top;
	left = element('dchar').style.left;
	//Get current number in an integer, not string
	topInt = parseInt(top);
	leftInt = parseInt(left);
	//Get X and Y values from top and left
	charX = leftInt/16;
	charY = (topInt + 240)/16;
	checkAllowed();
	checkObstacles();
}
function checkAllowed() {
	upAllowed = true;
	leftAllowed = true;
	downAllowed = true;
	rightAllowed = true;
	if (charY == 0) {
		upAllowed = false;
	}
	if (charX == 0) {
		leftAllowed = false;
	}
	if (charY == mapLimit) {
		downAllowed = false;
	}
	if (charX == mapLimit) {
		rightAllowed = false;
	}
}
function checkObstacles() {
//Check the space above the character
	upX = charX;
	upY = charY - 1;
	upTile = coord2Tile(upX, upY);;
	//Get the array value of the tile number
	upValue = arrayMap2[upTile];
	if (upValue > 0) {
		upAllowed = false;
	}
//Check the space below the character
	downX = charX;
	downY = charY + 1;
	downTile = coord2Tile(downX, downY);
	//Get the array value of the tile number
	downValue = arrayMap2[downTile];
	if (downValue > 0) {
		downAllowed = false;
	}
//Check the space on the right of character
	rightX = charX + 1;
	rightY = charY;
	rightTile = coord2Tile(rightX, rightY);
	//Get the array value of the tile number
	rightValue = arrayMap2[rightTile];
	if (rightValue > 0) {
		rightAllowed = false;
	}
//Check the space on the left of character
	leftX = charX - 1;
	leftY = charY;
	leftTile = coord2Tile(leftX, leftY);
	//Get the array value of the tile number
	leftValue = arrayMap2[leftTile];
	if (leftValue > 0) {
		leftAllowed = false;
	}
}
function moveChar(direction) {
	getValues();
	//Calculate movements
	moveUp = (topInt-16)+"px";
	moveDown = (topInt+16)+"px";
	moveLeft = (leftInt-16)+"px";
	moveRight = (leftInt+16)+"px";
	moved = "no";
	//Move guy
	if (upAllowed) {
		if (direction == "up") {
			//element('dchar').style.backgroundPosition = "top right";
			element('dchar').style.top = moveUp;
			moved = "yes";
		}
	}
	if (downAllowed) {
		if (direction == "down") {
			//element.style.backgroundPosition = "top left";
			element('dchar').style.top = moveDown;
			moved = "yes";
		}
	}
	if (leftAllowed) {
		if (direction == "left") {
			//element.style.backgroundPosition = "bottom left";
			element('dchar').style.left = moveLeft;
			moved = "yes";
		}
	}
	if (rightAllowed) {
		if (direction == "right") {
			//element.style.backgroundPosition = "bottom right";
			element('dchar').style.left = moveRight;
			moved = "yes";
		}
	}
	if (moved == "yes") {
	//updateData(direction);
	getValues();
	}
}