$(document).ready(function() {
    $('.sortable').sortable({
        connectWith: 'ul.trash',
        update: function() {
                var x = 0;
                var li = $('li:not(.trash)', $(this));
                if(li.length>0) {
                  var type = $(li.get(0)).attr('id').replace(/[\d]*/g,'');
                var list = '';
                li.each(function() {
                    list += $(this).attr('id').replace(/[\D]*/,':');
                });
                if(!$(this).hasClass('trash')) {
                    $.ajax({url: '/xml/admin/order.xml',
                           type: 'GET',
                           data: ({type: type, order: list}),
                           success: function() {}
                    });
                }
                }
            },
        items: 'li',
        receive: function(event, ui) {
                $('li',$(this)).each(function() {
                    if(!$(this).hasClass('target')) {
                        $(this).hide('slow');
                        var type = $(this).attr('id').replace(/[\d]*/g,'');
                        var id = $(this).attr('id').replace(/[\D]*/g,'');
                        $.ajax({url: '/xml/admin/remove.xml',
                                type: 'GET',
                                data: ({type: type, id: id}),
                                success: function() {}
                        });
                    }
                });
            },
            cursor: 'pointer'
        });

    $('.draggable > li').draggable({
        revert: 'invalid'
    });
    $('.droppable').droppable({
        over:function(event, ui) {$('.droppable').addClass('over')},
        out:function(event, ui) {$('.droppable').removeClass('over')},
        drop:function(event, ui) {
            $(ui.draggable).hide('slow').remove();
            var type = $(ui.draggable).attr('id').replace(/[\d]*/g,'');
            var id = $(ui.draggable).attr('id').replace(/[\D]*/g,'');
            $.ajax({url: '/xml/admin/remove.xml',
                    type: 'GET',
                    data: ({type: type, id: id}),
                    success: function() {}
            });
            $('.droppable').removeClass('over','slow');
        }
        });
});
