﻿namespace('nnet');

nnet.PopUpWindow = Ext.extend(Ext.Window, {
    minWidth: 200,
    minHeight: 100,
    url: "",
    titlePrefix: "",
    closable: true,
    maximizable: false,
    width: 500,
    height: 600,

    initComponent: function() {
        // if require height is more than window height adjust
        browserHeight = $j(window).height();
        if (browserHeight < this.height) this.height = browserHeight - 25;
        var winId = (new Date()).getTime().toString();
        var taggedURL = this.url + (this.url.indexOf('?') == -1 ? "?" : "&") + "WID=" + winId;
        Ext.apply(this, {
            closable: this.closable,
            maximizable: this.maximizable,
            id: winId,
            height: this.height,
            width: this.width,
            layout: 'fit',
            items: [{
                xtype: 'iframepanel',
                id: "f" + winId,
                layout: 'fit',
                defaultSrc: taggedURL,
                loadMask: { msg: 'Loading. Please wait...' },
                listeners: { documentloaded: function() { this.ownerCt.setTitleFromDocument(this), this.ownerCt.resize(this) } }
}]
            });

            if (this.onClose) this.addListener("close", this.onClose);


            // Call parent (required)
            nnet.PopUpWindow.superclass.initComponent.apply(this, arguments);

            // After parent code
            // e.g. install event handlers on rendered component
        },

        setTitleFromDocument: function(iframe) {
            this.setTitle(this.titlePrefix + iframe.getFrameDocument().title);
        },

        resize: function(iframe) {
            try {
                //calc current offsets for Window body border and padding
                var iframeContents = iframe.getFrameBody();
                if (iframeContents != null) {
                    var maxHeight = $j(window).height() - 25;
                    var heightAdjust = 15;
                    if (BrowserDetect.browser == "Firefox") heightAdjust = 40;
                    var requiredWidth = iframe.getFrameBody().scrollWidth + this.getFrameWidth() + 10;
                    var requiredHeight = iframe.getFrameBody().scrollHeight + this.getFrameHeight() + heightAdjust;
                    var height = requiredHeight > maxHeight ? maxHeight : (requiredHeight < this.minHeight ? this.minHeight : requiredHeight);
                    var width = requiredWidth > this.minWidth ? requiredWidth : this.minWidth;
                    this.setSize(width, height);
                }
            } catch (e) { logE("NNet.PopUpWindow.resize", e); }
        }
    })

nnet.PopUpWindow.resizeFromPopup = function(winId) {
    iframe = Ext.getCmp("f" + winId);
    win = iframe.ownerCt;
    win.resize(iframe);
};

// Not working, need to find reference to iframe in iframe object to call refresh.
// called by parent.nnet.PopUpWindow.refreshAll();
nnet.PopUpWindow.refreshAll = function() {
    Ext.WindowMgr.each(function(win) {
        iframe = win.body.dom.children[0];
        iframe.Refresh();
    });
};

Ext.reg('popupwindowxtype', nnet.PopUpWindow);


