Uwd.System.Login = function(config){
	if (!config) {
		config = {
			id: 'loginWindow',
			iconCls: 'login_window_icon',
			title: Uwd.System.LoginLanguage.login,
			width:352,
			//height:265,
			border: false,
			shim:true,
			minimizable: false,
			maximizable: false,
			resizable: false,
			closable: false,
			animCollapse:true,
			constrainHeader:true,
			autoScroll: true,
			
			
			plugins : [
				new Ext.ux.WindowDrawer({
					title	  : Uwd.System.LoginLanguage.forgotPassword.title,
					items      : [new Uwd.System.ForgotPassword()],
					side      : 'e',
					animate   : true,
					layout	  : 'fit',
					resizable : false,
					border	  : false,
					width     : 250
				}),
				new Ext.ux.WindowDrawer({
					title	  : Uwd.System.LoginLanguage.register.title,
					items      : [new Uwd.System.Register()],
					side      : 'w',
					layout	  : 'fit',
					animate   : true,
					resizable : false,
					border	  : false,
					width     : 250
				})
			]
		};	
	}
	Ext.apply(this, config);
	
	this._forgotPassIsShow = false;
	this._registerIsShow = false;
	
	this.template = new Ext.Template(
		'<div id="loginMainContainer">' +
		'	<div id="loginTopContainer">' +
		'	  <div id="forgot-pass"><a id="forgot-pass-link" onclick="return false;" href="#">' + Uwd.System.LoginLanguage.forgotPassword.title + '</a></div>' +
		'	  <div id="register"><a id="register-link" onclick="return false;" href="#">' + Uwd.System.LoginLanguage.register.title + '</a></div>' +
		'	</div>' +
		'	<div id="loginBottomContainer">' +
		
		'		<label class="loginLabel"><b>' + (window.system.config.getLoginType() === 'email' ? Uwd.System.LoginLanguage.forgotPassword.field : Uwd.System.LoginLanguage.username) + '</b></label>' +
		'		<input type="text" id="loginFormUserField" />' +
		'		<br />' +
		'		<div class="loginSpacer"><img src="./images/1px_leer.gif" height="4" width="1" /></div>' +
		
		'		<label class="loginLabel"><b>' + Uwd.System.LoginLanguage.password + '</b></label>' +
		'		<input type="password" id="loginFormPassField" />' +
		'		<div class="loginSpacer"><img src="./images/1px_leer.gif" height="4" width="1" /></div>' +
		
		'		<div id="loginButtonContainer">' +
		//'			<input type="text" id="language" value="" style="width:20px !important;" />' +
		'			<img style="cursor: pointer" width="25" height="25" id="loginButton" src="./resources/default/images/login/login_button.gif" alt="' + Uwd.System.LoginLanguage.login + '">' +
		'		</div>' +
		'	</div>' +
		'</div>');
	
	this.items = [this.template];

	Uwd.System.Login.superclass.constructor.call(this);
	
	this.on({
		'show': {
			fn: this.renderForm,
			scope: this
		}
	});
};

Ext.extend(Uwd.System.Login, Ext.Window, {
	
	renderForm : function() {
		var formFieldUser = new Ext.form.Field({
			applyTo : 'loginFormUserField',
			id : 'loginFormUserField',
			name: 'user',
			width: 120,
			blankText: 'username',
			allowBlank:false
		});

		var formFieldPass = new Ext.form.Field({
			applyTo : 'loginFormPassField',
			id : 'loginFormPassField',
			width: 120,
			name: 'pass',
			allowBlank:false
		});
		
		window.setTimeout(function() {
			formFieldUser.focus();
			Ext.get('forgot-pass-link').on('click', function(){
				if (this._forgotPassIsShow === false) {
					this.drawers.e.show();
					this._forgotPassIsShow = true;
				} else {
					this.drawers.e.hide();
					this._forgotPassIsShow = false;
				}
			}.createDelegate(this));

			Ext.get('register-link').on('click', function(){
				if (this._registerIsShow === false) {
					this.drawers.w.show();
					this._registerIsShow = true;
				} else {
					this.drawers.w.hide();
					this._registerIsShow = false;
				}
			}.createDelegate(this));
			
			
			Ext.get('loginButton').on({
				'click': {
					fn: this.sendRequest,
					scope: this
				}
			});
			Ext.get('loginFormUserField').on('keydown', this.sendRequest.createDelegate(this));
			Ext.get('loginFormPassField').on('keydown', this.sendRequest.createDelegate(this));
		}.createDelegate(this), 300);
	},
	
	sendRequest : function(e) {
		// Wenn Mausklick oder Enter aus einem Formfeld
		if(!e.getKey() || e.getKey() == e.ENTER){
			this.body.mask();
			// Request um Login Daten abzufragen
			Ext.Ajax.request({
				url: window.system.getBackendUrl(),
				method:'post',
				params: {
					controller: 'Login',
					basePath: 'system/',
					action: 'login',
					user: Ext.get('loginFormUserField').getValue(),
					pass: Ext.get('loginFormPassField').getValue()
				},
				success: function(result, request) {
					this.body.unmask();
					
					//alertBox.close();
					var jsonData = Ext.decode(result.responseText);
					if (jsonData.success === false) {
						var errorWin = Ext.MessageBox.alert(Uwd.System.SystemLanguage.error, '<img src="./resources/default/icons/icon-warning.gif" style="float: left; padding-right: 10px;" />' + jsonData.data.message);
						errorWin.getDialog().on('hide', function() {
							Ext.get('loginFormUserField').focus();
					    });
					} else {
						// reload page
						window.location.replace('index.php');
					}
				}.createDelegate(this),
				failure: function(result, request) {
					this.body.unmask();
					Ext.MessageBox.alert(Uwd.System.SystemLanguage.error, Uwd.System.SystemLanguage.connectionError);
				}.createDelegate(this)
			});
		}
	}
});

