// Methods for EINSTIEG Tools
//
// @author		Andreas Beutel <typo3@mehrwert.de>
// @version		$Id: tx_dvgwregelwerk_fe.js 1522 2010-02-23 23:09:19Z ab $
// @package		TYPO3
// @subpackage	tx_mwestools
// @license		GPL

var mwestools_fe = {

	ajaxUri: '/',
	eID: 'tx_mwestools_fe_eidtools',
	throbber: '<img src="/typo3conf/ext/mw_es_tools/res/images/icons/gfx_throbber.gif" width="16" height="16" alt="Loading" />',

		// Get the login status
	getLoginStatus: function() {
		elementID = 'login-status';
		if ($(elementID)) {
			$(elementID).innerHTML = this.throbber;
			new Ajax.Request(this.ajaxUri, {
				method: 'get',
				parameters: { eID: this.eID, action: 'getLoginStatus', no_cache: 1 },
				onSuccess: function(xhr) {
						// Get the results
					$(elementID).innerHTML = xhr.responseText;
				},
				onFailure: function() {
					$(elementID).innerHTML = this.getLoginStatus();
				}
			});
		}
	},

		// Log in
	login: function() {
		elementID = 'login-status';
		new Ajax.Request(this.ajaxUri, {
			method: 'post',
			parameters: $('mwestools_login_form').serialize(true),
			onSuccess: function(xhr) {
					// Get the results
				$(elementID).innerHTML = xhr.responseText;
					// Reload page
			//	window.location.reload();
			},
			onFailure: function() {
				$(elementID).innerHTML = this.getLoginStatus();
			}
		});
	},

		// Log out
	logout: function() {
		elementID = 'login-status';
		$(elementID).innerHTML = this.throbber;
		new Ajax.Request(this.ajaxUri, {
			method: 'post',
			parameters: { eID: this.eID, action: 'logout', logintype: 'logout', no_cache: 1 },
			onSuccess: function(xhr) {
					// Get the results
				$(elementID).innerHTML = xhr.responseText;
					// After successful logout redirect to startpage
				// window.location.href = '/';
			},
			onFailure: function() {
				$(elementID).innerHTML = this.getLoginStatus();
			}
		});
	},

		// Link to users profile view
	showUserDetail: function(userId) {
		window.location.href = '/community/kontakte/profildetail/profildetail/' +userId+ '/';
	}
};

document.observe('dom:loaded', function() {
	mwestools_fe.getLoginStatus();
});
