// 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 pmove = new Array();
var cmove = 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_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 valid(x,y)
{
return (x >= 0 && x < 8 && y >= 0 && y < 8)
}

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

function is_other_color(color,x,y)
{
if (!valid(x,y)) return 0;
else if (get_spot(x,y) == "") { return 0; }
othercolor = document.getElementById(get_spot(x,y)).alt.substring(0,1);
if (othercolor == color) return 0;
light_square(x,y);
}

function is_my_rook(color,x,y)
{
if (!valid(x,y)) return 0;
else if (get_spot(x,y) == "") { return 0; }
othercolor = document.getElementById(get_spot(x,y)).alt;
type = othercolor.substring(1,2);
othercolor = othercolor.substring(0,1);
if (othercolor != color) return 0;
if (type != "r") return 0;
return 1;
}

function is_end(color,x,y)
{
if (!valid(x,y)) return 2;
else if (get_spot(x,y) == "") { light_square(x,y); return 0; }
othercolor = document.getElementById(get_spot(x,y)).alt.substring(0,1);
if (othercolor == color) return 2;
light_square(x,y);
return 1;
}

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

function unlight_board()
{
ter = 0;
one_lit = 0;
for(i=0;i<8;i++)
	{
	for(j=0;j<8;j++)
		{
		ter++;
		bid = get_square(i,j);
		curclass = bid.className;
		if (ter % 2) bid.className = "dark";
		else bid.className = "white";
		}
	ter++;
	}
}

function red_square(x,y)
{
spot = get_square(x,y);
spot.className = "red";
}

function comp_move(x,y)
{
spot = get_square(x,y);
spot.className = "compMove";
}

function light_square(x,y)
{
if (!valid(x,y)) return;
spot = get_square(x,y)
curclass = spot.className;
if (curclass == "dark") spot.className = "darkh";
else if (curclass == "white") spot.className = "whiteh";
one_lit = 1;
}


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;

return Math.floor(code / 8);
}

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;

return code % 8;
}

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');

use_gif = (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 6.0") >= 0);

if (use_gif)
	newp.src = "../boardimg/small/" +  id + ".gif";
else
	newp.src = "../boardimg/small/" +  id + ".png";
	

newp.alt = id;
newp.className = 'dg';
newp.id = 'p' + counter;
counter++;
document.body.appendChild(newp);
return newp.id;
}

function set_init_board()
{
clear_board();
reset_board();
hidehints();
hint_counter = 0;
var bib = ib;

orient = bib.charAt(0);

bib = bib.substring(1);

for(i=0;i<8;i++)
	for(j=0;j<8;j++)
	{
	cur = bib.substring(0,2);
	bib = bib.substring(2);	
	if (cur != "--")
		set_spot(i,j,make_new(cur));	
	}

if (m.length <= 2)
	return;

mcounter = 0;
for(i=0;i<m.length;i+=4)
	{
	pmove[mcounter] = m.substring(i,i+2);
	cmove[mcounter++] = m.substring(i+2,i+4);
	}
total_moves = mcounter;

tempx = pmove[0].charAt(0);

first_move = get_spot(get_x(tempx),get_y(tempx));
first_move = document.getElementById(first_move).alt.charAt(0);

first_color = first_move;

for(i=0;i<8;i++)
	for(j=0;j<8;j++)
	{
	if (get_spot(i,j) != "")
		{
		curname = document.getElementById(get_spot(i,j)).alt.charAt(0)
		if (curname == first_move)
			{
			document.getElementById(get_spot(i,j)).style.display = "none";
			set_spot(i,j,make_new(document.getElementById(get_spot(i,j)).alt));
			}
		}
	}



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

switch_counter = 0;

fix_resize();
}

function init()
{
locateboard();
set_init_board();
unlight_board();
move_num = 0;

if (total_moves > 1)
	setstatus(total_moves + " moves left...");
else
	setstatus("1 move left...");

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 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];
}


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 + 20*i;
			tobj.style.top = boardy +  20*j;		
			}
		}

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 / 20);
}

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

return Math.floor(oy / 20);
}

function movemouse(e)
{

  if (isdrag)
  {

    

    scrolly = nn6 ? window.pageYOffset : document.body.scrollTop;
    scrollx = nn6 ? window.pageXOffset : document.body.scrollLeft;

    mx = nn6 ? e.clientX : event.clientX;
    my = nn6 ? e.clientY : event.clientY;

    my += scrolly;
    mx += scrollx;  
 
    if (mx > boardx && mx < boardx + 320 && my > boardy && my <= boardy + 320)
	{
		fixx = -boardx + mx - 10;
		fixy = -boardy + my - 10;

		row = 0; col = 0;		
		if (fixx < 0)
			col = 0;
		else
			{
			modx = fixx % 20;
			if (modx < 10)
				col = Math.floor(fixx / 20);
			else
				col = Math.floor(fixx / 20) + 1;
			}		

		if (fixy < 0)
			row = 0;
		else
			{
			mody = fixy % 20;
			if (mody < 10)
				row = Math.floor(fixy / 20);
			else
				row = Math.floor(fixy / 20) + 1;
			}	
	
			dobj.style.left = (col * 20) + boardx;	
			dobj.style.top = (row * 20) + boardy;	
	}

    return false;
  }
}

function highlight_board(type,x,y)
{
color = type.substring(0,1);
type = type.substring(1,2);

if (color != first_color) return;

red_square(x,y);

is_end(color,x,y);

if (type == "r" || type == "q")
	{
	for(i = x+1; i < 8; i++)
		{
		if (is_end(color,i,y) > 0)
			break;
		}	
	for(i = x - 1; i >= 0;i--)
		{
		if (is_end(color,i,y) > 0)
			break;
		}
	for(i = y - 1; i >= 0; i--)
		{
		if (is_end(color,x,i) > 0)
			break;
		}
	for(i = y + 1; i < 8; i++)
		{
		if (is_end(color,x,i) > 0)
			break;
		}
	}

if (type == "b" || type == "q")
	{
	for(i = 1;i < 8; i++)
		{
		if(is_end(color,x-i,y-i) > 0 )
			break;
		}
	for(i = 1;i < 8; i++)
		{
		if(is_end(color,x+i,y+i) > 0 )
			break;
		}
	for(i = 1;i < 8; i++)
		{
		if(is_end(color,x-i,y+i) > 0 )
			break;
		}
	for(i = 1;i < 8; i++)
		{
		if(is_end(color,x+i,y-i) > 0 )
			break;
		}
	}

if (type == "n")
	{
	is_end(color,x+2,y+1);
	is_end(color,x-2,y+1);
	is_end(color,x-2,y-1);
	is_end(color,x+2,y-1);
	is_end(color,x+1,y+2);
	is_end(color,x-1,y+2);
	is_end(color,x-1,y-2);
	is_end(color,x+1,y-2);
	}

if (type == "p" && color == "b")
	{
	if (orient == "1")
		{
		if (get_spot(x,y+1) == "")
			light_square(x,y+1);
		if (y == 1 && get_spot(x,y+2) == "" && get_spot(x,y+1) == "")
			is_end(color,x,y+2);
		is_other_color(color,x+1,y+1);
		is_other_color(color,x-1,y+1);
		}
	if (orient == "0")
		{
		if (get_spot(x,y-1) == "")
			light_square(x,y-1);
		if (y == 6 && get_spot(x,y-2) == "" && get_spot(x,y-1) == "")
			is_end(color,x,y-2);
		is_other_color(color,x+1,y-1);
		is_other_color(color,x-1,y-1);
		}
	}

if (type == "p" && color == "w")
	{
	if (orient == "0")
		{
		if (get_spot(x,y+1) == "")
			light_square(x,y+1);
		if (y == 1 && get_spot(x,y+2) == "" && get_spot(x,y+1) == "")
			is_end(color,x,y+2);
		is_other_color(color,x+1,y+1);
		is_other_color(color,x-1,y+1);
		}
	if (orient == "1")
		{
		if (get_spot(x,y-1) == "")
			light_square(x,y-1);
		if (y == 6 && get_spot(x,y-2) == "" && get_spot(x,y-1) == "")
			is_end(color,x,y-2);
		is_other_color(color,x+1,y-1);
		is_other_color(color,x-1,y-1);
		}
	}

if (type == "k")
	{
	for(i=-1;i<=1;i++)
		for(j=-1;j<=1;j++)
			is_end(color,x+i,y+j)	
	

	if (orient == "1" && x == 4 && (y == 0 || y == 7))
		{
		if (get_spot(x+1,y) == "" && get_spot(x+2,y) == "" && is_my_rook(color,x+3,y))
			light_square(x+2,y); 
		if (get_spot(x-1,y) == "" && get_spot(x-2,y) == "" && get_spot(x-3,y) == "" && is_my_rook(color,x-4,y))
			light_square(x-2,y);
		}

	if (orient == "0" && x == 3 && (y == 0 || y == 7))
		{
		if (get_spot(x-1,y) == "" && get_spot(x-2,y) == "" && is_my_rook(color,x-3,y))
			light_square(x-2,y); 
		if (get_spot(x+1,y) == "" && get_spot(x+2,y) == "" && get_spot(x+3,y) == "" && is_my_rook(color,x+4,y))
			light_square(x+2,y);
		}
	}

}


function selectmouse(e) 
{

if (isdrag == true)
	return false;

  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = "HTML";

  while (fobj.tagName != topelement && fobj.className != "dg")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dg")
  {
    unlight_board();
    isdrag = true;
    dobj = fobj;
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    sx = getcol(fobj);
    sy = getrow(fobj);
    document.onmousemove=movemouse;
    highlight_board(fobj.alt,sx,sy);
    if (one_lit == 0)
	{
	unlight_board();
	isdrag = false;
	}
    return false;
  }
}

function killdrag(e)
{

if (isdrag == false)
	return false;

unlight_board();

fobj = dobj;

type = fobj.alt.substring(1,2);
color = fobj.alt.substring(0,1);
  
endx = getcol(fobj);
endy = getrow(fobj);
isdrag = false;

old_spot = false;

if (endx < 0 || endx > 7 || endy < 0 || endy > 7)
	old_spot = true;
	
var move_checker = generate_char(sx,sy) + generate_char(endx,endy);

//var side = (endx == sx || endy == sy);
//var diag = (Math.abs(endx - endy) == Math.abs(sx - sy) || (endx + endy == sx + sy));

if (pmove[move_num] == move_checker && !old_spot)
	{


		if (type == 'k' && (endx - sx) == 2)
			{
			set_spot(endx-1,endy,get_spot(7,endy));
			set_spot(7,endy,"");
			}			

		if (type == 'k' && (sx - endx) == 2)
			{
			set_spot(endx+1,endy,get_spot(0,endy));
			set_spot(0,endy,"");
			}			

		if (type == "p" && color == "w")
		{
			if (orient == "0")
			{
				if (endy == 7)
					fobj.src = "../boardimg/small/wq.gif";
			}
			if (orient == "1")
			{
				if (endy == 0)
					fobj.src = "../boardimg/small/wq.gif";
			}
		}
		if (type == "p" && color == "b")
		{
			if (orient == "1")
			{
				if (endy == 7)
					fobj.src = "../boardimg/small/bq.gif";
			}
			if (orient == "0")
			{
				if (endy == 0)
					fobj.src = "../boardimg/small/bq.gif";
			}
		}



		if (move_num == total_moves - 1)
			{
			if (get_spot(endx,endy) != "")
				document.getElementById(get_spot(endx,endy)).style.display = "none";		

			set_spot(endx,endy,get_spot(sx,sy));		
			set_spot(sx,sy,"");
			setstatus(em);
			fix_resize();
			return false;
			}

		var mx,my,nx,ny;
		mx = get_x(cmove[move_num].charAt(0));
		my = get_y(cmove[move_num].charAt(0));
		nx = get_x(cmove[move_num].charAt(1));
		ny = get_y(cmove[move_num].charAt(1));

		if (get_spot(endx,endy) != "")
			document.getElementById(get_spot(endx,endy)).style.display = "none";		
	
		set_spot(endx,endy,get_spot(sx,sy));		
		set_spot(sx,sy,"");
		if (get_spot(nx,ny) != "")
			document.getElementById(get_spot(nx,ny)).style.display = "none";		
	
		light_square(mx,my);	
		set_spot(nx,ny,get_spot(mx,my));
		set_spot(mx,my,"");

		cobj = document.getElementById(get_spot(nx,ny));
	
		type = cobj.alt.substring(1,2);
		color = cobj.alt.substring(0,1);

		if (type == "p" && color == "b")
		{
			if (orient == "0")
			{
				if (ny == 0)
					cobj.src = "../boardimg/small/bq" + cobj.src.substring(cobj.src.length - 4);
			}
			if (orient == "1")
			{
				if (ny == 7)
					cobj.src = "../boardimg/small/bq" + cobj.src.substring(cobj.src.length - 4);
			}
		}
		if (type == "p" && color == "w")
		{
			if (orient == "1")
			{
				if (ny == 0)
					cobj.src = "../boardimg/small/wq" + cobj.src.substring(cobj.src.length - 4);
			}
			if (orient == "0")
			{
				if (ny == 7)
					cobj.src = "../boardimg/small/wq" + cobj.src.substring(cobj.src.length - 4);
			}
		}



	
		left = total_moves - move_num - 1;
		if (left != 1)	
			setstatus(left + " moves left...");
		else
			setstatus("1 move left...");
		
		move_num++;
		old_spot = false;
	}
else
	{
	set_spot(sx,sy,fobj.id);
	}


light_square(nx,ny);
fix_resize();
calculateboard();

return false;
}

function hidehints()
{
//document.getElementById('h1').style.display = "none";
//document.getElementById('h2').style.display = "none";
}

function showhint()
{
hint_counter++;
if(hint_counter > 2)
	return;

document.getElementById('h' + hint_counter).style.display = "block"; 

fix_resize();
}

function setstatus(text)
{
//if (text != em)
//	text = text + '<a href = \"javascript:void(\'\')\" onclick=\"showhint()\">[Need a hint?]</a>';

nn6 ? document.getElementById("status").innerHTML = text : document.all.status.innerHTML = text;

}

window.onresize = fix_resize;
document.onmousedown=selectmouse;
document.onmouseup=killdrag;


