GlobalPly = 0;
Boards = new Array();
ActFrom = new Array();
ActTo = new Array();
Status = 'Auto';
UserClicks = 0;
UserAction = new Array();
StartT = 0;
StopT = 0;
AccuT = 0;

function moveindication(Ply)
{
    if(Ply==PlyUBound-1)
    {
        document.panel0.src = 'board/blank.gif';
        document.panel7.src = 'board/blank.gif';
        return;
    }
    if(FlipBoard == 'w')
    {
        if(Boards[Ply].charAt(65) == 'w')
        {
            document.panel0.src = 'board/whiteanim.gif';
            document.panel7.src = 'board/black.gif';
        }
        else
        {
            document.panel7.src = 'board/blackanim.gif';
            document.panel0.src = 'board/white.gif';
        }
    }
    else
    {
         if(Boards[Ply].charAt(65) == 'w')
         {
             document.panel0.src = 'board/black.gif';
             document.panel7.src = 'board/whiteanim.gif';
         }
         else
         {
             document.panel0.src = 'board/blackanim.gif';
             document.panel7.src = 'board/white.gif';
         }
    }
}

function setposition(Ply)
{
    var statement;
    var i, fi;

    document.getElementById('p' + GlobalPly).className = 'grey';

    for(i=0;i<64;i++)
    {
        if(FlipBoard == 'w')fi = 63-i;
        else fi=i;

        document['f' + fi].src = getimage(Boards[Ply].charAt(i),0);
    }
    GlobalPly = Ply;
    document.getElementById('p' + GlobalPly).className = 'current';
}

function about()
{
     document.write('cts javascript version 1.22 2005-02-19');
}

function start()
{
     window.setTimeout('movebegin(0)',6000);
}

function movebegin(ply)
{
    from = ActFrom[ply];
    to = ActTo[ply];

    fromImage = getimage(Boards[ply].charAt(from),1);
    toImage = getimage(Boards[ply].charAt(to),1);

    gfrom=getGUIIndex(from);
    gto=getGUIIndex(to);

    document['f' + gfrom].src = fromImage;
    document['f' + gto].src = toImage;

    window.setTimeout('moveinner(' + ply + ')',500);
}

function moveinner(ply)
{
    from = ActFrom[ply];
    to = ActTo[ply];

    fromImage = getimage(Boards[ply+1].charAt(from),1);
    toImage = getimage(Boards[ply+1].charAt(to),1);

    gfrom=getGUIIndex(from);
    gto=getGUIIndex(to);

    document['f' + gfrom].src = fromImage;
    document['f' + gto].src = toImage;

    window.setTimeout('moveend(' + ply + ')',500);
}

function getGUIIndex(Index)
{
    if(FlipBoard == 'w')return 63-Index;
    else return Index;
}

function moveend(Ply)
{
    var statement;
    for(i=0;i<64;i++)
    {
        gi = getGUIIndex(i);
        document['f' + gi].src = getimage(Boards[Ply + 1].charAt(i),0);
    }


}

function userclick(field)
{
     if(Status=="Auto")
         return;

     gf = getGUIIndex(field);
     UserAction[UserClicks] = gf;

     if(CheckField(field,UserClicks)== -1)
     {
         if(UserAction[0]==UserAction[1])
         {
             UserClicks=0;
             image = getimage(Boards[GlobalPly].charAt(gf),0);
             document['f' + field].src = image;
         }
         return;
     }

     image = getimage(Boards[GlobalPly].charAt(gf),1);
     document['f' + field].src = image;

     UserClicks++;

     if(UserClicks == 2)
     {
         if((UserAction[0]==ActFrom[GlobalPly])&&(UserAction[1]==ActTo[GlobalPly]))
         {

             var now = new Date();
             StopT = now.getTime();
             document.respic.src = 'pics/biggrin.gif';

             AccuT += (StopT - StartT);
             UserClicks=0;
             moveinner(GlobalPly);
         }
         else
         {
             document.respic.src = 'pics/sad.gif';
             window.setTimeout('ExitFail()',1000);
         }
     }
}

function ExitFail()
{
    this.location.replace('rating.php?user=' + UserName + '&position=' + Position + '&time=-1&last=' + document.lastform.last.checked + '&PHPSESSID=' + SessionID+ '&Mode=' + Mode);
    document.close();
}

function CheckField(Field,Clicks)
{
    var Source = 1;
    if(Clicks == 1) Source = -1;

    var Gui   = getGUIIndex(Field);
    var CCode = Boards[GlobalPly].charCodeAt(Gui);

    if(CCode == 120) return (-1 * Source);

    var Flip = -1;
    if(FlipBoard != 'w') Flip = 1;

    var Case = -1;
    if(CCode < 90) Case = 1;

    return (Case * Source * Flip);
}

function getimage(fig,high)
{
    var image;
    var folder = 'board';
    if (Font == 'berlin') folder = 'berlin';

    if(fig=="K")image = folder + "/white_king";
    if(fig=="Q")image = folder + "/white_queen";
    if(fig=="B")image = folder + "/white_bishop";
    if(fig=="N")image = folder + "/white_knight";
    if(fig=="R")image = folder + "/white_rook";
    if(fig=="P")image = folder + "/white_pawn";
    if(fig=="k")image = folder + "/black_king";
    if(fig=="q")image = folder + "/black_queen";
    if(fig=="b")image = folder + "/black_bishop";
    if(fig=="n")image = folder + "/black_knight";
    if(fig=="r")image = folder + "/black_rook";
    if(fig=="p")image = folder + "/black_pawn";
    if(fig=="x")image = "board/blank";

    if(high==1)
        image += "_highlighted";

    image += ".gif";

    return image;
}

function initboard()
{

document.writeln("<table cellspacing=0 cellpadding=0>");
document.writeln("<tr><td class=sol colspan=9>&nbsp;<\/td><\/tr>");

var color = "white";
var index = 0;
for(i=0;i<8;i++)
{
    var row = i+1; if(FlipBoard == 'b') row = 8 - i;
    document.writeln("<tr><td class=sol>" + row + "<\/td>");
    for(j=0;j<8;j++)
    {
        document.writeln("<td class="  + color + "><img border=0 name='f" + index + "' src='board/blank.gif' alt=''><\/td>");
        index++;
        color=toggle(color);
    }
    color=toggle(color);
    document.writeln("<\/tr>");
}
if(FlipBoard == 'w')
    document.writeln("<tr><td class=sol>&nbsp;<\/td><td class=sol>h<\/td><td class=sol>g<\/td><td class=sol>f<\/td><td class=sol>e<\/td><td class=sol>d<\/td><td class=sol>c<\/td><td class=sol>b<\/td><td class=sol>a<\/td><\/tr><\/table>");
else
    document.writeln("<tr><td class=sol>&nbsp;<\/td><td class=sol>a<\/td><td class=sol>b<\/td><td class=sol>c<\/td><td class=sol>d<\/td><td class=sol>e<\/td><td class=sol>f<\/td><td class=sol>g<\/td><td class=sol>h<\/td><\/tr><\/table>");
}

function toggle(old)
{
  if(old=="white")
      return "black";
  else
      return "white";
}

function forward()
{
    if(GlobalPly < PlyUBound-1)
    {
         //document.getElementById('p' + GlobalPly).className = 'grey';
         //movebegin(GlobalPly++);
         //document.getElementById('p' + GlobalPly).className = 'current';
         setposition(GlobalPly + 1);
    }
}
function back()
{
    if(GlobalPly > 0)
        setposition(GlobalPly - 1);
}



