Ext.onReady(function() {
	var formButtons = [];
	if (signUpEnabled) {
		formButtons.push({
			text: '&laquo; '+labels.signup,
			handler: function() {
				if (Settings.ui_display_language_menu) {
					langPar = '&language='+encodeURIComponent(Ext.getCmp('lang').getValue());
				} else {
					langPar = '';
				}
				document.location.href = signUpURL+langPar;
			}
		});
	}
	formButtons.push({text: labels.login, handler: submitForm});
	
	
	var formFields = [
		{
		id: 'usr',
		fieldLabel: labels.username,
		name: 'username',
		labelStyle: 'white-space:nowrap',
		width:150,
		value: prefilledUsername
		},{
		id: 'pass',
		fieldLabel: labels.password,
		labelStyle: 'white-space:nowrap',
		name: 'password',
		inputType: 'password',
		width:150,
		value: prefilledPassword
		},{
		xtype: 'hidden',
		name: 'redirectAfterLogin',
		value: redirectAfterLogin
		}
	];
	
	if (Settings.ui_display_language_menu)	{ 
		formFields.push({
			id: 'lang',
			xtype: 'combo',
			hidden: !Settings.ui_display_language_menu,
			hideParent: true,
			fieldLabel: '&nbsp;',
			labelSeparator: '',
			autoCreate:true,
			mode: 'local',
			displayField:'n',
			valueField:'k',
			hiddenName:'language',
			name: 'language',
			editable: false,
			triggerAction:'all',
		 	disableKeyFilter: true,
			forceSelection:true,
			value: selectedLang,
			width:100,
			store: new Ext.data.SimpleStore({
				fields: ['n', 'k'],
				data: Languages
			}),
			listeners: {
				select: function(el) {
					usrName = Ext.getCmp('usr').getValue();
					if (!usrName || usrName.length == 0) {
						document.location.href = URLRoot+'/?language='+encodeURIComponent(el.getValue());
					}
				}
			}
		});
	}
	
	if (allowPersistentLogins)	{ 
		formFields.push({
			xtype: 'checkbox',
			fieldLabel: '',
			labelSeparator: '',
			id: 'track-tip',
			boxLabel: labels.keepLogged,
			inputValue : '1',
			name: 'persistent'
		});
	}
	
	win = new Ext.Window({
		el: 'viewport',
		layout: 'fit',
		constrain: true,
		closable: false,
		resizable: false,
		draggable: false,
		title: Settings.title,
		autoHeight: true,
		iconCls: 'headerIcons-appWindow',
		width:350,
		bodyStyle:'background-color:white',
		hideBorders: true,
		items: [
			new Ext.Panel({
				autoHeight: true,
				autoWidth: true,
				html:'<div id="loginLogo"><img src="'+URLRoot+'/images/fileman/interface/logo_and_tagline.png" alt="" border="0" height="82" width="376"></div>'
			}),
			loginForm = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
				labelAlign: 'right',
		        defaultType: 'textfield',
				autoHeight: true,
				bodyStyle: 'padding-top:25px;padding-left:20px;padding-bottom:25px;',
		        items: formFields
		    })
		],
		buttons: formButtons
	});

	function submitForm() {
		loadMask.show();
		loginForm.getForm().submit({
			url:loginURL, 
			failure: function(frm, act) {
				loadMask.hide();
				Ext.Msg.show({
				   title:'',
				   msg: act.result.error,
				   buttons: Ext.Msg.OK,
				   modal: false
				});
			},
			success: function() {
				document.location.href = URLRoot+(startFolder ? '/?folder='+encodeURIComponent(startFolder): '');
			}
		});
	}
	win.show();
	if (allowPersistentLogins)	{
		new Ext.ToolTip({
			target: Ext.getCmp('track-tip').labelEl.dom,
			defaultAlign: 'bl',
			showDelay: 100,
			dismissDelay: 0,
			width: 300,
			html: labels.keepLoggedInDescription
		});
	}
	
	loadMask = new Ext.LoadMask(win.body, {msg: labels.waitMsg});
	if (passwordRecoveryEnabled) {
		if (Settings.ui_display_language_menu) {
			langPar = '&language='+encodeURIComponent(Ext.getCmp('lang').getValue())
		} else {
			langPar = '';
		}
		Ext.get(win.footer.dom).first().first().insertFirst({tag: 'div', style:'float:left;font-size:11px;line-height:20px;', html: '<a href="'+URLRoot+'/?module=fileman&page=password_recovery'+langPar+'">'+labels.forgotPassword+'</a>'})
	}
	win.anchorTo(Ext.get('theBODY'), 'c-c');
	new Ext.KeyMap("usr", {
	    key: Ext.EventObject.ENTER,
	    fn: submitForm
	});
	new Ext.KeyMap("pass", {
	    key: Ext.EventObject.ENTER,
	    fn: submitForm
	});
	window.setTimeout(function() {Ext.getCmp('usr').focus();}, 100);
});
