/*
 * 
 * Logout : ui-userLogout
 * GetUserInformations : ui-userInfo
 * 
 */

// Javascript - User-Objekt
var UI_USER = function() {

	var self = null;
	self = $.extend(true, self, {

		getUserData : function(success) {

			$.post(PAGE["rootPath"] + PAGE["currentURL"], {

				clearPage : true,
				clearContent : true,
				modUserManagement_getUserData : true

			}, function(data) {

				if (success != "") {
					success(data);
				
				self = $.extend(true, data, self);
				}
			},'json').error(self._error);

		},

		login : function(name, password) {

			var data = {

				clearPage : true,
				login : true,
				"name" : name,
				"password" : password

			};

			$.post(PAGE["rootPath"] + "public/page/userManagement-login.html",
					data, function(html) {

						self._loggedHandler((html == ''));

					});

		},

		logout : function() {

			var data = {

				clearPage : true,
				logout : true

			};

			$.post(PAGE["rootPath"] + "public/page/userManagement-login.html",
					data, function(html) {

						self._loggedOutHandler((html == ''));

					});

		},

		_logged : [],
		// Tritt ein wenn der Login erfolgreich war.
		logged : function(data) {

			if (data == null)
				self._loggedHandler(null);

			if (typeof data == 'object')
				self._logged = $.extend([], _logged, data);
			else
				self._logged.push(data);

		},
		_loggedHandler : function(data) {

			$.each(self._logged, function(i, v) {
				;
				v(data);
			});
		},

		_loggedOut : [],
		// Tritt ein wenn der Login erfolgreich war.
		loggedOut : function(data) {

			if (data == null)
				self._loggedHandler(null);

			if (typeof data == 'object')
				self._loggedOut = $.extend([], _loggedOut, data);
			else
				self._loggedOut.push(data);

		},
		_loggedOutHandler : function(data) {

			$.each(self._loggedOut, function(i, v) {
				;
				v(data);
			});
		},
		
		_error : function (a,b,c){
			
			
			alert(a+"\n"+b+"\n"+c);
		},

	});
	return self;

};

$(document).ready(function() {

	$('a[href$="#ui-userLogout"]').click(function() {

		user = UI_USER();
		user.loggedOut(function() {

			location = PAGE["rootPath"] + PAGE["currentURL"];

		});
		user.logout();

	});

});

