﻿function dialog() {
    var insertLocation = document.body.childNodes[0];
    var me = this;
    
    this.background = document.createElement('div');
    this.background.setAttribute('id', 'dialogBackground');
    this.background.style.display='none';
    this.background.style.width='100%';
    this.background.style.height='100%';
    document.body.insertBefore(this.background, insertLocation);
    this.background.style.top=document.body.scrollTop;
    
    this.container = document.createElement('div');
    this.container.setAttribute('id', 'dialogContainer');
    this.container.style.display='none';
    this.container.style.width='100%';
    this.container.style.height='100%';
    this.container.dialog = this;
    document.body.insertBefore(this.container, insertLocation);
    this.container.style.top=document.body.scrollTop;
    
    this.frame = document.createElement('div');
    this.frame.setAttribute('id', 'dialogFrame');
    this.frame.style.display='block';
    this.container.appendChild(this.frame);
    
    this.titleBar = document.createElement('div');
    this.titleBar.setAttribute('id', 'dialogTitleBar');
    this.titleBar.style.display='block';
    this.frame.appendChild(this.titleBar);
    
    this.closeBox = document.createElement('div');
    this.closeBox.setAttribute('id', 'dialogCloseBox');
    this.closeBox.style.display='block';
    this.closeBox.onclick=function() { document.getElementById('dialogContainer').dialog.close(); };
//    this.closeBox.innerHTML = '&nbsp;'
    this.titleBar.appendChild(this.closeBox);
    
    this.title = document.createElement('span');
    this.title.setAttribute('id', 'dialogTitle');
    this.title.style.display='block';
    this.titleBar.appendChild(this.title);

    this.setWidth = function(w) {
        me.frame.style.width='' + w + 'px';
        me.frame.style.marginLeft = '-' + Math.floor(w / 2) + 'px';
    };

    this.setHeight = function(h) {
        me.frame.style.height='' + h + 'px';
        me.frame.style.marginTop = '-' + Math.floor(h / 2) + 'px';
    };
    
    this.setTitle = function(t) {
        me.title.innerHTML = t;
    };
    
    this.setContent = function(html) {
        if(me.content) {
            me.frame.removeChild(me.content);
        }
        me.content = document.createElement('div');
        me.content.setAttribute('id', 'dialogContentDiv');
        me.content.innerHTML = html;
        me.frame.appendChild(me.content);
    };
    
    this.loadContent = function(url) {
        if(me.content) {
            me.frame.removeChild(me.content);
        }
        me.content = document.createElement('iframe');
        me.content.setAttribute('id', 'dialogContentFrame');
        me.content.frameborder = 0;
        me.content.src = url;
        me.frame.appendChild(me.content);
    };
    
    this.show = function(showClose) {
        if(showClose) {
            me.closeBox.style.display='block';
        } else {
            me.closeBox.style.display='none';
        }
        me.background.style.display = 'block';
        me.container.style.display = 'block';
    };
    
    this.hide = function() {
        me.background.style.display = 'none';
        me.container.style.display = 'none';
    };
    
    this.close = function() {
        me.hide();
        document.body.removeChild(me.background);
        document.body.removeChild(me.container);
    }
}
