var intId = 0;

//Sessions expire after 1440 seconds.  Warn user 2 minutes before that.
var warningThreshold = 1440-120;
//Check Idle Time every 120 seconds.
var interval = 120000;

var intFunc = function(){
	getIdleTime(session_id);
}

function getIdleTime(session_id){
	
	var handler = function(response){
		
		if (response.session_expired == 1){
			clearInterval(intId);
			alert('Your session has expired.  Please log in again.');
			window.location = "index.php";
		} else {
		
			var seconds = response.idle_time;
			if (seconds >= warningThreshold){
				if (confirm("Your session will expire soon.  Would you like to keep your session active?")){
					//Call an Ajax function to revive the session.
					jQuery.post("includes/handler.php",{'action':'revive_session'});
				}
			}
		
		}
		
	}
	var data = {
		'action':'get_idle_time',
		'session_id':session_id
	};
	jQuery.post("includes/handler.php",data,handler,"json");
	
}

function startSessionTracking (session_id){
	intId = setInterval('intFunc()',interval);
}
