
(function () {
	var zindex = 2000;

	var FrameInstance = function (session, name, onregister, oncomplete, oncancel) {
		this.session = session;
		this.onReact = null;
		this.onComplete = oncomplete;
		this.onCancel = oncancel;
		this.onRegister = onregister;

		this.dialog = session.getActiveDialog ();
		this.dialog.deactivate ();

		var body = document.getElementsByTagName ('body')[0];
		this.mist = new Mist (body, zindex - 1);

		this.iframe = document.createElement ('iframe');
		HTML.setClass (this.iframe, 'frame');
		this.iframe.src = httphandler + '?httphandlercall=WFPage&page=' + name + '&session=' + session.id + '&language=' + Framework.getLanguage ();
//		this.iframe.src = 'WFPages/' + name + '.html';
		this.iframe.style.zIndex = zindex;
		HTML.setAttribute (this.iframe, 'scrolling', 'no')
		HTML.setAttribute (this.iframe, 'frameBorder', '0');
		HTML.add (body, this.iframe);

		zindex+= 100;
	};

	FrameInstance.prototype.destroy = function () {
		zindex -= 100;
		this.mist.destroy ();
		this.dialog.activate ();
		var body = document.getElementsByTagName ('body')[0];
		HTML.remove (body, this.iframe);
	};

	FrameInstance.prototype.setSize = function (width, height) {
		this.iframe.style.width = width + 'px';
		this.iframe.style.height = height + 'px';
	};




	Frames = {};
	
	var frames = {};

	Frames.open = function (session, name, onregister, oncomplete, oncancel) {
		frames[name] = new FrameInstance (session, name, onregister, oncomplete, oncancel);
	};

	Frames.cancel = function (name) {
		var frame = frames[name];
		delete frames[name];
		frame.onCancel ();
		frame.destroy ();
	};

	Frames.complete = function (name) {
		var frame = frames[name];
		delete frames[name];
		frame.onComplete ();
		frame.destroy ();
	};

	Frames.react = function (name, msg) {
		frames[name].onReact (msg);
	};

	Frames.register = function (name, onreact) {
		frames[name].onReact = onreact;
		var onregister = frames[name].onRegister;
		return (onregister ? onregister () : null);
	};

	Frames.setFrameSize = function (name, width, height) {
		frames[name].setSize (width, height);
	};

	Frames.act = function (name, msg) {
		new Request.FrameAct (frames[name].session, name, msg).send ();
	};

	var killFrames = function () {
		for (var idx in frames) {
			frames[idx].destroy ();
		}
	};

	Framework.onFatalError.add (killFrames);

}) ();
