/**
 * Responsible for: -
 * 
 * Handling the HTTP session by issuing keep alive requests.
 * 
 * Note you should always create an instance of SessionController called 
 * sessionController otherwise the repeated keep alive will fail.
 */
var SessionController = Class.create({
	/**
	 * Constructor.
	 * 
	 * @param inter, the number of ms between keep alives.
	 * @param max, the number of keepalive attempts to issue.
	 */
	initialize: function(inter, max){
		this.interval = inter;
		this.maxAttempts = max;
		this.attempts = -1;
		this.lock = false;
	},
	/**
	 * Start a new keep alive request will keep attempting as long as attempts < maxAttempts.
	 */
	start: function(){
		try{
			if(this.attempts < 0){
				//First time around set delay
				this.attempts++;
				window.setTimeout('sessionController.start()', this.interval);
				return;
			}
			if(this.attempts < this.maxAttempts && !this.lock){
				//Obtain lock
				this.lock = true;				
				//Pump up the attempts straight away
				this.attempts++;
				log.info('This is keep alive attempt ' + this.attempts);
				//Start another check which will only pass if there are any attempts remaining.
				window.setTimeout('sessionController.start()', this.interval);
				//Issue keep alive request.
				KeepAlive.keepAlive();
				//Release lock
				this.lock = false;
			}
		}catch(err){
			log.error('Problem issuing keep alive: ' + err);
		}
	}
});

dwr.engine.setErrorHandler(function(msg){
	log.error("DWR Error " + msg);
});

dwr.engine.setWarningHandler(function(msg){
	log.warn("DWR Warning " + msg);
});