try {
	var userControllerStorage = new Array();
	var userControllerIsInitialized = false;
} catch(e) { }

function userController(location, publicationId) {
	// "location" is used as string to identify login boxes and masks as well as callback handlers ...
	var _location = location;
	var _locationId = 0; // will be used for sebo's
	var _publicationId = parseInt(publicationId);
	var _instance = this;

	// returns name of "this" controller instance
	// is very important to add new locations here!
	this.getObjectName = function() {
		if (_location == 'article_comment') {
			return 'userctrl';
		} else if (_location == 'sebo') {
			return 'userSeBoCtrl_'+_locationId;
		}
	}


	this.doLogin = function() {
		var formValues = false;
		if (_location == 'article_comment') {
			formValues = xajax.tools.getFormValues('articleLoginForm');
		} else if(_location == 'sebo') {
			formValues = xajax.tools.getFormValues('seBoUserAuthForm_'+_locationId);
		}

		if (formValues != false) {
			xajax_userWebserviceAuth(formValues['username'], formValues['password'], _publicationId, this.getObjectName()+'.handleLoginResult')
		}
	}


	this.doLogout = function() {
		xajax_userWebserviceRemoveAuth(this.getObjectName()+'.handleLogout');
	}


	this.doCheckAuth = function() {
		xajax_userWebserviceIsAuth(this.getObjectName()+'.handleCheckAuth');
	}


	this.handleLogout = function() {
		jQuery.each(userControllerStorage, function(i, userControllerObj) {
			userControllerObj.performLogout();
		});
	}


	this.handleLoginResult = function(loginOk, userParameters, errorMessage) {
		if (loginOk) {
			jQuery.each(userControllerStorage, function(i, userControllerObj) {
				userControllerObj.performLogin(userParameters);
			});
		} else {
			alert('Es ist ein Fehler aufgetreten:\n'+errorMessage);
		}
	}


	this.handleCheckAuth = function(isAuth, userParameters) {
		jQuery.each(userControllerStorage, function(i, userControllerObj) {
			if (isAuth) {
				this.performLogin(userParameters);
			} else {
				this.performLogout();
			}
		});
	}


	this.performLogout = function() {
		if (_location == 'article_comment') {
			$('div#article_comment_user_status').hide();
			$('div#article_comment_user_form')
				.show()
				.find(":input[type!='button']").val('');
		} else if(_location == 'sebo') {
			$('div#seBoUserAuthStatus_'+_locationId).hide();
			$('div#seBoUserAuthFormContainer_'+_locationId)
				.show()
				.find(":input[type!='button']").val('');
		}
	}


	this.performLogin = function(userParameters) {
		SetCookie('dewezet_frontend_grz', userParameters['SESSIONID'], null, '/');
		if (_location == 'article_comment') {
			$('div#article_comment_user_form').hide();
			$('div#article_comment_user_status')
				.show()
				.find('span#username')
				.html(userParameters['VORNAME']+' '+userParameters['NACHNAME']);
		} else if(_location == 'sebo') {
			$('div#seBoUserAuthFormContainer_'+_locationId).hide();
			$('div#seBoUserAuthStatus_'+_locationId)
				.show()
				.find('span#username')
				.html(userParameters['VORNAME']+' '+userParameters['NACHNAME']);
		}
	}


	// ------------------------- CONSTRUCTOR -------------------------
	if(_location.substring(0, 4) == 'sebo') {
		locationParts = _location.split('_');
		_locationId = parseInt(locationParts[1]);
		if (_locationId != 0) {
			_location = locationParts[0];
		}
	}

	$(document).ready(function() {
		// check user authentication by default
		// AJAX is necessary cause static page is cached!
		if (!userControllerIsInitialized) {
			// if more than one userController is initialized at page, checkAuth function is only called once!
			// it will initialize all userController instances at this page
			userControllerIsInitialized = true;
			_instance.doCheckAuth();
		}
	});

	userControllerStorage.push(this);
}