function ModalDialog(id, properties) {
	this.id = id;
	this.properties = properties;

	this.prev_body_height = null;
	this.prev_body_overflow = null;
	this.prev_html_height = null;
	this.prev_html_overflow = null;
	this.param = null;

	this.initialize = function() {
		this.holder = $(this.id);
		if (this.holder) {
			this.holder.modal_dlg = this;
			
			this.holder.style.position = 'absolute';
			this.holder.style.zIndex = 1000;
			this.holder.style.top = '50%';
			this.holder.style.left = '50%';
			var dim = this.holder.getDimensions();
			this.holder.style.marginTop = -(dim.height/2) + 'px';
			this.holder.style.marginLeft = -(dim.width/2) + 'px';
		}
	}

	this.show = function(param) {
	    this.param = param;
	    this.holder.remove();
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(this.holder);

		this.block_window();
		this.store_scroll();
		this.show_dd('hidden');
		this.show_overlay();
		if (this.holder) this.holder.style.display = 'block';
	}

	this.close = function(result) {
	    if (this.properties && this.properties.onvalidate)
	        if (!this.properties.onvalidate(result, this.param)) return;
	        
		this.unblock_window();
		this.restore_scroll();
		this.show_dd('visible');
		this.close_overlay();
		if (this.holder) this.holder.style.display = 'none';
		if (this.properties && this.properties.onclose) this.properties.onclose(result,this.param);
		this.holder.remove();
		this.holder.modal_dlg = null;
		this.holder = null;
	}

	this.block_window = function() {
		var e = document.getElementsByTagName('body')[0];
		this.prev_body_overflow = e.style.overflow;
		e.style.overflow = 'hidden';

		e = document.getElementsByTagName('html')[0];
		this.prev_html_overflow = e.style.overflow;
		e.style.overflow = 'hidden'; 
	}

	this.unblock_window = function() {
		var e = document.getElementsByTagName('body')[0];
		e.style.overflow = this.prev_body_overflow;

		e = document.getElementsByTagName('html')[0];
		e.style.overflow = this.prev_html_overflow;
	}

	this.show_overlay = function() {
		this.get_overlay().style.display = 'block';
	}

	this.close_overlay = function() {
		this.get_overlay().style.display = 'none';
	}

	this.get_overlay = function() {
		var ret = $('MODAL_OVERLAY');
		if (!ret) {
			var body = document.getElementsByTagName('body')[0];
			ret = document.createElement('div');
			Element.extend(ret);
			ret.id = 'MODAL_OVERLAY';
			ret.style.display = 'none';
			ret.style.position = 'absolute';
			ret.style.top = '0px';
			ret.style.left = '0px';
			ret.style.width = '100%';
			ret.style.height = '100%';
			ret.style.zIndex = 999;
			ret.style.backgroundColor = '#000000';
			ret.setOpacity(0.8);
			body.appendChild(ret);
		}
		return ret;
	}

	this.show_dd = function(show) {
		var dds = document.getElementsByTagName('select');
		for(i = 0; i < dds.length; i++) {
		    var inside = false;
		    var pt = dds[i].parentNode;
		    while (pt) {
		        if (pt == this.holder) {
		            inside = true;
		            break;
		        }
		        pt = pt.parentNode;
		    }
		    if (!inside) dds[i].style.visibility=show;
		}
	}

	this.store_scroll = function() {
		if (document.documentElement && document.documentElement.scrollTop) this.y_scroll = document.documentElement.scrollTop; 
		else if (document.body) this.y_scroll = document.body.scrollTop;
		window.scrollTo(0, 0); 
	}

	this.restore_scroll = function() {
		window.scrollTo(0, this.y_scroll); 
	}

	this.initialize();

}

function getModalDialog(id)
{
    var holder = $(id);
    return (holder ? holder.modal_dlg : null);
}

function MODAL_CLOSE(id, btn)
{
    var dlg = getModalDialog(id);
    if (dlg) dlg.close(btn.innerHTML);
}


var MD_NONE = 0;
var MD_OK = 1;
var MD_CANCEL = 2;
var MD_OK_CANCEL = 3;

function showModalDialog(html, type, css)
{
    var dist = true;
    var t = type;
    if (t < 0) {
        dist = false;
        t = -type;
    }

    var id = "_std_modaldialog" + t;
    var md = getModalDialog(id);
    if (!md)
    {
        var dlg = document.createElement("div");
        dlg.id = id;
        dlg.className = (css ? css :  'modal');
        document.body.appendChild(dlg);
        md = new ModalDialog(dlg.id);
    }

    var h = html;
    
    if (dist) h += "<br/><br/>";
    h += "<center>";
    
    switch (t)
    {
    case MD_OK:
        h += "<button onclick='MODAL_CLOSE(\"" + id + "\",this)' id='" + id + "_ok'>OK</button>";
        break;

    case MD_CANCEL:
        h += "<button onclick='MODAL_CLOSE(\"" + id + "\",this)' id='" + id + "_cancel'>Cancel</button>";
        break;
        
    case MD_OK_CANCEL:
        h += "<button onclick='MODAL_CLOSE(\"" + id + "\",this)' id='" + id + "_ok'>OK</button>&nbsp;<button onclick='MODAL_CLOSE(\"" + id + "\",this)' id='" + id + "_cancel'>Cancel</button>";
        break;
        

    }
    
    h += "</center>";
    
    md.holder.innerHTML = h;
    
    md.show();
    
    return md;
}
