FR.initLayout = function() {

Ext.get('theBODY').on('mousemove', function(e){
	FR.UI.xy = e.getXY();
});

FR.UI.adjustViewPort = function() {
	if (Settings.ui_enable_full_screen) {return false;}
	var s = Ext.getBody().getSize();
	if (!FR.UI.window.maximized && (FR.UI.window.width > s.width || FR.UI.window.height > s.height)) {
		FR.UI.window.maximize();
	} else if (FR.UI.window.maximized && FR.UI.window.restoreSize.width < s.width && FR.UI.window.restoreSize.height < s.height) {
		FR.UI.window.restore();
	}
}

FR.UI.window = new Ext.Window({
	title: Settings.title,
	height: Settings.ui_window_height, width: Settings.ui_window_width,
	id: 'FR-MainInterface-Window', el: 'viewport',
	stateful: true,	stateEvents: ['resize'],
	layout: 'border',
	maximizable: true, closable: false, draggable: false, constrain: true,
	iconCls: 'headerIcons-appWindow',
	listeners: {
		resize: function() {FR.UI.window.anchorTo(Ext.get('theBODY'), 'c-c');},
		activate: function(win) {Ext.WindowMgr.sendToBack(win);}
	},
	items: [
		{
			id: 'FR-Tree-Region',
			layout: 'border',
			region: 'west',
			contentEl: 'leftCol',
			width: 190, minSize: 170, maxSize: 300,
			border:false, hideBorders: true,
			split: true, collapsible: true, hideCollapseTool: true, collapseMode: 'mini',
			tbar: FR.UI.treeToolbar.config,
			items: [
				{
					layout: 'fit',
					region: 'center',
					border: false,
					defaults: {
	                    autoHeight: false, autoScroll: false, closable: false, fitToFrame: true
                    },
					items: FR.UI.tree.panel
				}
			]
		},
		{
			id: 'rightCol',
			layout: 'border',
			region: 'center',
			border: false, hideBorders: true,
			items: [
				{
					region:'center',
					layout: 'fit',
					border: false, hideBorders: true,
					items: FR.UI.grid.panel
				}
			]
		}, {
			id: 'southRegion',
			region: 'south',
			border:false, hideBorders: true,
			bodyStyle: 'display:none',
			bbar: FR.UI.bottomToolbar.config
		}
	]
});

FR.UI.window.show();
FR.UI.window.alignTo(Ext.get('theBODY'), 'c-c');
FR.UI.window.on('resize', function (w) {w.alignTo(Ext.get('theBODY'), 'c-c');});
if (Settings.ui_enable_full_screen) {
	FR.UI.window.maximize();
} else {
	FR.UI.adjustViewPort();
}
Ext.EventManager.onWindowResize(function(w, h) {
	FR.UI.adjustViewPort();
});

FR.UI.restoreView();

Ext.getCmp('southRegion').getBottomToolbar().addDom({
	tag: 'div',
	id: 'trafficQuotaIndicator',
	style: 'width:200px;margin-right:5px;',
	onclick: 'FR.UI.reloadStatusBar()'
});
Ext.getCmp('southRegion').getBottomToolbar().addDom({
	tag: 'div',
	id: 'spaceQuotaIndicator',
	style: 'width:200px;',
	onclick: 'FR.UI.reloadStatusBar()'
});
Ext.getCmp('southRegion').getBottomToolbar().doLayout();

if (User.perms.space_quota_max) {
	FR.UI.spaceQuotaIndicator = new Ext.ProgressBar({
		text: 'asd',
		autoWidth: true,
		autoHeight: true,
		renderTo: 'spaceQuotaIndicator'
	});
	FR.UI.spaceQuotaIndicator.setSize(false, 18);
}
if (User.perms.traffic_quota_max) {
	FR.UI.trafficQuotaIndicator = new Ext.ProgressBar({
		autoWidth: true,
		renderTo: 'trafficQuotaIndicator'
	});
	FR.UI.trafficQuotaIndicator.setSize(false, 18);
}
if (User.perms.space_quota_max || User.perms.traffic_quota_max) {FR.UI.setQuotaStatus();}
Ext.EventManager.addListener('viewport', 'contextmenu', function(e) {
	e.stopEvent();
});
}

FR.UI.setQuotaStatus = function() {
	if (FR.UI.trafficQuotaIndicator) {
	FR.UI.trafficQuotaIndicator.updateProgress(User.perms.traffic_quota_percent_used/100, FR.T('Traffic:')+' '+User.perms.traffic_quota_used+' / '+User.perms.traffic_quota_max_formated);
	}
	if (FR.UI.spaceQuotaIndicator) {
	FR.UI.spaceQuotaIndicator.updateProgress(User.perms.space_quota_percent_used/100, FR.T('Space:')+' '+User.perms.space_quota_used+' / '+User.perms.space_quota_max_formated);
	}
}
FR.UI.confirm = function(question, confirmHandler, cancelHandler) {
	Ext.Msg.show({
		title:FR.T('Confirmation'),
		msg: question,
		buttons: Ext.MessageBox.OKCANCEL,
		closable: false, modal: false,
		fn: function (btn, txt) {
			if (btn == 'ok') {
				confirmHandler();
			} else {
				cancelHandler();
			}
		}
	});
}
FR.UI.prompt = function(question, confirmHandler, cancelHandler, value) {
	m = Ext.Msg.show({
		msg: question,
		buttons: Ext.MessageBox.OKCANCEL,
		closable: false, modal: false, prompt: true,
		value: value,
		fn: function (btn, txt) {
			if (btn == 'ok') {
				confirmHandler(txt);
			} else {
				cancelHandler();
			}
		}
	});
}

FR.UI.feedback = function(text) {
	var msgCt;
	if (text.length > 0) {
		delay = text.length/15;
	} else {
		delay = 1;
	}
	if(!msgCt){
		msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div', style:'width:300px;z-index:9999999'}, true);
	}
	msgCt.alignTo(Ext.get('viewport'), 'tr-tr', [-50, 3]);
	var m = Ext.DomHelper.append(msgCt, {html:['<div class="msg" style="z-index:9999998">',
	'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
	'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">', text, '</div></div></div>',
	'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
	'</div>'].join('')}, true);
	m.slideIn('t').pause(delay).ghost('t', {remove:true});
}
FR.UI.popupCount = 0;
FR.UI.popup = function(args) {
	id = 'popups_'+(++this.popupCount);
	windowId = args.id || id+'_window';
	args.src = args.src+'&_popup_id='+windowId;
	dialog = new Ext.Window({
		id: windowId,
		stateful: false,
		autoDestroy: args.autoDestroy,
		closeAction: (args.autoDestroy ? 'close' : 'hide'),
		constrainHeader: true,
		layout: 'fit',
		width: args.width, height: args.height,
		title: args.title || false,
		tools: args.tools || false,
		resizable: args.resizable || false,
		collapsible: args.collapsible || false,
		maximizable: args.maximizable || false,
		constrain: args.constrain || false,
		iconCls: args.iconCls || false,
		closable: true, plain: true,
		html: '<div style="background-color:white;height:100%;"><iframe src="'+(args.post ? 'about:blank' : args.src)+'" style="width:100%;height:100%;position:relative" marginheight="0" marginwidth="0" frameborder="0" id="'+id+'" name="'+id+'"></iframe></div>'
    });
	dialog.frameId = id;
	args.centerTo = args.centerTo || false;
	dialog.show(args.centerTo);
	if (args.loadingMsg) {
		Ext.get(dialog.getLayout().container.body.dom).mask(FR.T(args.loadingMsg));
		Ext.get(dialog.frameId).on('load', function() {
			Ext.get(dialog.getLayout().container.body.dom).unmask();
		});
	}
	FR.UI.popups[windowId] = dialog;
	if (args.autoDestroy) {
		dialog.on('afterhide', function(dlg){
			dlg.destroy(true);
			delete FR.UI.popups[dlg.id];
		});
	}
	if (args.post) {
		var frm = document.createElement('FORM');
		frm.action = args.src;
		frm.method = 'POST';
		frm.target = id;
		Ext.each(args.post, function(param) {
			inpt = document.createElement('INPUT');
			inpt.type = 'hidden';
			inpt.name = param.name;
			inpt.value = encodeURIComponent(param.value);
			frm.appendChild(inpt);
		})
		Ext.get('theBODY').appendChild(frm);
		frm.submit();
		Ext.get(frm).remove();
	}
	return dialog;
}

FR.UI.showUploadForm = function() {
	if (FR.currentSection == 'userWithShares' || (FR.currentFolderPerms && !FR.currentFolderPerms.upload)) {
		FR.UI.feedback(FR.T("You are not allowed to upload files in this folder."));
		return false;
	}
	if (!FR.UI.uploadDialog) {
		if (Settings.ui_default_upload_mode == 'dnd'){var activeTabStrip = 1;
		} else if (Settings.ui_default_upload_mode == 'flash') {var activeTabStrip = 0;
		} else {var activeTabStrip = 2;}
		
		dialog = new Ext.Window({
			title: FR.T('Upload'),
			width: 400, height: 360,
			modal: true, resizable:false, collapsible:false, plain: true, closeAction: 'hide',
			listeners: {
				'show': function() {
					if (Ext.getCmp('uploadDialogTP').getActiveTab().id == 'DNDTabPanel') {
						FR.UI.loadDNDApplet();
					}
					try {
						FR.UI.uploadDialogSWFiframe.contentWindow.Ext.getCmp('PluploadPanel').onDeleteAll();
					} catch(er) {}
				}
			},
            items: new Ext.TabPanel({
				id: 'uploadDialogTP',
				height: 330, border: false,
                activeTab: activeTabStrip,
                deferredRender: false,
				tabPosition: 'top',
				listeners: {
					tabchange: function(tp, tab) {
						if (Ext.isSafari) {//safari for windows doesn't hide the applet on tab change
							if (tab.id == 'DNDTabPanel') {
								FR.UI.loadDNDApplet();
							} else {
								Ext.get('DNDTabPanel').update('');
								FR.UI.uploadTabs.java = false;
							}
						}
					}
				},
				items: [
					{
						title: FR.T('Regular'),
						html: '',
						listeners: {
							activate: function() {
								if (!FR.UI.uploadTabs.flash) {
									FR.UI.uploadDialogSWFiframe = Ext.DomHelper.insertFirst(this.el, {tag: 'iframe', frameBorder: 0, src: URLRoot+'/?module=fileman_myfiles&section=ajax&page=upload_swf', style: 'width:100%;height:298px;position:relative'});
									FR.UI.uploadTabs.flash = true;
								}
							}
						}
					},
					{
						title: FR.T('Java'),
						id: 'DNDTabPanel',
						contentEl: Ext.DomHelper.append(Ext.get('theBODY'), {tag: 'DIV', id: 'uploadJavaDiv', style: 'height:327px;overflow:hidden;'}),
						listeners: {activate: FR.UI.loadDNDApplet}
					},
					{
						title: FR.T('Import e-mail attachments'),
						html: '',
						listeners: {
							activate: function() {
								if (!FR.UI.uploadTabs.popImporter) {
									FR.UI.uploadDialogPopIframe = Ext.DomHelper.insertFirst(this.el, {tag: 'iframe', frameBorder: 0, src: FR.baseURL+'/?module=pop_importer&page=index&path='+encodeURIComponent(FR.currentPath), style: 'width:100%;height:298px;position:relative'});
									FR.UI.uploadTabs.popImporter = true;
								}
							}
						}
					}
				]
            })
	    });
		FR.UI.uploadDialog = dialog;
	}
	FR.UI.uploadDialog.show();
	if (!Settings.ui_enable_flash_upload) {
		Ext.getCmp('uploadDialogTP').hideTabStripItem(0);
	}
	if (!Settings.ui_enable_dnd_upload) {
		Ext.getCmp('uploadDialogTP').hideTabStripItem(1);
	}
	if (!Settings.ui_enable_pop_importer) {
		Ext.getCmp('uploadDialogTP').hideTabStripItem(2);
	}
}
FR.UI.loadDNDApplet = function(tab) {
	if (!FR.UI.uploadTabs.java /*|| (Ext.isMac && Ext.isSafari)*/) {
		tab.update('');
		FR.UI.uploadDialogDNDiframe = Ext.DomHelper.insertFirst(tab.el, {tag: 'iframe', id:'dndifrm', frameBorder: 0, src: 'about:blank', height:300, width:382});
		if (Ext.isIE) {
			var frameDoc = document.frames['dndifrm'].document;
		} else {
			var frameDoc = FR.UI.uploadDialogDNDiframe.contentDocument;
		}
		frameDoc.write('<div style="color:dimgray;font-family:arial,tahoma,helvetica,sans-serif;font-size:11px;">'+FR.T('Loading Java applet, please wait...')+'</div>');
		FR.UI.uploadDialogDNDiframe.src = URLRoot+'/?module=fileman_myfiles&section=ajax&page=upload_form';
		FR.UI.uploadTabs.java = true;
	} else {
		try {
			FR.UI.uploadDialogDNDiframe.contentWindow.insertApplet();
		} catch (er) {}
	}
}
FR.UI.reloadStatusBar = function() {
	if (User.perms.space_quota_max || User.perms.traffic_quota_max) {
		Ext.Ajax.request({
		url: FR.baseURL+'/?module=fileman&section=utils&page=status_bar',
		method: 'get',
		success: function(req){
			try {
				var rs = Ext.util.JSON.decode(req.responseText);
			} catch (er){return false;}
			User.perms.space_quota_percent_used = rs.space_quota_percent_used;
			User.perms.space_quota_free = rs.space_quota_free;
			User.perms.space_quota_used = rs.space_quota_used;
			User.perms.traffic_quota_percent_used = rs.traffic_quota_percent_used;
			User.perms.traffic_quota_free = rs.traffic_quota_free;
			User.perms.traffic_quota_used = rs.traffic_quota_used;
			FR.UI.setQuotaStatus();
		}
	});
}}
FR.UI.showPreview = function(path, filename) {
	if ((FR.currentFolderPerms && !FR.currentFolderPerms.download) || !User.perms.download) {
		FR.UI.feedback(FR.T("You are not allowed to download or preview files."));
		return false;
	}
	var path = path || FR.currentPath;
	var filename = filename || FR.currentSelectedFile.filename;
	FR.previewData = {path: path, filename: filename};
	FR.UI.popup({
		title: filename,
		width: 500, height: 500,
		src: FR.baseURL+'/?module=fileman&section=utils&page=file_preview',
		post: [
			{name: 'filename', value: filename},
			{name: 'path', value: path}
		],
		autoDestroy: true, resizable: true, maximizable: true,
		tools: [{
		    id: 'save',
			qtip: FR.T('Download'),
		    handler: function(event, toolEl, win){
				FR.actions.downloadOneFile(FR.previewData.path, FR.previewData.filename)
		    }
		}]
	});
	return true;
}
FR.UI.changeView = function(view, toggle) {
	FR.cookieProvider.set('fr-view-mode', view);
	if (toggle) {
		Ext.getCmp('tbarView-'+view).toggle('pressed');
	}
	if (view == 'thumbnails') {
		FR.UI.grid.panel.getView().tpl = FR.UI.grid.thumbnailTpl;
		FR.UI.grid.panel.getView().refresh(true);
	} else {
		FR.UI.grid.panel.getView().tpl = null;
		FR.UI.grid.panel.getView().refresh(true);
	}	FR.UI.grid.currentView = view;
}
FR.UI.restoreView = function() {
	savedViewMode = FR.cookieProvider.get('fr-view-mode', false);
	if (savedViewMode) {
		Ext.getCmp('tbarView-'+savedViewMode).toggle('pressed');
		FR.UI.changeView(savedViewMode);
	} else {
		FR.UI.changeView(Settings.ui_default_view, 1);
	}
}
FR.UI.persistentWindow = function(args) {
	var win = FR.UI.popups[args.id];
	if (!win) {
		win = FR.UI.popup(args);	
		Ext.get(win.getLayout().container.body.dom).mask(args.initMsg);
	} else {
		win.show();
		Ext.get(win.frameId).dom.contentWindow.FR.update(args);
	}
}
FR.UI.showLoading = function(msg, onlyTreePane) {
	if (onlyTreePane) {
		Ext.getCmp('FR-Tree-Region').getEl().mask(msg);
	} else {
		FR.UI.window.getEl().mask(msg);
	}
}
FR.UI.doneLoading = function() {
	Ext.getCmp('FR-Tree-Region').getEl().unmask();
	FR.UI.window.getEl().unmask();
}
