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

  var menuList = [];
  var scPartieId = scInput_PartieId;
  var scJoueurId = scInput_JoueurId;
  var menuMode = '';

  function refreshScreen(data) {
    if(data['mode'] == 'g') {
      if(scJoueurId > 0)
        window.location.href = 'index.php?j=' + scJoueurId;
      else
        window.location.href = 'index.php';
      return;
    }

    if(data['mode'] != menuMode) {
      menuMode = data['mode'];
      if(menuMode == 'p') {
        $('#creerTitre').html(scInput_JeuNom.toUpperCase() + ' - Choisir une partie');
        $('#creerTitre').removeClass('creer-titre-attente');
        $('#creerTitre').removeClass('creer-titre-joueur');
        $('#creerTitre').addClass('creer-titre-partie');
        $('#creerForm').show();
        $('#creerGo').hide();
        scPartieId = 0;
        scJoueurId = 0;
      }
      if(menuMode == 'j') {
        $('#creerTitre').html('<div class="creer-titre-retour creer-btn">&lt;&lt;</div>' + scInput_JeuNom.toUpperCase() + ' [ Partie : ' + data['partie'] + ' ] Choisir un joueur');
        $('#creerTitre').removeClass('creer-titre-attente');
        $('#creerTitre').removeClass('creer-titre-partie');
        $('#creerTitre').addClass('creer-titre-joueur');
        $('#creerForm').show();
        $('#creerGo').hide();
        scJoueurId = 0;
      }
      if(menuMode == 's') {
        $('#creerTitre').html('<div class="creer-titre-retour creer-btn">&lt;&lt;</div>' + scInput_JeuNom.toUpperCase() + ' [ Partie : ' + data['partie'] + ' ] [ Joueur : ' + data['joueur'] + " ] Salle d'attente");
        $('#creerTitre').removeClass('creer-titre-partie');
        $('#creerTitre').removeClass('creer-titre-joueur');
        $('#creerTitre').addClass('creer-titre-attente');
        $('#creerForm').hide();
      }
      $('#creerListeTab').empty();
      menuList = [];
    }

    // Parties/Joueurs ajoutes ou modifies ?
    var dataListeTaille = 0;
    for(var key in data['liste']) {
      dataListeTaille++;
      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>' + (scJoueurId > 0 ? '' : '<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;
      }
    }

    if(data['mode'] == 's') {
      if( dataListeTaille > 1 )
        $('#creerGo').show();
      else
        $('#creerGo').hide();
    }
  }

  function refresh() {
    $.get('backend/creer.php?p=' + scPartieId + '&j=' + scJoueurId)
      .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)
      scJoueurId = id;
    else
      scPartieId = id;

    clearTimeout(refreshTimer);
    refresh();
  }

  $('#creerTitre').on('click', '.creer-titre-retour', function(e) {
    if(scJoueurId > 0) {
      var tempJ = scJoueurId;
      scJoueurId = 0;
      $.get('backend/creer.php?p=' + scPartieId + '&j=' + tempJ + '&out=1')
        .done(function(data, text, jqxhr) {

        })
        .fail(function(jqxhr){
  //        alert(jqxhr.responseText);
        })
        .always(function(){
          clearTimeout(refreshTimer);
          refresh();
        });
    }
    else {
      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(){

      });
  });

  $('#creerGo .creer-go-btn').on('click', function(e) {
    e.preventDefault();
    $('#creerGo').fadeOut();

    $.get('backend/creer.php?p=' + scPartieId + '&j=' + scJoueurId + '&go=1')
      .done(function(data, text, jqxhr) {

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

  $('#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);