
var Pop = {
    Version: '1.0.0',
    REQUIRED_PROTOTYPE: '1.6.0.3',
    PageElemID: "WholePage",
    PopIFrameElemID: "PopIFrame",
    PopBGElemID: "PopBG",
    PopCloseElemID: "PopClose",
    CloseButtonPosition: 3, // 1=topleft, 2=topcenter, 3=topright
    load: function() {
        // verify the prototype is loaded and right version
        function convertVersionString(versionString) {
            var v = versionString.replace(/_.*|\./g, '');
            v = parseInt(v + '0'.times(4 - v.length));
            return versionString.indexOf('_') > -1 ? v - 1 : v;
        }
        if ((typeof Prototype == 'undefined') || (typeof Element == 'undefined') || (typeof Element.Methods == 'undefined') || (convertVersionString(Prototype.Version) < convertVersionString(Pop.REQUIRED_PROTOTYPE))) {
            throw ("CommerceCM requires the Prototype JavaScript framework >= " + Pop.REQUIRED_PROTOTYPE);
        }
    },
    show: function(id) {

        /*alert("scroll:" + this.getScrollAmount());
        alert("wheight:" + this.getWindowHeight());
        alert("pheight:" + this.getAddedPopupHeight(id));
        alert("wwidth:" + this.getWindowWidth());
        alert("pwidth:" + this.getAddedPopupWidth(id));*/

        // grayout background
        $(this.PopBGElemID).setStyle({ top: 0 + "px", left: 0 + "px" });
        // calculate where center is
        var popupCenterY = Math.round(this.getScrollAmount() + (this.getWindowHeight() / 2) - (this.getAddedPopupHeight(id) / 2));
        var popupCenterX = Math.round((this.getWindowWidth() / 2) - (this.getAddedPopupWidth(id) / 2));
        // position the added to cart message
        if (popupCenterY < 20) { popupCenterY = 20; }
        if (popupCenterX < 20) { popupCenterX = 20; }

        /*alert(popupCenterY);
        alert(popupCenterX);*/

        $(this.PopIFrameElemID).setStyle({ top: popupCenterY + "px", left: popupCenterX + "px", height: this.getAddedPopupHeight(id) + "px" });
        $(id).setStyle({ top: popupCenterY + "px", left: popupCenterX + "px" });
        if (this.CloseButtonPosition == 3) {
            var closeX = popupCenterX + this.getAddedPopupWidth(id) - $(this.PopCloseElemID).getWidth() - 20;
            $(this.PopCloseElemID).setStyle({ top: (popupCenterY + $(this.PopCloseElemID).getHeight() / 4) + "px", left: closeX + "px" });
        }
        if ($(this.PageElemID)) {
            var pageHeight = $(this.PageElemID).getHeight();
            if (pageHeight > this.getWindowHeight()) {
                $(this.PopBGElemID).setStyle({ width: this.getWindowWidth() + "px", height: pageHeight + "px" });
            }
        }
        return 0;
    },
    hide: function(id) {
        $(id).setStyle({ top: -2000 + "px", left: -2000 + "px" });
        $(this.PopCloseElemID).setStyle({ top: -2000 + "px", left: -2000 + "px" });
        $(this.PopIFrameElemID).setStyle({ top: -2000 + "px", left: -2000 + "px" });
        $(this.PopBGElemID).setStyle({ top: -3000 + "px", left: -3000 + "px", height: 100 + "%" });
        return 0;
    },
    getWindowWidth: function(win) {
        var width;
        win = win ? win : window;
        width = win.innerWidth || (win.document.documentElement.clientWidth || win.document.body.clientWidth);
        return width;
    },
    getWindowHeight: function(win) {
        var height;
        win = win ? win : window;
        height = win.innerHeight || (win.document.documentElement.clientHeight || win.document.body.clientHeight);
        return height;
    },
    getAddedPopupWidth: function(id) {
        return $(id).getWidth();
    },
    getAddedPopupHeight: function(id) {
        return $(id).getHeight();
    },
    getScrollAmount: function() {
        if (typeof window.pageYOffset != "undefined") {
            var scroll = window.pageYOffset;
        } else if (typeof document.documentElement.scrollTop != "undefined") {
            var scroll = document.documentElement.scrollTop;
        } else {
            var scroll = document.body.scrollTop;
        }
        return scroll;
    }
};

function loadPop() {
    Pop.load();
}

addPageLoad_Handler(loadPop);

