(function($) {
  var app = $.sammy(function() {
    this.element_selector = '#inad_members_window ul';
    this.use(Sammy.Template);
    this.use(Sammy.Session);
    this.use(Sammy.NestedParams);

    this.before(function() {
      var context = this;
      $.ajax({
        url: '/liste_des_membres_adherents',
        dataType: 'json',
        async: false,
        success: function(items) {
          context.items = items;
        }
      });

    });

    this.get('#/membres/', function(context) {
      $("#javascript_unabled_message").remove();
      $("#container").before('<div id="inad_members_window"></div>');
      $("#inad_members_window").append('<span class="close"><a href="#/index/">Masquer</a></span>');
      $("#inad_members_window").append('<h1 class="titre">Liste des membres-adh&eacute;rents</h1>');
      $("#inad_members_window").append('<ul id="liste_des_membres_adherents"></ul>');

      context.app.swap('');

      $.each(context.items, function(i, item) {
        context.partial('/templates/member.template', {id: i, fiche: item}, function(rendered) {
          context.$element().append(rendered);
          $('img.profile').defaultImageIfError();
        });
      });
    });

    this.get('#/index/', function(context) {
      $("#inad_members_window").remove();
    });
  });

  $(function() {
    app.run('#/membres/');
  });

})(jQuery);