var Authorization = {

	INVISIBLE_CLASS_NAME  : 'invisible',

	oActionQueue           : null,
	bAuthorized            : false,
	sLogin                 : '',
	oStatusAuthorized      : null,
	oStatusNoAuthorized    : null,
	oForm                  : null,
	oLogin                 : null,
	oPassword              : null,
	oSubmit                : null,
	oCancel                : null,
	iErrorAnimationTimer   : null,
	iErrorAnimationCounter : 0,
	oFormCoords            : { iTop : 0, iLeft : 0 },

	init : function(
		sControllerUrl,
		oStatus,
		oForm,
		sCookieName
		) {

		this.oActionQueue = new	ActionQueue(sControllerUrl);
		this.oStatusNoAuthorized = oStatus.getElementsByTagName('div')[0];
		this.oStatusAuthorized = oStatus.getElementsByTagName('div')[1];
		this.oForm = oForm;
		this.oUserAction = this.oStatusAuthorized.getElementsByTagName('span')[0];
		this.oError = oForm.getElementsByTagName('dd')[3];

		this.oForm.getElementsByTagName('form')[0].onsubmit = function() {
			Authorization.logon();
			return false;
		};

		var
			oShowLoginFormAction = this.oStatusNoAuthorized.getElementsByTagName('a')[0],
			oLogoutAction = this.oStatusAuthorized.getElementsByTagName('a')[1],
			aInputs = this.oForm.getElementsByTagName('input')
			;

		oShowLoginFormAction.onclick = function() {

			Authorization.showLoginForm();
			Authorization.oLogin.focus();

			return false;

		};

		oLogoutAction.onclick = function() {

			Authorization.logout();

			return false;

		};

		this.oLogin = aInputs[0];
		this.oPassword = aInputs[1];
		this.oSubmit = aInputs[2];
		this.oCancel = aInputs[3];

		if(Common.Cookie.get(sCookieName)) {

			this.updateLogin();

			this.setAuthorized(true);

		}

		this.oCancel.onclick = oForm.getElementsByTagName('a')[0].onclick = function() {

			Authorization.closeLoginForm();

			return false;

		};

		this.showStatus();

	},

	isAuthorized : function() {

		return this.bAuthorized;

	},

	setAuthorized : function(bAuthorized) {

		this.bAuthorized = bAuthorized;

	},

	showStatus : function() {

		if(this.isAuthorized()) {

			Common.Class.add(this.oStatusNoAuthorized, this.INVISIBLE_CLASS_NAME);
			Common.Class.remove(this.oStatusAuthorized, this.INVISIBLE_CLASS_NAME);

		}
		else {

			Common.Class.add(this.oStatusAuthorized, this.INVISIBLE_CLASS_NAME);
			Common.Class.remove(this.oStatusNoAuthorized, this.INVISIBLE_CLASS_NAME);

		}

	},

	showLoginForm : function() {

		Common.Class.remove(this.oForm, this.INVISIBLE_CLASS_NAME);

		this.clearLoginForm();

	},

	closeLoginForm : function() {

		Common.Class.add(this.oForm, this.INVISIBLE_CLASS_NAME);

	},

	clearLoginForm : function() {

		this.oLogin.value = '';
		this.oPassword.value = '';
		this.displayError(false);

	},

	updateLogin : function() {

		this.oUserAction.innerHTML = Common.Cookie.get(this.oLogin.id);

	},

	logon : function() {

		var aData = [];

		aData['auth-name'] = this.oLogin.value;
		aData['auth-passwd'] = this.oPassword.value;

		this.oActionQueue.addAction(
			new Action.instance(
				'auth-logon',
				aData,
				null,
				this,
				'logonOk',
				'logonError'
				)
			);

		this.progress();
		this.oActionQueue.process();

	},

	logout : function() {

		var aResult = [];

		this.oActionQueue.addAction(
			new Action.instance(
				'auth-logout',
				null,
				null,
				this,
				'logoutOk'
				)
			);

		this.progress();

		this.oActionQueue.process();

	},

	displayError : function(bShow) {

		bShow? Common.Class.remove(this.oError, this.INVISIBLE_CLASS_NAME) : Common.Class.add(this.oError, this.INVISIBLE_CLASS_NAME);

	},

	progress : function() {

		this.displayError(false);
		this.oSubmit.disabled = true;
		this.oCancel.disabled = true;

	},

	unprogress : function() {

		this.oSubmit.disabled = false;
		this.oCancel.disabled = false;

	},

	logonOk : function(aResult) {

		if(aResult['refresh']) {
			document.location = aResult['refresh'];
		}

		this.displayError(false);
		this.unprogress();
		this.updateLogin();
		this.setAuthorized(true);
		this.closeLoginForm();
		this.showStatus();

	},

	logonError : function(sSign, aAddData) {

		this.displayError(true);

		this.iErrorAnimationCounter = 0;
		this.oFormCoords = {
			iLeft    : this.oForm.offsetLeft,
			iTop     : this.oForm.offsetTop,
			iInverse : 1
			};

		this.iErrorAnimationTimer = setInterval('Authorization.errorAnimation()', 1);
		this.unprogress();

	},

	logoutOk : function(aResult) {

		if(aResult['refresh']) {
			document.location = aResult['refresh'];
		}

		this.setAuthorized(false);
		this.showStatus();

	},

	errorAnimation : function() {

		if(this.iErrorAnimationCounter++ < 20) {

			if(this.iErrorAnimationCounter % 2 == 0) {
				this.oFormCoords.iInverse *= -1;
			}

			this.oFormCoords.iLeft = this.oFormCoords.iLeft + this.oFormCoords.iInverse;
			this.oFormCoords.iTop = this.oFormCoords.iTop + this.oFormCoords.iInverse;

			this.oForm.style.left = this.oFormCoords.iLeft + 'px';
			this.oForm.style.top = this.oFormCoords.iTop + 'px';

		}
		else {
			clearInterval(this.iErrorAnimationTimer);
		}

	}

};