﻿/*
    css_popup.js
    controls css div popup for flash demos
    3lcd.com
*/

/**
 * Singleton class representing a modal css popup 
 */
var CssModalPopup = new function() {
    // private members
    var POPUP_WIDTH = 381;
    var POPUP_HEIGHT = 272;
    var CSS_POPUP_ZINDEX = 99999;
    var CSS_MODAL_POPUP_ORIG_DIV = "css_modal_popup";
    var CSS_MODAL_POPUP_BACKGROUND_ID = "css_modal_popup_background";
    var CSS_MODAL_POPUP_BACKGROUND_COLOR = "#000000";

    var m_self = this;
    var m_popup = null;
    var m_popupBackground = null;
    
    // public methods
    this.show = function(demoDivID) {
        var CSS_MODAL_POPUP_ID = demoDivID;
        // ctl00_ctl00_content_CPH_DIV_content_CPH_TOP_content_CPH_

        // OLD IE6 SNIFF TO LAUNCH NEW POPUP - REMOVED
        
        /*if (isIE6OrLess()) {
            var vp = new Viewport();
            var winl = (vp.windowX - POPUP_WIDTH) / 2;
            var wint = (vp.windowY - POPUP_HEIGHT) / 2;
            presenter = window.open("/includes/css_modal_popup.aspx",
                                    "cssModalPopup",
                                    "height=" + POPUP_HEIGHT + 
                                    ",width=" + POPUP_WIDTH + 
                                    ",top=" + wint + 
                                    ",left=" + winl + 
                                    ",toolbar=no,directories=no,status=no" + 
                                    ",menubar=no,scrollbars=no,resizable=no");
            presenter.focus();
            window.handlePopupContentLoaded = function(placeholder) {
                m_self.writeContentToNewWindow(placeholder);
            }
        } else {*/
			if (m_popupBackground) {
                m_popupBackground.style.display = "block";
            }
            else
            {
                m_popupBackground = document.createElement("div");            
                m_popupBackground.id = CSS_MODAL_POPUP_BACKGROUND_ID;
                //divPopupBG = document.getElementById(CSS_MODAL_POPUP_BACKGROUND_ID);
                //returns error!
                m_popupBackground.style.zIndex = CSS_POPUP_ZINDEX;
                
                    var x,y;
                    var test1 = document.body.scrollHeight;
                    var test2 = document.body.offsetHeight
                    if (test1 > test2) // all but Explorer Mac
                    {
	                    x = document.body.scrollWidth;
	                    y = document.body.scrollHeight;
                    }
                    else // Explorer Mac;
                         //would also work in Explorer 6 Strict, Mozilla and Safari
                    {
	                    x = document.body.offsetWidth;
	                    y = document.body.offsetHeight;
                    }
                    //alert('X: '+x+', Y: '+y);
                    
                    //alert(y);
                    var newPageHeight = y+28;
                    m_popupBackground.setAttribute("style", "height:"+newPageHeight+"px;z-index:"+CSS_POPUP_ZINDEX+";");
                document.body.insertBefore(m_popupBackground, document.body.firstChild);
            }

            //m_popup = document.getElementById(CSS_MODAL_POPUP_ID);
            //m_popup = originalNode.cloneNode(true);
            //originalNode.parentNode.removeChild(originalNode);

            var originalNode = document.getElementById(CSS_MODAL_POPUP_ID);
            m_popup = originalNode.cloneNode(true);
            originalNode.parentNode.removeChild(originalNode);
            m_popup.style.display = "block";
            m_popup.style.zIndex = CSS_POPUP_ZINDEX + 1;
            m_popup.style.width = getPxString(POPUP_WIDTH);
            m_popup.style.height = getPxString(POPUP_HEIGHT);
            //m_popup.setAttribute("style", "z-index:100000;");
            document.body.insertBefore(m_popup, document.body.firstChild);
            positionPopupInCenter();
            //window.onresize = window.onscroll = positionPopupInCenter;
            window.onresize = positionPopupInCenter;
        //}

    }

    this.hide = function() {
        window.onresize = window.onscroll = null;
        m_popup.style.display = "none";
        m_popupBackground.style.display = "none";
    }
    
    this.writeContentToNewWindow = function(placeholder) {
        var originalNode = document.getElementById(CSS_MODAL_POPUP_ID);
        m_popup = originalNode.cloneNode(true);
        m_popup.style.display = "block";
        
        for (i in m_popup.childNodes) {
            switch (m_popup.childNodes[i].id) {
                case "close_win_btn":
                    m_popup.removeChild(m_popup.childNodes[i]);
            }
        }
        
        placeholder.innerHTML = m_popup.outerHTML;
    }
    
    // private methods
    function positionPopupInCenter() {
        var viewport = new Viewport();
        //m_popupBackground.style.width = getPxString(viewport.pageX);
        //m_popupBackground.style.height = getPxString(viewport.pageY);        

        m_popup.style.left = getPxString(
            ((viewport.windowX - parseInt(m_popup.style.width)) / 2) + 
            viewport.scrollX);
            
        m_popup.style.top = getPxString(
            ((viewport.windowY - parseInt(m_popup.style.height)) / 2) +
            viewport.scrollY);
    }
    
    function isIE6OrLess() {
        var msie = "msie", i = 5, j = 1;
        var agent = navigator.userAgent.toLowerCase();
        var version = agent.substr(agent.indexOf(msie) + i, j);
        version = (version > -1) ? version : -1;
        return (version > -1) && (version < 7);
    }
    
    function getPxString(val) {
        return val + "px";
    }
}

/**
 * Helper class represents the browser window
 */
function Viewport() { 
    this.windowX = (document.documentElement && 
                    document.documentElement.clientWidth) || 
                    window.innerWidth || 
                    self.innerWidth || 
                    document.body.clientWidth; 
    this.windowY = (document.documentElement && 
                    document.documentElement.clientHeight) || 
                    window.innerHeight || self.innerHeight || 
                    document.body.clientHeight; 
    this.scrollX = (document.documentElement && 
                    document.documentElement.scrollLeft) || 
                    window.pageXOffset || 
                    self.pageXOffset || 
                    document.body.scrollLeft; 
    this.scrollY = (document.documentElement && 
                    document.documentElement.scrollTop) || 
                    window.pageYOffset || 
                    self.pageYOffset || 
                    document.body.scrollTop; 
    this.pageX = (document.documentElement && 
                  document.documentElement.scrollWidth) 
               ? document.documentElement.scrollWidth 
               : (document.body.scrollWidth > document.body.offsetWidth) 
                   ? document.body.scrollWidth 
                   : document.body.offsetWidth; 
    this.pageY = (document.documentElement && 
                  document.documentElement.scrollHeight) 
               ? document.documentElement.scrollHeight 
               : (document.body.scrollHeight > document.body.offsetHeight) 
                   ? document.body.scrollHeight 
                   : document.body.offsetHeight;
}
