;
(function ($) {
    /**
     * 标签控件
     *
     *     @example
     *     var $root = $('<div>').css({position : 'absolute', top : 5, left : 20}).appendTo('body');
     *     var label = new FR.Label({
     *             renderEl : $root,
     *             width : 200,
     *             height : 40,
     *             value : "我是一个标签",
     *             textalign : "right",     //对齐方式
     *             border : true,     //是否有边框
     *             title : "FineReport",     //悬浮提示信息
     *             wrap : true,       //true换行,false不换行(默认)
     *             verticalcenter : true,   //是否垂直居中
     *             fontsize : 16,         //字体大小
     *             color : "red",          //文字颜色
     *             decoration : "underline",          //文字修饰
     *             fontfamily : "SimSun",    //字体
     *             fontweight : "normal",     //文字粗细
     *             fontstyle : "normal"         //字体风格
     *     });
     *
     * @class FR.Label
     * @extends FR.BaseEditor
     *
     * @cfg {JSON} options 配置属性
     * @cfg {String} options.cls 文字层样式类
     * @cfg {String} options.value 文字内容
     * @cfg {Boolean} [options.border=false] 是否有边框
     * @cfg {String} [options.title] 悬浮提示信息
     * @cfg {Boolean} [options.noWrap=false] 是否不换行,true表示不换行
     * @cfg {Boolean} [options.verticalcenter=false] 是否垂直居中
     * @cfg {Number} [options.fontsize] 文字大小
     * @cfg {String} [options.color] 文字颜色
     * @cfg {String} [options.decoration] 文字修饰
     * @cfg {Number} [options.lineheight] 行间距
     * @cfg {String} [options.textalign] 对齐方式
     * @cfg {String} [options.fontfamily] 字体
     * @cfg {Number} [options.fontweight] 文字粗细
     * @cfg {String} [options.fontstyle] 字体风格
     */
    FR.Label = FR.extend(FR.BaseEditor, /**@class FR.Label*/{
        _defaultConfig: function () {
            return $.extend(FR.Label.superclass._defaultConfig.apply(), {
                cls: 'fr-label',
                wrap: true,
                width: 120,
                height: 24
            });
        },
        _init: function () {
            FR.Label.superclass._init.apply(this, arguments);
            // 控件属性
            this.element.css('overflow', 'hidden');
            var o = this.options;
            //<pre/> 文字层,用于丰富的字体样式
            this.labelComp = $("<pre/>").addClass(o.cls);
            //<table/> 容器层,用于调整文字位置
            this.textContainer = $('<table cellpadding="0" cellspacing="0"/>');
            this.textContainer.css({
                'table-layout': 'fixed',
                width: this.options.width,
                height: this.options.height,
                '-webkit-text-size-adjust': 'none' //CSS3,文字大小不随设备(浏览器)来调整
            }).append($('<tr/>').append($('<td/>').append(this.labelComp))).appendTo(this.element);
            var self = this;
            this.labelComp.click(function () {
                if (self.isEnabled()) {
                    self.fireEvent(FR.Events.CLICK);
                }
            });

            if (this._getEvents().click) {
                this.labelComp.addClass('fr-widget-click');
            }
            if (o.fontsize) {
                this.labelComp.css('font-size', o.fontsize);
            }
            if (o.color) {
                this.labelComp.css('color', o.color);
            }
            if (o.decoration) {
                this.labelComp.css('text-decoration', o.decoration);
            }
            if (o.lineheight) {
                this.labelComp.css('line-height', o.lineheight + 'px');
            }
            if (o.textalign) {
                this.labelComp.css('text-align', o.textalign);
            }
            if (o.fontfamily) {
                this.labelComp.css('font-family', o.fontfamily);
            }
            if (o.fontweight) {
                this.labelComp.css('font-weight', o.fontweight);
            }
            if (o.fontstyle) {
                this.labelComp.css('font-style', o.fontstyle);
            }
            if (o.border === true) {
                this.labelComp.css({
                    border: "1px solid #AAAAAA"
                });
            }
            // 背景给整个控件区域,不是内容区域
            if (o.widgetBackground) {
                //ie8低版本, 设置图片背景, 无效, position从父容器继承的, 不知道为啥无效, 再设置一下又好了
                if(FR.Browser.isIE8Before()){
                    this.textContainer.css('position', 'absolute');
                }

                FR.setBackground(this.element, o.widgetBackground, this.element.height());
            }
            //表单处标题用到边框,为组件边框
            if (o.borderStyle) {
                //为标题控件时不画边框阴影
                this.element.css('border-style', o.borderStyle.type);
                this.element.css('border-color', o.borderStyle.color);
                this.element.css('border-width', o.borderStyle.width);
                this.element.css('border-bottom-width', 0);
            }
            if (o.width > -1) {
                this.element.css({
                    width: o.width
                });
            }
            if (o.height > -1) {
                this.element.css({
                    height: o.height
                });
            }

            if (o.title) {
                this.labelComp.attr('title', o.title);
            }
            if (o.id) {
                this.labelComp.attr('id', o.id);
            }
            if (o.wrap) {
                this.labelComp.css('word-wrap', 'break-word').css(
                    'white-space', 'pre-wrap');
            } else {
                if (!FR.Browser.isIE()) {
                    this.labelComp.css('white-space', 'pre');
                } else {
                    this.labelComp.css('white-space', 'pre-wrap').css(
                        'word-break', "keep-all");
                }
            }
            if (o.value != null) {
                //标签的值改变事件在初始化的时候还是触发一下
                this.setValue(o.value);
            }
        },


        setValue: function (value) {
            var oldValue = this.getValue();
            this.options.value = value;
            if (FR.Browser.isIE()) {
                this.labelComp[0].innerText = value || '';
            } else {
                this.labelComp.text(value);
            }
            if (arguments[1] !== false) {
                this.fireEvent(FR.Events.CHANGE, value, oldValue);
            }
        },

        getValue: function () {
            return this.options.value;
        },

        doResize: function (give) {
            FR.Label.superclass.doResize.call(this, give);
            this.textContainer.css({
                width: give.width,
                height: give.height
            });
            this.labelComp.css({
                width: give.width
            });
            if (!this.options.verticalcenter) {
                this.labelComp.css('height', give.height + 'px');
            }
        }
    });
    $.shortcut("label", FR.Label);
})(jQuery);