var timer, playerName;
var phases = {
    names : [
        'status',
        'action'
    ],
    times : [
        10 * 1000,
        10 * 1000
    ]
};

var turnLength = phases.times.reduce(function(pv, cv) { return pv + parseInt(cv); }, 0);
var currentPhase = 0;

$(document).ready(function(){
    $('body').on('click', '#start', function(e){
        e.preventDefault();
        conn.send(JSON.stringify({call:'timer'}));
    });

    $('body').on('click', '.choose button.btn-default', function(e){
        e.preventDefault();
        var data = {
            call : $(this).parent('.choose').attr('id'),
            param : $(this).attr('data-choice')
        }

        if(conn.readyState == 1){
            conn.send(JSON.stringify(data));
        }
    });
});

function manageGame(start){
    $(document).ready(function(){
        var elapsed = (microtime(true) - start) * 1000;
        var moment = elapsed % turnLength;
        var realPhase;

        var countDown = turnLength - phases.times[1 - currentPhase] - moment;
        if(moment < phases.times[0]){
            realPhase = 0;
        }else{
            countDown += phases.times[0];
            realPhase = 1;
        }
        if(realPhase != currentPhase){
            currentPhase = realPhase;
            $('#phase').html(phases.names[currentPhase]);
        }

        $('#timeleft').text(sprintf('%01.2f', countDown / 1000));
    });
}