// Though "Dialog2" looks like an object, it isn't really an object.  Instead
// it's just namespace for protecting global symbols.

/* OBSOLETE. Use Dialog2 */
//function Dialog2(url, width, height, init, action)
//{
//	return Dialog2(url, width, height, init, action);
//};

/* This new name is to avoid conflicts with the Dialog2 function in the content editor*/
function Dialog2(url, width, height, init, action) {

	if (document.all)
	{	// here we hope that Mozilla will never support document.all
		//alert("Changed");
		var value =
			showModalDialog(url, init,
				"resizable: yes; help: no; status: no; scroll: no");// +
				//"dialogWidth:" + width + "px; dialogHeight:" + height + "px;");
		if (action) {
			action(value);
		}
	} else {
		//backward compatibility
		//window.Dialog2 = this;
		
		//reduce window size to be approx same as IE		
		width -= 5;
		height -= 35;
		
		return Dialog2._geckoOpenModal(url, width, height, init, action);
	}
};

Dialog2._parentEvent = function(ev) {
	if (Dialog2._modal && !Dialog2._modal.closed) {
		Dialog2._modal.focus();
		ev.preventDefault();
		ev.stopPropagation();
	}
};

// should be a function, the return handler of the currently opened Dialog2.
Dialog2._return = null;

// constant, the currently opened Dialog2
Dialog2._modal = null;

// the Dialog2 will read it's args from this variable
Dialog2._arguments = null;

Dialog2._geckoOpenModal = function(url, width, height, init, action) {
	var dlg = window.open(url, "dv_modal",
				"toolbar=no, location=no, directories=no, status=no, menubar=no, " +
				"scrollbars=no, resizable=no, width=" + width + ", height=" + height);
	Dialog2._modal = dlg;
	Dialog2._arguments = init;

	// capture some window's events
	function capwin(w) {
		w.addEventListener("click", Dialog2._parentEvent, true);
		w.addEventListener("mousedown", Dialog2._parentEvent, true);
		w.addEventListener("focus", Dialog2._parentEvent, true);
	};
	// release the captured events
	function relwin(w) {
		w.removeEventListener("focus", Dialog2._parentEvent, true);
		w.removeEventListener("mousedown", Dialog2._parentEvent, true);
		w.removeEventListener("click", Dialog2._parentEvent, true);
	};
	capwin(window);
	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));
	// make up a function to be called when the Dialog2 ends.
	Dialog2._return = function (val) {
		if (val != undefined && action) {
			action(val);
		}
		relwin(window);
		// capture other frames
		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
		Dialog2._modal = null;

		window.focus();
	};
};
