// 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 ie7 =document.documentElement && typeof document.documentElement.style.maxHeight!="undefined";

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

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

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

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)
{
return boardv[x*8 + y];
}


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

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

function make_new(id)
{
var baseobj = document.getElementById('a' + id);
var newp = document.createElement('img');
newp.src = baseobj.src;
newp.className = 'dg';
newp.id = 'p' + counter;
newp.style.left = baseobj.style.left;
newp.style.top = baseobj.style.top;
counter++;
document.getElementById("board").appendChild(newp);
return newp.id;
}


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

for(i = 0;i < 8;i++)
	{
	set_spot(i,1,make_new(5));
	set_spot(i,6,make_new(11));
	}
for(i = 0;i < 2;i++)
	{
	set_spot(Math.abs(7*i-0),7,make_new(8));
	set_spot(Math.abs(7*i-1),7,make_new(10));
	set_spot(Math.abs(7*i-2),7,make_new(9));
	set_spot(Math.abs(7*i-0),0,make_new(2));
	set_spot(Math.abs(7*i-1),0,make_new(4));
	set_spot(Math.abs(7*i-2),0,make_new(3));
	}

set_spot(3,0,make_new(1));
set_spot(4,0,make_new(0));
set_spot(3,7,make_new(7));
set_spot(4,7,make_new(6));


document.getElementById('sideimage').src = "http://www.chessvideos.tv/boardimg/321.png";
document.getElementById('bottomimage').src = "http://www.chessvideos.tv/boardimg/abc.png";
switch_counter = 0;

fix_resize();
}

function load_board(data)
{
clear_board();
reset_board();

for(i = 0; i < 64; i++)
	{
	first = data.substring(0,2);
	data = data.substring(2);
	x = Math.floor(i/8);
	y = i % 8;

	if_set(first,"bp",5,x,y);
	if_set(first,"bq",1,x,y);
	if_set(first,"bk",0,x,y);
	if_set(first,"bb",3,x,y);
	if_set(first,"bn",4,x,y);
	if_set(first,"br",2,x,y);

	if_set(first,"wp",11,x,y);
	if_set(first,"wq",7,x,y);
	if_set(first,"wk",6,x,y);
	if_set(first,"wb",9,x,y);
	if_set(first,"wn",10,x,y);
	if_set(first,"wr",8,x,y);
	}

}

function if_set(val,thing,num,x,y)
{
if (val == thing)
	set_spot(x,y,make_new(num));
}

function init()
{
set_init_board();
fix_resize();
}

function init(val,layout)
{
if (val == "")
	set_init_board();
else
	load_board(val);

if (layout == "1")
	{
	switch_counter = 1;
	document.getElementById('sideimage').src = "boardimg/123.png";
	document.getElementById('bottomimage').src = "boardimg/cba.png";
	}

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.getElementById("board").removeChild(killobj);	
	}
counter = 1;
calculateboard();
}

function switch_board()
{
/*for(i = 1; i < counter; i++)
	{
	var sobj = document.getElementById('p' + i);
	var color = sobj.src;
	c = color.substring(color.length-6,color.length-5);	
	var newsrc = "http://www.chess-videos.com/img/";
	// This fucks up the obfuscator, so after doing that make sure to replace "REPLACEME" with the URL in the above line
	//var newsrc = "REPLACEME";
	if (c == 'b')
		newsrc += 'w';
	else if (c == 'w')
		newsrc += 'b';
	newsrc += color.substring(color.length-5);
	sobj.src = newsrc;	
	}
*/
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)
		{
		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];


var fen = "";
var nocount = 0;
for(j = 0; j < 8; j++)
	{
		for(i = 0; i < 8; i++)
		{
		if (get_output(i,j) == "--")
			nocount++;
		else
			{
			if (nocount > 0)
				fen += nocount;
			color = get_output(i,j).charAt(0);
			if (color == "w")
				piece = get_output(i,j).charAt(1).toUpperCase();
			else
				piece = get_output(i,j).charAt(1);
			fen += piece;
			nocount = 0;
			}
		}
		if (nocount > 0)
			fen += nocount;
		if (j != 7)
			fen += "/";
		nocount = 0;
	}


document.getElementById("boardata").value = outputstr;
document.getElementById("griddata").value = switch_counter % 2;
document.getElementById("fenstring").value = fen;
document.getElementById("fenstring2").value = fen;
}


function fix_resize()
{
positionPieces();
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 = 40*i + "px";
			tobj.style.top = 40*j + "px";		
			}
		}

calculateboard();
}

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

function getcol(obj)
{
ox = obj.offsetLeft;
return Math.floor(ox / 40);
}

function getrow(ob)
{
oy = ob.offsetTop;
return Math.floor(oy / 40);
}

function positionPieces()
{
locateboard();

for(i = 0;i<12;i++)
	{
	var pobj = document.getElementById('a' + i);

	if (i < 6)
		pobj.style.left = boardx + 390 + "px";
	else
		pobj.style.left = boardx + 440 + "px";
	
	pobj.style.top = boardy + 15 + (i % 6) * 50 + "px";
	}

}

function movemouse(e)
{

  if (isdrag)
  {
    calculateboard();


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

    if (!nn6)
	{
	scrolly = document.documentElement.scrollTop;
	scrollx = document.documentElement.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 - 20;
		fixy = -boardy + my - 20;

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

		if (fixy < 0)
			row = 0;
		else
			{
			mody = fixy % 40;
			if (mody < 20)
				row = Math.floor(fixy / 40);
			else
				row = Math.floor(fixy / 40) + 1;
			}	
		if (get_spot(col,row) == "")
		{
			dobj.style.left = (col * 40) + "px";	
			dobj.style.top = (row * 40) + "px";	
		}
	}

   else
	{
	    dobj.style.left = mx - 20 - boardx  + "px";
	    dobj.style.top = my - 20 - boardy  + "px";
	}
    return false;
  }
}

function selectmouse(e) 
{

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

  if (ie7)
	topelement = "HTML";

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

  var id = fobj.id;
  if (id.charAt(0) == 'a')
	{
	  var newp = document.createElement('img');
	  newp.src = fobj.src;
	  newp.className = 'dg';
	  newp.id = 'p' + counter;
//	  newp.style.left = fobj.style.left;  
//	  newp.style.top = fobj.style.top;
	  newp.style.position = 'absolute';
          counter++;
//	  document.body.appendChild(newp);
	  document.getElementById("board").appendChild(newp);
	  fobj =	 newp;
	}



  if (fobj.className=="dg")
  {
    isdrag = true;
    dobj = fobj;
    //tx = parseInt(dobj.style.left+0);
    //ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }
}

function killdrag(e)
{
isdrag = false;
}

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

