function MModalDialog(parent,W,H) {
	this.self = this;
	this.parent = parent ? parent : document.body;
	this.dOpacity = 40;
	this.panelWidth = W ? W : 800;
	this.panelHeight = H ? H : 400;

	this.mask = this.createMask();
	this.panel = this.createPanel();

	this.contentDiv = this.createContentDiv();
	this.buttonBar = this.createButtonBar();

};




MModalDialog.prototype.createContentDiv = function(){
	var self = this.self;

	var panel = document.createElement('DIV');
	this.panel.appendChild(panel);
	panel.style.height = (this.panelHeight - 30) + 'px';
	panel.style.width = this.panelWidth + 'px';
	panel.style.zIndex = '9999999';
	panel.style.overflow = 'auto';

	panel.onclick = function(ev){
		try{ev.stopPropagation()}catch(e){event.cancelBubble=true};
	};

	return panel;
};

MModalDialog.prototype.createButtonBar = function(){
	var self = this.self;

	var buttonBar = document.createElement('TABLE');
	this.panel.appendChild(buttonBar);

	buttonBar.setAttribute('width','100%');
	buttonBar.setAttribute('cellSpacing',0);
	buttonBar.setAttribute('cellPadding',2);

	oTBody = document.createElement('TBODY');
	buttonBar.appendChild(oTBody);

// ------ collection name row
	var oRow = document.createElement('TR');
	oTBody.appendChild(oRow);

	var oCell = document.createElement('TD');
	oRow.appendChild(oCell);

	var closeButton = this.createButton('Close','gray');
	closeButton.onclick = function(){self.cancelButtonClick()};
	oCell.setAttribute('align','center');
	oCell.style.borderTop = '1px solid gray';

	oCell.appendChild(closeButton);

	return buttonBar;
};


MModalDialog.prototype.createPanel = function(){
	var self = this.self;

	var panel = document.createElement('DIV');
	this.parent.appendChild(panel);
	panel.style.height = this.panelHeight + 'px';
	panel.style.width = this.panelWidth + 'px';
	panel.style.background = '#ffffff';
	panel.style.border = '2px solid black';
	panel.style.display = 'none';
	panel.style.position = 'absolute';
	panel.style.top = '100px';
	panel.style.left = Math.round(this.parent.clientWidth / 2 - this.panelWidth / 2) + 'px';
	panel.style.zIndex = '9999999';

	panel.onclick = function(ev){
		try{ev.stopPropagation()}catch(e){event.cancelBubble=true};
	};

	return panel;
};


MModalDialog.prototype.createMask = function(){
	var mask = document.createElement('DIV');
	this.parent.appendChild(mask);
	mask.style.height = '100%';
	mask.style.width = '100%';
	mask.style.background = '#000000';
	mask.style.display = 'none';
	mask.style.position = 'absolute';
	mask.style.top = '0px';
	mask.style.zIndex = '9999998';
	return mask;
};


MModalDialog.prototype.createButton = function(label,color){
	var self = this.self;
	var oButton = document.createElement('DIV');
	oButton.style.padding = '5px';
	oButton.style.width = '100px';
	oButton.style.background = color;
	oButton.style.color = 'white';
	oButton.style.border = '1px solid white';
	oButton.style.textAlign = 'center';
	oButton.style.font = 'bold 12px verdana';
	oButton.style.cursor = 'pointer';
	oButton.innerHTML = label;
	return oButton;
};


// ------------------------------------------------------------
MModalDialog.prototype.cancelButtonClick = function(){
	this.hide();
};

MModalDialog.prototype.okButtonClick = function(){
	this.hide();
};


MModalDialog.prototype.show = function(){
	this.fadeIn();
	this.panel.style.display = '';
};

MModalDialog.prototype.hide = function(){
	this.fadeOut(this.dOpacity);
	this.panel.style.display = 'none';
};

MModalDialog.prototype.fadeIn = function(val) {
	var self = this.self;

	val = typeof(val) != 'undefined' ? val : 10;
	this.mask.style.display = '';
	this.setOpacity(this.mask,val);
	if (val < self.dOpacity) {
		window.setTimeout(function(){self.fadeIn(val+10)},10);
	}
};

MModalDialog.prototype.fadeOut = function(val) {
	var self = this.self;

	val = typeof(val) != 'undefined' ? val : 100;
	this.setOpacity(this.mask,val);
	if (val > 0) {
		window.setTimeout(function(){self.fadeOut(val-10)},10);
	}
	else {
		this.mask.style.display = 'none';
	}
};


MModalDialog.prototype.setOpacity = function(obj,val) {
	if (document.all) {
		obj.style.filter = 'alpha(opacity='+val+')';
	}
	else {
		obj.style.opacity = (val/100);
	}
};

