(function ($) {
    $.extend({
        plCallFromGUI: function (options)
        {
            var defaults = {
                ws: '',
                canonizeNumberURL: ''
            };

            options = $.extend({}, defaults, options);

            $(document).on("click", ".callableMakeCall", function () {
                if (options.ws) {
                    var phoneNumber;
                    var fromAddressbook = false;
                    if ($(this).hasClass("addressBookCallable")) {
                        var inputEl = $(this).parent().parent().find("input");
                        fromAddressbook = true;

                        if (!inputEl.is(":disabled")) {
                            return false;
                        }
                        phoneNumber = inputEl.val();
                    } else {
                        phoneNumber = $(this).parent().parent().attr("data-phone-number");
                    }

                    $.ajax({
                        type: "POST",
                        url: options.canonizeNumberURL,
                        data: {
                            number: phoneNumber
                        },
                        dataType: "json"
                    }).done(function (canonizedNumber) {
                        if (!fromAddressbook) {
                            canonizedNumber = phoneNumber;
                        }

                        var callCommand = {
                            data: {
                                number: canonizedNumber
                            },
                            name: "dialNumber",
                            type: "command",
                            uuid: "generate it later"
                        };
                        options.ws.command(callCommand);
                    });
                } else {
                    console.log('ws not found');
                }
            });
        }
    });
}(jQuery));