(function($) {
    
    var endpoint = base_path + '/wp-content/plugins/bmpress/edit.php';
    
    function edit_post_link()
    {
        // detect post_ID from the DOM id
        var mark = $(this).parent().parent().parent();
        if (!mark || mark.attr("id") == '') {
          return true;
        }
        var id = mark.attr("id").replace('post-', '');
        // get form from backend
        $.get(endpoint, {'post':id}, function(data){
            // hide existing mark
            $('#mark' + id).hide();
            // remove existing form if already there
            $('#post-' + id + ' .wrap').remove();
            // append the form sent by the ajax request
            $('#post-' + id).append(data);
          /*$('#mark-form-tags-public').suggest(
                base_path + '/wp-admin/admin-ajax.php?action=ajax-tag-search',
                { delay: 300, minchars: 1, multiple: true, multipleSep: ", " }
            );*/
            // cancel button behavior
            $('#post-' + id + ' form .cancel').click(function() {
                $('#mark' + id).show();
                $('#post-' + id + ' .wrap').remove();
            })
            // when we submit the form
            $('#post-' + id + ' form').submit(function() {
                // get serialized form (use jquery form plugin)
                var queryString = $(this).formSerialize();
                // post to the backend
                $.post(endpoint, queryString, function(data) {
                    // reinject mark from the backend
                    $('#post-' + id).html(data);
                    // add behavior
                    $('#post-' + id + ' .edit_post_link').click(edit_post_link);
                });
                return false;
            });
        });
        return false;
    }
    
    $(document).ready(function($){
        $('.edit_post_link').click(edit_post_link);
    });
    
    $(document).ready(function($){
        $('.delete_post_link').click(function() {
          return confirm("Are you sure you want to delete this mark?");
        });
    });
    
    // $(document).ready(function() {
    //      $('.mark .description').editable(endpoint, {
    //          type      : 'textarea',
    //          width     : '400px',
    //          height    : '75px',
    //          submit    : 'save',
    //          cancel    : 'cancel',
    //          submitdata: {
    //            action: 'edit-content'
    //         }
    //      });
    //  });

})(jQuery);
