(function ($) {
    /**
     * 文本控件
     *
     *     @example
     *     var editor = new FR.TextEditor({
     *             renderEl : 'body',
     *             value : 'Hello'
     *     });
     *
     * @class FR.TextEditor
     * @extends FR.EditComp
     *
     * @cfg {JSON} options 属性配置
     * @cfg {Boolean} [options.autoVerify=true] 自动校验规则
     */
    FR.TextEditor = FR.extend(FR.EditComp, {
        _defaultConfig: function () {
            return $.extend(FR.TextEditor.superclass._defaultConfig.apply(), {
                autoVerify: true
            });
        },
        /**
         * 判断是否是不符合规则的输入
         * @param e Event
         * @returns {boolean|*} 返回输入是否符合规则
         */
        isValidateInput: function (e) {
            return !FR.isSpecialKey(e);
        },

        _dealValueWithEvents: function (value) {
            var oldValue = this.getValue();
            this.editComp.val(value);
            if (arguments[1] !== false) {
                this.fireEvent(FR.Events.CHANGE, value, oldValue); // fire value
                // change event,
                // newValue &
                // oldValue
                this.fireEvent(FR.Events.AFTEREDIT);
            }
        },

        recoveryValue: function () {
            this.editComp.val(this.options.oldValue);
        },

        select: function () {
            if (this.editComp.select) {
                this.editComp.select();
            }
        },
        getValue: function () {
            return this.editComp.val();
        },

        doResize: function(give){
            FR.TextEditor.superclass.doResize.call(this, give);
        }
    });
    $.shortcut("text", FR.TextEditor);

    /**
     * 密码控件
     * @class FR.Password
     * @extends FR.TextEditor
     * @cfg {Boolean} [passwordText=false] 是否是密码文本
     */
    FR.Password = FR.extend(FR.TextEditor, /**@class FR.Password*/{
        _defaultConfig: function () {
            return $.extend(FR.TextEditor.superclass._defaultConfig.apply(), {
                passwordText: true //属性保留,主要用于填报,识别密码控件
            });
        },
        getValue: function () {
            var value = FR.Password.superclass.getValue.apply(this, arguments);
            // denny: 如果是密码控件,且需要客户端加密,加密之
            value.encryption = true;
            return value;
        },
        _createEditComp: function () {
            return $("<input type=\"password\"/>");
        }
    });
    $.shortcut("password", FR.Password);

    /**
     * 文本域控件
     * @class FR.TextArea
     * @extends FR.TextEditor
     * @cfg {Boolean} [multiline=false] 是否支持多行显示
     */
    FR.TextArea = FR.extend(FR.TextEditor, /**@class FR.TextArea*/{
        _defaultConfig: function () {
            return $.extend(FR.TextEditor.superclass._defaultConfig.apply(), {
                multiline: true
            });
        },
        _init: function () {
            FR.TextArea.superclass._init.apply(this, arguments);
            var o = this.options, self = this;
            if (o.multiline === true) {
                this.editComp.keydown(function (e) {
                    if (self.options.write && FR.keyCode.UP == e.keyCode
                        || FR.keyCode.DOWN == e.keyCode) {
                        // 填报正在编辑时,上下键会moveTDCell,与文本域冲突,这里屏蔽了。
                        e.stopPropagation();
                        return;
                    }
                    // b:影响填报,为什么阻止冒泡
                    // wei:回车会触发参数界面的查询按钮,因此阻止冒泡为了不触发查询按钮。填报时不阻止,因为回车时
                    // 希望格子往下切换。
                    if (e.keyCode === 13) {
                        if (self.options.form) {
                            self.fireEvent(FR.Events.AFTEREDIT);
                            e.stopPropagation();
                        }
                    }
                    var eel = this;
                    if (e.ctrlKey && e.keyCode === 13) {
                        e.ctrlKey = false;
                        // 改成从光标所在位置换行
                        var val = eel.value;
                        var inputEl = $('.'+ o.cls, self.element).eq(0)[0];
                        if (typeof inputEl.selectionStart == "number" && typeof inputEl.selectionEnd == "number") {
                            var start = inputEl.selectionStart;
                            eel.value = val.slice(0, start) + "\n" + val.slice(inputEl.selectionEnd);
                            inputEl.selectionStart = inputEl.selectionEnd = start + 1;
                        }
                        else if (document.selection && document.selection.createRange) {
                            this.focus();
                            var range = document.selection.createRange();
                            range.text = "\r\n";
                            range.collapse(false);
                            range.select();
                        }
                        self.fireEvent(FR.Events.AFTEREDIT);
                    }
                });
                // shoc:杂项模式修改white-space解决换行问题,客户bug28462
                //火狐下自动换行也有问题
                if (!jQuery.support.boxModel || FR.Browser.r.gecko) {
                    this.editComp.css({'white-space': 'normal'});
                }
            }
        },
        _createEditComp: function () {
            return $("<textarea style='overflow-y:auto;resize:none'></textarea>");
        },
        /**
         * 判断是否是不符合规则的输入
         * @param e Event
         * @returns {boolean|*} 返回输入是否符合规则
         */
        isValidateInput: function (e) {
            // wei: 如果是可多行输入的文本框,那么输入回车也应该触发编辑后事件。
            return FR.TextArea.superclass.isValidateInput.call(this, e) || (e.keyCode === 13 && this.options.form);
        },

        doResize: function (give) {
            FR.TextEditor.superclass.doResize.call(this, give);
        }
    });
    $.shortcut("textarea", FR.TextArea);
})(jQuery);