$(document).ready(
    function () {
        if ($('ul.biglist.sortable').length) {
            /* work around a jquery bug with .sortable() called on a container
             * set with position: relative and overflow properties */
            $('#main-content').css('overflow', 'inherit');
        }
        $('ul.biglist.sortable li').each(function(i, elem) {
            $('<span class="handle">⣿</span>').prependTo(elem);
        });
        $('ul.biglist.sortable').sortable(
            {
                handle: '.handle',
                accept: 'biglistitem',
                scroll: true,
                helper: 'clone',
                helperclass: 'sortHelper',
                activeclass :     'sortableactive',
                hoverclass :     'sortablehover',
                tolerance: 'pointer',
                update : function(event, ui)
                {
                    var page_no_label = $(this).data('page-no-label');
                    var page_index = 1;
                    result = '';
                    items = $(ui.item).parent().find('li');
                    for (i=0; i < items.length; i++) {
                        var item = items[i];
                        var item_id = item.id.substr(7, 50);
                        if (item_id.length) {
                          result += item_id + ';';
                        }
                        if ($(item).find('span.page-no').length) {
                          $(item).find('span.page-no').text(page_no_label.replace('***', page_index));
                          page_index += 1;
                        }
                    }
                    var order_function = $(this).data('order-function') || 'update_order';
                    $.post(order_function, {'order': result});
                },
            }
        );
    }
);

