function AdfSessionTimeoutHelper() {
  this.Init();
}
AdfObject.createSubclass(AdfSessionTimeoutHelper);
AdfSessionTimeoutHelper.TimeoutWindowStates = {NONE:0, CANCEL:1, LAUNCHED:2};
AdfSessionTimeoutHelper.prototype.getTimeoutWindowState = function() {
  return this._timeoutWindowState;
};
AdfSessionTimeoutHelper.prototype.Init = function() {
  AdfSessionTimeoutHelper.superclass.Init.call(this);
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.NONE;
  this._AFTimeOutGlassPane = "AFTimeOutGlassPane";
};
AdfSessionTimeoutHelper.prototype.__initializeSessionTimeoutTimer = function(timeout, warningBeforeTimeout, newSessionURL) {
  this._setSessionTimeoutInWebLocalStorage(timeout);
  this._cancelAndRescheduleSessionTimeoutTimer(timeout, warningBeforeTimeout, newSessionURL);
  this._timeout = timeout;
  this._warningBeforeTimeout = warningBeforeTimeout;
  this._newSessionURL = newSessionURL;
  var agent = AdfAgent.AGENT;
  var localStorageSessionKeysCleaned = agent.isSessionStorageEnabled() ? agent.getSessionStorageProperty("oracle.adfinternal.view.rich.sessionKeysCleaned") : null;
  if (localStorageSessionKeysCleaned == null) {
    this._timeoutWarningTimer = AdfPage.PAGE.scheduleTimer(this, this._cleanupLocalStorageSessionKeys, null, 6E4);
    if (agent.isSessionStorageEnabled()) {
      agent.setSessionStorageProperty("oracle.adfinternal.view.rich.sessionKeysCleaned", "scheduled");
    }
  }
};
AdfSessionTimeoutHelper.prototype.__cancelSessionTimeoutTimer = function() {
  var logger = AdfLogger.LOGGER;
  if (this._timeoutWarningTimer) {
    AdfPage.PAGE.cancelTimer(this._timeoutWarningTimer);
    if (logger.isLoggable(AdfLogger.INFO)) {
      logger.info("Cancelled session timeout warning dialog timer with id ", this._timeoutWarningTimer);
    }
    this._timeoutWarningTimer = null;
  }
  if (this._timeoutTimer) {
    AdfPage.PAGE.cancelTimer(this._timeoutTimer);
    if (logger.isLoggable(AdfLogger.INFO)) {
      logger.info("Cancelled final session timeout dialog timer with id ", this._timeoutTimer);
    }
    this._timeoutTimer = null;
  }
};
AdfSessionTimeoutHelper.prototype.__startSessionTimeoutTimer = function() {
  this._setSessionTimeoutInWebLocalStorage(this._timeout);
  this._startSessionTimeoutTimer(this._timeout, this._warningBeforeTimeout, this._newSessionURL);
};
AdfSessionTimeoutHelper.prototype.__alertTimeout = function() {
  this._timeout = 0;
  this._removeSessionTimeoutFromWebLocalStorage();
  this._cancelAndRescheduleSessionTimeoutTimer(this._timeout, this._warningBeforeTimeout, this._newSessionURL);
};
AdfSessionTimeoutHelper.prototype.__recordUserActivity = function(event) {
  if (event.type == "click" || event.type == "keypress") {
    this._setActivityTimeInWebLocalStorage();
  }
};
AdfSessionTimeoutHelper.prototype._cleanupLocalStorageSessionKeys = function() {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var currentTimeValue = (new Date).getTime();
    var localStorageKeys = agentHelper.getLocalStorageKeys();
    AdfLogger.LOGGER.info("Cleaning expired session info from web local storage");
    for (var i = 0;i < localStorageKeys.length;i++) {
      if (localStorageKeys[i].indexOf("oracle.adfinternal.view.rich.sessionKey_") == 0) {
        var valueInStore = agentHelper.getLocalStorageProperty(localStorageKeys[i]);
        if (currentTimeValue - valueInStore > 36E4) {
          agentHelper.removeLocalStorageProperty(localStorageKeys[i]);
          var userActivityKey = "oracle.adfinternal.view.rich.userActivityKey_" + localStorageKeys[i].split("_")[1];
          agentHelper.removeLocalStorageProperty(userActivityKey);
        }
      }
    }
    var agent = AdfAgent.AGENT;
    if (agent.isSessionStorageEnabled()) {
      agent.setSessionStorageProperty("oracle.adfinternal.view.rich.sessionKeysCleaned", "complete");
    } else {
      AdfLogger.LOGGER.info("Could not mark that we cleaned web local storage because session storage is not enabled");
    }
  }
};
AdfSessionTimeoutHelper.prototype._startSessionTimeoutTimer = function(timeout, warningBeforeTimeout, newSessionURL) {
  var currentTimeVal = (new Date).getTime();
  var logger = AdfLogger.LOGGER;
  var timeToWarn = timeout - warningBeforeTimeout;
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.NONE;
  if (timeToWarn > 0 && warningBeforeTimeout >= 12E4) {
    this._timeoutWarningTimer = AdfPage.PAGE.scheduleTimer(this, this._showSessionTimeoutWarningDialog, null, timeToWarn);
    if (logger.isLoggable(AdfLogger.INFO)) {
      logger.info("Scheduled session timeout warning dialog timer with id ", this._timeoutWarningTimer, " to be shown at : ", new Date(currentTimeVal + timeToWarn));
    }
  } else {
    this._timeoutTimer = AdfPage.PAGE.scheduleTimer(this, this._showFinalSessionTimeoutAlertDialog, null, timeout);
    if (logger.isLoggable(AdfLogger.INFO)) {
      logger.info("Scheduled final session timeout dialog timer with id ", this._timeoutTimer, " to be shown at : ", new Date(currentTimeVal + timeout));
    }
  }
};
AdfSessionTimeoutHelper.prototype._cancelAndRescheduleSessionTimeoutTimer = function(timeout, warningBeforeTimeout, newSessionURL) {
  this.__cancelSessionTimeoutTimer();
  this._startSessionTimeoutTimer(timeout, warningBeforeTimeout, newSessionURL);
};
AdfSessionTimeoutHelper.prototype._setSessionTimeoutInWebLocalStorage = function(timeout) {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var timeValueAtTimeout = (new Date).getTime() + timeout;
    var sessionKey = "oracle.adfinternal.view.rich.sessionKey_" + AdfPage.PAGE.getSessionIdentifier();
    var storedSessionTimeoutVal = agentHelper.getLocalStorageProperty(sessionKey);
    if (storedSessionTimeoutVal == null || storedSessionTimeoutVal < timeValueAtTimeout) {
      agentHelper.setLocalStorageProperty(sessionKey, timeValueAtTimeout);
      var logger = AdfLogger.LOGGER;
      if (logger.isLoggable(AdfLogger.INFO)) {
        logger.info("Web local storage for key ", sessionKey, " reset to : ", new Date(timeValueAtTimeout));
      }
    }
  }
};
AdfSessionTimeoutHelper.prototype._setActivityTimeInWebLocalStorage = function() {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var userActivityTime = new Date;
    this._setSessionTimeoutInWebLocalStorage(this._timeout);
    this._cancelAndRescheduleSessionTimeoutTimer(this._timeout, this._warningBeforeTimeout, this._newSessionURL);
    var userActivityKey = "oracle.adfinternal.view.rich.userActivityKey_" + AdfPage.PAGE.getSessionIdentifier();
    agentHelper.setLocalStorageProperty(userActivityKey, userActivityTime.getTime());
    var logger = AdfLogger.LOGGER;
    if (logger.isLoggable(AdfLogger.INFO)) {
      logger.info("Last user activity on client for ", userActivityKey, " at ", userActivityTime);
    }
  }
};
AdfSessionTimeoutHelper.prototype._isUserActiveOnClient = function(duration) {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var userActivityKey = "oracle.adfinternal.view.rich.userActivityKey_" + AdfPage.PAGE.getSessionIdentifier();
    var lastActiveTime = agentHelper.getLocalStorageProperty(userActivityKey);
    if (lastActiveTime) {
      var timeSinceLastActivity = (new Date).getTime() - lastActiveTime;
      if (timeSinceLastActivity < duration) {
        var logger = AdfLogger.LOGGER;
        if (logger.isLoggable(AdfLogger.INFO)) {
          logger.info("User activity on client ", timeSinceLastActivity / 1E3, " seconds ago. Duration checked for activity ", duration / 1E3, " seconds");
        }
        return true;
      }
    }
  }
  return false;
};
AdfSessionTimeoutHelper.prototype._removeSessionTimeoutFromWebLocalStorage = function(timeout) {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var sessionKey = "oracle.adfinternal.view.rich.sessionKey_" + AdfPage.PAGE.getSessionIdentifier();
    agentHelper.removeLocalStorageProperty(sessionKey);
  }
};
AdfSessionTimeoutHelper.prototype._getTimeLeftToSessionTimeout = function() {
  var agentHelper = AdfAgentHelper.getInstance();
  if (agentHelper.isLocalStorageEnabled()) {
    var sessionKey = "oracle.adfinternal.view.rich.sessionKey_" + AdfPage.PAGE.getSessionIdentifier();
    var timeoutValInStorage = agentHelper.getLocalStorageProperty(sessionKey);
    if (timeoutValInStorage) {
      return timeoutValInStorage - (new Date).getTime();
    }
  }
  return null;
};
AdfSessionTimeoutHelper.prototype._showSessionTimeoutWarningDialog = function() {
  var timeLeftToTimeout = this._getTimeLeftToSessionTimeout();
  if (timeLeftToTimeout && (timeLeftToTimeout < this._warningBeforeTimeout - 2E3 || timeLeftToTimeout > this._warningBeforeTimeout + 2E3)) {
    this._cancelAndRescheduleSessionTimeoutTimer(timeLeftToTimeout, this._warningBeforeTimeout, this._newSessionURL);
    return -1;
  }
  if (this._isUserActiveOnClient(this._timeout - this._warningBeforeTimeout)) {
    this._extendSessionInServer();
    return -1;
  }
  this._timeoutWarningTimer = null;
  var page = AdfPage.PAGE;
  if (this._timeoutTimer) {
    page.cancelTimer(this._timeoutTimer);
    this._timeoutTimer = null;
  }
  var laf = AdfPage.PAGE.getLookAndFeel();
  var preTimeoutMsg;
  if (this._warningBeforeTimeout >= 6E4) {
    preTimeoutMsg = laf.getTranslatedString("af_document.PRE_SESSION_TIMEOUT_MSG", Math.floor(this._warningBeforeTimeout / 6E4));
  } else {
    preTimeoutMsg = laf.getTranslatedString("af_document.PRE_SESSION_TIMEOUT_MSG_SECOND", this._warningBeforeTimeout / 1E3);
  }
  var preTimeoutTitle = laf.getTranslatedString("af_document.PRE_SESSION_TIMEOUT_CONFIRM_TITLE");
  if (preTimeoutTitle == null) {
    preTimeoutTitle = "Expiration Warning";
  }
  var callback = this.createCallback(this._handleSessionTimeoutWarningDialogOutcome);
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.LAUNCHED;
  this._addAFTimeOutGlassPane();
  this._preSessionTimeoutWarningTime = (new Date).getTime();
  page.__confirm(preTimeoutTitle, preTimeoutMsg, callback, this._warningBeforeTimeout, true);
  return -1;
};
AdfSessionTimeoutHelper.prototype._handleSessionTimeoutWarningDialogOutcome = function(adfDialogEvent) {
  this._removeAFTimeOutGlassPane();
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.NONE;
  if (adfDialogEvent.getOutcome() == AdfDialogEvent.OUTCOME_OK) {
    this._extendSessionInServer();
  } else {
    if (adfDialogEvent.getOutcome() == AdfDialogEvent.OUTCOME_CANCEL) {
      var timeLeftToTimeout = this._getTimeLeftToSessionTimeout();
      if (!timeLeftToTimeout) {
        var currentTimeValue = (new Date).getTime();
        timeLeftToTimeout = this._warningBeforeTimeout - (currentTimeValue - this._preSessionTimeoutWarningTime);
        this._preSessionTimeoutWarningTime = null;
      }
      this._cancelAndRescheduleSessionTimeoutTimer(timeLeftToTimeout, this._warningBeforeTimeout, this._newSessionURL);
    }
  }
};
AdfSessionTimeoutHelper.prototype._showFinalSessionTimeoutAlertDialog = function() {
  var timeLeftToTimeout = this._getTimeLeftToSessionTimeout();
  if (timeLeftToTimeout && timeLeftToTimeout > 0) {
    if (timeLeftToTimeout > 0) {
      this._cancelAndRescheduleSessionTimeoutTimer(timeLeftToTimeout, this._warningBeforeTimeout, this._newSessionURL);
      return -1;
    }
  }
  if (this._isUserActiveOnClient(this._timeout)) {
    this._extendSessionInServer();
    return -1;
  }
  this._timeoutTimer = null;
  var page = AdfPage.PAGE;
  if (this._timeoutWarningTimer) {
    page.cancelTimer(this._timeoutWarningTimer);
    this._timeoutWarningTimer = null;
  }
  var laf = page.getLookAndFeel();
  var postTimeoutMsg = laf.getTranslatedString("af_document.POST_SESSION_TIMEOUT_MSG");
  if (this._newSessionURL) {
    postTimeoutMsg += " " + laf.getTranslatedString("af_document.POST_SESSION_TIMEOUT_MSG_CONTINUE");
  }
  var postTimeoutTitle = laf.getTranslatedString("af_document.POST_SESSION_TIMEOUT_ALERT_TITLE");
  if (postTimeoutTitle == null) {
    postTimeoutTitle = "Window Expired";
  }
  AdfDhtmlRichDialogService.getInstance().cancelAll();
  var documentClientId = page.getDocumentClientId();
  var docDom = AdfAgent.AGENT.getElementById(documentClientId);
  AdfDomUtils.setVisible(docDom, false);
  var callback = this.createCallback(this._handleFinalSessionTimeoutAlert);
  this._addAFTimeOutGlassPane();
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.LAUNCHED;
  page.__alert(postTimeoutTitle, postTimeoutMsg, callback);
  return -1;
};
AdfSessionTimeoutHelper.prototype._handleFinalSessionTimeoutAlert = function(adfDialogEvent) {
  this._removeAFTimeOutGlassPane();
  this._timeoutTimer = null;
  var page = AdfPage.PAGE;
  this._timeoutWindowState = AdfSessionTimeoutHelper.TimeoutWindowStates.NONE;
  if (this._newSessionURL) {
    page.getDomWindow().location.replace(this._newSessionURL);
  } else {
    var laf = page.getLookAndFeel();
    var postTimeoutMsg = laf.getTranslatedString("af_document.POST_SESSION_TIMEOUT_MSG");
    var agent = AdfAgent.AGENT;
    var documentDom = page.getDomDocument();
    var deadDom = documentDom.createElement("div");
    AdfDomUtils.setVisible(deadDom, false);
    agent.setTextContent(deadDom, postTimeoutMsg);
    var documentClientId = page.getDocumentClientId();
    var docDom = agent.getElementById(documentClientId);
    docDom.parentNode.appendChild(deadDom);
    AdfDomUtils.setVisible(deadDom, true);
    page.getDomWindow().setTimeout(page.createCallback(page.Dispose), 1);
  }
};
AdfSessionTimeoutHelper.prototype._extendSessionInServer = function() {
  this._touchServerSession();
  AdfLogger.LOGGER.info("Extended session lifetime");
  this._setSessionTimeoutInWebLocalStorage(this._timeout);
  this._cancelAndRescheduleSessionTimeoutTimer(this._timeout, this._warningBeforeTimeout, this._newSessionURL);
};
AdfSessionTimeoutHelper.prototype._touchServerSession = function() {
  var actionURL = AdfPage.PAGE.getDomDocument().location.href;
  if (actionURL.indexOf("?") == -1) {
    actionURL += "?";
  } else {
    actionURL += "\x26";
  }
  actionURL += "_afrTS\x3d" + (new Date).getTime();
  var xmlr = new AdfXMLRequest;
  xmlr.setSynchronous(false);
  xmlr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlr.send(actionURL, null);
  xmlr.cleanup();
  delete xmlr;
};
AdfSessionTimeoutHelper.prototype._addAFTimeOutGlassPane = function() {
  var domDocument = AdfPage.PAGE.getDomDocument();
  var bodyElement = domDocument.getElementsByTagName("body")[0];
  AdfDomUtils.addCSSClassName(bodyElement, this._AFTimeOutGlassPane);
};
AdfSessionTimeoutHelper.prototype._removeAFTimeOutGlassPane = function() {
  var domDocument = AdfPage.PAGE.getDomDocument();
  var bodyElement = domDocument.getElementsByTagName("body")[0];
  AdfDomUtils.removeCSSClassName(bodyElement, this._AFTimeOutGlassPane);
};
