//
// Copyright 2007 weaktight.com
// Vince Jewlal
//

//
// string helpers
//
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
String.prototype.stripcomma = function() {
    return this.replace(/,/,"");
}

//change the opacity for different browsers
function setAlpha(opacity, id) {
    var object = document.getElementById(id);
    if (object)
    {
        object.style.opacity = (opacity / 100);
        object.style.MozOpacity = (opacity / 100);
        object.style.KhtmlOpacity = (opacity / 100);
        object.style.filter = "alpha(opacity=" + opacity + ")";
    }
}
var WT_REP_mNumStreets = 5;
var WT_REP_mMaxPlayers = 10;
var WT_REP_mHandHistory = {};// = new Array(mMaxHands);
var WT_REP_mHTMLHH = {};
var WT_REP_mHandIndex = 0;
var WT_REP_mNumHands = 0;
var WT_REP_mspost_id = 0;
var WT_REP_mKnown = 0;
var WT_REP_mDelay = 1;
var WT_REP_userPause = 1;
var WT_REP_trny = 0;
var WT_REP_dollar = '$';

var WT_REP_mInitialized=false;

var WT_REP_timerID=null;
var WT_REP_delay=955;
var WT_REP_nline=0;
var WT_REP_nplayer=0;
var WT_REP_street=0;
var WT_REP_pot=0;
var WT_REP_btn=0;
var WT_REP_hand_done=0;
var WT_REP_playerlist=new Array(WT_REP_mMaxPlayers);
var WT_REP_potlist=new Array(WT_REP_mNumStreets);
var WT_REP_betlist=new Array(WT_REP_mNumStreets);
var WT_REP_stacklist=new Array(WT_REP_mMaxPlayers);
var WT_REP_holelist=new Array(WT_REP_mMaxPlayers);
//var WT_REP_hole2list=new Array(WT_REP_mMaxPlayers);

function WT_REP_initTable(_hh, _handindex, _numhands, _spost_id)
{
    //alert(_handindex);
    //alert(_numhands);
    //alert(_spost_id);
    //WT_REP_hh = _hh;
    WT_REP_mHandIndex = _handindex;
    WT_REP_mNumHands = _numhands;
    WT_REP_m_spost_id = _spost_id;

    //WT_REP_mSavedHandHistory	= new Array(WT_REP_mNumHands);
    WT_REP_mHandHistory = new Array(WT_REP_mNumHands);
    WT_REP_mHandHistory[WT_REP_mHandIndex] = _hh;

    // init HTML array and grab contents
    
    //alert(WT_REP_mHandHistory[0]);
    
    WT_REP_mInitialized = true;
    
    //WT_REP_playerlist = new Array(WT_REP_mMaxPlayers);
    
    for(j=0;j<WT_REP_mMaxPlayers;j++)
    {
        WT_REP_playerlist[j] = "empty";
        WT_REP_holelist[j]= new Array(2);
    }

    for(j=0;j<WT_REP_mNumStreets;j++)
    {
        WT_REP_potlist[j] = new Array(WT_REP_mMaxPlayers);
        WT_REP_betlist[j] = new Array(WT_REP_mMaxPlayers);
        
        for(i=0;i<WT_REP_mMaxPlayers;i++)
            WT_REP_betlist[j][i] = 0;
    }
    
    WT_REP_cache();
}

function WT_REP_setButton(nplayer)
{
    thebtn = document.getElementById("button");
    WT_REP_btn = 1;
    //nplayer = line.split('#')[1];
    //alert(nplayer);
    if (thebtn)
    {
        thebtn.style.top="";
        
        if (nplayer==1)
        {
            thebtn.style.top="160px";
            thebtn.style.left="161px";
        }
        else if (nplayer==2)
        {
            thebtn.style.top="149px";
            thebtn.style.left="267px";
        }
        else if (nplayer==3)
        {
            thebtn.style.top="149px";
            thebtn.style.left="325px";
        }
        else if (nplayer==4)
        {
            thebtn.style.top="159px";
            thebtn.style.left="429px";
        }
        else if (nplayer==5)
        {
            thebtn.style.top="211px";
            thebtn.style.left="472px";
        }
        else if (nplayer==6)
        {
            thebtn.style.top="280px";
            thebtn.style.left="478px";
        }
        else if (nplayer==7)
        {
            thebtn.style.top="322px";
            thebtn.style.left="336px";
        }
        else if (nplayer==8)
        {
            thebtn.style.top="322px";
            thebtn.style.left="186px";
        }
        else if (nplayer==9)
        {
            thebtn.style.top="269px";
            thebtn.style.left="101px";
        }
        else if (nplayer==10)
        {
            thebtn.style.top="211px";
            thebtn.style.left="104px";
        }
    }		   
    
}

function WT_REP_setDelay()
{
    var o = document.getElementById("replayspeed");
    if (o)
    {
        var value = o.options[o.selectedIndex].value;
        
        if (value == 1)
            WT_REP_delay = 2155;
        else if (value == 2)
            WT_REP_delay = 1255;
        else if (value == 3)
            WT_REP_delay = 755;
    }
    else
        WT_REP_delay = 955;
}

function WT_REP_resetCode()
{
    WT_REP_hand_done=0;
    //delay = 955;
    WT_REP_setDelay();
    WT_REP_nline = 0;
    WT_REP_nplayer = 0;
    WT_REP_street = 0;
    WT_REP_btn = 0;
    WT_REP_pot = 0;
    //WT_REP_last_line = 0;
    WT_REP_resetPlayerBets();
    WT_REP_resetHoleCards();
    WT_REP_updateStacks();
    WT_REP_parseCode();
    clearTimeout(WT_REP_timerID);
    WT_REP_timerID=null;
    
    
}

function WT_REP_parseCode()
{
    if (!WT_REP_mInitialized)
        alert('InitTable first!');
        
    for(i=0;i<WT_REP_mNumStreets;i++)
    {
        for(j=0;j<WT_REP_mMaxPlayers;j++)
        {
            WT_REP_potlist[i][j]=0;
        }
    }
    
    WT_REP_clearBets();
            
    lastaction = document.getElementById("action1");
    for (i = 1;i<WT_REP_mMaxPlayers;i++)
    {
        thetext = document.getElementById("bet"+i);
        
         if (thetext)
         {
             thetext = document.getElementById("action"+i);
             thetext.firstChild.nodeValue="";
         }
    }

   thecard = document.getElementById("board1");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("board2");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("board3");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("board5");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("board4");
   if (thecard) thecard.style.visibility="hidden";

   thecard = document.getElementById("card01a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card01b");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card02a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card02b");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card03a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card03b");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card04a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card04b");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card05a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card05b");
   if (thecard) thecard.style.visibility="hidden";
   
   thecard = document.getElementById("card06a");
   if (thecard) thecard.style.visibility="hidden";
   thecard = document.getElementById("card06b");
   if (thecard) thecard.style.visibility="hidden";
   
   thetext = document.getElementById("pot");
   if (thetext) thetext.style.visibility="visible";

   thetext = document.getElementById("forminput");
   if (thetext) thetext.style.visibility="hidden";

    
    while (WT_REP_street == 0 && !WT_REP_hand_done)
        WT_REP_parseAction();

    o = document.getElementById("loadingid");
    if (o) o.style.visibility="hidden";

    thetext = document.getElementById("maintable");
    if (thetext)
       thetext.style.visibility="visible";
}

function WT_REP_getPlayerIndex(name)
{
    for (i = 0;i<WT_REP_mMaxPlayers;i++)
    {
       // which player is this?
       if (WT_REP_playerlist[i] == name)
       {
           return i+1;
       }
    }
    return -1;
}

function WT_REP_show(index, card1, card2)
{
    c1 = document.getElementById("card0"+index+"a");
    if (c1) c1.style.backgroundImage="url(http://weaktight.com/images/"+card1.toLowerCase()+".gif)";
    if (c1) c1.style.visibility="visible";
    if (c1) c1.style.width="25px";
    if (c1) c1.style.height="15px";

    c2 = document.getElementById("card0"+index+"b");
    if (c2) c2.style.backgroundImage="url(http://weaktight.com/images/"+card2.toLowerCase()+".gif)";
    if (c2) c2.style.visibility="visible";
    if (c2) c2.style.width="25px";
    if (c2) c2.style.height="15px";
}

function WT_REP_getPotValue()
{
    pot = 0;
    for (i = 0;i<WT_REP_mNumStreets;i++)
    {
        for (j = 0;j<6;j++)
            pot += WT_REP_potlist[i][j];
    }
    return pot;	
}

function WT_REP_updateStacks()
{
    //alert('updat stacks');
    for (j = 0;j<WT_REP_mMaxPlayers;j++)
    {
        var stack = 0;
        var bets = 0;
        var left = 0;
        
        // add up bets
        for (i = 0;i<WT_REP_mNumStreets;i++)
        {
            //alert('WT_REP_betlist['+i+']['+j+'] =  '+WT_REP_betlist[i][j]);
            //
            if (WT_REP_betlist[i][j])
            {
                bets += WT_REP_betlist[i][j];// = WT_REP_getValueOf(amount);
                //alert(bets);
                //alert(WT_REP_stacklist[j] - bets);
            }
        }
        
        left = WT_REP_stacklist[j] - bets;
        if (left < 0) left = 0;
        
        if (WT_REP_trny)
            stack = (left).toFixed(0);// = getValueOf(stack);
        else
            stack = (left).toFixed(2);// = getValueOf(stack);
        
        //alert('j = '+j+' '+stack);
        
        if (!isNaN(stack))
        {
            //alert("set "+j+" to "+stack);
            thetext = document.getElementById("stack"+(j+1));
            if (WT_REP_trny)
            {
                if (thetext) thetext.firstChild.nodeValue=stack;
            }
            else
            {
                if (thetext) thetext.firstChild.nodeValue="$"+stack;
            }
                
            if (thetext) thetext.style.visibility="visible";
        }
        
    }
}

function WT_REP_updatePot()
{
    WT_REP_pot = 0;
    for (i = 0;i<WT_REP_mNumStreets;i++)
    {
        for (j = 0;j<WT_REP_mMaxPlayers;j++)
            WT_REP_pot += WT_REP_potlist[i][j];
    }
    
    //potlist[WT_REP_street][pindex] = getValueOf(amount);			   	
    thepot = document.getElementById("potvalue");
    

    if (WT_REP_pot.toFixed)
    {
        if (WT_REP_trny)
            thepot.firstChild.nodeValue=WT_REP_dollar+WT_REP_pot.toFixed(0);
        else
            thepot.firstChild.nodeValue=WT_REP_dollar+WT_REP_pot.toFixed(2);
    }
    else
        thepot.firstChild.nodeValue=WT_REP_dollar+WT_REP_pot;
    
}

function WT_REP_setKnown()
{
    if (document.replayform.k_known.checked)
    {
        WT_REP_mKnown = 1;
        WT_REP_showHoleCards();
    }
    else
        WT_REP_mKnown = 0;
}

function WT_REP_showHoleCards()
{
    for (j = 0;j<WT_REP_mMaxPlayers;j++)
    {
        if (WT_REP_holelist[j][0] != undefined && WT_REP_holelist[j][1] != undefined &&
            WT_REP_holelist[j][0].length==2 && WT_REP_holelist[j][1].length==2)
        {
            WT_REP_show(j+1, WT_REP_holelist[j][0], WT_REP_holelist[j][1]);
        }
    }
}

function WT_REP_resetHoleCards()
{
    //alert('reset bets');
    for (j = 0;j<WT_REP_mMaxPlayers;j++)
    {
        WT_REP_holelist[j][0]='';
        WT_REP_holelist[j][1]='';
    }
    
}

function WT_REP_resetPlayerBets()
{
    //alert('reset bets');
    for (j = 0;j<WT_REP_mMaxPlayers;j++)
    {
        for (i = 0;i<WT_REP_mNumStreets;i++)
        {
            WT_REP_betlist[i][j] = 0;
        }
    }
    
}

function WT_REP_clearBets()
{
    for (i = 1;i<WT_REP_mMaxPlayers+1;i++)
    {
        thetext = document.getElementById("bet"+i);
        if (thetext)
        {
            thetext.firstChild.nodeValue="";

            thetext = document.getElementById("action"+i);
            if (thetext.firstChild.nodeValue!="folded")
            {
                thetext.firstChild.nodeValue="";
                thetext.style.backgroundColor="#aaaaaa";
                //thetext.style.backgroundColor="#aaaaaa";
            }
        }
    }
}

function WT_REP_getValueOf(amt_str)
{
    //var val = 1.0;
    
    amt = amt_str.stripcomma();
    if (amt.indexOf('$') != -1)
        val = ((amt.split('$')[1])*1);//.toFixed(2);
    else
        val = amt*1;
    //alert(val);
    //return val.toFixed(2);
    return val;//.toFixed(2);
            //.toFixed(2);
}

function WT_REP_getCardStrMini(cardstr, arg_path)
{
    path=arg_path;
    rank="";
    suit="";

    cardstr.toLowerCase();
    return path+cardstr+".gif";//cardstr.split(" ")[3];
}

function WT_REP_getCardStr(cardstr, arg_path)
{
    path=arg_path;
    rank="";
    suit="";

    return path+cardstr+".gif";//cardstr.split(" ")[3];
}


function WT_REP_parseAction()
{
    if (WT_REP_mHandHistory[WT_REP_mHandIndex] == undefined)
        return;
    
    var cont = 0;
    //alert('parseAction'+this);
    var lines = (WT_REP_mHandHistory[WT_REP_mHandIndex]).split(';');

    // completed hand
    if (WT_REP_nline > lines.length-1 || lines.length<8)
    {
        WT_REP_hand_done=1;
        return;
    }
    
    // skip conditions
    while (
        (lines[WT_REP_nline].length<3)
    )
    {
        WT_REP_nline++;
        
        if (WT_REP_nline > lines.length-1)
        {
            WT_REP_hand_done=1;
            //alert('done');
               
            return;
        }

        /*	
        if (WT_REP_nline >= max_line-1)
        {
            thetext = document.getElementById("forminput");
            if (thetext) thetext.style.visibility="visible";

            hand_done=1;
            //alert('done');
            return;
        }*/
    }
    
    if (WT_REP_nline > lines.length-1)
    {
        WT_REP_hand_done=1;
        //alert('done');
        return;
    }

   var line = (lines[WT_REP_nline++]).trim();
   
    o = document.getElementById("label_debug");
    if (o) o.innerHTML="line="+line;
   
   WT_REP_updateReveal();

   if (lastaction) {
        lastaction.style.color="#555555";
        lastaction.style.backgroundColor="#aaaaaa";
   }
   
   var thetext = document.getElementById("label1");
   if (thetext) thetext.firstChild.nodeValue=line;

   if (line.indexOf('Pre-flop: ') != -1)
   {
        WT_REP_street = 1;
        
        var player = line.split(":")[2];
        //.split(':')[0]
        
        //alert(player);
        
        // upDate holdings
        cardlist_str = line.split(":");
        if (cardlist_str.length>4)
        {
            card1 = cardlist_str[3].trim();
            card2 = cardlist_str[4].trim();
            index = WT_REP_getPlayerIndex(player);
            WT_REP_show(index,card1,card2);
        }
   }
   else if (line.indexOf('shows: ') != -1)
   {
       var player = line.split(' shows')[0];

       cardlist_str = line.split(":");
       card1 = cardlist_str[2].trim();
       card2 = cardlist_str[3].trim();

       index = WT_REP_getPlayerIndex(player);
       
        thetext = document.getElementById("action"+index);
        if (thetext) thetext.firstChild.nodeValue="shows";
       
       
       WT_REP_show(index,card1,card2);
       
       cont = 1;
   }
   else if (line.indexOf('known: ') != -1)
   {
        var player = line.split(' known')[0];
    
        cardlist_str = line.split(":");
        card1 = cardlist_str[2].trim();
        card2 = cardlist_str[3].trim();
    
        index = WT_REP_getPlayerIndex(player);
        if (index > 0)
        {
            WT_REP_holelist[index-1][0]=card1;
            WT_REP_holelist[index-1][1]=card2;
        }
        else
        {
 //           alert(line);
        }

        /*
       
       c1 = document.getElementById("card0"+index+"a");
       //if (c1) c1.style.backgroundImage="url("+WT_REP_getCardStr(card1,"../../images/poker/crop/")+")";
       if (c1) c1.style.backgroundImage="url(http://weaktight.com/images/"+card1.toLowerCase()+".gif)";
       if (c1) c1.style.visibility="visible";
       if (c1) c1.style.width="25px";
       if (c1) c1.style.height="15px";
       

       c2 = document.getElementById("card0"+index+"b");
       //if (c2) c2.style.backgroundImage="url("+WT_REP_getCardStr(card2,"../../images/poker/crop/")+")";
       //if (c2) c2.style.visibility="visible";
       if (c2) c2.style.backgroundImage="url(http://weaktight.com/images/"+card2.toLowerCase()+".gif)";
       if (c2) c2.style.visibility="visible";
       if (c2) c2.style.width="25px";
       if (c2) c2.style.height="15px";
        */
       cont = 1;
       
   }
   else if (line.indexOf(' wins ') != -1)
   {
       WT_REP_clearBets();
       var player = line.split(' wins ')[0];
       var chips = line.split(' wins ')[1];
       
       //chips = getValueOf(amount);
               
       pindex=WT_REP_getPlayerIndex(player);

       thetext = document.getElementById("bet"+pindex);
       
       if (WT_REP_trny)
       {
            thetext.firstChild.nodeValue=WT_REP_getValueOf(chips).toFixed(0);
       }
       else
       {
            thetext.firstChild.nodeValue='$'+WT_REP_getValueOf(chips).toFixed(2);
       }
       
       //thetext.style.fontWeight="bold";

       thetext = document.getElementById("action"+pindex);
       thetext.firstChild.nodeValue="wins";
       //thetext.style.color="#ffffff";
       
       //thediv = document.getElementById("div0"+pindex);
       //thediv.style.borderColor = "#aa0000";

       thetext = document.getElementById("pot");
       thetext.style.visibility="hidden";
       
   }
   else if (line.indexOf('Flop: ') != -1)
   {
       WT_REP_street = 2;

       WT_REP_clearBets();
       
       //upDate cards
       cardlist_str = line.split(":");
       card1 = cardlist_str[2].trim();
       card2 = cardlist_str[3].trim();
       card3 = cardlist_str[4].trim();
       //alert(card2);
       //alert(card3);
       
       thecard = document.getElementById("board1");
       if (thecard) thecard.style.backgroundImage="url("+WT_REP_getCardStr(card1,"/img/d54c/")+")";
       
       thecard = document.getElementById("board2");
       if (thecard) thecard.style.backgroundImage="url("+WT_REP_getCardStr(card2,"/img/d54c/")+")";
       
       thecard = document.getElementById("board3");
       if (thecard) thecard.style.backgroundImage="url("+WT_REP_getCardStr(card3,"/img/d54c/")+")";
       
       thecard = document.getElementById("board1");
       thecard.style.visibility="visible";
       thecard = document.getElementById("board2");
       thecard.style.visibility="visible";
       thecard = document.getElementById("board3");
       thecard.style.visibility="visible";
       //alert(1);
       WT_REP_updatePot();
   }
   else if (line.indexOf('Turn: ') != -1)
   {
       WT_REP_street = 3;

       WT_REP_clearBets();
       //upDate cards
       cardlist_str = line.split(":");
       card1 = cardlist_str[2].trim();
       
       thecard = document.getElementById("board4");
       thecard.style.backgroundImage="url("+WT_REP_getCardStr(card1,"/img/d54c/")+")";
       
       thecard = document.getElementById("board4");
       thecard.style.visibility="visible";
       WT_REP_updatePot();
   }
   else if (line.indexOf('River: ') != -1)
   {
       WT_REP_street = 4;

       WT_REP_clearBets();
       
       //upDate cards
       cardlist_str = line.split(":");
       card1 = cardlist_str[2].trim();
       
       thecard = document.getElementById("board5");
       thecard.style.backgroundImage="url("+WT_REP_getCardStr(card1,"/img/d54c/")+")";
       
       thecard = document.getElementById("board5");
       thecard.style.visibility="visible";
       WT_REP_updatePot();
    }
    else if (line.indexOf('Blind:') != -1)
    {
        var player = line.split(':')[1];
        var amount = line.split(':')[2];
        
        //alert("*"+player+"*"+amount);
        pindex=WT_REP_getPlayerIndex(player);
        
        WT_REP_betlist[WT_REP_street+1][pindex-1] = WT_REP_getValueOf(amount);
        WT_REP_potlist[WT_REP_street+1][pindex-1] += WT_REP_getValueOf(amount);
        //alert(pindex);
        thetext = document.getElementById("bet"+pindex);
        if (thetext)
        {
            thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street+1][pindex-1];//amount;
        }
        //alert(WT_REP_potlist[WT_REP_street+1][pindex-1]);
        if (WT_REP_trny)
        {
            if (thetext) thetext.firstChild.nodeValue=WT_REP_potlist[WT_REP_street+1][pindex-1].toFixed(0);//amount;
        }
        else
        {
            if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street+1][pindex-1].toFixed(2);//amount;
        }
        
        //WT_REP_updatePot();
        //WT_REP_updateStacks();
        WT_REP_updatePot();
        
    }
    else if (line.indexOf('Seat:') != -1)
    {
        var player = line.split(':')[2];
        var pos = line.split(':')[1];
        var stack = line.split(':')[3];
        var seat = line.split(':')[4];
        
        if (stack.indexOf('$') == -1)
        {
            WT_REP_trny = 1;
            WT_REP_dollar = '';
        }
        else
        {
            WT_REP_trny = 0;
            WT_REP_dollar = '$';
        }
        
        player = player.trim();
        if (player.length==0)
            player = pos;
        
        //alert("*"+player+"*"+stack);
        ++WT_REP_nplayer;
        if (pos == "BTN")
            WT_REP_setButton(seat);
        else if (pos == "SB" && !WT_REP_btn)
            WT_REP_setButton(seat);
                  
        
        thetext = document.getElementById("player"+seat);
        if (thetext) thetext.firstChild.nodeValue=player;
        if (thetext) thetext.style.visibility="visible";
        
        WT_REP_playerlist[seat-1] = player;
        WT_REP_stacklist[seat-1] = WT_REP_getValueOf(stack);
        //alert(WT_REP_getValueOf(stack));
        
        
        thetext = document.getElementById("stack"+seat);
        if (thetext) thetext.firstChild.nodeValue=stack;
        if (thetext) thetext.style.visibility="visible";

        setAlpha(100, "mask"+seat);
        thetext = document.getElementById("playerblock"+seat);
        if (thetext) thetext.style.visibility="visible";

        c1 = document.getElementById("card0"+seat+"a");
        //if (c1) c1.style.backgroundImage="url("+WT_REP_getCardStr(card1,"../../images/poker/crop/")+")";
        if (c1) c1.style.backgroundImage="url(http://weaktight.com/images/bk.gif)";
        if (c1) c1.style.visibility="visible";

        if (c1) c1.style.width="25px";
        if (c1) c1.style.height="15px";


        
        c2 = document.getElementById("card0"+seat+"b");
        if (c2) c2.style.backgroundImage="url(http://weaktight.com/images/bk.gif)";
        if (c2) c2.style.visibility="visible";
        if (c2) c2.style.width="25px";
        if (c2) c2.style.height="15px";
        /*
       if (c2) c2.style.width="25px";
        pindex=getPlayerIndex(player);
        
        potlist[WT_REP_street+1][pindex-1] += getValueOf(amount);
        thetext = document.getElementById("bet"+pindex);
        if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street+1][pindex-1];//amount;
        */
        
        /*
        if (line.indexOf('sitting out') != -1)
        {
            pindex=getPlayerIndex(player);
            thediv = document.getElementById("div0"+pindex);
            if (thediv) thediv.style.borderColor = "#220000";
        }*/
    }
    else if (line.indexOf('posts the') != -1)
    {
           var thetext = "";
           var player = line.split(' posts the')[0];
           var amount = line.split(' blind of ')[1];
           var pindex = 0;
           var i = 0;

           //alert(amount);
           //alert(player);

           pindex=WT_REP_getPlayerIndex(player);

           WT_REP_potlist[WT_REP_street+1][pindex-1] += WT_REP_getValueOf(amount);
           thetext = document.getElementById("bet"+pindex);
           if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street+1][pindex-1].toFixed(2);//amount;
           
           //pot += getValueOf(amount);
   }
   else if (line.indexOf('checks') != -1)
   {
        var thetext = "";
        var player = line.split(' checks')[0];
        var action = "checks";
        var pindex = 0;
        var i = 0;
        
        pindex=WT_REP_getPlayerIndex(player);
        
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;
        lastaction=thetext;
        
        if (line.indexOf('folded') != -1)
        {
           thediv = document.getElementById("div0"+pindex);
           if (thediv) thediv.style.borderColor = "#220000";
        }
        //delay=955;
   }
   else if (line.indexOf('folds') != -1 || line.indexOf('mucks') != -1)
   {
        var thetext = "";
        var player = line.split(' folds')[0];
        var action = "folds";
        var pindex = 0;
        var i = 0;
        
        //alert(player+' folds');
        
        pindex=WT_REP_getPlayerIndex(player);
        
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;
        //if (thetext) thetext.visibility=visible;
        
        thetext = document.getElementById("mask"+pindex);
        setAlpha(50, "mask"+pindex);
        //if (thetext) thetext.opacity=0.5;
        //if (thetext) thetext.style="filter: alpha(opacity = 50)";

        //if (thetext) thetext.style.color="#ffffff";
        lastaction=thetext;
        
        thediv = document.getElementById("div0"+pindex);
        if (thediv) thediv.style.borderColor = "#220000";
        //delay=955;
        
        // did i just fold?
        /*
        if (document.tableform.speed.selectedIndex == 2 && "weaktight.com"==player)
        {
            hand_done=1;
        }*/
   }
   else if ( line.indexOf('raises') != -1 )
   {
        //document.write(line);
        var thetext = "";
        var player = line.split(' raises to ')[0];
        var amount = (line.split(' raises to ')[1]).split(',')[0];
        var action = "raises";
        var pindex = 0;
        var i = 0;

        if (line.indexOf('and is all in') != -1) action = "all-in";

        pindex=WT_REP_getPlayerIndex(player);
        WT_REP_potlist[WT_REP_street][pindex-1] = WT_REP_getValueOf(amount);
        WT_REP_betlist[WT_REP_street][pindex-1] = WT_REP_getValueOf(amount);
        //alert(WT_REP_betlist[WT_REP_street][pindex-1]);
        //alert('set '+WT_REP_street+' index = '+(pindex-1));
        //alert('$'+WT_REP_potlist[WT_REP_street][pindex-1].toFixed(2));
        

        thetext = document.getElementById("bet"+pindex);
        //alert(thetext);
        if (WT_REP_trny)
        {
            if (thetext) thetext.firstChild.nodeValue=WT_REP_potlist[WT_REP_street][pindex-1].toFixed(0);//amount;
        }
        else
        {
            if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street][pindex-1].toFixed(2);//amount;
        }
            
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;

        lastaction=thetext;
        //WT_REP_updateStacks();
        
        //delay=955;
    }
    else if ( line.indexOf('calls') != -1 )
    {
        //document.write(line);
        var thetext = "";
        var player = line.split(' calls ')[0];
        var amount = line.split(' calls ')[1];
        var action = "calls";
        var pindex = 0;
        var i = 0;
        
        if (line.indexOf('and is all in') != -1) action = "all-in";
        
        //alert(amount);
        //alert(WT_REP_getValueOf(amount));
        //alert(WT_REP_getValueOf(amount));
        
        pindex=WT_REP_getPlayerIndex(player);
        WT_REP_potlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        WT_REP_betlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        
        thetext = document.getElementById("bet"+pindex);
        
        if (WT_REP_trny)
        {
            if (thetext) thetext.firstChild.nodeValue=WT_REP_potlist[WT_REP_street][pindex-1].toFixed(0);//amount;
        }
        else
        {
            if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street][pindex-1].toFixed(2);//amount;
        }
        
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;
        
        lastaction=thetext;
        //delay=955;
   }
   else if ( line.indexOf('bets') != -1 )
   {
        //document.write(line);
        var thetext = "";
        var player = line.split(' bets ')[0];
        var amount = line.split(' bets ')[1];
        //.split(',')[0];;
        var action = "bets";
        var pindex = 0;
        var i = 0;
        
        //alert(player+amount);
        
        //if (line.indexOf('and is all in') != -1) action = "all-in";
        
        pindex=WT_REP_getPlayerIndex(player);
        WT_REP_potlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        WT_REP_betlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        
        thetext = document.getElementById("bet"+pindex);
        if (WT_REP_trny)
        {
            if (thetext) thetext.firstChild.nodeValue=WT_REP_potlist[WT_REP_street][pindex-1].toFixed(0);
        }
        else
        {
            if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street][pindex-1].toFixed(2);
        }
        
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;
        
        lastaction=thetext;
        //delay=955;
        //setDelay()
   }
   else if ( line.indexOf('goes all-in') != -1 )
   {
        //document.write(line);
        var thetext = "";
        var player = line.split(' goes all-in ')[0];
        var amount = line.split(' goes all-in ')[1];
        //.split(',')[0];;
        var action = "bets";
        var pindex = 0;
        var i = 0;
        
        //alert(player+amount);
        
        //if (line.indexOf('and is all in') != -1) action = "all-in";
        
        pindex=WT_REP_getPlayerIndex(player);
        WT_REP_potlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        WT_REP_betlist[WT_REP_street][pindex-1] += WT_REP_getValueOf(amount);
        
        thetext = document.getElementById("bet"+pindex);
        if (WT_REP_trny)
        {
            if (thetext) thetext.firstChild.nodeValue=WT_REP_potlist[WT_REP_street][pindex-1].toFixed(0);
        }
        else
        {
            if (thetext) thetext.firstChild.nodeValue='$'+WT_REP_potlist[WT_REP_street][pindex-1].toFixed(2);
        }
        
        thetext = document.getElementById("action"+pindex);
        if (thetext) thetext.firstChild.nodeValue=action;
        
        lastaction=thetext;
   }
   
    //lastaction.style.color="#eeee66";
    //&& lastaction.firstChild.nodeValue!="wins"
    if (lastaction && lastaction.firstChild.nodeValue!="" && lastaction.firstChild.nodeValue!="shows") {
        lastaction.style.color="#0202d8";
        lastaction.style.backgroundColor="#ffffff";
    }
    
    o = document.getElementById("label_debug");
    if (o) o.innerHTML+="<br>WT_REP_nline="+WT_REP_nline;
    
    WT_REP_updateStacks();
    return cont;
}

function WT_REP_changeText()
{
    //var thetext = document.getElementById("mytext");
    //thetext.firstChild.nodeValue="go";

    var thetext2 = document.getElementById("stack1");
    thetext2.firstChild.nodeValue=hh;

}

function WT_REP_pause()
{
    //alert('upDate'+this);
    //alert(WT);
    //alert(WTreplaytable);
    
    
    if (!WT_REP_mInitialized)
        alert('InitTable first!');
    
    clearTimeout(WT_REP_timerID);
    clearTimeout(WT_REP_timerID);
    clearTimeout(WT_REP_timerID);
    clearTimeout(WT_REP_timerID);
    clearTimeout(WT_REP_timerID);
    WT_REP_timerID=null;
    
    o = document.getElementById("label_status");
    if (o) o.innerHTML="Paused.";
    o = document.getElementById("b_playstop");
    //if (o) o.firstChild.nodeValue="Play";
    if (o) o.innerHTML="<span><em>Play</span></em>";
}

function WT_REP_stepBack()
{
    // save current line
    //alert(WT_REP_nline);
    var lines = (WT_REP_mHandHistory[WT_REP_mHandIndex]).split(';');
    
    last_line = WT_REP_nline;
    
    do {
        last_line--;

        /*			
        o = document.getElementById("label_debug");
        if (o) o.innerHTML="WT_REP_nline="+last_line+" - "+lines[last_line].length+" : "+lines[last_line];
        
        alert(0);*/
    
    } while (lines[last_line].length<4
             || lines[last_line].indexOf('Flop: ') != -1
             || lines[last_line].indexOf('Turn: ') != -1
             || lines[last_line].indexOf('River: ') != -1
             || lines[last_line].indexOf('shows: ') != -1
             || lines[last_line].indexOf(' wins ') != -1);
    
    WT_REP_resetCode();
    
    while (WT_REP_nline < last_line)
        WT_REP_parseAction();
    
    //alert(WT_REP_nline);
}

function WT_REP_togglePlay()
{
    o = document.getElementById("play_start");
    if (o) o.style.display="none";
    
    if (WT_REP_timerID == null)
    {
        WT_REP_userPause = 0;
        WT_REP_start();
    }
    else
    {
        WT_REP_userPause = 1;
        WT_REP_pause();
    }
}

function WT_REP_start()
{
    //alert(WT_REP_timerID);
    //WT_REP_timerID
    
    if (WT_REP_timerID == null)
    {
        //WT_REP_timerID = setTimeout(WT_REP_update, 0);
        WT_REP_update();
        o = document.getElementById("label_status");
        if (o) o.innerHTML="Playing.";
        o = document.getElementById("b_playstop");
        if (o) o.innerHTML="<span><em>Stop</span></em>";
        //if (o) o.firstChild.nodeValue="Stop";
    }
}

function WT_REP_updateReveal()
{
    if (WT_REP_mKnown)
        WT_REP_showHoleCards();
}

function WT_REP_update()
{
    //alert('upDate'+this);
    //alert(WT);
    //alert(WTreplaytable);
    
    if (!WT_REP_mInitialized)
        alert('InitTable first!');
    
    if (WT_REP_hand_done)
    {
        
        var o = document.getElementById("replayauto");
        if (o)
        {
            var value = o.options[o.selectedIndex].value;
            
            if (value == 1)
            {
                o = document.getElementById("label_status");
                if (o) o.innerHTML="Loading...";
                
                WT_REP_getNextHand();
            }
            else if (value == 2)
            {
                o = document.getElementById("label_status");
                if (o) o.innerHTML="Done.";
            }
        }
        
        // disabled this for now v2k
        //WT_REP_getNextHand();
    }
    else
    {
        while (WT_REP_parseAction()) {}
        
        //WT_REP_updateReveal();
        WT_REP_setDelay();
        WT_REP_timerID = setTimeout(WT_REP_update, WT_REP_delay);
        o = document.getElementById("label_status");
        if (o) o.innerHTML="Playing.";
    }
}

function WT_REP_updateIndexBar()
{
        o = document.getElementById("td_session");
        if (o)
        {
            $pct = (WT_REP_mHandIndex.toFixed(2)/WT_REP_mNumHands.toFixed(2))*100.0;
            o.innerHTML="Hand "+WT_REP_mHandIndex+"/"+WT_REP_mNumHands+" from <a href=\"/s"+WT_REP_m_spost_id+"\">session #"+WT_REP_m_spost_id+"</a>\
            <style type=\"text/css\">            \
\
div.progress-container { \
  border: 1px solid #ccc; \
  margin: 2px 5px 2px 0;\
width: 90%;  \
  padding: 1px; \
  float: left; \
  background: white;\
}\
\
div.internal {\
  background-color: #ACE97C; \
  height: 12px \
}\
\
</style>\
\
<div class=\"progress-container\">          \
<div class=\"internal\" style=\"width: "+$pct+"%\"></div>\
</div> \
";

        }
        
        //alert(1);
}

function WT_REP_cb_addHH(x)
{
    if(x.responseText !== undefined){
        //alert(x.responseText);
        
        //alert(x.argument);
        WT_REP_mHandHistory[x.argument] = (x.responseText).split('***')[0];
        
        WT_REP_resetCode();
        o = document.getElementById("loadingid");
        if (o) o.style.visibility="hidden";
        
        if (!WT_REP_userPause)
            WT_REP_start();
    }
    
}

function WT_REP_preload($x)
{
    if (typeof WT_REP_mHandHistory[$x] == "undefined")
    {
        // no! we have to get it
        //alert('get new hand');
        
        var callback =
        {
          success:WT_REP_cb_addHH,
          failure:WT_REP_cb_responseFailure,
          argument: $x
          
        };
      
        //WT_REP_mHandHistory[WT_REP_mHandIndex];
      
        //var o = document.getElementById("status_div");
        //if (o) o.style.visibility="visible";            
        var postData = "gethand="+$x+"&spost_id="+WT_REP_m_spost_id;
        
        var transaction = YAHOO.util.Connect.asyncRequest('POST', "p_replay.php", callback, postData);
        o = document.getElementById("loadingid");
        if (o) o.style.visibility="visible";
    }
}

function WT_REP_cache()
{
    if (WT_REP_m_spost_id > 0)
    {
        WT_REP_preload(WT_REP_mHandIndex+1);
        //WT_REP_preload(WT_REP_mHandIndex+2);
        //WT_REP_preload(WT_REP_mHandIndex+3);
        //WT_REP_preload(WT_REP_mHandIndex+4);
        //WT_REP_preload(WT_REP_mHandIndex+5);
    }
}

function WT_REP_cb_responseFailure(o)
{
    // Access the response object\'s properties in the
    // same manner as listed in responseSuccess( ).
    // Please see the Failure Case section and
    // Communication Error sub-section for more details on the
    // response object\'s properties.
    if(o.responseText !== undefined){
        alert("Error: Response Failure "+o.responseText);
        /*
    var div = document.getElementById("yuidiv");
    div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
    div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
    div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";*/
    }else alert("Error: Response Failure");
}

function WT_REP_getHand($x)
{
    if (WT_REP_mHandIndex >= WT_REP_mNumHands)
    {
        //alert('no more hands');
        return;
    }
    
    WT_REP_mHandIndex = $x;
    REP_index(WT_REP_mHandIndex);
    WT_REP_updateIndexBar();
    
    // do we already have this hand?
    if (typeof WT_REP_mHandHistory[WT_REP_mHandIndex] == "undefined")
    {
        // no! we have to get it
        //alert('get new hand');
        
        var callback =
        {
          success:WT_REP_cb_addHH,
          failure:WT_REP_cb_responseFailure
        };
      
        //WT_REP_mHandHistory[WT_REP_mHandIndex];
      
        //var o = document.getElementById("status_div");
        //if (o) o.style.visibility="visible";            
        var postData = "gethand="+WT_REP_mHandIndex+"&spost_id="+WT_REP_m_spost_id;
        
        var transaction = YAHOO.util.Connect.asyncRequest('POST', "p_replay.php", callback, postData);
        o = document.getElementById("loadingid");
        if (o) o.style.visibility="visible";
        
    }
    else
    {
        // use it
        WT_REP_resetCode();
        //WT_REP_update();
        if (!WT_REP_userPause)
            WT_REP_start();
        
    }
    
}

function WT_REP_getNextHand()
{
    //WT_REP_mHandIndex++;
    
    if (WT_REP_mHandIndex >= WT_REP_mNumHands)
    {
        //alert('no more hands');
        return;
    }
    
    WT_REP_mHandIndex++;
    REP_index(WT_REP_mHandIndex);
    WT_REP_updateIndexBar();
    //WT_REP_resetCode();
    
    // do we already have this hand?
    //alert(WT_REP_mHandHistory[WT_REP_mHandIndex]);
    if (typeof WT_REP_mHandHistory[WT_REP_mHandIndex] == "undefined")
    {
        // no! we have to get it
        //alert('get new hand');
        
        var callback =
        {
          success:WT_REP_cb_addHH,
          failure:WT_REP_cb_responseFailure
        };
      
        //WT_REP_mHandHistory[WT_REP_mHandIndex];
      
        //var o = document.getElementById("status_div");
        //if (o) o.style.visibility="visible";            
        var postData = "gethand="+WT_REP_mHandIndex+"&spost_id="+WT_REP_m_spost_id;
        
        var transaction = YAHOO.util.Connect.asyncRequest('POST', "p_replay.php", callback, postData);
        o = document.getElementById("loadingid");
        if (o) o.style.visibility="visible";
        
    }
    else
    {
        // use it
        WT_REP_resetCode();
        //WT_REP_update();
        if (!WT_REP_userPause)
            WT_REP_start();
            
        // cache the next ones
        WT_REP_cache();
        
    }
    
}

function WT_REP_getPrevHand()
{
    //WT_REP_mHandIndex++;
    
    if (WT_REP_mHandIndex <= 1)//WT_REP_mNumHands)
    {
        //alert('no more hands');
        return;
    }
    
    WT_REP_mHandIndex--;
    REP_index(WT_REP_mHandIndex);
    WT_REP_updateIndexBar();
    
    // do we already have this hand?
    if (typeof WT_REP_mHandHistory[WT_REP_mHandIndex] == "undefined")
    {
        // no! we have to get it
        //alert('get new hand');
        
        var callback =
        {
          success:WT_REP_cb_addHH,
          failure:WT_REP_cb_responseFailure
        };
      
        //WT_REP_mHandHistory[WT_REP_mHandIndex];
      
        //var o = document.getElementById("status_div");
        //if (o) o.style.visibility="visible";            
        var postData = "gethand="+WT_REP_mHandIndex+"&spost_id="+WT_REP_m_spost_id;
        
        var transaction = YAHOO.util.Connect.asyncRequest('POST', "p_replay.php", callback, postData);
        o = document.getElementById("loadingid");
        if (o) o.style.visibility="visible";
        
    }
    else
    {
        // use it
        
        WT_REP_resetCode();
        if (!WT_REP_userPause)
            WT_REP_start();
        //WT_REP_update();
        
    }
    
}



