jQuery(document).ready(function () {
    initLcSortableList();
});

function initLcSortableList() {

    if ($('.lc-sortable').length > 0) {
        $('.lc-sortable tbody').sortable({
            placeholder: "ui-state-highlight"
        });

        $('.lc-sortable tbody').on("sortupdate", function (event, ui) {

            prototype = $('#form_entities').data('prototype');

            $('.lc-sortable tr.lc-draggable').each(function (index, li) {
                // instead be a number based on how many items we have
                var newForm = prototype.replace(/__name__/g, index);

                // Replace '__name__' in the prototype's HTML to
                $(li).find('div:last-child').remove();

                $(li).append(newForm);
                $(li).find('#form_entities_' + index + '_id').val($(li).data('id'));
                if ($('.lc-sortable').data('parent-position') !== '') val = $('.lc-sortable').data('parent-position') + '.' + index
                else val = index;

                $(li).find('#form_entities_' + index + '_position').val(val);
            });

        });
    }
}

