(function($) {
$(document).ready(function() {

  var menuList = [];
  var scPartieId = 0;
  var menuMode = '';

  function refreshScreen(data) {
    if(data['mode'] != menuMode) {
      menuMode = data['mode'];
      if(menuMode == 'p') {
        $('#creerTitre').html('Karuba - Choisir une partie');
        $('#creerTitre').removeClass('creer-titre-joueur');
        $('#creerTitre').addClass('creer-titre-partie');
        scPartieId = 0;
      }
      if(menuMode == 'j') {
        $('#creerTitre').html('<div class="creer-titre-retour creer-btn">&lt;&lt;</div>Karuba - Partie : ' + data['nom'] + ' - Choisir un joueur');
        $('#creerTitre').removeClass('creer-titre-partie');
        $('#creerTitre').addClass('creer-titre-joueur');
      }
      $('#creerListeTab').empty();
      menuList = [];
    }

    // Parties/Joueurs ajoutes ou modifies ?
    for(var key in data['liste']) {
      var val = data['liste'][key];
      if( menuList[key] ) {
        if( menuList[key] != val )
          $('#creerListeItem' + key + ' .creer-liste-nom').html( val );
      }
      else {
        var tr = $('<tr id="creerListeItem' + key + '"><td class="creer-liste-nom">' + val + '</td><td class="creer-liste-btn"><div class="creer-liste-btn-entrer creer-btn">Entrer</div><div class="creer-liste-btn-suppr creer-btn">Supprimer</div></td></tr>');
        $('#creerListeTab').prepend(tr);
        tr.hide().fadeIn();
        menuList[key] = val;
      }
    }

    // Parties/Joueurs supprimes ?
    for(var key in menuList) {
      if( !data['liste'][key] ) {
        $('#creerListeItem' + key).fadeOut();
        menuList[key] = false;
      }
    }
  }

  function refresh() {
    $.get('backend/creer.php?p=' + scPartieId)
      .done(function(data, text, jqxhr) {
        refreshScreen(  JSON.parse(jqxhr.responseText) );
      })
      .fail(function(jqxhr){
//        alert(jqxhr.responseText);
      })
      .always(function(){
        refreshTimer = setTimeout(refresh, 1000);
      });
  }

  refresh();
  var refreshTimer;


  function creerSwitchTo(id) {
    if(scPartieId > 0)
      window.location.href = 'index.php?j=' + id;
    else {
      scPartieId = id;
      clearTimeout(refreshTimer);
      refresh();
    }
  }

  $('#creerTitre').on('click', '.creer-titre-retour', function(e) {
    scPartieId = 0;
    creerSwitchTo(0);
  });

  $('#creerListeTab').on('click', '.creer-liste-btn-entrer', function(e) {
    e.preventDefault();
    var id = $(this).parents('tr').attr('id').substring('creerListeItem'.length);
    creerSwitchTo(id);
  });

  $('#creerListeTab').on('click', '.creer-liste-btn-suppr', function(e) {
    e.preventDefault();
    var id = $(this).parents('tr').attr('id').substring('creerListeItem'.length);
//    $(this).parents('td').empty();
    $(this).parents('tr').fadeOut();
    menuList[id] = false;
    $.get('backend/creer.php?p=' + scPartieId + '&suppr=' + id)
      .done(function(data, text, jqxhr) {

      })
      .fail(function(jqxhr){
//        alert(jqxhr.responseText);
      })
      .always(function(){

      });
  });


  $('#creerForm').on('submit', function(e) {
    e.preventDefault();
    if($('#creerForm .creer-form-input').val().length < 2) return;
    var $form = $(this);

    $form.hide();

    $.post('backend/creer.php?p=' + scPartieId, $form.serializeArray())
      .done(function(data, text, jqxhr) {
        creerSwitchTo( jqxhr.responseText );
        $('#creerForm .creer-form-input').val('');
      })
      .fail(function(jqxhr){
//        alert(jqxhr.responseText);
      })
      .always(function(){
        $form.show();
      });
  });


});
})(jQuery);