// Copyright 2007 Chess-Videos.com
// Author: Dan Birken
// Do not reproduce any code from this file without permission from author

var ie=document.all;
var nn6=document.getElementById&&!document.all;

var isdrag=false;
var x,y;
var dobj;
var switch_counter = 0;

var boardx=0,boardy=0;
var counter=1;

var sx=-1,sy=-1;

var boardv = new Array();
var output = new Array();

var move = new Array();
var movecapture = new Array();
var pgn_move = new Array();
var move_num = 0;
var total_moves = -1;
var hint_counter = 0;
var one_lit = 0;

var first_color;

var orient;

for(i=0;i<64;i++)
	{
	boardv[i] = "";
	output[i] = "";
	}

function set_raw(spot, val) {
  boardv[spot] = val;
}

function set_spot(x,y,val)
{
boardv[x * 8 + y] = val;
}

function get_spot(x,y)
{
if (!valid(x,y)) return "ER";
return boardv[x*8 + y];
}

function get_raw(raw)
{
return boardv[raw];
}

function valid(x,y)
{
return (x >= 0 && x < 8 && y >= 0 && y < 8)
}

function get_value(x,y)
{
return y*8+x;
}

function get_square(x,y)
{
spot = 'b' + get_value(x,y);
return document.getElementById(spot);
}

function generate_char(x,y)
{
val = 48 + x*8 + y;
if (val == 96) val = 124;
if (val == 92) val = 123;
return String.fromCharCode(val);
}


function get_x(val)
{
var str = new String;
str += val;
code = str.charCodeAt(0); 

if (code == 124) code = 96;
if (code == 123) code = 92;
code = code - 48;

retval = Math.floor(code / 8);

if ((switch_counter % 2) == 1)
	return 7 - retval;

return retval;

}

function get_y(val)
{
var str = new String;
str += val;
code = str.charCodeAt(0);

if (code == 124) code = 96;
if (code == 123) code = 92;
code = code - 48;

retval = code % 8;

if ((switch_counter % 2) == 1)
	return 7 - retval;

return retval;
}

function set_output(x,y,val)
{
output[x * 8 + y] = val;
}

function reset_board()
{
for(i = 0; i < 64;i++)
	{
	boardv[i] = "";
	output[i] = "--";
	}
}

function make_new(id)
{
var newp = document.createElement('img');
newp.src = "../boardimg/" +  id + ".gif";
newp.alt = id;
newp.className = 'dg';
newp.id = 'p' + counter;
counter++;
document.body.appendChild(newp);
return newp.id;
}

function flip_board()
{
var temp = new Array(64);

cc = 0;
for(i = 0; i < 8; i++)
	for(j = 0; j < 8; j++)
		{
		temp[cc++] = get_spot(i,j);	
		}

cc = 0;
for(i = 7; i >=0; i--)
	for(j =7; j>=0;j--)
		{
		set_spot(i,j,temp[cc++]);
		}

switch_counter++;

if ((switch_counter % 2) == 1)	
	{
	document.getElementById('sideimage').src = "../boardimg/123.png";
	document.getElementById('bottomimage').src = "../boardimg/cba.png";
	}
else
	{
	document.getElementById('sideimage').src = "../boardimg/321.png";
	document.getElementById('bottomimage').src = "../boardimg/abc.png";
	}


fix_resize();

calculateboard();

}

function set_init_board()
{
clear_board();
reset_board();

for(i = 0; i < 8; i++)
	{
	set_spot(i,6,make_new("wp"));
	set_spot(i,1,make_new("bp"));
	}

set_spot(0,0,make_new("br"));
set_spot(7,0,make_new("br"));
set_spot(0,7,make_new("wr"));
set_spot(7,7,make_new("wr"));

set_spot(1,0,make_new("bn"));
set_spot(6,0,make_new("bn"));
set_spot(1,7,make_new("wn"));
set_spot(6,7,make_new("wn"));

set_spot(2,0,make_new("bb"));
set_spot(5,0,make_new("bb"));
set_spot(2,7,make_new("wb"));
set_spot(5,7,make_new("wb"));

set_spot(3,0,make_new("bq"));
set_spot(3,7,make_new("wq"));
set_spot(4,0,make_new("bk"));
set_spot(4,7,make_new("wk"));

switch_counter = 0;

if (typeof(mp) != "undefined") {
  for(var piece in mp) {
    document.getElementById(get_raw(mp[piece])).style.display = "none";		
    set_raw(mp[piece], "");
  }
}

fix_resize();
}

function init()
{
locateboard();
set_init_board();
move_num = 0;
setstatus("-");
cnt = 0;
var wmm = wm;
while(wmm.length > 0)
	{
	if (wmm.charAt(0) == "!")
		{
		move[cnt] = wmm.substring(0,5);
		wmm = wmm.substring(5);
		}
	else if (wmm.charAt(0) == "#")
		{
		move[cnt] = wmm.substring(0,4);
		wmm = wmm.substring(4);
		}
	else if (wmm.charAt(0) == "&")
		{
		move[cnt] = wmm.substring(0,4);
		wmm = wmm.substring(4);
		}
	else
		{
		move[cnt] = wmm.substring(0,2);
		wmm = wmm.substring(2);
		}
	cnt++;
	}

pgn_move = pgn.split(" ");


total_moves = cnt;
fix_resize();
}

function unprocess(cur_move,promote)
{
x1 = get_x(cur_move.charAt(0));
y1 = 7 - get_y(cur_move.charAt(0));
x2 = get_x(cur_move.charAt(1));
y2 = 7 - get_y(cur_move.charAt(1));

if (typeof (movecapture[move_num-1]) != "undefined")
	{
	document.getElementById(movecapture[move_num-1]).style.display = "inline";		
	set_spot(x1,y1,get_spot(x2,y2));
	set_spot(x2,y2,movecapture[move_num-1]);
	}
else
	{
	set_spot(x1,y1,get_spot(x2,y2));
	set_spot(x2,y2,"");
	}

if(promote == true)
	{
	pimg = document.getElementById(get_spot(x1,y1));
	clr = pimg.src.charAt(pimg.src.length - 6);
	if (clr == "w")
		pimg.src = "http://www.chessvideos.tv/boardimg/wp.gif";
	else
		pimg.src = "http://www.chessvideos.tv/boardimg/bp.gif";
	}
}

function process(cur_move,change)
{
x1 = get_x(cur_move.charAt(0));
y1 = 7 - get_y(cur_move.charAt(0));
x2 = get_x(cur_move.charAt(1));
y2 = 7 - get_y(cur_move.charAt(1));

if (get_spot(x2,y2) != "")
	{
	movecapture[move_num] = get_spot(x2,y2);
	document.getElementById(get_spot(x2,y2)).style.display = "none";		
	}


set_spot(x2,y2,get_spot(x1,y1));
set_spot(x1,y1,"");

if (change == "Q" || change == "R" || change == "B" || change == "N")
	{
	pimg = document.getElementById(get_spot(x2,y2));
	clr = pimg.src.charAt(pimg.src.length - 6);
	change = change.toLowerCase();
	pimg.src = "http://www.chessvideos.tv/boardimg/" + clr + "" + change + ".gif";
	}
}


function kill_spot(location)
{
x1 = get_x(location);
y1 = 7 - get_y(location);
movecapture[move_num] = get_spot(x1,y1);
document.getElementById(get_spot(x1,y1)).style.display = "none";		
set_spot(x1,y1,"");
}

function move_back()
{
if (move_num <= 0)
	return;

cur_move = move[move_num - 1];


if (cur_move.charAt(0) == "!")
	{
 	unprocess(cur_move.substring(3));
	unprocess(cur_move.substring(1));
	}
else if (cur_move.charAt(0) == "#")
	{
	unprocess(cur_move.substring(1),true);
	}
else if (cur_move.charAt(0) == "&")
	{
	unprocess(cur_move.substring(1));
	process(cur_move.substring(2));
	}	
else
	unprocess(cur_move);

if (move_num > 1)
	setstatus("",move_num-2);
else
	setstatus("-");

move_num--;

fix_resize();
}

function move_end()
{
while(move_num < total_moves)
	{
	move_forward();
	}
}

function move_i(val) {
  if (val >= 0) {
    for (var i=0; i<val; i++) {
      if (move_num < total_moves) {
        move_forward();
      }
    }
  } else {
    for (var i=0; i<-val; i++) {
      if (move_num > 0) {
        move_back();
      }
    }
  }
}

function move_forward()
{
if (move_num == total_moves)
	{
	setresult(gameresult);
	return;
	}

cur_move = move[move_num];
setstatus("",move_num);
if (cur_move.charAt(0) == "!")
	{
	process(cur_move.substring(1));
	process(cur_move.substring(3));
	}
else if (cur_move.charAt(0) == "#")
	{
	process(cur_move.substring(1),cur_move.substring(3));
	}
else if (cur_move.charAt(0) == "&")
	{
	process(cur_move.substring(1));
	kill_spot(cur_move.substring(3));
	}
else
	process(cur_move);
move_num++;

fix_resize();
}

function locateboard()
{
var bobj = document.getElementById("board");
boardx = bobj.offsetLeft;
boardy = bobj.offsetTop;
}

function clear_board()
{
reset_board();
for(i = 1; i < counter; i++)
	{
	var killobj = document.getElementById('p' + i);
	document.body.removeChild(killobj);	
	}
counter = 1;
calculateboard();
}

function switch_board()
{
var temp = new Array(64);

cc = 0;
for(i = 0; i < 8; i++)
	for(j = 0; j < 8; j++)
		{
		temp[cc++] = get_spot(i,j);	
		}

cc = 0;
for(i = 7; i >=0; i--)
	for(j =7; j>=0;j--)
		{
		set_spot(i,j,temp[cc++]);
		}

switch_counter++;

if ((switch_counter % 2) == 1)	
	{
	document.getElementById('sideimage').src = "../boardimg/123.png";
	document.getElementById('bottomimage').src = "../boardimg/cba.png";
	}
else
	{
	document.getElementById('sideimage').src = "../boardimg/321.png";
	document.getElementById('bottomimage').src = "../boardimg/abc.png";
	}


fix_resize();

calculateboard();
}

function calculateboard()
{
reset_board();

for(i = 1; i < counter; i++)
	{
	var cobj = document.getElementById('p' + i);	
 	
	// Now go through the board and find out where they are all located (so we can do something with it)
	if (onboard(cobj) == true && cobj.style.display != "none")
		{
		px = getcol(cobj);
		py = getrow(cobj);
		set_spot(px,py,cobj.id);
		tempstr = cobj.src.substring(cobj.src.length - 6,cobj.src.length - 4);
		set_output(px,py,tempstr);
		}

	}



var outputstr = "";
for(i = 0; i < 64;i++)
	outputstr += output[i];

if (document.getElementById('boardval'))
	document.getElementById('boardval').value = outputstr;

if (document.getElementById('gridlayout'))
	document.getElementById('gridlayout').value = switch_counter % 2; 
}


function fix_resize()
{
locateboard();
for(i=0;i<8;i++)
	for(j=0;j<8;j++)
		{
		if(get_spot(i,j) != "")
			{
			tobj = document.getElementById(get_spot(i,j));
			tobj.style.left = boardx + 40*i + "px";
			tobj.style.top = boardy +  40*j + "px";		
			}
		}

calculateboard();
}

function onboard(obj)
{
ox = obj.offsetLeft;
oy = obj.offsetTop;
return (ox >= boardx && ox < boardx + 300 && oy >= boardy && oy < boardy + 300);
}

function getcol(obj)
{
ox = obj.offsetLeft;
ox = ox - boardx;

return Math.floor(ox / 40);
}

function getrow(ob)
{
oy = ob.offsetTop;
oy = oy - boardy;

return Math.floor(oy / 40);
}

function setstatus(text,num)
{
if (text == "-")
	{
	text = "-";
	nn6 ? document.getElementById("status").innerHTML = text : document.all.status.innerHTML = text;
	}

text = "<table>";
if (num < 24)
	{
	for(i=0;i<=num;i++)
		{
		if (i % 8 == 0)
			text += "<tr>";
		if (i != num)
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> " + pgn_move[i];
			else
				text += "<td class = move>" + pgn_move[i] + "  ";
			}
		else
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> <b>" + pgn_move[i] + "</b>";
			else
				text += "<td class = move><b>" + pgn_move[i] + "</b>  ";
			}
		}
	}
else
	{
	val = 16 + num % 8;
	for(i=num - val; i <= num;i++)
		{
		if (i%8 == 0)
			text += "<tr>";
		if (i != num)
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> " + pgn_move[i];
			else
				text += "<td class = move>" + pgn_move[i] + "";
			}
		else
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> <b>" + pgn_move[i] + "</b>";
			else
				text += "<td class = move><b>" + pgn_move[i] + "</b>";
			}
		}
	}
text += "</table>";
nn6 ? document.getElementById("status").innerHTML = text : document.all.status.innerHTML = text;
}

function setresult(text)
{
result = text;

num = move_num-1;

text = "<table>";
if (num < 16)
	{
	for(i=0;i<=num;i++)
		{
		if (i % 8 == 0)
			text += "<tr>";
		if (i != num)
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> " + pgn_move[i];
			else
				text += "<td class = move>" + pgn_move[i] + "  ";
			}
		else
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> <b>" + pgn_move[i] + "</b>";
			else
				text += "<td class = move><b>" + pgn_move[i] + "</b>  ";
			}
		}
	}
else
	{
	val = 8 + num % 8;
	for(i=num - val; i <= num;i++)
		{
		if (i%8 == 0)
			text += "<tr>";
		if (i != num)
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> " + pgn_move[i];
			else
				text += "<td class = move>" + pgn_move[i] + "";
			}
		else
			{
			if (i % 2 == 0)
				text += "<td class = move><b>" + ((i / 2) + 1) + ".</b> <b>" + pgn_move[i] + "</b>";
			else
				text += "<td class = move><b>" + pgn_move[i] + "</b>";
			}
		}
	}
text += "<tr><td colspan = 8 class = move><b>Result: " + result;
text += "</table>";
nn6 ? document.getElementById("status").innerHTML = text : document.all.status.innerHTML = text;
}

window.onresize = fix_resize;

