$('.game').blockrain();


var callbackPlayPause = function(){
    if($('#playpause').hasClass('red') == true)
    {
        $('#playpause').removeClass();
        $('#playpause').addClass('btn-floating btn-large waves-effect waves-light green');
        $('#playpause').html('<i class="material-icons">play_arrow</i>');
        $('.game').blockrain('pause');
        $('.game').blockrain('controls', false);
    }
    else 
    {
        $('#playpause').removeClass();
        $('#playpause').addClass('btn-floating btn-large waves-effect waves-light red');
        $('#playpause').html('<i class="material-icons">pause</i>');
        $('.game').blockrain('resume');
        $('.game').blockrain('controls', true);
    }
}
$('#playpause').click(callbackPlayPause);
cheet('p', callbackPlayPause);



var callbackRestart = function(){
    $('.game').blockrain('restart');
}
$('#restart').click(callbackRestart);
cheet('r', callbackRestart);



var callbackPerf = function(){
    if($("#background-canvas").is(":visible"))
        {
            $('#perf').removeClass();
            $('#perf').addClass('btn-floating btn-large waves-effect waves-light red');
            $("#background-canvas").fadeOut(1000);
        }
    else
        {
            $('#perf').removeClass();
            $('#perf').addClass('btn-floating btn-large waves-effect waves-light green');
            $("#background-canvas").fadeIn(1000);
        }
}
$('#perf').click(callbackPerf);
cheet('i', callbackPerf);

$("#envoi").click(function(){
    $("#modal4").leanModal();
});






$(document).ready(function(){
    
    $('#ValidScore').click(function(){
         var leScore = $(".game").blockrain('score');
         $.ajax({
             url: '/tetris/traitement.php',
             data: {score : leScore},
             type: 'POST',
             dataType: 'json',
             success:
                function(donnees, status, xhr)
                {
                    switch(donnees)
                    {
                        case "v":
                            Materialize.toast('Votre score a bien été mis à jour.', 5000);
                            break;
                        case "n":
                            Materialize.toast('Une erreur est survenue.', 5000);
                            break;
                        case "m":
                            Materialize.toast('Votre score est inférieur à celui déjà enregistré.', 5000);
                            break;
                    }
                },
            error:
                function (xhr, status, error) {
                    alert("param : " + xhr.responseText);
                    alert("status : " + status);
                    alert("error : " + error);
                }
         });
    });
    
});
