/**
 * Popup-frame related classes and functions
 *
 * Some content pages (e.g. shopping cart) are stored in hidden iframes
 * on the main page, to allow better client-side performance
 * and request-scoped forms without much hassle.
 *
 * @author Daniel Lichtenberger, UCS
 */

function PopupDispatcher() {
    this.activePopup = null;
    this.popups = new Array();
    this.inShow = false;
    this.inHide = false;
    this.contentHidden = false;
    this.keepCartPopup = false;
    this.showOnFrameLoaded = null;
}

PopupDispatcher.prototype = {
    add: function(popup) {
        popup.dispatcher = this;
        this.popups.push(popup);
    },

    show: function(popup) {
        if (this.inShow) {
            return;     // prevent recursion
        }
        this.inShow = true;
        document.getElementById("content").style.visibility = "hidden";
        this.contentHidden = true;
        for (var i = 0; i < this.popups.length; i++) {
            try {
                this.popups[i].visible = (popup == this.popups[i]);
                this.popups[i].refresh();
            } catch (e) {
                this.inShow = false;
                throw e;
            }
        }
        this.inShow = false;
    },

    hide: function() {
        for (var i = 0; i < this.popups.length; i++) {
            if (!(this.keepCartPopup && this.popups[i].name == "cartFrame")) {
                this.popups[i].visible = false;
            } else {
                this.keepCartPopup = false;
            }
            this.popups[i].refresh();
        }
    },

    onPopupHide: function() {
        if (this.inHide) {
            return;     // prevent recursion
        }
        var contentVisible = true;
        for (var i = 0; i < this.popups.length; i++) {
            if (this.popups[i].visible) {
                contentVisible = false;
                break;
            }
        }
        if (contentVisible && this.contentHidden) {
            // display hidden content
            document.getElementById("content").style.visibility = "visible";
            this.contentHidden = false;
        }
    },

    // reloads all initialized popups
    reload: function() {
        for (var i = 0; i < this.popups.length; i++) {
            this.popups[i].reload();
        }
    }
}

function PopupFrameInfo(id, name, contentUrl, contentForm, lazyInit) {
    this.id = id;
    this.name = name;
    this.contentUrl = contentUrl;
    this.contentForm = contentForm;
    this.lazyInit = lazyInit;
    this.visible = false;
    this.initialized = !lazyInit;
    this.dispatcher = null; // inserted by dispatcher
    this.iframeRendered = false;    // status flag
    this.skipReload = false;    // allow a popup to skip a single reload (e.g. after a login in the shopping cart frame)
    // store DOM context
    this.document = document;
    this.window = window;
    this.frames = frames;

    // callback handlers
    this.onShow = null;     // parameter-less handler called when frame is shown
}

PopupFrameInfo.prototype = {
    emptyPage: getBase() + "empty.jsp",

    show: function() {
        this.visible = true;
        if(this.name=="searchFormFrame" && getObj("extendedsearchtext_close") && getObj("extendedsearchtext_open")) {
            getObj("extendedsearchtext_close").style.display = "";
            getObj("extendedsearchtext_open").style.display = "none";
        }
        if (browserInfo.isIE) {
            this.getObject().style.display = "";
        }
        if (!this.initialized) {
            // load content
            this.getFrame().location.href = this.contentUrl;
            this.initialized = true;
        } else {
            if (this.onShow) {
                // call custom onshow handler
                this.onShow();
            }
        }
        this.getObject().style.width = dimensions.contentWidth + "px";
        this.getObject().style.height = Math.max(1, getWindowHeight(this.document)
                - dimensions.headerHeight - dimensions.menuBarHeight) + "px";
        if (this.dispatcher != null) {
            this.dispatcher.show(this);     // notify dispatcher, hide other popups
        }
    },
                                                                                                       
    hide: function() {
        if(this.name=="searchFormFrame" && getObj("extendedsearchtext_close") && getObj("extendedsearchtext_open")) {
            getObj("extendedsearchtext_close").style.display = "none";
            getObj("extendedsearchtext_open").style.display = "";
        }
        // currently we can't use display:none to hide iframes, because
        // KHTML then drops the iframe completely
        this.visible = false;
        if (this.getObject()) {
            this.getObject().style.width = "0px";
            this.getObject().style.height = "0px";
            if (browserInfo.isIE) {
                // need to hide the element because the onresize event won't update
                // the layout when the frame is "invisible" - bug 1830
                this.getObject().style.display = "none";
                window.focus(); // prevents blinking cursor from hidden frame in IE6
            }
        }
        if (this.dispatcher != null) {
            this.dispatcher.onPopupHide();
        }
    },

    refresh: function() {
        if (this.visible) {
            this.show();
        } else {
            this.hide();
        }
    },

    toggle: function() {
        this.visible = !this.visible;
        this.refresh();
    },

    load: function(url) {
        this.getFrame().location.href = url;
        this.initialized = true;
    },

    reload: function(action) {
        if (!this.isInitialized() || this.skipReload) {
            this.skipReload = false;    // skip reload only once
            return;
        }
        if (this.getForm() != null) {
            // popup contains form, re-submit it
            this.getForm()["action"].value = action != null ? action : "reload";
            this.getForm().submit();
        } else {
            // stateless popup page, simply reload it
            this.getFrame().location.reload(true);
        }
    },

    renderIframe: function() {
        this.iframeRendered = true;
        this.document.writeln("<iframe src=\"" + this._getInitialIframeUrl() + "\" name=\"" + this.name + "\" id=\"" + this.name + "\" "
            + "scrolling=\"no\" frameBorder=\"0\" width=\"0\" height=\"0\" class=\"popupFrame\" style=\"z-index: 100\" allowTransparency=\"true\" style=\"background-color:transparent\"></iframe>");
    },

    getFrame: function() {
        return this.iframeRendered ? this.frames[this.name] : null;
    },

    getForm: function() {
        return this.contentForm != null ? this.getFrame().document.forms[this.contentForm] : null;
    },

    isInitialized: function() {
        return this.initialized;
    },

    getObject: function() {
        return this.iframeRendered ? this.document.getElementById(this.name) : null;
    },

    _getInitialIframeUrl: function() {
        return this.lazyInit ? (this.emptyPage + "?id=" + this.id) : this.contentUrl;
    }
}


