/**
 * refresh the current session every 15 minutes
 * 
 * the session initiator should invoke ge_keepalive.set() to start the periodic refreshes.
 * ge_keepalive.set() may be invoked again at any time.
 *
 * this script then fetches go/keepalive.php periodically as long as it is included in every page.
 *
 */

ge_keepalive=new function()
{
 this.timeout = 18 * 60 * 1000; // in milliseconds

 // set the keepalive cookie and start periodic keepalives
 this.set=function()
 {
  // only set if not existing to not reset running keepalive timer
  if(this.getCookieValue('last_keepalive')==null)
  {
   var timestamp=new Date().getTime();
   this.setCookie('last_keepalive', timestamp, false, '/');
  } 

  this.refresh();
 }

 // check if keepalive cookie is set and refresh it if timeout is expired. 
 // schedules itself for further refreshes, so only call once in a page.
 this.refresh=function()
 {
  var lastRefresh=this.getCookieValue('last_keepalive');
  if(lastRefresh==null) return;

  var timestamp=new Date().getTime();
  var timeToRefresh=parseInt(lastRefresh) + this.timeout - timestamp;
  if (timeToRefresh<0)
  { 
   this.setCookie('last_keepalive', timestamp, false, '/');
   var request=new XMLHttpRequest();
   var url=this.getBasePath()+'go/keepalive.php';
   // for debugging:
   // request.onreadystatechange = function() {
   //  if(request.readyState==4) alert(url+' '+request.responseText);
   // };
   request.open('GET', url);
   request.send(null);
   timeToRefresh=this.timeout+1000;
  } 

  var me=this; // any clean way getting 'this' to setTimeout's scope?
  window.setTimeout(function(){me.refresh();}, timeToRefresh);
 }

 // get the path of this script for relative file lookup
 this.getBasePath=function()
 {
  var scripts=document.getElementsByTagName('script'); 

  for(var i=0; i<scripts.length; i++)
  {
   var path=scripts[i].src;
   if(path.indexOf('elements/images/keepalive.js')>-1)
   {
    return path.replace('elements/images/keepalive.js','');
   }
  }
  
  throw "cannot determine script path";
 }

 //  from savescroll.js:
 this.getCookieValue=function(cookieName)
 {
  var value=null;
  if(document.cookie != "") 
  {
   cookieName=cookieName+"=";
   
   var start=document.cookie.indexOf(cookieName);
   if(start>=0) 
   {
    start=start+cookieName.length;
    
    var end=document.cookie.indexOf(";", start);
    if(end<0) end=document.cookie.length;
    
    value=document.cookie.substring(start,end);
    value=unescape(value);
   }
  }
  return value;
 }

 //  from savescroll.js:
 this.setCookie=function(cookieName,cookieValue, expires, path, domain, secure)
 {
  var cookie=cookieName+"="+escape(cookieValue)+";";
  if (expires) cookie+=" expires="+expires+";";
  if (path)    cookie+=" path="+path+";";
  if (domain)  cookie+=" domain="+domain+";";
  if (secure)  cookie+=" secure;";
  document.cookie=cookie;
 }

 // start keepalive refreshing 
 this.refresh();
}

