﻿var _state;                  // the _state that the user is currently in (0 - New game, 1 - Dealt first 5 cards, 2 - Held cards, new ones drawn)
var imagePath = 'images/';  // the path to the images
var maxBidAmount;           // the max amount of chips that the user can bid (not used?)
var _chips;                 // number of chips at the user's disposal
var _wager;                 // the amount of the current wager
var _hand;                  // represents the user's hand of cards (not used?)
var _counter = 0;

var _fbPoker = null;              // represents the _fbPoker game.


function Init()
{
    // This method is only called on first page load.
    // Do some initialization stuff here ...
    NewGame();
}

function NewGame()
{
    //$('pnlSide').innerHTML = _counter;
    if(_counter == 100000)
    {
        ShowRewards();
        _counter = 0;
    }

    if(_fbPoker == null)
    {
        _fbPoker = new fbPoker();  
    }
    
    _fbPoker.NewGame();
    //if(_fbPoker == null)
    //{
    //    _fbPoker = new fbPoker();    // Initialize a new fbPoker object.    
    //}


    //_hand = new Array(5);       // Initialize the user's hand (not used?)
    _state = 0;                  // This is a new game, set the _state to 0;
    
    var pnlPoker = $('pnlPoker');
    var txtBidAmount = $('txtBidAmount');
    var fldChips = document.getElementById('fldChips');
        
    if(pnlPoker != null)
    {
        if(fldChips != null)
        {
            $('pnlChips').innerHTML = GetChips();
        }
        
        // A flag to determine if the user already played the current game (cheat prevention)
        $('fldGamePlayed').value = 0;
    
        pnlPoker.style.display = 'block';
        
        if(txtBidAmount != null)
        {
            txtBidAmount.value = GetMaxBid();
        }
    
        var chk;
        var img;
        var lbl;
        
        for(i=0; i<5; i++)
        {   
            chk = $('chkHold' + i);
            img = $('imgCard' + i);
            lbl = $('lblHold' + i);
            
            if(chk != null)
            {
                chk.disabled = false;
                chk.checked = false;
            }
            
            if(img != null)
            {
                img.className = 'card0';
                img.src = imagePath + 'back.gif';
            }
            
            if(lbl != null)
            {
                lbl.className = 'lblCheck0';
            }
        }
        
        var rowHold = $('rowHold'); 
        var rowPlaceBet = $('rowPlaceBet');
        var rowDraw = $('rowDraw');
        var rowPlayAgain = $('rowPlayAgain');
        
        // Hide the check boxes until after a bet has been placed.
        if(rowHold != null) { rowHold.style.visibility = 'hidden'; }
        if(rowPlaceBet != null) { rowPlaceBet.style.display = ''; }
        if(rowDraw != null) { rowDraw.style.display = 'none'; }
        if(rowPlayAgain != null) { rowPlayAgain.style.display = 'none'; }

         
        DisplayMessage(false, 'Place Your Wager.');
    }

    
}

// Returns the number of chips that the user has to play with. 
// Might make this an AJAX callback, instead of relying on the hidden field.
// Or this can initialize the hiden field.
function GetChips()
{
    var fldChips = document.getElementById('fldChips');
    
    if(fldChips != null)
    {
        _chips = fldChips.value;
    }
        
	return _chips;
}

// Checks to see if the user is attempting to cheat.
function CheckSubmit()
{
    var fldGamePlayed = $('fldGamePlayed');
    var pnlMessage = $('pnlMessage');

    if(fldGamePlayed != null)
    {   
        if(fldGamePlayed.value == '0')
        {   
            fldGamePlayed.value = '1';
            return true;
        }
        else
        {
            if(pnlMessage != null)
            {
                pnlMessage.innerHTML = 'Are you trying to cheat?';
            }
            return false;        
        }
    }
    return false;
}

// Handles the btnPlaceBet button click event
function btnPlaceBet_Click()
{   
    var cardsNeeded = new Array('0','1','2','3','4');
    var totalChips = _chips;    // $('fldChips').value;
    var wager;
    
    if(CheckSubmit())
    {
        if(isNaN($('txtBidAmount').value))
        {
            DisplayMessage(true, 'Your Wager isn\'t valid');
        }
        else
        {  
            _wager = $('txtBidAmount').value;
            
            if (wager > GetMaxBid())
            {
                DisplayMessage(true, "Your Wager is Invalid.");
            }
            else
            {
                _chips -= _wager;
                _fbPoker.SetWager(_wager);
                $('pnlChips').innerHTML = _chips;
                _state = 1;
                
                DealCards(cardsNeeded, 'btnPlaceBet');
            }
            
            //PlaceBet(wager);
        }     
    }

    return false; 
}

function btnDraw_Click()
{
    _state = 2
    
    var chk;
    
    for(i = 0; i <= 4; i++)
    {
        chk = $('chkHold' + i);
        
        if(chk != null)
        {
            chk.disabled = true;
        }
    }
    
    DealCards(GetCardsNeeded(), 'btnDraw'); 
}


function OnPlaceBetError(error)
{
    DisplayMessage(true,'Error placing bet');
}


// Returns a comma delimited string of cards positions that need to be filled.       
function GetCardsNeeded()
{
    var chk;
    var count = 0;
    var cardsNeeded;
    var tmp = '';
    
    for(i = 0; i <= 4; i++)
    {
        chk = $('chkHold' + i);
        
        if(chk != null)
        {
            if(!chk.checked)
            {
                count++;
                tmp += i + ',';
            }
        }
    }

    if(tmp.substr(tmp.length-1,1) == ',')
    {
        tmp = tmp.substr(0, tmp.length-1);
    }

    cardsNeeded = tmp.split(',');
    
    return cardsNeeded;
}       
       
function DealCards(cardsNeeded, msg)
{   
    var chk;        // represents a checkbox object
    var img;        // represents and image object
    var count = 0;  // not sure
    var cards;      // a collection of cards objects returned from _fbPoker
    var card;       // represents a card object
    
    if(cardsNeeded.length <=5)
    {
        cards = _fbPoker.DealCards(cardsNeeded);

        // New cards were drawn. Fill up the available slots, and output the final hand + winnings
        if(_state == 2)
        {
            for(i=0; i<cards.length; i++)
            {
                //chk = $('chkHold' + i);
                //img = $('imgCard' + i);
                
                chk = $('chkHold' + i);
                img = $('imgCard' + i);
                
                if(chk != null && img != null)
                {
                    card = cards[i];                   
                    img.src = imagePath + card.Image();
                    img.className = 'card0';
                }
                

            }
            
            $('rowPlaceBet').style.display = 'none';
            $('rowDraw').style.display = 'none';
            $('rowHold').style.visibility = 'visible';
            $('rowPlayAgain').style.display = '';
            
            _fbPoker.Checkhand();
            
           
            var winnings = ((_wager * _fbPoker.Multiplier()));
            $('fldChips').value = _chips + winnings;
            $('pnlChips').innerHTML = GetChips();
            
            DisplayMessage(false, _fbPoker.Score() + ' (+' + winnings + ')');
            
            _counter++;
            
            //_fbPoker = null;
        }
        // A bet was placed, fill up the 5 slots
        else if(_state == 1)
        {
            for(i=0; i<cardsNeeded.length; i++)
            {
                chk = $('chkHold' + i);
                img = $('imgCard' + i);
                
                if(chk != null && img != null)
                {
                    card = cards[i];
                    
                    //_hand[i] = card;
                    img.src = imagePath + card.Image();
                }
                
                img.className = 'card0';
            }
            
            $('rowPlaceBet').style.display = 'none';
            $('rowDraw').style.display = '';
            $('rowHold').style.visibility = 'visible';
            $('rowPlayAgain').style.display = 'none';
            
            DisplayMessage(false, "Select the Cards to Hold."); 
        }
        else
        {
            alert('????');
        }
    }

}

function DisplayGameOutput()
{

}


function OnDealCardsError(error)
{

}

function GetMaxBid()
{
	//var fldChips = document.getElementById('fldChips');

    //if(fldChips != null)
    //{
        return parseInt(_chips * 0.1);
    //}
    
    //return 0;
    //PageMethods.GetMaxBid(OnGetMaxBidComplete,OnGetMaxBidError);
} 
        
function OnGetMaxBidComplete(result)
{
    var txtBidAmount = $('txtBidAmount');
    
    maxBidAmount = result
  
    if(txtBidAmount != null)
    {
        txtBidAmount.value = maxBidAmount;
    }
}
    
function OnGetMaxBidError(error)
{

}
