var gameid = 0;
var loading = false;
var promFrom;
var promTo;
var clicked = '';
var picLoader;
var moves = new Array();
var movesCount = 0;
var level = 'You Turn';
var resume;

var diff = 1;//0 = easy, 1=medium, 2=hard
var skin = 1;//0=wood, 1=3d, 2=stone
var skinpics = new Array();
skinpics[0] = false;
skinpics[1] = false;
skinpics[2] = false;
var idExt = '';

function startup() {
	startLoading();
	var pics = new Array("overlay.gif",
						"blank.gif",
						"loading.gif",
	 					"panel/img_1.1.jpg",
						"panel/img_2.1.jpg",
						"panel/img_3.1.jpg",
						"panel/img_4.1.jpg",
						"panel/img_4.2.jpg",
						"panel/img_4.21.jpg",
						"panel/img_4.3.jpg",
						"panel/img_4.31.jpg",
						"panel/img_4.4.jpg",
						"panel/img_5.1.jpg",
	 					"splash/img_1.1.jpg",
						"splash/img_1.2.jpg",
						"splash/img_1.3.jpg",
						"splash/img_2.1.jpg",
						"splash/img_2.2.jpg",
						"splash/img_2.3.jpg",
						"splash/img_3.1.jpg",
						"splash/img_3.2.jpg",
						"splash/img_3.21.jpg",
						"splash/img_3.3.jpg",
						"splash/img_3.31.jpg",
						"splash/img_3.4.jpg",
						"splash/img_3.41.jpg",
						"splash/img_3.5.jpg",
						"splash/img_4.1.jpg",
						"splash/img_4.2.jpg",
						"splash/img_4.3.jpg",
						"splash/img_5.1.jpg",
						"splash/img_5.2.jpg",
						"splash/img_5.21.jpg",
						"splash/img_5.22.jpg",
						"splash/img_5.3.jpg",
						"splash/img_6.1.jpg");
	picLoader = new Array();
	for(i = 0;i<pics.length;i++) {
		picLoader[i] = new Image();
		picLoader[i].src = pics[i];
	}
	document.getElementById('chess_splash').style.display = 'block';
	stopLoading();
	resume = readCookie('saveid');
	if(resume != null && resume != 0) {
		resumeGame();
	}
}
function doMoves() {
	if(movesCount >= moves.length) {
		stopLoading();
		movesCount = 0;
	}
	else {
		switch(moves[movesCount]) {
			case 'begin':
				clearTimeout(longerror);
				setTimeout("doMoves()", 1);
				break;
			case 'move':
				from = moves[++movesCount];
				to = moves[++movesCount];
        movePeice(from, to);
				break;
			case 'change':
				peice = moves[++movesCount];
				into = moves[++movesCount];
				changePeice(peice, into);
				setTimeout("doMoves()", 1000);
				break;
			case 'take':
				peice = moves[++movesCount];
				takePeice(peice);
				setTimeout("doMoves()", 1000);
				break;
			case 'end':
				info = moves[++movesCount];
				endGame(info);
				setTimeout("doMoves()", 1000);
				break;
			case 'level':
				level = moves[++movesCount];
				document.getElementById('chess_level').innerHTML = level;
				setTimeout("doMoves()", 1000);
				break;
		}
		movesCount++;
	}
}
function choosePeice(f, t) {
	promFrom = f;
	promTo = t;
	document.getElementById('chess_over').style.display = 'block';
	document.getElementById('chess_choose').style.display = 'block';
}
function movePeice(peiceId, newId) {
	setFromTo(peiceId, newId);
	after = newId;
	var peice1 = document.getElementById('pos'+idExt+peiceId);
	var peice2 = document.getElementById('pos'+idExt+newId);
	peice2.parentNode.removeChild(peice2);
	peice1.id = 'moving';
	peice1.style.top = fromY+'px';
	peice1.style.left = fromX+'px';
	peice1.style.width = fromW+'px';
	peice1.style.height = fromH+'px';
	newPeice = document.createElement('div');
	newPeice.id = 'pos'+idExt+peiceId;
	newPeice.innerHTML = '<img src="blank.gif" class="filler" alt="" />';
	peice1.parentNode.appendChild(newPeice);
	setTimeout("movePeiceTime()", 100);
}
var movePercent = 10;
var after;
function movePeiceTime() {
	if(movePercent >= 100) {
		movePercent = 10;
		var peice = document.getElementById('moving');
		peice.id = 'pos'+idExt+after;
		peice.style.top = toY+"px";
		peice.style.left = toX+"px";
		peice.style.width = toW+"px";
		peice.style.height = toH+"px";
		peice.style.zIndex = toZ;
		setTimeout("doMoves()", 1);
	}
	else {
		var peice = document.getElementById('moving');
		var top = fromY + ( (toY-fromY)*movePercent*0.01);
		var left = fromX + ( (toX-fromX)*movePercent*0.01);
		var width = fromW +( (toW-fromW)*movePercent*0.01);
		var height = fromH +( (toH-fromH)*movePercent*0.01);
		peice.style.top = top+"px";
		peice.style.left = left+"px";
		peice.style.width = width+"px";
		peice.style.height = height+"px";
		movePercent += 10;
		setTimeout("movePeiceTime()", 100);
	}
}
function takePeice(peiceId) {
	var peice = document.getElementById('pos'+idExt+peiceId);
	var rent = peice.parentNode;
	rent.removeChild(peice);
	newPeice = document.createElement('div');
	newPeice.innerHTML = '<img src="blank.gif" class="filler" alt="" />';
	newPeice.id = 'pos'+idExt+peiceId;
	rent.appendChild(newPeice);
}
function changePeice(peiceId, to) {
	var peice = document.getElementById('pos'+idExt+peiceId);
	peice.src = "skin"+skin+"/"+to+".gif";
}
function startLoading() {
	document.getElementById('chess_loading').style.display = 'block';
	loading = true;
}
function stopLoading() {
	document.getElementById('chess_loading').style.display = 'none';
	loading = false;
}
function highlight(peiceId) {
	var peice = document.getElementById('pos'+idExt+peiceId);
	peice.style.backgroundImage ="url(\"skin"+skin+"/highlight.gif\")";
}
function unhighlight(peiceId) {
	var peice = document.getElementById('pos'+idExt+peiceId);
	peice.style.backgroundImage = "";
}
function endGame(a, b) {
	document.getElementById('chess_over').style.display = 'block';
	var end = document.getElementById('chess_win_mess');
	switch(a) {
		case '0-1':
			end.innerHTML = 'Game Over';
			break;
		case '1-0':
			end.innerHTML = 'You Win';
			break;
		case '1/2-1/2':
			end.innerHTML = 'Draw';
			break;
	}
	end.parentNode.style.display = 'block';
	createCookie('saveid', 0, 5);
}
function showPanel() {
	document.getElementById('chess_over').style.width = "5px";
	if(skin == 1) {
		setup3d();
	}
	else {
		setup2d();
	}
	document.getElementById('chess_panel').style.display = 'block';
	document.getElementById('chess_board').style.display = 'block';
	document.getElementById('chess_over').style.display = 'none';
}
function setup3d() {
		document.getElementById('chess').style.width = "929px";
		document.getElementById('chess_panel').style.left="712px";
		document.getElementById('chess_panel').style.top="50px";
		document.getElementById('chess_board').style.width="712px";
		document.getElementById('chess_board').style.height="586px";
		document.getElementById('chess_board_img').style.top = "50px";
		document.getElementById('chess_board_img').style.width="712px";
		document.getElementById('chess_loading').style.left = "340px";
		document.getElementById('chess_choose').style.width="712px";
		document.getElementById('chess_quit').style.width="712px";
		document.getElementById('chess_win').style.width="712px";
		document.getElementById('chess_over').style.width="712px";
		document.getElementById('chess_over').style.height="586px";
		document.getElementById('chess_choose_peices').style.left="196px";
		idExt = '3d';
}
function setup2d() {
		document.getElementById('chess').style.width = "752px";
		document.getElementById('chess_panel').style.left="535px";
		document.getElementById('chess_panel').style.top="0";
		document.getElementById('chess_board').style.width="535px";
		document.getElementById('chess_board').style.height="536px";
		document.getElementById('chess_board_img').style.top = "0";
		document.getElementById('chess_board_img').style.width="535px";
		document.getElementById('chess_loading').style.left = "251px";
		document.getElementById('chess_choose').style.width="535px";
		document.getElementById('chess_quit').style.width="535px";
		document.getElementById('chess_win').style.width="535px";
		document.getElementById('chess_over').style.width="535px";
		document.getElementById('chess_over').style.height="536px";
		document.getElementById('chess_choose_peices').style.left="107px";
		idExt = '2d';
}
function hidePanel() {
	document.getElementById('chess_board').style.display = 'none';	
	document.getElementById('chess_panel').style.display = 'none';
	document.getElementById('chess').style.width = "487px";
	document.getElementById('chess_loading').style.left = "227px";
}
function loadNewGame() {
	document.getElementById('chess_splash').style.display = 'none';
	startLoading();
	if(skinpics[skin] == false) {
		var pics = new Array("skin"+skin+"/board.jpg",
							"skin"+skin+"/highlight.gif",
							"skin"+skin+"/bishop_w.gif",
							"skin"+skin+"/bishop_b.gif",
							"skin"+skin+"/king_w.gif",
							"skin"+skin+"/king_b.gif",
							"skin"+skin+"/knight_w.gif",
							"skin"+skin+"/knight_b.gif",
							"skin"+skin+"/pawn_w.gif",
							"skin"+skin+"/pawn_b.gif",
							"skin"+skin+"/queen_w.gif",
							"skin"+skin+"/queen_b.gif",
							"skin"+skin+"/rook_w.gif",
							"skin"+skin+"/rook_b.gif");
		var loaderOffset = picLoader.length;
		for(i = 0;i<pics.length;i++) {
			picLoader[loaderOffset+i] = new Image();
			picLoader[loaderOffset+i].src = pics[i];
		}
		skinpics[skin] = true;
	}
	startLoading();
	xajax_loadNewGame(diff, skin);
}
function resumeGame() {
	document.getElementById('chess_splash').style.display = 'none';
	startLoading();
	xajax_resume(resume);
}
function onPromClick(p) {
	document.getElementById('chess_over').style.display = 'none';
	document.getElementById('chess_choose').display = 'none';
	startLoading();
	promTo += p;
	startLoading();
	document.getElementById('chess_level').innerHTML = 'Thinking...';
	xajax_onMove(gameid, promFrom, promTo);
	clicked = '';
}
var longerror;
var xclicked;
var xid;
var xlongerror;
function onPlaceClick(id) {
	if(!loading) {
		if(clicked == '') {
			var x = document.getElementById('pos'+idExt+id);
			if(x.tagName == 'IMG') {
				clicked = id;
				highlight(id);
			}
		}
		else if(clicked == id) {
			document.getElementById('chess_level').innerHTML = level;
			unhighlight(clicked);
			clicked = '';
		}
		else {
			startLoading();
			document.getElementById('chess_level').innerHTML = 'Thinking...';
			unhighlight(clicked);
			xajax_onMove(gameid, clicked, id);
			xclicked = clicked;
			xid = id;
			xlongerror = false;
			longerror = setTimeout('toolongerror', 60000);
			clicked = '';
		}
	}
}
function toolongerror() {
	if(xlongerror == false) {
		xajax_onMove(gameid, xclicked, xid);
		longerror = setTimeout('toolongerror', 30000);
		xlongerror = true;
		document.getElementById('chess_log').innerHTML = "Comunication Error\n<br /> Trying again" + document.getElementById('chess_log').innerHTML;
	}
	else {
		document.getElementById('chess_log').innerHTML = "Error Again...reloading page..." + document.getElementById('chess_log').innerHTML;
		document.location = "http://onlychess.com/onlinechessgame/index.php";
	}
}
function startNewGame() {
	gameid = 0;
	document.getElementById('chess_board_peices').innerHTML = '';
	hidePanel();
	document.getElementById('chess_win').style.display = 'none';
	document.getElementById('chess_quit').style.display = 'none';
	document.getElementById('chess_splash').style.display = 'block';
	document.getElementById('chess_log').innerHTML = '';
	document.getElementById('chess_over').style.display = 'none';
	document.getElementById('chess_choose').style.display = 'none';
	createCookie('saveid', 0, 5);
}
function quit() {
	document.getElementById('chess_over').style.display = 'block';
	document.getElementById('chess_quit').style.display = 'block';
	document.getElementById('chess_win').style.display = 'none';
	document.getElementById('chess_choose').style.display = 'none';
}
function quitNo() {
	document.getElementById('chess_over').style.display = 'none';
	document.getElementById('chess_quit').style.display = 'none';
}
function showBoardAfter() {
	document.getElementById('chess_over').style.display = 'none';
	document.getElementById('chess_win').style.display = 'none';
	loading = true;
}
var fromX;
var fromY;
var fromW;
var fromH;
var fromZ;
var toX;
var toY;
var toW;
var toH;
var toZ;
function setFromTo(from, to) {
	fromEl = document.getElementById('pos'+idExt+from);
	fromZ = getStyle(fromEl, 'zIndex');
	fromX = fromEl.offsetLeft;
	fromY = fromEl.offsetTop;
	fromW = fromEl.offsetWidth;
	fromH = fromEl.offsetHeight;
	toEl = document.getElementById('pos'+idExt+to);
	toZ = getStyle(toEl, 'zIndex');
	toX = toEl.offsetLeft;
	toY = toEl.offsetTop;
	toW = toEl.offsetWidth;
	toH = toEl.offsetHeight;
}
function getStyle(el,styleProp) {
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}