$(document).ready(function() {
    function formatItem(row) {
        return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    }
    function formatResult(row) {
        return row[0].replace(/(<.+?>)/gi, '');
    }
    
    $(".submit").click(function() {
        $("form:first").submit();
    });
//  $("ul.invitees li").append(" <a href='#'>resend</a> <a href='#' class='delete'>delete</a>");
    $("ul.invitees li").append(" <a href='#' class='resend'>resend</a> <a href='#' class='delete'>delete</a>");
    $('ul.invitees li a.delete').click(function() {
        var id = $(this).parent().attr('id');
        if(confirm('Weet je zeker dat je deze gebruiker geen toegang meer wilt geven tot je discussie?'))
        {
            $.ajax({
                url: "/nl/delete/user_discussion",
                type: "POST",
                data: {user_discussion_id: id}
            });
            $(this).parent().fadeOut();
        }
        return false;
        
    })
    $('ul.invitees li a.resend').click(function() {
        var id = $(this).parent().attr('id');
        $.ajax({
                url: "/nl/action/user_invite",
                type: "POST",
                data: {user_id: id}
        });
        $(this).hide();
        return true;
    })
    
    $("#discussion_invited_id").autocomplete('/new/invite.php', {
        minChars: 2,
        width: 310,
        matchContains: true,
        highlightItem: false,
        dataType: "json",
        multiple: true,
        parse: function(data) {
            return $.map(data, function(row) {
                return {
                    data: row,
                    value: row.name,
                    result: row.to
                }
            });
        },
        formatItem: function(row, i, max) {
            return row.name + ' &lt;' + row.to + '&gt;';
        },
        formatMatch: function(row, i, max) {
            return row.name + " " + row.to;
        },
        formatResult: function(row) {
            return row.to;
        }
    });
    $('#network_search textarea').click(function() {
        if($(this).hasClass('hint')) {
            $(this).val('');
            $(this).removeClass('hint');
        }
    });
    
    $('#search_submit').click(function() {
        var search_str = $('#network_search textarea').val();
        $.ajax({
                url: "/new/nl/raadskamers/-/fetch/network",
                type: "POST",
                data: {s: search_str},
                success: function(data) {
                    $('#network_list').html(data);
                  }
                
        });
        return false;
    });

    $("#txt_remarks").keyup(function() {
        $.ajax({url: "/my-choice/ajax.php", type: "POST", data: {remarks: $("#txt_remarks").val()}});
    });
});

