document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

var dragObject = null;
var mouseOffset = null;
var conSq = [];
var dropTargets = [];
var promData;
var promoting = false;

function mouseCoords(ev) {
	if(ev.pageX || ev.pageY){
		return {x: ev.pageX, y: ev.pageY};
	}
	return {
		x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y: ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function getMouseOffset(target, ev) {
	ev = ev || window.event;

	var docPos = getPosition(target);
	var mousePos = mouseCoords(ev);
	var height = target.offsetHeight;
	var width = target.offsetWidth;
	return {x: Math.floor(width/2), y: Math.floor(height/2)};
}

function getPosition(e) {
	var left = 0;
	var top = 0;
	try {
		while (e.offsetParent) {
			left += e.offsetLeft;
			top  += e.offsetTop;
			e = e.offsetParent;
		}
	}
	catch(ex) {
	}
	

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x: left, y: top};
}

function mouseMove(ev) {
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);

	if(dragObject) {
		dragObject.style.position = 'absolute';
		var board = document.getElementById("chessBoard");
		boardEdges = getPosition(board);
		if (mousePos.y < boardEdges.y || mousePos.y > boardEdges.y + 428 || mousePos.x < boardEdges.x || mousePos.x > boardEdges.x + 428) {
			// Sits on the edges until released
		} else {
			dragObject.style.top = mousePos.y - mouseOffset.y;
			dragObject.style.left = mousePos.x - mouseOffset.x;
		}

		return false;
	}
}

function addDropTarget(dropTarget) {
	dropTargets.push(dropTarget);
}

function mouseUp(ev){
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);

	if (dragObject) {
		for(var i = 0; i < dropTargets.length; i++) {
			var curTarget = dropTargets[i];
			var targPos = getPosition(curTarget);
			var targWidth = parseInt(curTarget.offsetWidth);
			var targHeight = parseInt(curTarget.offsetHeight);

			var board = document.getElementById("chessBoard");
			var boardPos = getPosition(board);
			var boardWidth = parseInt(board.offsetWidth);
			var boardHeight = parseInt(board.offsetHeight);
			
			if (BrowserDetect.browser == "Safari") {
				//targPos.x = targPos.x + 20;
				targPos.y = targPos.y - 25;
				//alert("Safari");
			}

			if(dragObject && (mousePos.x > targPos.x) && (mousePos.x < (targPos.x + targWidth)) && (mousePos.y > targPos.y) && (mousePos.y < (targPos.y + targHeight))) {
				var coords = GUI.getSquare(dragObject.id);
				var piece = game.squares[coords.x][coords.y].piece;
				var square = piece.square;
				var oldSquare = GUI.getGUISquare(square.x, square.y);
				var oldPos = getPosition(oldSquare);
				var oldWidth = parseInt(oldSquare.offsetWidth);
				var oldHeight = parseInt(oldSquare.offsetHeight);
				var tempSq;
				var curCoords = GUI.getSquare(curTarget.id);
				var currSquare = GUI.getGUISquare(curCoords.x, curCoords.y);
				if (piece.name == "pawn" && (curCoords.y == 1 || curCoords.y == 8)) {
					var promBox = document.getElementById("promotion");
					promBox.style.display = 'block';
					var promWidth = parseInt(promBox.offsetWidth);
					var promHeight = parseInt(promBox.offsetHeight);
					var promX;
					var promY;
					if (boardPos.x + boardWidth < targPos.x + promWidth) {
						var promX = targPos.x - (targPos.x + promWidth - boardPos.x - boardWidth);
					} else
						var promX = targPos.x;
					if (boardPos.y + boardHeight < targPos.y + promHeight) {
						var promY = targPos.y - (targPos.y + promHeight - boardPos.y - boardHeight);
					} else
						var promY = targPos.y;
					promData = {
						x: promX - 5,
						y: promY - 5,
						name: "pawn",
						fromX: coords.x,
						fromY: coords.y,
						toX: curCoords.x,
						toY: curCoords.y
					}
					var pieceImg = document.createElement("IMG");
					pieceImg.id = "piece" + square.x + square.y;
					pieceImg.src = GUI.baseImages + "pieces/" + GUI.piecesTheme + "/" + GUI.piecesSize + "/" + piece.color.charAt(0) + "p.png";
					curTarget.appendChild(pieceImg);
					removeChildNodes(oldSquare);
					removeChildNodes(currSquare);
					GUI.popPromotion();
					promoting = true;
				} else {
					dragObject.style.position = 'static';
					dragObject = null;
					var found = false;
					var gs = document.getElementById("gamestate");
					var corr = game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)];
					//alert(hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))),"movehidden"));
					if (corr.toX == curCoords.x && corr.toY == curCoords.y && corr.fromX == coords.x && corr.fromY == coords.y && hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))),"movehidden"))  {
						
						if (game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)].variated.length == 0)
							game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)].guess = false;
						if (variation == -1) {
							currMove++;
							setTimeout("advanceMove()", 1000);
						} else {
							varMove++;
							setTimeout("advanceVariation()", 1000);
						}
						loadPosition();
						removeChildNodes(oldSquare);
						found = true;
						gs.innerHTML = "Correct!";
					} else if (corr.variated.length > 0) {
						for (var i = 0; i < corr.variated.length; i++) {
							varCorr = game.moves[corr.variated[i] + 1][1];
							//alert(varCorr.toX + varCorr.toY);
							if (varCorr.toX == curCoords.x && varCorr.toY == curCoords.y && varCorr.fromX == coords.x && varCorr.fromY == coords.y && hasClass(document.getElementById("notmove" + corr.variated[i] + "_" + 1),"movehidden")) {
								variation = corr.variated[i];
								corr.variated.splice(i, 1);
								varMove = 1;
								setTimeout("advanceVariation()", 1000);
								loadPosition();
								removeChildNodes(oldSquare);
								gs.innerHTML = "Correct!";
								found = true;
								corr = varCorr;
								break;
							}
						}
					}
					if (found)
						game.guesses--;
					if (!found) {
						guessAttempt++;
						//GUI.flushPosition();
						//alert(guessAttempt);
						if (guessAttempt == 1) {
							if(hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden") && corr.markers != "") {
								var markers = corr.markers.split(" ");
								for (var i = 0; i < markers.length; i++) {
									if (markers[i].match(/(A|D)\([a-h][1-8]\)/)) {
										coords = markers[i].match(/(A|D)\(([a-h])([1-8])\)/);
										var curTarget = GUI.getGUISquare(coords[2], coords[3]);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										if (coords[1] == "A")
											jg.setColor("#b80057");
										else if (coords[1] == "D")
											jg.setColor("#00b879");
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											jg.drawRect(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
										} else if (BrowserDetect.browser == "Safari") {
											jg.drawRect(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
										} else
											jg.drawRect(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
									} else if (markers[i].match(/L(A|D)\([a-h][1-8]\:[a-h][1-8]\)/)) {
										coords = markers[i].match(/L(A|D)\(([a-h])([1-8])\:([a-h])([1-8])\)/);
										var curTarget = GUI.getGUISquare(coords[4], coords[5]);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										
										var oldSquare = GUI.getGUISquare(coords[2], coords[3]);
										var oldPos = getPosition(oldSquare);
										var oldWidth = parseInt(oldSquare.offsetWidth);
										var oldHeight = parseInt(oldSquare.offsetHeight);

										if (coords[1] == "A")
											var color ="#b80057";
										else if (coords[1] == "D")
											var color ="#00b879";
										var x1 = targPos.x + Math.round(targWidth/2);
										var y1 = targPos.y + Math.round(targHeight/2);
										var xC = oldPos.x + Math.round(oldWidth/2);
										var yC = oldPos.y + Math.round(oldHeight/2);
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											y1 = y1 - 25;
										}
										if (BrowserDetect.browser == "Safari" && !oldSquare.childNodes[0]) {
											yC = yC - 25;
										}
										GUI.drawArrow(x1, y1, xC, yC, color);
									}
								}
								corr.markers = "";
							} else if (!hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden") && corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][1];
									if(hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden") && newCorr.markers != "") {
										var markers = newCorr.markers.split(" ");
										for (var i = 0; i < markers.length; i++) {
											if (markers[i].match(/(A|D)\([a-h][1-8]\)/)) {
												coords = markers[i].match(/(A|D)\(([a-h])([1-8])\)/);
												var curTarget = GUI.getGUISquare(coords[2], coords[3]);
												var targPos = getPosition(curTarget);
												var targWidth = parseInt(curTarget.offsetWidth);
												var targHeight = parseInt(curTarget.offsetHeight);
												jg.setStroke(3);
												if (coords[1] == "A")
													jg.setColor("#b80057");
												else if (coords[1] == "D")
													jg.setColor("#00b879");
												if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
													jg.drawRect(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
												} else if (BrowserDetect.browser == "Safari") {
													jg.drawRect(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
												} else
													jg.drawRect(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
												jg.paint();
											} else if (markers[i].match(/L(A|D)\([a-h][1-8]\:[a-h][1-8]\)/)) {
												coords = markers[i].match(/L(A|D)\(([a-h])([1-8])\:([a-h])([1-8])\)/);
												var curTarget = GUI.getGUISquare(coords[4], coords[5]);
												var targPos = getPosition(curTarget);
												var targWidth = parseInt(curTarget.offsetWidth);
												var targHeight = parseInt(curTarget.offsetHeight);
												
												var oldSquare = GUI.getGUISquare(coords[2], coords[3]);
												var oldPos = getPosition(oldSquare);
												var oldWidth = parseInt(oldSquare.offsetWidth);
												var oldHeight = parseInt(oldSquare.offsetHeight);

												if (coords[1] == "A")
													var color ="#b80057";
												else if (coords[1] == "D")
													var color ="#00b879	";
												var x1 = targPos.x + Math.round(targWidth/2);
												var y1 = targPos.y + Math.round(targHeight/2);
												var xC = oldPos.x + Math.round(oldWidth/2);
												var yC = oldPos.y + Math.round(oldHeight/2);
												if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
													y1 = y1 - 25;
												}
												if (BrowserDetect.browser == "Safari" && !oldSquare.childNodes[0]) {
													yc = yC - 25;
												}
												GUI.drawArrow(x1, y1, xC, yC, color);
											}
										}
										
										break;
									} else if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										jg.clear();
										GUI.drawPosition();
										var curTarget = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
										} else if (BrowserDetect.browser == "Safari") {
											jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
										} else
										jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
										guessAttempt++;
										break;
									}
								}
							} else {
								if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {
									jg.clear();
									GUI.drawPosition();
									var curTarget = GUI.getGUISquare(corr.fromX,corr.fromY);
									var targPos = getPosition(curTarget);
									var targWidth = parseInt(curTarget.offsetWidth);
									var targHeight = parseInt(curTarget.offsetHeight);
									jg.setStroke(3);
									jg.setColor("#b80057");	
									if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
										jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
									} else if (BrowserDetect.browser == "Safari") {
										jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
									} else
										jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
									jg.paint();
									guessAttempt++;
								}

							}
						} else if (guessAttempt == 2) {
							if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {	
								jg.clear();
								GUI.drawPosition();
								var curTarget = GUI.getGUISquare(corr.fromX, corr.fromY);
								var targPos = getPosition(curTarget);
								var targWidth = parseInt(curTarget.offsetWidth);
								var targHeight = parseInt(curTarget.offsetHeight);
								jg.setStroke(3);
								jg.setColor("#b80057");	
								if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
									jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
								} else if (BrowserDetect.browser == "Safari") {
									jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
								} else
									jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
								jg.paint();
							} else if (corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][1];
									if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										jg.clear();
										GUI.drawPosition();
										var curTarget = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
										} else if (BrowserDetect.browser == "Safari") {
											jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
										} else
											jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
										guessAttempt++;
										break;
									}
								}
							}
						} else if (guessAttempt == 3) {
							if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {
								var curTarget = GUI.getGUISquare(corr.toX, corr.toY);
								var targPos = getPosition(curTarget);
								var targWidth = parseInt(curTarget.offsetWidth);
								var targHeight = parseInt(curTarget.offsetHeight);
								jg.setStroke(3);
								jg.setColor("#b80057");	
								if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
									jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
								} else if (BrowserDetect.browser == "Safari") {
									jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
								} else
									jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
								
								var oldSquare = GUI.getGUISquare(corr.fromX, corr.fromY);
								var oldPos = getPosition(oldSquare);
								var oldWidth = parseInt(oldSquare.offsetWidth);
								var oldHeight = parseInt(oldSquare.offsetHeight);

								var x1 = targPos.x + Math.round(targWidth/2);
								var y1 = targPos.y + Math.round(targHeight/2);
								var xC = oldPos.x + Math.round(oldWidth/2);
								var yC = oldPos.y + Math.round(oldHeight/2);
								if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
													y1 = y1 - 25;
												}
												if (BrowserDetect.browser == "Safari" && !oldSquare.childNodes[0]) {
													yc = yC - 25;
												}
								GUI.drawArrow(x1, y1, xC, yC, "#b80057");
								jg.paint();
							} else if (corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][1];
									if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										var curTarget = GUI.getGUISquare(newCorr.toX, newCorr.toY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											jg.drawEllipse(targPos.x + 2, targPos.y - 23, targWidth - 3, targHeight - 3);
										} else if (BrowserDetect.browser == "Safari") {
											jg.drawEllipse(targPos.x + 2, targPos.y, targWidth - 3, targHeight - 3);
										} else
											jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
											
										var oldSquare = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var oldPos = getPosition(oldSquare);
										var oldWidth = parseInt(oldSquare.offsetWidth);
										var oldHeight = parseInt(oldSquare.offsetHeight);

										var x1 = targPos.x + Math.round(targWidth/2);
										var y1 = targPos.y + Math.round(targHeight/2);
										var xC = oldPos.x + Math.round(oldWidth/2);
										var yC = oldPos.y + Math.round(oldHeight/2);
										if (BrowserDetect.browser == "Safari" && !curTarget.childNodes[0]) {
											y1 = y1 - 25;
										}
										if (BrowserDetect.browser == "Safari" && !oldSquare.childNodes[0]) {
											yc = yC - 25;
										}
										GUI.drawArrow(x1, y1, xC, yC, "#b80057");
										break;
									}
								}
							}
						}
						gs.innerHTML = "Incorrect move! Try again";
					}
					// innerHTML way
					//oldSquare.innerHTML = "";
				}
			}
		}
		if (!promoting && dragObject)
			dragObject.style.position = 'static';
		dropTargets = [];
		for (var i = 0; i < conSq.length; i++) {
			tempSq = GUI.getGUISquare(conSq[i].charAt(0), conSq[i].charAt(1));
			if (hasClass(tempSq, "white")) removeClass(tempSq, "whitecontrolled");
			if (hasClass(tempSq, "black")) removeClass(tempSq, "blackcontrolled");
		}
		conSq = [];
	}
	dragObject = null;
}

function promoteTo(promPiece) {
	var promBox = document.getElementById("promotion");
	promBox.style.visibility = 'hidden';
	promBox.style.display = 'none';
	promBox.style.position = 'static';
	promoting = false;
	coords = {x: promData.fromX, y: promData.fromY};
	curCoords = {x: promData.toX, y: promData.toY};
					dragObject = null;
					var found = false;
					var gs = document.getElementById("gamestate");
					var corr = game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)];
					//alert((variation + 1) + corr.promoteTo + corr.fromX + corr.fromY + corr.toX + corr.toY + promPiece.charAt(0) + curCoords.x + curCoords.y + coords.x + coords.y);
					if (promPiece.charAt(0) == corr.promoteTo && corr.toX == curCoords.x && corr.toY == curCoords.y && corr.fromX == coords.x && corr.fromY == coords.y && hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))),"movehidden"))  {
						
						if (game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)].variated.length == 0)
							game.moves[variation + 1][(variation == -1 ? currMove : varMove + 1)].guess = false;
						if (variation == -1) {
							currMove++;
							setTimeout("advanceMove()", 1000);
						} else {
							varMove++;
							setTimeout("advanceVariation()", 1000);
						}
						loadPosition();
						//var oldSquare = GUI.getGUISquare(promData.fromX, promData.fromX);
						//removeChildNodes(oldSquare);
						found = true;
						gs.innerHTML = "Correct!";
					} else if (corr.variated.length > 0) {
						for (var i = 0; i < corr.variated.length; i++) {
							varCorr = game.moves[corr.variated[i] + 1][1];
							//alert(varCorr.promoteTo + varCorr.fromX + varCorr.fromY + varCorr.toX + varCorr.toY);
							//alert(varCorr.toX + varCorr.toY);
							if (promPiece.charAt(0) == varCorr.promoteTo && varCorr.toX == curCoords.x && varCorr.toY == curCoords.y && varCorr.fromX == coords.x && varCorr.fromY == coords.y && hasClass(document.getElementById("notmove" + corr.variated[i] + "_" + 1),"movehidden")) {
							
								variation = corr.variated[i];
								corr.variated.splice(i, 1);
								varMove = 1;
								setTimeout("advanceVariation()", 1000);
								loadPosition();
								//removeChildNodes(oldSquare);
								gs.innerHTML = "Correct!";
								found = true;
								corr = varCorr;
								break;
							}
						}
					}
					if (!found) {
						guessAttempt++;
						//alert(guessAttempt);
						if (guessAttempt == 1) {
							if(hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden") && corr.markers != "") {
								var markers = corr.markers.split(" ");
								for (var i = 0; i < markers.length; i++) {
									if (markers[i].match(/(A|D)\([a-h][1-8]\)/)) {
										coords = markers[i].match(/(A|D)\(([a-h])([1-8])\)/);
										var curTarget = GUI.getGUISquare(coords[2], coords[3]);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										if (coords[1] == "A")
											jg.setColor("#b80057");
										else if (coords[1] == "D")
											jg.setColor("#00b879");
										jg.drawRect(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
									} else if (markers[i].match(/L(A|D)\([a-h][1-8]\:[a-h][1-8]\)/)) {
										coords = markers[i].match(/L(A|D)\(([a-h])([1-8])\:([a-h])([1-8])\)/);
										var curTarget = GUI.getGUISquare(coords[4], coords[5]);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										
										var oldSquare = GUI.getGUISquare(coords[2], coords[3]);
										var oldPos = getPosition(oldSquare);
										var oldWidth = parseInt(oldSquare.offsetWidth);
										var oldHeight = parseInt(oldSquare.offsetHeight);

										if (coords[1] == "A")
											var color ="#b80057";
										else if (coords[1] == "D")
											var color ="#b80057";
										var x1 = targPos.x + Math.round(targWidth/2);
										var y1 = targPos.y + Math.round(targHeight/2);
										var xC = oldPos.x + Math.round(oldWidth/2);
										var yC = oldPos.y + Math.round(oldHeight/2);
										if (BrowserDetect.browser == "Safari") {
											yC = yC - 25;
										}
										GUI.drawArrow(x1, y1, xC, yC, color);
									}
								}
								corr.markers = "";
							} else if (!hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden") && corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][0];
									if(hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden") && newCorr.markers != "") {
										var markers = newCorr.markers.split(" ");
										for (var i = 0; i < markers.length; i++) {
											if (markers[i].match(/(A|D)\([a-h][1-8]\)/)) {
												coords = markers[i].match(/(A|D)\(([a-h])([1-8])\)/);
												var curTarget = GUI.getGUISquare(coords[2], coords[3]);
												var targPos = getPosition(curTarget);
												var targWidth = parseInt(curTarget.offsetWidth);
												var targHeight = parseInt(curTarget.offsetHeight);
												jg.setStroke(3);
												if (coords[1] == "A")
													jg.setColor("#b80057");
												else if (coords[1] == "D")
													jg.setColor("#00b879");
												jg.drawRect(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
												jg.paint();
											} else if (markers[i].match(/L(A|D)\([a-h][1-8]\:[a-h][1-8]\)/)) {
												coords = markers[i].match(/L(A|D)\(([a-h])([1-8])\:([a-h])([1-8])\)/);
												var curTarget = GUI.getGUISquare(coords[4], coords[5]);
												var targPos = getPosition(curTarget);
												var targWidth = parseInt(curTarget.offsetWidth);
												var targHeight = parseInt(curTarget.offsetHeight);
												
												var oldSquare = GUI.getGUISquare(coords[2], coords[3]);
												var oldPos = getPosition(oldSquare);
												var oldWidth = parseInt(oldSquare.offsetWidth);
												var oldHeight = parseInt(oldSquare.offsetHeight);

												if (coords[1] == "A")
													var color ="#b80057";
												else if (coords[1] == "D")
													var color ="#b80057";
												var x1 = targPos.x + Math.round(targWidth/2);
												var y1 = targPos.y + Math.round(targHeight/2);
												var xC = oldPos.x + Math.round(oldWidth/2);
												var yC = oldPos.y + Math.round(oldHeight/2);
												if (BrowserDetect.browser == "Safari") {
													yC = yC - 25;
												}
												GUI.drawArrow(x1, y1, xC, yC, color);
											}
										}
										
										break;
									} else if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										jg.clear();
										GUI.drawPosition();
										var curTarget = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
										guessAttempt++;
										break;
									}
								}
							} else {
								if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {
									jg.clear();
									GUI.drawPosition();
									var curTarget = GUI.getGUISquare(corr.fromX,corr.fromY);
									var targPos = getPosition(curTarget);
									var targWidth = parseInt(curTarget.offsetWidth);
									var targHeight = parseInt(curTarget.offsetHeight);
									jg.setStroke(3);
									jg.setColor("#b80057");	
									jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
									jg.paint();
									guessAttempt++;
								}

							}
						} else if (guessAttempt == 2) {
							if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {	
								jg.clear();
								GUI.drawPosition();
								var curTarget = GUI.getGUISquare(corr.fromX, corr.fromY);
								var targPos = getPosition(curTarget);
								var targWidth = parseInt(curTarget.offsetWidth);
								var targHeight = parseInt(curTarget.offsetHeight);
								jg.setStroke(3);
								jg.setColor("#b80057");	
								jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
								jg.paint();
							} else if (corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][0];
									if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										jg.clear();
										GUI.drawPosition();
										var curTarget = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										jg.paint();
										guessAttempt++;
										break;
									}
								}
							}
						} else if (guessAttempt == 3) {
							if (hasClass(document.getElementById("notmove" + (variation == -1 ? currMove + 1 : variation + "_" + (varMove + 1))), "movehidden")) {
								var curTarget = GUI.getGUISquare(corr.toX, corr.toY);
								var targPos = getPosition(curTarget);
								var targWidth = parseInt(curTarget.offsetWidth);
								var targHeight = parseInt(curTarget.offsetHeight);
								jg.setStroke(3);
								jg.setColor("#b80057");	
								jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
								
								var oldSquare = GUI.getGUISquare(corr.fromX, corr.fromY);
								var oldPos = getPosition(oldSquare);
								var oldWidth = parseInt(oldSquare.offsetWidth);
								var oldHeight = parseInt(oldSquare.offsetHeight);

								var x1 = targPos.x + Math.round(targWidth/2);
								var y1 = targPos.y + Math.round(targHeight/2);
								var xC = oldPos.x + Math.round(oldWidth/2);
								var yC = oldPos.y + Math.round(oldHeight/2);
								if (BrowserDetect.browser == "Safari") {
									yC = yC - 25;
								}
								GUI.drawArrow(x1, y1, xC, yC, "#b80057");
								jg.paint();
							} else if (corr.variated.length > 0) {
								for (var i = 0; i < corr.variated.length; i++) {
									newCorr = game.moves[corr.variated[i] + 1][0];
									if (hasClass(document.getElementById("notmove" + (corr.variated[i]) + "_" + "1"), "movehidden")) {
										var curTarget = GUI.getGUISquare(newCorr.toX, newCorr.toY);
										var targPos = getPosition(curTarget);
										var targWidth = parseInt(curTarget.offsetWidth);
										var targHeight = parseInt(curTarget.offsetHeight);
										jg.setStroke(3);
										jg.setColor("#b80057");	
										jg.drawEllipse(targPos.x, targPos.y, targWidth - 3, targHeight - 3);
										
										var oldSquare = GUI.getGUISquare(newCorr.fromX, newCorr.fromY);
										var oldPos = getPosition(oldSquare);
										var oldWidth = parseInt(oldSquare.offsetWidth);
										var oldHeight = parseInt(oldSquare.offsetHeight);

										var x1 = targPos.x + Math.round(targWidth/2);
										var y1 = targPos.y + Math.round(targHeight/2);
										var xC = oldPos.x + Math.round(oldWidth/2);
										var yC = oldPos.y + Math.round(oldHeight/2);
										if (BrowserDetect.browser == "Safari") {
											yC = yC - 25;
										}
										GUI.drawArrow(x1, y1, xC, yC, "#b80057");
										break;
									}
								}
							}
						}
						gs.innerHTML = "Incorrect move! Try again";
					}
					// innerHTML way
					//oldSquare.innerHTML = "";
	//game.moveHandler(promData.name, promData.fromX, promData.fromY, promData.toX, promData.toY, '', true, promPiece);
	promData = undefined;
	GUI.flushPosition();
	GUI.drawPosition();
}

function makeDraggable(item) {
	item.onmousedown = function(ev) {
		var coords = GUI.getSquare(this.id);
		var tempSq;
		conSq = game.controlledSquares(coords.x, coords.y);
		//alert(conSq);
		if (!promoting && conSq && conSq.length > 0) {
			for (var i = 0; i < conSq.length; i++) {
				tempSq = GUI.getGUISquare(conSq[i].charAt(0), conSq[i].charAt(1));
				if (hasClass(tempSq, "white")) addClass(tempSq, "whitecontrolled");
				if (hasClass(tempSq, "black")) addClass(tempSq, "blackcontrolled");
				addDropTarget(tempSq);
			}
			ev = ev || window.event;
			dragObject = document.getElementById("piece" + coords.x + coords.y);
			mouseOffset = getMouseOffset(dragObject, ev);
			var mousePos = mouseCoords(ev);
			dragObject.style.position = 'absolute';
			dragObject.style.top = mousePos.y - mouseOffset.y;
			dragObject.style.left = mousePos.x - mouseOffset.x;
			return false;
		}
		
	}
}
