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 },
	sRefresh               : './',

	init : function(
		sControllerUrl,
		oStatus,
		oForm,
		sCookieName
		) {							
				
		this.oActionQueue = new	ActionQueue(sControllerUrl);
		this.oStatusNoAuthorized = oStatus.getElementsByTagName('div')[0];
		this.oStatusAuthorized = oStatus.getElementsByTagName('div')[2];
		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.sRefresh = './';
			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);
			var obj = document.getElementById("shedule_mode");
			if(obj){
				obj.style.display = "block";
				if(Common.Cookie.get('group_ids')){
					var sGroups= unescape(Common.Cookie.get('group_ids')).split(',');
					var aGroups = new Array();
					for(var i=0;i<sGroups.length;i++){
						var a = sGroups[i].split('|');
						aGroups.push({id: a[0], name: a[1], subject_id: a[2]})
					}										
					if(sGroups.length>0){
						var sTmp='';
						var sTemp='';
						var aUrlParts = window.location.href.split('?');
						var iCurrentGroup = 0;
						if(aUrlParts[1]){
							aParamParts = aUrlParts[1].split('=');
							if(aParamParts[0]=='group_id'){iCurrentGroup = aParamParts[1]}
						}						
						for(var i=0;i<aGroups.length;i++){
							if(aGroups.length==1){
								sTemp='Расписание '+ Common.Cookie.get('genitive');
							}else{
								sTemp=aGroups[i].name;
							}
							if(aGroups[i].id==iCurrentGroup){
								sTmp=sTmp+' <span style="background-color:#E3E3E3;margin-right:0.5em;padding:0.1em 0.3em;"><noindex>'+sTemp+'</noindex></span>';
							}else{
								sTmp=sTmp+' <a href="javascript:gotoGroup('+aGroups[i].id+','+aGroups[i].subject_id+')"><noindex>'+sTemp+'</noindex></a>';
							}							
						}
						document.getElementById('time_table_personal').innerHTML = sTmp;
					}
				}				
			}
			
			var iDiscont = Common.Cookie.get('discount');
			if (iDiscont=='20'){
				var oLamp = document.getElementById('lamp_order');
				if(oLamp){oLamp.style.display = 'none'}
			}
			
		}
		else {					
		
			Common.Class.add(this.oStatusAuthorized, this.INVISIBLE_CLASS_NAME);		
			Common.Class.remove(this.oStatusNoAuthorized, this.INVISIBLE_CLASS_NAME);
			var oForAuth = document.getElementsByTagName('a');
			for(var i=0;i<oForAuth.length;i++){
				var s = oForAuth[i].href;
				if(s.substr(s.length-8) == '/forums/'){
				oForAuth[i].onclick = function(){
					Authorization.sRefresh = '/forums/';
					Authorization.showLoginForm();
					Authorization.oLogin.focus();
					return false;
					}
				}
				if(s.substr(s.length-9) == '/profile/'){
				oForAuth[i].onclick = function(){
					Authorization.sRefresh = '/profile/';
					Authorization.showLoginForm();
					window.location="#";
					return false;
					}
				}
			}			
		}
	
	},

	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('username');
	
	},
	
	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) {
		document.location = this.sRefresh;
	
		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) {
		document.location = '/';		
			
		this.setAuthorized(false);		
		this.showStatus();
		this.unprogress();
	
	},
	
	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);
		}
	
	}

};