var FileManagerDialogHelper = {
	path: null,
	baseUrl: null,
	dialogs: {},
	index: -1,

	init: function(params) {
		FileManagerDialogHelper.path = params.path;
		FileManagerDialogHelper.baseUrl = params.baseUrl;
	},

	open: function(options) {
		var fm = new FileManagerDialog(options);
		return fm;
	},

	getCurrent: function() {
		return FileManagerDialogHelper.dialogs[FileManagerDialogHelper.index];
	}
}

function FileManagerDialog(params) {
	if (params == null) params = {};

	this.path = FileManagerDialogHelper.path;
	this.baseUrl = FileManagerDialogHelper.baseUrl;
	this.$ = window.$;

	this.settingsKey = params.settingsKey;
	this.iframe = null;
	this.container = null;

	var _this = this;
	init();

	function init() {
		if (_this.settingsKey == null) throw "FileManagerDialog requires settingsKey";

		params.width = params.width ? params.width : 820;
		params.height = params.height ? params.height : 420;
		params.title = params.title ? params.title : "File Manager";
		var file = _this.path + params.file;

		_this.iframe = $("<iframe src='" + file + "'>")
			.attr("scrolling", "no")
			.attr("frameborder", "0")
			.css({
				"border": "solid 1px #BFBFBF",
				"height": params.height,
				"width": params.width
			})
		;

		_this.iframe.fadeTo(0, 0.0);
		_this.iframe.load(function() {
			$(this).fadeTo(500, 1.0);
			_this.container.css("background", "none");
		});

		_this.container = $("<div>").css({ width: params.width, height: params.height }).append(_this.iframe);
		_this.container.css("background", "url(" + _this.path + "styles/images/loading.gif" + ") no-repeat center center");

		if (params.embed == null) {
			_this.container.dialog({
				title: params.title,
				width: params.width + 28,
				height: params.height + 55,
				bgiframe: true,
				resizable: false,
				modal: true,
				dialogClass: "fileManagerDialog"
			});
		} else {
			params.embed.append(_this.container);
		}

		FileManagerDialogHelper.dialogs[++FileManagerDialogHelper.index] = _this;
	}

	this.alert = function(message) {
		$("<div>").html(message).dialog({
			title: "Alert",
			resizable: false,
			modal: true,
			buttons: {
				"Ok": function() { $(this).dialog("close"); }
			}
		});
	}

	this.confirm = function(message, onConfirm) {
		$("<div>").html(message).dialog({
			title: "Confirm",
			resizable: false,
			modal: true,
			buttons: {
				"Cancel": function() { $(this).dialog("close"); },
				"Ok": function() {
					var result = onConfirm();
					if (result == null || result) $(this).dialog("close");
				}
			}
		});
	}

	this.insertContent = function(content) {
	}

	this.close = function() {
		_this.container.dialog("close");
	}

	this.getSelection = function() {
		return null;
	},

	this.addCssFile = function(file) {
		var path = this.path + "styles/" + file;
		if ($("head link [href='" + path + "']").length == 0) {
			if ($.browser.msie) {
				document.createStyleSheet(path);
			} else {
				$("head").append($("<link type='text/css' rel='stylesheet'>").attr("href", path));
			}
		}
	}
}