/* Session.js */
//total length of session in seconds
if (!sessionLength) var sessionLength = 3600;
//time warning shown (10 = warning box shown 10 seconds before session ends)
if (!warning) var warning = 10;
//time redirect forced (10 = redirect forced 10 seconds after session ends)
if (!forceRedirect) var forceRedirect = 10; 
//address to force relocation to
if (!relocateTo) var relocateTo = "index.cfm";

//event to check session time variable declaration
var checkSessionTimeEvent = 0;

$(document).ready(function() {
	//event to check session time left (times 1000 to convert seconds to milliseconds)
	checkSessionTimeEvent = setInterval("checkSessionTime()",10*1000);
});

//time session started
var requestTime = new Date();

//session timeout length
var timeoutLength = sessionLength*1000;

//set time for first warning, ten seconds before session expires
var warningTime = timeoutLength - (warning*1000);

//force redirect to log in page length (session timeout plus 10 seconds)
var forceRedirectLength = timeoutLength + (forceRedirect*1000);

//set number of seconds to count down from for countdown ticker
var countdownTime = warning;

//warning dialog open; countdown underway
var warningStarted = false;

function checkSessionTime() {
	//get time now
	var timeNow = new Date(); 
	
	//event create countdown ticker variable declaration
	var countdownTickerEvent; 	
	
	//difference between time now and time session started variable declaration
	var timeDifference = 0;
	
	timeDifference = timeNow - requestTime;

	if (timeDifference > warningTime && warningStarted === false) {            
		//call now for initial dialog box text (time left until session timeout)
		countdownTicker(); 
            
		//set as interval event to countdown seconds to session timeout
		countdownTickerEvent = setInterval("countdownTicker()", 1000);
            
		$('#dialogWarning').dialog('open');
		warningStarted = true;
	}
	else if (timeDifference > timeoutLength) {
		//close warning dialog box if open
		if ($('#dialogWarning').dialog('isOpen')) $('#dialogWarning').dialog('close');
		$('#dialogExpired').dialog('open');
			//clear (stop) countdown ticker
			clearInterval(countdownTickerEvent);
	}
        
	if (timeDifference > forceRedirectLength) {    
		//clear (stop) checksession event
		clearInterval(checkSessionTimeEvent);
		//force relocation
		window.location=relocateTo+"?expired=true";
	}
}

function countdownTicker() {
	//put countdown time left in dialog box
	$("span#dialogText-warning").html(countdownTime);
	//decrement countdownTime
	countdownTime--;
}

$(function(){              
	var warnDiv = $(document.createElement('div'));
	var expireDiv = $(document.createElement('div'));
	
	warnDiv.attr("id", "dialogWarning");
	warnDiv.attr("title", "Session Expiring!");
	var htmlString = "<p>";
	htmlString += '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span>';
	htmlString += ' Your session will expire in <span id="dialogText-warning"></span> seconds!';
	htmlString += "</p>";
	warnDiv.html(htmlString);
	
	expireDiv.attr("id", "dialogExpired");
	expireDiv.attr("title", "Session Expired!");
	var htmlString = "<p>";
	htmlString += '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span>';
	htmlString += ' Your session has expired!';
	htmlString += '<p id="dialogText-expired"></p>';
	htmlString += "</p>";
	expireDiv.html(htmlString);
	
	// jQuery UI Dialog    
	warnDiv.dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		resizable: false,
		buttons: {
			"Keep Session Alive": function() {
				//reset session on server
				$.post(relocateTo);
					
				//reset the variables
				requestTime = new Date();
				warningStarted = false;
				countdownTime = warning;

				//clear current checkSessionTimeEvent and start a new one
				clearInterval(checkSessionTimeEvent);
				checkSessionTimeEvent = "";
				checkSessionTimeEvent = setInterval("checkSessionTime(requestTime)", 10*1000);

				$('#dialogWarning').dialog('close');
			}
		}
	});
	
	expireDiv.dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		resizable: false,
		close: function() {
			clearInterval(checkSessionTimeEvent);
			checkSessionTimeEvent = "";
			window.location=relocateTo+"?expired=true";
		},
		buttons: {
			"Start Over": function() {
				clearInterval(checkSessionTimeEvent);
				checkSessionTimeEvent = "";
				window.location=relocateTo+"?expired=true";
			}
		}
	});
});

