var dragsort = ToolMan.dragsort();
$(function() {
	dragsort.makeTableSortable(document.getElementById("categories-list"),
	dotclear.sortable.setHandle, dotclear.sortable.saveOrder);
	
	$('#form-categories input[@name="removeaction"]').click(function() {
		return window.confirm(dotclear.msg.confirm_delete_categories);
	});
	
	if ($('#form-categories input[@name="remove[]"]').length == 0) {
		$('#form-categories input[@name="removeaction"]').hide();
	}
});

dotclear.sortable = {
	setHandle: function(item) {
		var handle = $(item).find('td.handle').get(0);
		while (handle.firstChild) {
			handle.removeChild(handle.firstChild);
		}
		
		item.toolManDragGroup.setHandle(handle);
		$(handle).addClass('handler');
	},
	
	saveOrder: function(item) {
		var group = item.toolManDragGroup;
		var order = $('#categories_order').get(0);
		group.register('dragend', function() {
			order.value = '';
			items = item.parentNode.getElementsByTagName('tr');
			
			for (var i=0; i<items.length; i++) {
				order.value += items[i].id.substr(2)+',';
			}
		});
	}
};