
Login = {
	sessionId: null,
	loginForm: null,
	loginFormBg: null,
	userInput: null,
	passwordInput: null,
	isLoggedIn: function() {
		return (this.sessionId != null);
	},
	logIn: function(user, pass, callback) {
		if(this.isLoggedIn()) {
			eval(callback);
			return true;
		} else {
			xajax_AjaxLogin.logIn(user, pass, 'Login.sessionId = true;Login.hideForm();'+callback, 'Login.loginForm.shake();');
		}		
		/*
		var body = document.getElementsByTagName('body')[0];
		if(!body) {
			alert('Login.logIn: No tag body found.');
		}
		var div = document.createElement('div');
		body.appendChild(createLoginDiv);
		*/
		// this.user = 'timo';
		return false;
	},
	logOut: function() {
		this.sessionId = null;
	},
	showForm: function(jsOnSuccess) {
		if(this.isLoggedIn()) {
			this.logIn(null, null, callback);
		}
		var body = document.getElementsByTagName('body')[0];
		if(this.loginForm == null) {
			this.loginFormBg = this.createLoginForm(jsOnSuccess);
			body.appendChild(this.loginFormBg);
		}
		this.loginFormBg.appear({duration:0.5});
		// this.logIn(user, pass, callback
	},
	hideForm: function() {
		if(this.loginFormBg != null) {
			this.loginFormBg.fade({duration:0.5});
		}
	},
	createLoginForm: function(jsOnSuccess) {
		var loginFormBg = document.createElement('div');
		loginFormBg.style.top = '0px';
		loginFormBg.style.left = '0px';
		loginFormBg.style.right = '0px';
		loginFormBg.style.bottom = '0px';
		loginFormBg.style.width = '100%';
		loginFormBg.style.position = 'fixed';
		loginFormBg.style.backgroundColor = 'white';
		loginFormBg.style.display = 'none';
		var loginForm = document.createElement('div');
		loginForm.style.width = '400px';
		loginForm.style.height = '150px';
		loginForm.style.marginTop = '100px';
		loginForm.style.marginLeft = 'auto';
		loginForm.style.marginRight = 'auto';
		loginForm.style.color = 'black';
		loginForm.style.backgroundColor = 'white';
		loginForm.style.border = '1px solid black';
		loginForm.style.position = 'relative';
		loginForm.appendChild(this.createLoginFormTitle());
		loginForm.appendChild(this.createLoginFormInputs());
		loginForm.appendChild(this.createLoginFormButtons(jsOnSuccess));
		loginFormBg.appendChild(loginForm);
		this.loginForm = loginForm;
		this.userInput.focus();
		return loginFormBg;
	},
	createLoginFormTitle: function() {
		var loginFormTitle = document.createElement('div');
		loginFormTitle.style.marginLeft = 'auto';
		loginFormTitle.style.marginRight = 'auto';
		loginFormTitle.style.fontWeight = 'bold';
		loginFormTitle.style.padding = '10px';
		loginFormTitle.innerHTML = 'Login';
		return loginFormTitle;
	},
	createLoginFormInputs: function() {
		var form = document.createElement('form');
		form.method = 'get';
		form.action = '/';
		/*
		form.appendChild(this.createLoginFormUserLabel());
		form.appendChild(this.createLoginFormUserField());
		*/
		form.appendChild(this.createLoginFormUserDiv());
		form.appendChild(this.createLoginFormPasswordDiv());
		form.style.display = 'block';
		form.style.cssFloat = 'left';
		return form;
	},
	createLoginFormButtons: function(onSuccessJs) {
		var div = document.createElement('div');
		var okBtn = document.createElement('input');
		var cancelBtn = document.createElement('input');
		okBtn.type = 'button';
		okBtn.value = 'Login';
		okBtn.onclick = function() {
			Login.logIn(Login.userInput.value, Login.passwordInput.value, onSuccessJs);
		}
		okBtn.style.margin = '10px';
		cancelBtn.type = 'button';
		cancelBtn.value = 'Abbrechen';
		cancelBtn.onclick = function() { Login.hideForm(); };
		cancelBtn.style.margin = '10px';
		div.appendChild(okBtn);
		div.appendChild(cancelBtn);
		return div;
	},
	createLoginFormUserDiv: function() {
		var div = document.createElement('div');
		div.appendChild(this.createLoginFormUserLabel());
		div.appendChild(this.createLoginFormUserField());
		return div;
	},
	createLoginFormPasswordDiv: function() {
		var div = document.createElement('div');
		div.appendChild(this.createLoginFormPasswordLabel());
		div.appendChild(this.createLoginFormPasswordField());
		return div;
	},
	createLoginFormUserLabel: function() {
		var label = document.createElement('label');
		label.innerHTML = 'Benutzername';
		label.style.width = '130px';
		label.style.margin = '10px';
		return label;
	},
	createLoginFormUserField: function() {
		var input = document.createElement('input');
		input.type = 'text';
		input.id = 'user';
		input.name = 'user';
		input.style.width = '230px';
		input.style.margin = '5px';
		this.userInput = input;
		return input;
	},
	createLoginFormPasswordLabel: function() {
		var label = document.createElement('label');
		label.innerHTML = 'Passwort';
		label.style.width = '130px';
		label.style.margin = '10px';
		return label;
	},
	createLoginFormPasswordField: function() {
		var input = document.createElement('input');
		input.type = 'password';
		input.id = 'pass';
		input.name = 'pass';
		input.style.width = '230px';
		input.style.margin = '5px';
		this.passwordInput = input;
		return input;
	}
};

