/*jslint browser: true, eqeqeq: true, undef: true, white: true, indent: 4 */
/*global window */
function PopupWindowController(id, clientId) {
    this.id = id;
    this.clientId = clientId;
    PopupWindowController.popups[id] = this;
    PopupWindowController.popups[clientId] = this;
}
     
PopupWindowController.popups = [];

PopupWindowController.form = null;

PopupWindowController.showPopup = function (id) {
    var popup = PopupWindowController.popups[id];
    if (!popup) {
        return false;
    } else if (PopupWindowController.form !== null) {
        var form = document.getElementById(PopupWindowController.form);
        var main = document.getElementById(popup.clientId + "popup_dialog");
        main.parentNode.removeChild(main);
        form.appendChild(main);
    }
    return popup.showPopup();
};

PopupWindowController.hidePopup = function (id) {
    if (!id) {
        if (PopupWindowController.currentPopup) {
            PopupWindowController.currentPopup.hidePopup();
        }
    } else {
        var popup = PopupWindowController.popups[id];
        if (!popup) {
            return;
        }
        popup.hidePopup();
    }
};

PopupWindowController.getPageSize = function ()
{
    var width = 0;
    var height = 0;
    if (typeof window.innerWidth === "number" &&
        typeof window.scrollMaxX === "number") {
        width = window.innerWidth - 18 + window.scrollMaxX;
        height = window.innerHeight + window.scrollMaxY;
    } else if (typeof document.body.offsetWidth === "number" &&
             typeof document.body.offsetLeft === "number") {
        width = document.body.offsetWidth + document.body.offsetLeft;
        height = document.body.offsetHeight + document.body.offsetTop;
    } else if (typeof document.body.scrollWidth === "number" &&
             typeof document.body.offsetWidth === "number") {
        width = document.body.scrollWidth;
        height = document.body.scrollHeight;
    }

    return { width: width, height: height };
};

PopupWindowController.getViewport = function () {
    var width = 0;
    var height = 0;
    if (document.body && typeof document.body.clientWidth === "number") {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    } else if (typeof window.innerWidth === "number") {
        width = window.innerWidth - 18;
        height = window.innerHeight - 18;
    } else if (document.documentElement &&
        typeof document.documentElement.clientWidth === "number") {
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    }
   
    if (width < 0) {
        width = 0;
    }

    if (height < 0) {
        height = 0;
    }

    return { width: width, height: height };
};

PopupWindowController.getScrolling = function ()
{
    var scrollX = 0;
    var scrollY = 0;
    if (document.documentElement &&
            typeof document.documentElement.scrollLeft === "number" && document.documentElement.scrollLeft !== 0) {
        // Case: IE compliant.
        scrollX = document.documentElement.scrollLeft;
        scrollY = document.documentElement.scrollTop;
    } else if (typeof window.pageYOffset === "number") {   
        // Case: Netscape compliant.
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    } else if (document.body && typeof document.body.scrollLeft === "number") {   
        // Case: DOM compliant.
        scrollX = document.body.scrollLeft;
        scrollY = document.body.scrollTop;
    }

    return { scrollX: scrollX, scrollY: scrollY };
};

PopupWindowController.getPopupWidth = function (popup) {
    return popup.offsetWidth;
};

PopupWindowController.getPopupHeight = function (popup) {
    return popup.offsetHeight;
};

PopupWindowController.prototype.showPopup = function () {
    var main = document.getElementById(this.clientId + "popup_dialog");
    var popup = document.getElementById(this.clientId + ":popup");
    var hidden = document.getElementById(this.clientId + "popup_display");
    if (!main || !popup) {
        return false;
    }
    
    var pageSize = PopupWindowController.getPageSize();
    main.style.display = 'block';
    main.style.width = pageSize.width + "px";
    main.style.height = pageSize.height + "px";
    hidden.value = "true";
    PopupWindowController.currentPopup = this;
    PopupWindowController.timer = setInterval(function () {
        PopupWindowController.currentPopup.repaint();
    }, 100);
    return true;
};

PopupWindowController.prototype.repaint = function () {
    var popup = document.getElementById(this.clientId + ":popup");
    var width = PopupWindowController.getPopupWidth(popup);
    var height = PopupWindowController.getPopupHeight(popup);
    
    var viewport = PopupWindowController.getViewport();
    var scrolling = PopupWindowController.getScrolling();
    
    var pageX = scrolling.scrollX + ((viewport.width - width) / 2);
    var pageY = scrolling.scrollY + ((viewport.height - height) / 2);

    popup.style.top = pageY + "px";
    popup.style.left = pageX + "px";
};

PopupWindowController.prototype.hidePopup = function () {
    var main = document.getElementById(this.clientId + "popup_dialog");
    var hidden = document.getElementById(this.clientId + "popup_display");
    if (!main) {
        return;
    }
    clearInterval(PopupWindowController.timer);
    hidden.value = "false";
    main.style.display = 'none';
    PopupWindowController.currentPopup = null;
};

