 // LOAD-ORDER: 70

/*
	"static" C_L_A_S_S
	
		or
		
	@module
	
	
		AjaxRelogin
		
		DEPENDS on Global: CONTEXT_PATH !!
	
*/

var AjaxRelogin = {
	
	MAX_LOGIN_ATTEMPTS : 3,
	LOGIN_REL_URL : 'login',
	LOGIN_WIN_ID : 'login_window',
	HTTP_Expectation_failed: 417,
	_login_attempts : 0 , 
	
	
	/* main public method */
	ajaxRelogin : function ( delayed_func ){
		
		if( delayed_func ) this._delayed_func = delayed_func;
		this._ajaxLoginServiceUrl = CONTEXT_PATH + this.LOGIN_REL_URL;
		 
		if ( this._login_attempts >= this.MAX_LOGIN_ATTEMPTS ) {
			/*bye - bye*/
			location = CONTEXT_PATH;
		} else {
			
			 	if( !$( this.LOGIN_WIN_ID ) ) { this.createAjaxReloginWindow(); }
			 	
			 	this.reApplyEventsToReloginForm();
			 	
			 	 this.unGrayAjaxReloginWindow();
			 	
			 	this.popupReloginWindow();
		}
					 	
		
	},	
	
	createAjaxReloginWindow: function() {		
		
		
			
			var div = $(document.createElement('div'));				
			div.writeAttribute('id', this.LOGIN_WIN_ID);
			div.addClassName( 'foxed');
			div.setStyle( { 'display':'none' } ); /* makes effect possible! */
			var login_body = "<h1>" + I18N['relogin.header'] + "</h1><form><div class='form_row'><span>" + I18N['username'] +
							 ":</span><input type='text' name='username'/></div>" +
							"<div class='form_row'><span>" + I18N['password'] + 
							":</span><input type='password' name='password'/></div>" +
							"<div class='form_row'><button class='accept'>" + I18N['login'] + "</button></div></form>";
							
				alert( "login_body is \n\n " +login_body );
							
			div.update(login_body);
			Element.insert( $$('body')[0], { 'bottom' : div } );
			
			this._ajaxReloginForm = $(this.LOGIN_WIN_ID).down('form');
			
	},
	
	reApplyEventsToReloginForm : function() {
		
		this._ajaxReloginForm.stopObserving( 'submit', null );		
		this._ajaxReloginForm.observe(	'submit', this.ajaxReloginFormSubmitted.bind(this) );
		this._ajaxReloginForm.enable();
	},
	
	ajaxReloginFormSubmitted: function(eventObj) {
		
		this.grayAjaxReloginWindow();
		this._ajaxReloginForm.disable();
		
		/* ajax.starts.here */
		var usr = $F(this._ajaxReloginForm['username']);
		var pwd = $F(this._ajaxReloginForm['password']);
		
		new Ajax.Request( this._ajaxLoginServiceUrl, {
														parameters: { 'user' : usr , 'password' : pwd } ,
										  				method: 'post' ,
										  				onSuccess: this.succesfullLoginRequest.bind(this),
										  				onFailure: this.failedLoginRequest.bind(this)
														}
							);						
		
		eventObj.preventDefault();
	},
			
	grayAjaxReloginWindow: function() {
		$(this.LOGIN_WIN_ID).addClassName('gray');
	},
	
	unGrayAjaxReloginWindow: function() {
		$(this.LOGIN_WIN_ID).removeClassName('gray');
	},
	
	popupReloginWindow: function() {	
		
		if ( this.showOverlay ) this.showOverlay(this);		/* Kwack! */
				 
		if( ! $(this.LOGIN_WIN_ID).visible() )
			Effect.Appear( $(this.LOGIN_WIN_ID), { afterFinish: this.setFocusInAjaxReloginForm.bind(this)  } );	
		else
			this.setFocusInAjaxReloginForm();
		
	},
	
	setFocusInAjaxReloginForm: function() {
		
		this._ajaxReloginForm.down('input').focus();		
	},	
	
	
	succesfullLoginRequest: function(resp) {
		$(this.LOGIN_WIN_ID).hide();
		if ( this.hideOverlay ) this.hideOverlay();		/* Kwack! */
		this._login_attempts = 0;
		/* return in GLORY */
		this._delayed_func();
	},
	
	failedLoginRequest: function(resp) {
		
		if (resp.status == this.HTTP_Expectation_failed ) {
			this._login_attempts++;
			Effect.Shake( $(this.LOGIN_WIN_ID) );
			var aR_func = this.ajaxRelogin.bind(this);
			aR_func.delay(1);
		} else {
		 	alert ("Feil svar ifra server!" + resp.status);
		 	/*bye - bye*/
			location = CONTEXT_PATH;
		}
	},
	
	/* @private */
	onOverlayHide: function() {
		
		this.showOverlay(this);		/* å nei du! */
	}
	
	
	
	
};

