/**
 * This class implements a session timeout warning. It creates a 
 * message which will be displayed a configured number of minutes
 * before the current servlet session is expected to time out. 
 *
 * The displayed warning gives the user the opportunity to make a
 * request to the server to renew their session without reloading the
 * current page. If the user doesn't respond to the warning, the text
 * changes to indicate that the session has expired when the configured
 * timeout period has elapsed.
 */

var webtop;
if (!webtop)
    webtop = {};

/* webtop.SessionTimerProperties
 *   expirationTime
 *   warningDuration
 *   pingUrl
 *   continueHeaderText
 *   continueText
 *   continueTextWarnOnly
 *   expiredHeaderText
 *   expiredText
 *   renewFailedHeader
 *   renewFailedText
 *   yes
 *   no
 */
webtop.SessionTimerProperties = {}; 

webtop.SessionTimer = function(container)
{
  this.toString = function() { return "SessionTimer"; }
  var sessiontimer = this;

  var handleYes = function() 
  { 
    clearWarningTimer(); 
    this.submit(); 
  };

  var handleNo = function() 
  { 
    clearWarningTimer(); 
    this.cancel(); 
    this.hide(); 
  };

  var onSuccess = function(response)
  {
    if (response.responseText == "1")
      sessiontimer.dialog.hide(); 
    else
      onFailure(); 
  };

  var onFailure = function(response)
  {
    sessiontimer.dialog.setHeader(
      webtop.SessionTimerProperties.renewFailedHeader); 
    sessiontimer.dialog.setBody(webtop.SessionTimerProperties.renewFailedText); 
    sessiontimer.dialog.setFooter(""); 
  };

  var showWarning = function()
  {
    // If the dialog buttons aren't clicked, change the dialog text when
    // we think the session will have expired.
    sessiontimer.warningTimer = 
      YAHOO.lang.later(webtop.SessionTimerProperties.warningDuration, 
      sessiontimer, 
      function() { 
        sessiontimer.dialog.setHeader(
          webtop.SessionTimerProperties.expiredHeaderText); 
        sessiontimer.dialog.setBody(webtop.SessionTimerProperties.expiredText); 
        sessiontimer.dialog.setFooter(""); 
      }
    ); 
    sessiontimer.dialog.show(); 
  };

  var clearWarningTimer = function()
  {
    if (sessiontimer.warningTimer)
    {
      sessiontimer.warningTimer.cancel(); 
      sessiontimer.warningTimer = null; 
    }
  };

  this.dialog =  new YAHOO.widget.SimpleDialog(
    "sessiondialog", 
     { width: "300px",
       fixedcenter: true,
       visible: false,
       modal: true,
       postmethod: "async",
       hideaftersubmit: false,
       draggable: false,
       close: true,
       text: webtop.SessionTimerProperties.continueText,
       constraintoviewport: true,
       buttons: [ { text: webtop.SessionTimerProperties.yes, 
                    handler: handleYes, 
                    isDefault: true },
                  { text: webtop.SessionTimerProperties.no,  
                    handler: handleNo }
                ]
   } );

  this.dialog.setHeader(webtop.SessionTimerProperties.continueHeaderText);
  this.dialog.callback.success = onSuccess;
  this.dialog.render(container);

  // SimpleDialog creates the container and form for us, so we have to set
  // the form action here. 
  var form = YAHOO.util.Dom.getElementBy(
    function(e) { 
      return (e.name == "frm_sessiondialog"); // getAttribute fails in IE 7
    }, "form", "sessiondialog"); 
  if (form != null && "pingUrl" in webtop.SessionTimerProperties)
  {
    YAHOO.util.Dom.setAttribute(form, "action", 
      webtop.SessionTimerProperties.pingUrl); 
  }
  else
  {
    this.dialog.setBody(webtop.SessionTimerProperties.continueTextWarnOnly);
    this.dialog.setFooter(""); // Remove the buttons.
  }

  // Remove the YUI button styling.
  var buttonContainer = YAHOO.util.Dom.getElementBy(
    function(e) { 
      return (YAHOO.util.Dom.hasClass(e, "button-group"));
    }, "span", "sessiondialog"); 
  if (buttonContainer != null)
    YAHOO.util.Dom.removeClass(buttonContainer, "button-group");

  // Start the timer. 
  YAHOO.lang.later(webtop.SessionTimerProperties.expirationTime, sessiontimer, 
    showWarning);
}

