var confirm_leaving = false;
var keepalive = false;
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
$(function() {
  $(document).ajaxError(function(e, x) { if (x.status == 403) { if (confirm_leaving) confirm_leaving = false; window.location.href=window.location.href; }});
  if (!$('#logoutbtn').length)
    return;
  window.setInterval(function() {
    if (keepalive) {
      $.get('/backend.php?action=keepalive');
    } else {
      $.get('/backend.php?action=last_access', function (resp) {
        if (resp == 'OK') {
           $('#logoutbox').remove();
        } else if (resp == 'Warn') {
           if (!$('#logoutbox').length) {
             $('body').prepend('<div id="logoutbox">You will be logged out soon. <input type="button" id="abortlogout" value="Click here to stay logged in." </div>');
             $('#abortlogout').click(function() {
                $('#logoutbox').html('Refreshing session...');
                $.get('/backend.php?action=ping', function(resp) { 
                  if (resp == 'pong')
                    $('#logoutbox').remove();
                  else
                    alert('Unable to prevent logout.')
                });
              });
           }
        }
      });
    }
  }, 120000);
});

