var curCell = -1;
var celltds = new Array(width*height);
var backColors = new Array(width*height);
var curMoves = 0;
var moveArr = new Array();

var req;
function recordScore() {
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (req) {
		var poststr = moveArr[0];
		for (var i = 1; i < curMoves; i++)
			poststr += '-' + moveArr[i];
		req.onreadystatechange = processReqChange;
		req.open("POST", "/nurikabe/submit.php", true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send('&h='+poststr + '&t='+token);
	}
}

function processReqChange() {
}

function createTd(idx) {
	celltds[idx] = document.createElement('td');
	celltds[idx].id = "c" + idx;
	celltds[idx].style.fontSize = "24pt";
	celltds[idx].style.height = "1.5em";
	celltds[idx].style.width = "1.5em";
	celltds[idx].style.border = "1px solid black";
	celltds[idx].style.textAlign = "center";
	celltds[idx].onmouseover = mouseOver;
	celltds[idx].onmouseout = mouseOut;
	celltds[idx].onmouseup = mouseClick;
	celltds[idx].onmousedown = function(){return false};
	celltds[idx].onselectstart = function(){return false};
	celltds[idx].oncontextmenu = function(){return false};

	if (puzzle.charAt(idx) == '.') {
		celltds[idx].style.backgroundColor = "#888888";
		celltds[idx].innerHTML = "&nbsp;";
		backColors[idx] = "#888888";
	} else {
		celltds[idx].style.backgroundColor = "#FFFFFF";
		celltds[idx].innerHTML = curpuz.charAt(idx);
		backColors[idx] = "#FFFFFF";
	}
}

// create the table with all the sudoku cells
function initNurikabeGrid() {
	var mydiv = document.getElementById('contain');
	var tbl = document.createElement('table');
	var tbody = document.createElement('tbody');

	for (var i = 0; i < height; i++) {
		var newtr = document.createElement('tr');
		for (var j = 0; j < width; j++) {
			createTd(i*width+j);
			newtr.appendChild(celltds[i*width+j]);
		}
		tbody.appendChild(newtr);
	}
	tbl.appendChild(tbody);
	mydiv.appendChild(tbl);

	initAfterGrid();
}

function getEventId(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;
	return parseInt(targ.id.substring(1));
}

function mouseOver(e) {
	var idx = getEventId(e);
	curCell = idx;
}

function mouseOut(e) {
	var idx = getEventId(e);
	curCell = -1;
}



var timerID = 0;
var tstart = null;
var killTimer = false;

function updateTimer() {
	if (timerID) clearTimeout(timerID);
	if (!tstart) tstart = new Date();
	if (killTimer) return;

	var tdate = new Date();
	var tdiff = tdate.getTime() - tstart.getTime();

	var min = Math.floor(tdiff/60000), sec = Math.floor(tdiff/1000-60*min);
	var frmt = min + (sec > 9 ? ":" : ":0") + sec;
	document.getElementById('timer').innerHTML = "Time " + frmt;

	timerID = setTimeout("updateTimer()", 1000);
}

function initAfterGrid() {
	tstart = new Date();
	timerID = setTimeout("updateTimer()", 1000);
}

function solvePuzzle() {
	for (var i = 0; i < width*height; i++) {
		if (curpuz.charAt(i) != solpuz.charAt(i)) {
			backColors[i] = solpuz.charAt(i) == '#' ? "#000" : "#FFF";
			celltds[i].style.backgroundColor = backColors[i];
		}
	}
	curpuz = solpuz;
	killTimer = true;
	return true;
}

function checkPuzzle() {
	var gw = false, gb = false;
	for (var i = 0; i < width*height; i++) {
		if (curpuz.charAt(i) != solpuz.charAt(i)) {
			var pok = false;
			if (curpuz.charAt(i) == '.') {
				if (solpuz.charAt(i) == '#' && !gw) {
					gb = true;
					pok = true;
				} else if (solpuz.charAt(i) == ' ' && !gb) {
					gw = true;
					pok = true;
				}
			}
			if (!pok) {
				alert("There is an error in your solution.");
				return false;
			}
		}
	}
	if (gb || gw) solvePuzzle(); else killTimer = true;
	puzzleDone();
	alert("Congratulations!");
	return true;
}

function toggleState(idx, rb) {
	var ch;
	if (rb) {
		if (curpuz.charAt(idx) == '.') {
			backColors[idx] = "#000";
			ch = '#';
		} else if (curpuz.charAt(idx) == ' ') {
			backColors[idx] = "#888";
			ch = '.';
		} else {
			backColors[idx] = "#FFF";
			ch = ' ';
		}
	} else {
		if (curpuz.charAt(idx) == '.') {
			backColors[idx] = "#FFFFFF";
			ch = ' ';
		} else if (curpuz.charAt(idx) == ' ') {
			backColors[idx] = "#000000";
			ch = '#';
		} else {
			backColors[idx] = "#888888";
			ch = '.';
		}
	}
	moveArr[curMoves++] = idx + (rb ? 'r' : 'l');
	celltds[idx].style.backgroundColor = backColors[idx];
	curpuz = curpuz.substr(0, idx) + ch + curpuz.substr(idx+1);

	return curpuz.indexOf('.') == -1;
}

function mouseClick(e) {
	if (!e) var e = window.event;
	var idx = getEventId(e);
	if (curCell != idx) return true;
	if (puzzle.charAt(idx) != '.') return true;
	var right = false;
	if (e.which) right = (e.which == 3);
	else if (e.button) right = (e.button == 2);
	toggleState(idx, right);
	return false;
}


