jQuery(function($){
	var ele = $("div.sessionTimeout"),
	dialog = false,
	opts = {
		activeURL: '',
		redirectURL: '',
		showDialog: 25,
		autoRedirect: 30
	};
	if (!ele.length) {
		return;
	}
	$.extend(opts, ele.ParseClass());
	
	var showDialog = function() {
		if (!dialog) {
			dialog = ele.dialog({
				autoOpen: false,
				modal: true,
				width: 400
			});
			dialog.find("span.keepActive").click(function() {
				//make ajax call to keep session active
				$.getJSON(opts.activeURL, function(data) {
					if (data.success) {
						dialog.dialog("close");
						startTimer();
					} else {
						// what should we do if this errors? Currently it will log them out in the original 30 minutes
					}
				});
				return false;
			});
			dialog.find("span.logOut").click(function() {
				//redirect to logout url
				autoRedirect();
				return false;
			});
		}
		dialog.dialog("open");
	},
	autoRedirect = function() {
		window.location = "/Logout.ashx?redirect=" + opts.redirectURL;
	},
	dialogTimer = autoTimer = null,
	startTimer = function() {
		clearTimeout(dialogTimer);
		clearTimeout(autoTimer);
		dialogTimer = setTimeout(function() {
			showDialog();
		}, opts.showDialog*60*1000);
		autoTimer = setTimeout(function() {
			autoRedirect();
		}, opts.autoRedirect*60*1000);
	};
	startTimer();
});
