﻿var Dialog = Class.create({
    initialize: function(content) {
        this.build(content);
        if (content) {
            this.setContent(content);
        }
    },
    build: function(content) {
        this._container = new Element("DIV", { id: "divDialogContainer" }).hide();
        this._container.setStyle({ overflow: "hidden", backgroundColor: "#000000", top: "0px", left: "0px", zIndex: "500",
            position: "fixed", width: "100%", height: "100%"
        });
        this._container.setOpacity(0.4);

        $(document.body).insert(this._container);

        this._content = new Element("DIV", { align: "center", id: "divDialogContent" }).hide();
        this._content.setStyle({ zIndex: "500001", position: "fixed", top: "50px", width: "100%" });
        this._content.childId = null;
        $(document.body).insert(this._content);

        if (this.needIEFix()) {
            this.fixIE();
            this.bindIEEvents.bind(this).defer();
        }
    },

    setUsingAppendChild: function(objChild) {
        //debugger;
        this.clearContent();
        this._content.childId = objChild.id;
        this._content.oldParentId = objChild.parentNode.id; //objChild.parentElement.id;
        this._content.appendChild(objChild);

    },

    setContent: function(content) {
        this.clearContent();
        this._content.update(content);
    },

    clearContent: function() {
        if (this._content.childId) {
            $(this._content.oldParentId).appendChild($(this._content.childId));
            this._content.oldParentId = null;
            this._content.childId = null;
        }
        this._content.update("&nbsp;");
    },

    show: function(setFocusOn) {
        this._content.show();
        if (this._iframe) {
            this._iframe.show();
        }
        this._container.show();

        if ($(setFocusOn)) {
            $(setFocusOn).focus();
        }
    },
    hide: function() {
        this._content.hide();
        if (this._iframe) {
            this._iframe.hide();
        }
        this._container.hide();
    },
    needIEFix: function() {
        if (Prototype.Browser.IE) {
            var nav = navigator.userAgent;
            var index = navigator.userAgent.indexOf("MSIE");
            var version = "";
            while ((nav.charAt(index + 4) != ";") && (index < 100)) {
                version += nav.charAt(index + 4);
                index++
            }
            return (parseInt(version) < 7);
        }
        return false;
    },
    fixIE: function() {
        var height = $(document.body).getHeight();
        this._container.setStyle({ position: "absolute", height: height });
        this._content.setStyle({ position: "absolute" });
        this._iframe = new Element("IFRAME", { src: "about:blank", scrolling: "no", frameBorder: "0" }).hide();
        this._iframe.setStyle({ position: "absolute", width: "100%", height: height });
        this._iframe.setStyle({ top: "0px", left: "0px", zIndex: "499" });
        this._iframe.setOpacity(0.4);
        $(document.body).insert(this._iframe);
    },
    bindIEEvents: function() {
        Event.observe(window, "resize", this.onIEResize.bindAsEventListener(this));
        Event.observe(document.body, "scroll", this.onIEResize.bindAsEventListener(this));
    },
    onIEResize: function() {
        var height = $(document.body).getHeight();
        this._container.setStyle({ height: height });
        this._iframe.setStyle({ height: height });
        this.onIEScroll(null);
    },
    onIEScroll: function() {
        var offset = $(document.body).cumulativeScrollOffset();
        this._content.setStyle({ top: (offset.top + 100) + "px" });

        if (offset.left > 0) {
            this._container.setStyle({ left: offset.left + "px" });
            this._iframe.setStyle({ left: offset.left + "px" });
        }
        else {
            this._container.setStyle({ left: "0px" });
            this._iframe.setStyle({ left: "0px" });
        }
    }
});