(function ($) {
    // 复合控件,这里是指的显示页码的控件
    FR.Composite = FR.extend(FR.Widget, {
        type: "composite",

        _defaultConfig: function () {
            return $.extend(FR.Composite.superclass._defaultConfig.apply(),
                {
                    innerWidget: {}
                });
        },

        _init: function () {
            FR.Composite.superclass._init.apply(this, arguments);
            var o = this.options.innerWidget;
            o.renderEl = $("<div>").appendTo($("body"));
            var widget = FR.createWidget(o);
            if (widget.element.data('jlayout')) {
                widget.doResize(widget.element.data('jlayout')
                    .preferred(this.element));

            }
            widget.doLayout();
            widget.element.css({
                position: ""
            });
            o.renderEl.appendTo(this.element.css({
                position: "relative"
            }));
        }
    });
    $.shortcut("composite", FR.Composite);

    /**
     * 工具栏
     * @class FR.Toolbar
     * @extends FR.Widget
     */
    FR.Toolbar = FR.extend(FR.Widget, {
        type: "toolbar",

        _defaultConfig: function () {
            return $.extend(FR.Toolbar.superclass._defaultConfig.apply(), {
                baseCls: "x-toolbar",
                items: []
            });
        },

        _init: function () {
            FR.Toolbar.superclass._init.apply(this, arguments);
            var o = this.options;
            var toolbar_h = 28;

            this.element.addClass(o.baseCls);
            this.element.css('height', toolbar_h);
            // carl:设置背景,默认的就没有toolbarbg
            if (o.toolbarbg) {
                var bg = o.toolbarbg;
                var dom = this.element;
                FR.setBackground(dom, bg, dom.height());
            }

            this.$tr = $("<tr>");
            this.$tr.appendTo($("<tbody>")
                .appendTo($("<table cellspacing=0>").css({
                    "height": toolbar_h,
                    "vertical-align": "center"
                }).appendTo(this.element)));

            if (o.toolbarbg && (o.toolbarbg["type"] === 4 || o.toolbarbg["type"] === 2)
                && FR.Browser.isIE() && FR.ieForFixBackground()) {
                this.$tr.css({
                    position: 'absolute',
                    top : '0px'
                })
            }

            var addedItems = [];
            /*
             * alex:加入控件
             */
            if (o.items != null) {
                if (!FR.isArray(o.items)) {
                    o.items = [o.items];
                }

                for (var i = 0; i < o.items.length; i++) {
                    if (i > 0) {
                        this.addSep();
                    }
                    var item = o.items[i];
                    var comp = FR.createWidget(item);
                    addedItems.push(comp);
                    if (item.widgetName) {
                        this[item.widgetName.toLowerCase()] = comp;
                    }
                    this.addJQuery(comp.element);
                }
                o.items = addedItems;
            }
        },

        getWidgetByName : function(name) {
            var items = this.options.items;
            for (var i = 0, len = items.length; i < len; i ++) {
                var item = items[i];
                var wn = item.options.widgetName;
                if (wn && name && wn.toUpperCase() === name.toUpperCase()) {
                    return item;
                }
            }
        },

        /**
         * @private
         * @param {jQuery} $jq 将jQuery元素加到工具栏中
         */
        addJQuery: function ($jq) {
            if (FR.Browser.isIE() && !$.support.boxModel) {
                this.element.css('overflow', 'hidden');
            }
            if ($jq != null) {
                var $td = $("<td>").append($jq);
                // ie杂项 东西比较多的话会被撑大比如showCellValue这个label
                //bug78450.内容撑大vertical设成top没啥用吧。先注释了
                //if ($.browser.msie && !$.support.boxModel) {
                //    $td.css('vertical-align', 'top');
                //}
                this.$tr.append($td)
            }
        },

        /**
         * @private
         * 添加一个分隔符
         */
        addSep: function () {
            this.addJQuery("<span class='sep'> </span>")
        }
    });
    $.shortcut("toolbar", FR.Toolbar);

    /**
     * 菜单按钮
     * @class FR.MenuButton
     * @extends FR.Button
     */
    FR.MenuButton = FR.extend(FR.Button, {
        _init: function () {
            FR.MenuButton.superclass._init.apply(this, arguments);
            if(this.$em){
                this.$em.addClass('fr-btn-arrow');
            }
            var menu = this.options.menu;
            if (FR.isArray(menu)) {
                this.on("click", function () {
                    FR.showMenuByEl({
                        destroyOnClose: true,
                        items: menu,
                        minWidth: 175,
                        xxxfixclientH: this.options.istoolbarmenu
                            ? true
                            : false
                    }, this.element)
                }.createDelegate(this));
                if( this.$table){
                    this.$table.mouseleave(function (e) {
                        if (this.fr_menu) {
                            var bounds = {
                                x: this.fr_menu.menu.$menuRoot.offset().left,
                                y: this.fr_menu.menu.$menuRoot.offset().top
                                - 1,
                                width: this.fr_menu.menu.$menuRoot.bounds().width,
                                height: this.fr_menu.menu.$menuRoot.bounds().height
                            };
                            if (e.clientY < bounds.y
                                || e.clientY > bounds.y + bounds.height
                                || e.clientX < bounds.x
                                || e.clientX > bounds.x + bounds.width) {
                                this.fr_menu.menu.close();
                            }
                        }
                    }.createDelegate(this.element));
                }
            }
        }
    });
    $.shortcut("menubutton", FR.MenuButton);

})(jQuery);