// version 5.0.0
addDOMLoadEvent(setupSortableSubcats);
addDOMLoadEvent(setupSortableItems);
addDOMLoadEvent(setupItemForm);

function setupItemForm() {
    if ($('dsItemManage')) {
        hideFields($('dsItemManage').down(".dsType").down("select").value);
        Event.observe($('dsItemManage').down(".dsType").down("select"), 'change', function(e)     {
                hideFields(this.value);
        }, false);
    }
}

function hideFields(value) {
    if (value=="F") {
        $('dsItemManage').down(".dsLink").hide();
        $('dsItemManage').down(".dsFile").show();
    } else {
        $('dsItemManage').down(".dsLink").show();
        $('dsItemManage').down(".dsFile").hide();
    }
}

function setupSortableSubcats () {
    if ($('dsSortSubcats'))
    {
       $('dsSortSubcats').addClassName('sortableActive');
       $('dsSortSubcats').insert({after: '<p>Reorder by dragging and dropping the list above <strong>(changes will be automatically saved)</strong></p>'});
       Sortable.create("dsSortSubcats", {
        onUpdate: function() {
                new Ajax.Request("projects/documentStore/dsReorderSubcats.php", {
                    method: "post",
                    parameters: { data: Sortable.serialize("dsSortSubcats") }
                });
            }
        });
    }
}

function setupSortableItems () {
    if ($('dsSortItems'))
    {
       $('dsSortItems').addClassName('sortableActive');
       $('dsSortItems').insert({after: '<p>Reorder by dragging and dropping the list above <strong>(changes will be automatically saved)</strong></p>'});
       Sortable.create("dsSortItems", {
        onUpdate: function() {
                new Ajax.Request("projects/documentStore/dsReorderItems.php", {
                    method: "post",
                    parameters: { data: Sortable.serialize("dsSortItems") }
                });
            }
        });
    }
}
