var GUI = new chessGUI();
var moveRules = new moveRules();
var chessRules = new chessRules();
var game;
var difficulty = getCookie("difficulty");
if (!difficulty) difficulty = "l1";


var timer = new timer();
var parser = new parser();

GUI.drawDisplayOptions();
GUI.drawBoard();
var jg = new jsGraphics("boardCanvas");
GUI.drawGameOptions();

var side;
var blackdot = false;
var currMove;
var variation = -1;
var notMove;
var varMove;
var guessAttempt;

var firsttime = true;
var puzzle = new ajaxRequest();

loadPuzzles();


function changeDifficulty(index) {
	difficulty = document.getElementById("difficulty").options[index].value;
	setCookie("difficulty", difficulty, 365);
	loadPuzzles();
	var gs = document.getElementById("gamestate");
	GUI.flushPosition();
	gs.innerHTML = "";
	document.getElementById("tabcontent").innerHTML = "";
}

function loadPuzzles() {
	puzzle.method = "GET";
	puzzle.target = difficulty + ".pgn";
	puzzle.request = null;
	puzzle.init();
	puzzle.request.onreadystatechange = puzzle.getPuzzle;
	puzzle.send();
}

function newPuzzle() {
	document.getElementById("newpuzzle").blur();
	currMove = 0;
	variation = -1;
	notMove = undefined;
	varMove = undefined;
	var rnd = Math.floor(Math.random()*(parser.games.length - 1));
	game = parser.games[rnd];
	parser.loadGame(game);
	game.loadFEN(game.FENs[0]);
	if (game.currentMove == "black") {
		blackdot = true;
	} else
		blackdot = false;
	document.getElementById("tabcontent").innerHTML = GUI.drawmoveslist();
	if (game.currentMove == "black") {
		if (!GUI.flipped)
			GUI.flipBoard();
	} else if (GUI.flipped) {
		GUI.flipBoard();
	}
	loadMove(0);

	advanceMove();
	timer.restart();
}

function advanceMove() {
	var gs = document.getElementById("gamestate");
	var gmove = game.moves[0][currMove];
		if (gmove && gmove.variated.length != 0) {
			for (var i = 0; i < gmove.variated.length; i++) {
				if (hasClass(document.getElementById("variation" + gmove.variated[i]), "movehidden")) {
					removeClass(document.getElementById("variation" + gmove.variated[i]), "movehidden");
					advanceVarMove(game.moves[0][currMove].variated[i]);
				}
			}
		}
	if (game.FENs[currMove + 1] && !gmove.guess) {
		currMove++;
		loadMove(currMove);
		//game.loadFEN(game.FENs[currMove]);
		//GUI.flushPosition();
		//GUI.drawPosition();
		if (!game.moves[0][currMove] || game.moves[0][currMove].guess) {
			advanceMove();
		} else
			setTimeout("advanceMove()", 1000);
	} else if (!game.FENs[currMove + 1]) {
		gs.innerHTML = "Correct! Line solved";
		
		if (!game.guesses)
			gs.innerHTML = "Correct! Problem solved completely";
	} else {
		side = game.currentMove;
		loadPosition();
		gs.innerHTML = "Find the best move for " + game.currentMove + " <IMG src=\"" + GUI.baseImages + "p" + game.currentMove.charAt(0) + "14.gif\"/>";
		guessAttempt = 0;
	}
}

function advanceVariation() {
	var gs = document.getElementById("gamestate");
	var gmove = game.moves[variation + 1][varMove];
		if (gmove && gmove.variated.length != 0) {
			for (var i = 0; i < gmove.variated.length; i++) {
				if (hasClass(document.getElementById("variation" + gmove.variated[i]), "movehidden")) {
					removeClass(document.getElementById("variation" + gmove.variated[i]), "movehidden");
					advanceVarMove(game.moves[variation + 1][varMove].variated[i]);
				}
			}
		}
	if (game.varFENs[variation][varMove + 1] && !game.moves[variation + 1][varMove + 1].guess) {
		varMove++;
		loadVariation(variation, varMove);
		//game.loadFEN(game.FENs[currMove]);
		//GUI.flushPosition();
		//GUI.drawPosition();
		if (!game.moves[variation + 1][varMove + 1] || game.moves[variation + 1][varMove + 1].guess) {
			advanceVariation();
		} else
			setTimeout("advanceVariation()", 1000);
	} else if (!game.varFENs[variation][varMove + 1]) {
		gs.innerHTML = "Correct! Line solved";
		if (!game.guesses)
			gs.innerHTML = "Correct! Problem solved completely";
	} else {
		side = game.currentMove;
		loadPosition();
		gs.innerHTML = "Find the best move for " + game.currentMove + " <IMG src=\"" + GUI.baseImages + "p" + game.currentMove.charAt(0) + "14.gif\"/>";
		guessAttempt = 0;
	}
}

function advanceVarMove(vN) {
	for (var i = 1; i < game.moves[vN + 1].length; i++) {
		if (!game.moves[vN + 1][i].guess) {
			fullNotMove = document.getElementById("notmove" + vN + "_" + i);
			if (hasClass(fullNotMove, "movehidden") )
				removeClass(fullNotMove, "movehidden");
		} else
			break;
	}
	var gmove = game.moves[vN + 1][i];
		if (gmove && gmove.variated.length != 0) {
			for (var i = 0; i < gmove.variated.length; i++) {
				if (hasClass(document.getElementById("variation" + gmove.variated[i]), "movehidden")) {
					removeClass(document.getElementById("variation" + gmove.variated[i]), "movehidden");
					advanceVarMove(gmove.variated[i]);
				}
			}
		}
}

function loadPosition() {
	if (notMove != undefined)
		removeClass(notMove, "selected");
	if (variation != -1) {
		game.loadFEN(game.varFENs[variation][varMove]);
		notMove = document.getElementById("var" + variation + "_" + varMove);
		fullNotMove = document.getElementById("notmove" + variation + "_" + varMove);
		if (varMove != 0 && hasClass(fullNotMove, "movehidden") )
			removeClass(fullNotMove, "movehidden");
	} else {
		notMove = document.getElementById("move" + currMove);
		fullNotMove = document.getElementById("notmove" + currMove)
		game.loadFEN(game.FENs[currMove]);
		if (currMove != 0 && hasClass(fullNotMove, "movehidden") )
			removeClass(fullNotMove, "movehidden");
	}

	var gs = document.getElementById("gamestate");
	if ((variation == - 1 && game.FENs[currMove + 1] && game.moves[0][currMove].guess) || (variation != -1 && game.varFENs[variation][varMove + 1] && game.moves[variation + 1][varMove + 1].guess)) {
		gs.innerHTML = "Find the best move for " + game.currentMove + " <IMG src=\"" + GUI.baseImages + "p" + game.currentMove.charAt(0) + "14.gif\"/>";
	} else {
		gs.innerHTML = "";
	}

	addClass(notMove, "selected");
	GUI.flushPosition();
	GUI.drawPosition();
}

function loadMove(id) {
	variation = -1;
	currMove = id;
	guessAttempt = 0;
	loadPosition();
}

function loadVariation(v, id) {
	variation = v;
	varMove = id;
	guessAttempt = 0;
	loadPosition();
}

function previousMove() {
	if(currMove - 1 >= 0 && variation == -1) {
		currMove--;
		loadPosition();
	} else if (variation != -1 && varMove - 1 >= 0) {
		varMove--;
		loadPosition();
	}
}

function nextMove() {
	if(currMove + 1 <= game.moves[0].length && variation == -1 && !game.moves[0][currMove].guess) {
		currMove++;
		loadPosition();
	} else if (variation != -1 && varMove + 1 < game.moves[variation + 1].length && !game.moves[0][varMove + 1].guess) {
		varMove++;
		loadPosition();
	}
}

function sendStats() {
	puzzle.target = "stats.php";
	puzzle.request = null;
	puzzle.method = "GET";
	var q = [];
	q['service'] = "task";
	puzzle.setQuery(q);
	puzzle.init();
	puzzle.request.onreadystatechange = puzzle.getStats;
	puzzle.send();
}
