;
(function ($) {
    /**
     * 网页框控件,用于加载一般的网页
     *
     *      @example
     *      var editor = new FR.IframeEditor({
     *          renderEl : 'body',
     *          src : "http://www.baidu.com",
     *          showOverFlowX : true,
     *          showOverFlowY : true
     *      });
     *
     * @class FR.IframeEditor
     * @extends FR.BaseEditor
     * @cfg {String} src 网页框内容指向的地址
     * @cfg {Boolean} [showOverFlowX=true] 显示横向滚动条
     * @cfg {Boolean} [showOverFlowY=true] 显示纵向滚动条
     */
    FR.IframeEditor = FR.extend(FR.BaseEditor, {

        _defaultConfig: function () {
            return $.extend(FR.IframeEditor.superclass._defaultConfig.apply(), /**@class FR.IframeEditor*/{
                baseName: 'fr_iframe',
                baseClass: 'fr_iframeeditor',
                widgetName: "",
                src: "",
                width: "100%",
                height: "100%",
                showOverFlowX: true,
                showOverFlowY: true
            });
        },
        _init: function () {
            FR.IframeEditor.superclass._init.apply(this, arguments);
            this.initData();
            // 控件属性
            var o = this.options;

            this.editComp = this.element.css({
                width: '100%',
                height: '100%'
            });
            o.iframeName = o.widgetName || this.createNoRepeatName();
            this.$iframe = $("<iframe  name=" + o.iframeName + " id="
                + o.iframeName + ">").css({
                    width: o.width,
                    height: o.height
                }).addClass(o.baseClass).attr({
                    frameborder: 0,
                    scrolling: !o.showOverFlowX && !o.showOverFlowY
                        ? 'no'
                        : 'yes'
                }).appendTo(this.editComp);
            // 这里滚动条的设置 跟子页面的设置有关
            // 如果嵌入的是我们的报表或者表单 window的尺寸都是100%那就取了iframe的大小
            // 如果报表或表单内容尺寸超过的话 就会出现滚动条 那么就改下子元素 这时一般不跨域
            this.$iframe[0].onload = function () {
                if (!o.showOverFlowX || !o.showOverFlowY) {
                    try { // 可能跨域 那就不用管了
                        if (this.contentWindow) {
                            var doc = this.contentWindow.document;
                            var cc = $('.content-container', $(doc));

                            if (cc.length > 0) {
                                setOverflow(cc);
                            } else {
                                // 有可能是表单 html和body都要设置
                                setOverflow($(doc.documentElement));
                                setOverflow($(doc.body));
                            }
                            function setOverflow(cc) {
                                if (!o.showOverFlowX) {
                                    cc.css('overflow-x', 'hidden');
                                }
                                if (!o.showOverFlowY) {
                                    cc.css('overflow-y', 'hidden');
                                }
                            }
                        }
                    } catch (e) {
                        console && console.log("error");
                    }
                }
            }

            // IE里面需要设置rowspan为1才能正常显示....否则会跳过n行不显示,chrome应该是bug,一会儿好一会儿不好
            if ($.browser.msie && (this.$iframe.parent()).is("div")
                && (this.$iframe.parent().parent()).is("td")
                && (this.$iframe.parent().parent().attr("widget")) != null
                && (this.$iframe.parent().parent().parent()).is("tr")) {
//            this.$iframe.parent().parent().attr('rowspan', '1');
                // 上面这么搞有问题的,IE7或IE8杂项下,合并单元格中的网页框控件会只显示在第一行的,改成下面
                this.$iframe.parent().height("");
            } else {
                this.$iframe.css('overflow-x', o.showOverFlowX ? 'auto' : 'hidden');
                this.$iframe.css('overflow-y', o.showOverFlowY ? 'auto' : 'hidden');
            }

            if (o.controlAttr) {
                this.setValue(o.controlAttr);
            } else if (o.src) {
                this._loadIframeByGet();
            }
            if (o.disabled) {
                this._addDisableMask();
            }
        },

        createNoRepeatName: function () {
            var len = $('iframe' + this.options.baseClass).length;
            while ($('iframe[name=' + this.options.baseName + len + ']').length !== 0) {
                len++;
            }
            return this.options.baseName + len;
        },

        // richer:以get的方式获取ifram的参数
        _loadIframeByGet: function () {
            var src = this.options.src, self = this;
            // 获取参数组
            if (this.options.data) {
                //bug:63869这个遍历没有必要下面都有ifelse了
                //for (var i = 0, len = this.options.data.getLength(); i < len; i++) {
                //    var params = this.options.data.getRecord(i).getContent();
                //    src = src.appendQuery(params);
                //}

                var waitForInitComplete = [];

                for (var i = 0, len = this.options.data.getLength(); i < len; i++) {
                    var params = this.options.data.getRecord(i).getContent();
                    if (typeof params == 'object' && params.widgetName && this.options.form) {
                        var w = this.options.form.getWidgetByName(params.widgetName);
                        if (w) {
                            var para = {};
                            para[params.widgetName] = w.getValue();
                            src = src.appendQuery(para);
                        } else {
                            waitForInitComplete.push(params.widgetName);
                        }
                    } else {
                        src = src.appendQuery(params);
                    }
                }

                if (waitForInitComplete.length > 1) {
                    var form = this.options.form;
                    form.on(FR.Events.AFTERINIT, function () {
                        for (var c = 0; c < waitForInitComplete.length; c++) {
                            var name = waitForInitComplete[c];
                            var para = {};
                            para[name] = form.getWidgetByName(name).getValue();
                            src = src.appendQuery(para);
                        }
                        self._changeIframe(src);
                    });

                }
            }
            this._changeIframe(src);
        },

        /**
         * 添加一层遮罩层,用于屏蔽iframe内容的可用性
         * @private
         */
        _addDisableMask: function () {
            this.mask = $('<span/>').css({
                backgroundColor : '#66B9FF',
                height: this.options.height,
                width: this.options.width,
                left: this.element.offset().left
            }).appendTo(this.element);
        },

        /**
         * 获取网页框的src地址
         * @returns {String} 地址
         */
        getValue: function () {
            return this.$iframe.attr("src");
        },

        _dealValueWithEvents: function (src) {
            this.options.src = src;
            this._changeIframe(src);
        },

        /**
         * 改变iframe的src指向的地址
         * @param src 新的地址
         * @private
         */
        _changeIframe: function (src) {
            this.$iframe.attr("src", src);
        },

        setEnable: function (enable) {
            FR.IframeEditor.superclass.setEnable.apply(this, arguments);
            if (enable) {
                if (this.mask) {
                    this.mask.remove();
                }
            } else {
                if (this.mask) {
                    this.mask.show();
                } else {
                    this._addDisableMask();
                }
            }
        },

        doResize: function (give) {
            FR.IframeEditor.superclass.doResize.apply(this, arguments);
            if (this.submitForm) {
                this.submitForm.submit();
            }
        },
        reset: function () {
            this.options.data.clearData();
            this._loadIframeByGet();
        }
    });
    $.shortcut("iframe", FR.IframeEditor);
})(jQuery);