MapThatPad_BM = function() {
    var overlay,mainDiv,headerDiv,iu,pl,pt; 
    var B;
    var D;
    return {

        getDocHeight:function() {
            return Math.max(
                Math.max(B.scrollHeight, D.scrollHeight),
                Math.max(B.offsetHeight, D.offsetHeight),
                Math.max(B.clientHeight, D.clientHeight));
        },   
        getWindowHeight:function() {
            //IE
            if(!window.innerHeight) {
                if(!(D.clientHeight == 0))
                  return D.clientHeight;
                else
                    return B.clientHeight;  
            }
            else
                return window.innerHeight;
        },
        getWindowWidth:function() {
            //IE
            if(!window.innerWidth) {
                if(!(D.clientWidth == 0))
                  return D.clientWidth;
                else
                    return B.clientWidth;  
            }
            else
                return window.innerWidth;
            
        },
        getPositioning:function(){
            var scrolledY = 0;
            var scrolledX = 0;
            if(typeof pageXOffset != "undefined") 
                scrolledX = pageXOffset;
            else {
                temp = (D.clientWidth) ? D : B;
                scrolledX = temp.scrollLeft;
            }
            if(typeof pageYOffset != "undefined")
                scrolledY = pageYOffset;
            else {
                temp = (D.clientHeight) ? D: B;
                scrolledY = temp.scrollTop;
            }
            pl = scrolledX + parseInt(parseInt(this.getWindowWidth() - w)/2);
            viewable_height = this.getWindowHeight();
            height = Math.max(viewable_height, h + 50);
            pt = scrolledY + parseInt(parseInt(height - h)/4);
        },   
        
        addEscape:function(e){
            e=(e)?e:(window.event)?event:null;
            if(e){
                var charCode=(e.charCode)?e.charCode:((e.keyCode)?e.keyCode:((e.which)?e.which:0));
                if(charCode == 27){
                if(document.getElementById('mtp_overlay')){
                    MapThatPad_BM.close();}
                }
            }
        },

        bindEvents:function() {
            if(document.addEventListener) {
              document.addEventListener('keypress',MapThatPad_BM.addEscape,false)
              //overlay.setAttribute('onclick','MapThatPad_BM.close();return false;');
            }

            if(document.attachEvent) {
              document.attachEvent('onkeypress',MapThatPad_BM.addEscape);
              //overlay.onclick=function(){
              //  MapThatPad_BM.close();return false;} ; 
            }
        },

        style:function() {
            this.getPositioning(w,h);
            overlay.style.width = '100%';
            overlay.style.height = '100%';
            overlay.style.background = '#000000';
            overlay.style.opacity = '0.8';
            overlay.style.filter = "alpha(opacity=80)";
            overlay.style.top = '0px';
            overlay.style.left = '0px';
            overlay.style.position = 'absolute';
            overlay.style.zIndex = '999';
            overlay.style.height = Math.max(this.getDocHeight(), this.getWindowHeight()) + 'px';
            mainDiv.style.background = '#000000';
            mainDiv.style.color = "#FFFFFF";
            mainDiv.style.position = "absolute";
            mainDiv.style.zIndex = "1000";
            mainDiv.style.width = w + 'px';
            mainDiv.style.height = h + 'px';
            mainDiv.style.top = pt + 'px';
            mainDiv.style.left = pl + 'px';
            mainDiv.style.opacity = "1";
            mainDiv.style.filter = "alpha(opacity=100)";
            mainDiv.style.fontSize ='16px';
            mainDiv.style.fontFamily = 'verdana,arial,helvetica,sans-serif';
            headerDiv.style.background = "#000000";
            headerDiv.style.color = "#FFFFFF";
            headerDiv.style.textAlign = "right";
        },
        display:function(url,iw,ih) {
            B=document.body;
            D=document.documentElement;
            w=iw;
            h=ih;
            overlay = document.getElementById('mtp_overlay');
            mainDiv = document.getElementById('mtp_maindiv');
            if (overlay && mainDiv) {       
                    B.appendChild(overlay);
                    B.appendChild(mainDiv);
                    return false;
            }
            
            overlay = document.createElement('div');
            overlay.id = 'mtp_overlay';
            mainDiv = document.createElement('div');
            mainDiv.id = 'mtp_maindiv';
            headerDiv = document.createElement('div')
            headerDiv.innerHTML = '<a href="#" id="mtp_close" onclick="return MapThatPad_BM.close(); return false;"><img title="Close" alt="close" src="/site_media/images/close.gif" border="0" /></a>';
            mainDiv.appendChild(headerDiv);
            this.style(w,h);
            var iframe = document.createElement('iframe');
            iframe.id = 'mtp_iframe';
            iframe.style.width = '100%';
            iframe.style.border="0px solid #404040";
            iframe.frameBorder = "none";
            iframe.style.height = "100%";
            iframe.style.background = "#000000";
            iframe.src = url;
            // For IE
            iframe.onreadystatechange = function() { mainDiv.style.height = (document.getElementById("mtp_iframe").contentWindow.document.body.scrollHeight + 12) + 'px';  overlay.style.height = Math.max(this.getDocHeight(), this.getWindowHeight()) + 'px';};
            // For other browsers
            iframe.onload = function() { mainDiv.style.height = (document.getElementById("mtp_iframe").contentDocument.body.clientHeight + 12) + 'px'; overlay.style.height = Math.max(MapThatPad_BM.getDocHeight(), MapThatPad_BM.getWindowHeight()) + 'px';};
            var body = document.getElementsByTagName("body")[0];
            mainDiv.appendChild(iframe);
            body.appendChild(overlay);
            body.appendChild(mainDiv);
            this.bindEvents();
        },

        close:function() {
            B.removeChild(document.getElementById('mtp_overlay'));
            B.removeChild(document.getElementById('mtp_maindiv'));
            return false;
       } 
    }
}();

