define([
    'jquery',
    'prototype'
], function (jQuery) {

    var menuSubmit = function (url, useAjax) {
    var activeTab = $('active_tab_id');
    if (activeTab) {
        if (activeTab.tabsJsObject && activeTab.tabsJsObject.tabs('activeAnchor')) {
            activeTab.value = activeTab.tabsJsObject.tabs('activeAnchor').prop('id');
        }
    }

    var params = {};
    var fields = $('menu_edit_form').getElementsBySelector('input', 'select');
    for(var i=0;i<fields.length;i++){
        if (!fields[i].name) {
            continue;
        }
        params[fields[i].name] = fields[i].getValue();
    }

    // Get info about what we're submitting - to properly update tree nodes
    var menuId = params['menu[id]'] ? params['menu[id]'] : 0;
    var isCreating = menuId == 0; // Separate variable is needed because '0' in javascript converts to TRUE
    var path = params['menu[path]'].split('/');
    var parentId = path.pop();
    if (parentId == menuId) { // Maybe path includes Menu id itself
        parentId = path.pop();
    }

    // Make operations with Menu tree
    if (isCreating) {
        if (!tree.currentNodeId) {
            // First submit of form - select some node to be current
            tree.currentNodeId = parentId;
        }
        tree.addNodeTo = parentId;
    } 

    // Submit form
    jQuery('#menu_edit_form').trigger('submit');
};

    return function (config, element) {
        config = config || {};
        jQuery(element).on('click', function (event) {
            menuSubmit(config.url, config.ajax);
        });
    };
});
