/*jshint jquery:true browser:true*/
/*global Ajax:true alert:true*/
define([
    "jquery",
    "mage/backend/form",
    "jquery/ui",
    "prototype"
], function($){
    "use strict";

    $.widget("mage.menuForm", $.mage.form, {
        options: {
            menuIdSelector : 'input[name="menu[menu_id]"]',
            menuPathSelector : 'input[name="menu[path]"]'
        },

        /**
         * Form creation
         * @protected
         */
        _create: function() {
            this._super();
            $('body').on('menuMove.tree', $.proxy(this.refreshPath, this));
        },

        /**
         * Sending ajax to server to refresh field 'menu[path]'
         * @protected
         */
        refreshPath: function() {
            var that = this;
            if (!this.element.find(this.options.menuIdSelector).prop('value')) {
                return false;
            }
            $.ajax({
                type: 'POST',
                url: this.options.refreshUrl,
                dataType: 'json',
                data: {
                    form_key: FORM_KEY
                }
            }).success(function (data) {
                that._refreshPathSuccess(data);
            });
        },
        _refreshPathSuccess: function(response) {
            if (response.error) {
                alert(response.message);
            } else {
                if (this.element.find(this.options.menuIdSelector).prop('value') == response.id) {
                    this.element.find(this.options.menuPathSelector)
                        .prop('value', response.path);
                }
            }
        }
    });

    return $.mage.menuForm;
});
