// Namepsace setzen
Platform.Login = {};

Platform.Login.Worker = Class.create({
	/** @lends Platform.Login.Worker */
	element: false,
	authentication: false,
	onAuthFailed: Prototype.K,
	onAuthenticated: Prototype.K,
	onLoggedIn: Prototype.K,
	initialize: function(parameters) {
		if(parameters && parameters.element)
			this.element = parameters.element;
		
		if(parameters && parameters.onAuthFailed)
			this.onAuthFailed = parameters.onAuthFailed;
		
		if(parameters && parameters.onAuthenticated)
			this.onAuthenticated = parameters.onAuthenticated;
		
		if(parameters && parameters.onLoggedIn)
			this.onLoggedIn = parameters.onLoggedIn;
		
		$$(".lostPassword").invoke("observe", "click", this.displayPasswordLostDialog);
		this.element.select(".button").invoke("observe", "click", this.loginClicked.bind(this));
		this.element.select("input").invoke("enable");
		this.authentication = new Platform.Login.Authentication(parameters);
		this.authentication.authenticate();
	},
	loginClicked: function(clickEvent) {
		var validator = new Form.Validator({
			element: this.element
		});
		
		validator.validate();
		
		if(validator.getResult() == true) {
			new Platform.Salt({
				element: this.element,
				onSalted: this.submission.bind(this)
			});
		}
	},
	displayPasswordLostDialog: function(clickEvent) {
		var params = {
			requestParameters: $F($$("[name=userName]").first())
		};
		document.observe("dialog:complete", Platform.Login.PasswordLost);
		Platform.Dialog.show("PasswordLost", {
			requestParameters: params
		});
	},
	submission: function() {
		var componentsClass = new Element("input", {
			type: "hidden",
			name: "className",
			value: "LoginWorker"
		});
		
		this.element.insert({ "bottom": componentsClass });
		
		this.element.request({
			onComplete: function(transport){
				this.element.update(transport.responseText);
				
				this.onLoggedIn();
				this.onAuthenticated();
			}.bind(this),
			on401: this.onAuthFailed
		});
	},
	logout: function() {
		//console.log("Starte logout");
		var loginWorker = new Platform.Login.Authentication({
			element: "dummy"
		});
		//console.log("2. logout");
		loginWorker.logout();
		//console.log("Beende logout");
	}
});

Platform.Login.Authentication = Class.create({
	element: false, 
	onAuthFailed: Prototype.K,
	onAuthenticated: Prototype.K,
	initialize: function(parameters) {
		if(!parameters || !parameters.element)
			throw new Error("No element given to authentication.");
		
		if(parameters && parameters.onAuthFailed)
			this.onAuthFailed = parameters.onAuthFailed;
		
		if(parameters && parameters.onAuthenticated)
			this.onAuthenticated = parameters.onAuthenticated;
		
		this.element = parameters.element;
	},
	authenticate: function() {
		if(this.element && this.element.disable)
			this.element.disable();
		
		var params = {
			mode: "ajax",
			__method: "authenticate"
		};
		new Ajax.Request("/services/authenticationService.php", {
			method: "post",
			parameters: params,
			onSuccess: function(transport) {
				if(transport.responseJSON.authenticated) {
					new Ajax.Updater(this.element, "/component.php", {
						parameters: {
							mode: "ajax",
							className: "LoginWorker"
						},
						onComplete: function(transport) {
							$$(".deleteVehicle").invoke("observe", "click", this.deleteVehicle);
							$$(".logout").invoke("observe", "click", this.logout);
							$$(".lostPassword").invoke("observe", "click", this.displayPasswordLostDialog);
						}.bind(this)
					});
					
					this.onAuthenticated();
				} else {
					if(this.element && this.element.enable)
						this.element.enable();
					
					this.onAuthFailed();
				}
								
			}.bind(this),
			onFailure: function(transport) {
				//console.log("error: %o", transport);
			}.bind(this)
		});
	}, 
	logout: function() {
		var params = {
			mode: "ajax",
			__method: "logout"
		};
		new Ajax.Request("/services/authenticationService.php", {
			method: "post",
			parameters: params,
			onSuccess: function(transport) {
				document.location.reload();
			}
		});
	}
});

Platform.Login.PasswordLost = function() {
	try {
		$("passwordLostForm").select(".button").invoke("observe", "click", function(clickEvent) {
			try {
				var validator = new Form.Validator({
					element: $("passwordLostForm")
				});
				
				validator.validate();
				
				if(validator.getResult() == true) {
					new Platform.Salt({
						element: $("passwordLostForm"),
						onSalted: function(transport) {
							$("passwordLostForm").request({
								onComplete: function() {
									Platform.Dialog.close();
								}
							});
						}
					});
				}
			}catch(exc) {
				//console.log("Exception in Line 132 in Platform.Login: %o", exc);
			}
		});
	}catch(exc) {
		//console.log("Exception in Line 136 in Platform.Login: %o", exc);
	}
}

Platform.Login.LoginLogoutHandler = Class.create({
	element: false,
	initialize: function(parameters) {
		if(parameters && parameters.element)
			this.element = parameters.element;
		
		var params = {
			mode: "ajax",
			__method: "authenticate"
		};
		new Ajax.Request("/services/authenticationService.php", {
			method: "post",
			parameters: params,
			onSuccess: function(transport) {
				if(transport && transport.responseJSON && transport.responseJSON.authenticated) {
					this.element.writeAttribute("href", "javascript:;");
					this.element.update("Ausloggen");
					this.element.observe("click", function(clickEvent) {
						var authService = new Platform.Login.Authentication({
							element: this
						});
						
						authService.logout();
					});
				}
			}.bind(this)
		});
	}
});