
namespace ('Dialogs');

//---------------------------------------------------/ Constructors /-

Dialogs.createRootDialog = function (session, element) {
	return new Dialogs.Dialog (session, new Dialogs.RootContainer (element));
};

Dialogs.createModalDialog = function (session) {
	return new Dialogs.Dialog (session, new Dialogs.ModalContainer ());
};


//------------------------------------------/ Dialogs.RootContainer /-

Dialogs.RootContainer = function (element) {
	var t = this;
	t._element = element;
	t._note = HTML.createDiv ('pagenote');
	t._noteimg = HTML.createIcon ('', Framework.imageUrl ('6EDB::801::6EDB::2793'), true);
	t._notenode = HTML.createTextNode ('');
	HTML.add (t._element, HTML.add (t._note, t._noteimg, t._notenode));
	HTML.hide (t._note);
};

Dialogs.RootContainer.prototype = {
	destroy: function () {
		HTML.remove (this._element, this._note);
	},

	getDialogRoot: function () {
		return this._element;
	},

	setPageNote: function (note) {
		if (note) {
			HTML.setTextContent (this._notenode, note);
			HTML.show (this._note);
		} else {
			HTML.hide (this._note);
		}
	},
	
	isModal: function () {
		return false;
	}
};



//-----------------------------------------------------------/ Mist /-

Mist = function (elt, zindex) {
	var layer = HTML.createDiv('mist');
	HTML.add (elt, layer);
	if (zindex) layer.style.zIndex = zindex;
	
	var w = 0, h = 0;
	var vpchanged = function () {
		// adjust size of mist-layer if someone manages to get a viewport that is not entirely covered yet.
		var vp = windowViewport();
		w = Math.max(w, vp.x + vp.width);
		h = Math.max(h, vp.y + vp.height);
		layer.style.width =  w + 'px';
		layer.style.height = h + 'px';
	};
	
	Framework.onResizes.add (vpchanged);
	Framework.onScrolls.add (vpchanged);
	vpchanged ();
	
	this.destroy = function () {
		Framework.onResizes.remove (vpchanged);
		Framework.onScrolls.remove (vpchanged);
		HTML.remove (elt, layer);
	};
};



//-----------------------------------------/ Dialogs.ModalContainer /-

Dialogs.ModalContainer = function () {
	var t = this;
	t._element = document.getElementsByTagName('body')[0];
	t._mist = new Mist (t._element);

	t._container = HTML.createDiv ('modal');
	HTML.add (t._element, t._container);
};

Dialogs.ModalContainer.prototype = {
	destroy: function () {
		this._mist.destroy ();
		HTML.remove (this._element, this._container);
	},
	
	getDialogRoot: function () {
		return this._container;
	},

	isModal: function () {
		return true;
	}
};


//-------------------------------------------------/ Dialogs.Dialog /-

Dialogs.Dialog = function (session, container) {
	var t = this;
	t._session = session;
	t._container = container;

	t._dialog = null;
	t._inactive = 0;
	t._metacontrol = null;
};

Dialogs.Dialog.prototype = {
	destroy: function (unloading) {
		this._inactive++;
		this.clearDialog (unloading);
		this._container.destroy ();
	},
	
	clearDialog: function (unloading) {
		var d = this._dialog;
		if (d) {
			d.destroy (unloading);
			HTML.remove (this._container.getDialogRoot (), d.widget.getHTMLRoot ());
			this._dialog = null;
		}
	},
	
	setDialog: function (dialogmeta) {
		var t = this, c = t._container, d = new Controls.Dialog ();
		t.clearDialog ();
		t._dialog = d;
		dialogmeta.modal = c.isModal ();
		d.init (t, dialogmeta, {});
		HTML.add (c.getDialogRoot (), d.widget.getHTMLRoot ());
	},

	refresh: function (data, editable) {
		var d = this._dialog;
		if (d) {
			d.refresh (data, editable);
		}
	},

	getSessionId: function () {
		return this._session.id;
	},

	setMetaControl: function (control) {
		this._metacontrol = control;
	},

	getMetaControl: function () {
		return this._metacontrol;
	},
	
	deactivate: function () {
		var t = this;
		if (t._inactive == 0) {
			unfocusAll ();	// unfocus all controls to make sure changes are handled before the dialog is deactivated
			if (t._dialog && isIE6 ()) {
				t._dialog.hide ();
			}
		}
		t._inactive++;
	},
	
	activate: function () {
		var d = this._dialog;
		this._inactive--;
		if ((this._inactive == 0) && d && isIE6 ()) {
			d.show ();
		}
	},

	xmlData: function (notifiers) {
		var d = this._dialog;
		if (d) {
			return d.xmlData (notifiers, true) + d.xmlData (notifiers, false);
		}
	},
	
	notifyChanged: function (control, jumpqueue) {
		var req = new Request.Dialog(this._session, this, control);
		if (this._inactive == 0) {
			if (jumpqueue)
				req.sendNow ();
			else 
				req.send();
		} else {
			req.notifyProcessing ();
		}
	},

	setPageNote: function (note) {
		var c = this._container;
		if (c.setPageNote) {
			c.setPageNote (note);
		}
	}
};
