$(document).ready(function() {
    $('.toggle div').click(function(e) {
        if ($(this).hasClass('active')) {
            return;
        } else {
            // TODO make this thing cache the results after the first load
            var url = $(this).attr('rel');
            var cur = this;
            var seek = true;
            $(this).parents().each(function(i, el) {
                if ($(el).attr('data-action') && seek) {
                    url = $(el).attr('data-action') + url;
                    var target = $(el).find('ul.users')[0];
                    $(target).empty();
                    $(el).find('div.loading').show(); 
                    $.ajax({
                        url : url
                        ,dataType : 'twellow'
                        ,success : function(response) {
                            $(el).find('div.loading').hide();
                            $.tmpl(response.template, response.data).appendTo($(target));
                            $(el).find('.toggle div').removeClass('active');
                            $(cur).addClass('active');
                        }
                    });
                    seek = false;
                    return;
                }
            });
        }
    });
});

