//http://plugins.jquery.com/project/timeout_interval_idle
(function($) {

	if (typeof $.timeout != "undefined") return; 

	$.extend({
	  timeout : function (func,delay) {
			// init
			if (typeof $.timeout.count == "undefined") $.timeout.count = 0;
			if (typeof $.timeout.funcs == "undefined") $.timeout.funcs = new Array(); 
			// set timeout
			if (typeof func =='string') return setTimeout(func, delay); 
			if (typeof func =='function') {
				$.timeout.count++;
				$.timeout.funcs[$.timeout.count] = func;
				return setTimeout("$.timeout.funcs['"+$.timeout.count+"']();", delay);
			}
		},
	  interval : function (func,delay) {
			// init
			if (typeof $.interval.count == "undefined") $.interval.count = 0;
			if (typeof $.interval.funcs == "undefined") $.interval.funcs = new Array(); 
			// set interval
			if (typeof func =='string') return setInterval(func, delay); 
			if (typeof func =='function') {
				$.interval.count++;
				$.interval.funcs[$.interval.count] = func;
				return setInterval("$.interval.funcs['"+$.interval.count+"']();", delay);
			}
		},
	  idle : function (func,delay) {
			// init
			if (typeof $.idle.lasttimeout == "undefined") $.idle.lasttimeout = null;
			if (typeof $.idle.lastfunc == "undefined") $.idle.lastfunc = null;
			// set idle timeout
			if ($.idle.timeout) { clearTimeout($.idle.timeout); $.idle.timeout = null; $.idle.lastfunc = null; }
			if (typeof(func)=='string') { 
				$.idle.timeout = setTimeout(func, delay); 
				return $.idle.timeout;
			}		
			if (typeof(func)=='function') { 
				$.idle.lastfunc = func;
				$.idle.timeout = setTimeout("$.idle.lastfunc();", delay);
				return $.idle.timeout;
			}
		},
	  clear : function (countdown) {
		clearInterval(countdown);
		clearTimeout(countdown);
	  }	
		
	});
	
	
})(jQuery);

var idletimeoutwarning = 18;
var idletimeoutlogout = 2;
var lastidle = new Date();

function idle_timeout(){
  this.trigger_idle = function(){
    lastidle = new Date();
    if(typeof rcmail != "undefined"){
      idletimeoutwarning = rcmail.env.idle_timeout_warning;
      idletimeoutlogout = rcmail.env.idle_timeout_logout;
    }
    else{
      window.setTimeout('idle_timeout.trigger_idle()',5000);
      return;
    }
    //console.log('idle_timeout: trigger');
    window.clearTimeout(rcmail.env.idle_logout);
    
    var idle_handle = $.idle(function() {
      idle_timeout.trigger_logout();
    }, parseInt(idletimeoutwarning) * 60 * 1000);
  }
  
  this.trigger_logout = function(){
    if(new Date() - lastidle < 10000)
      return;
    if(rcmail.env.action == 'compose'){
      rcmail.command('savedraft');
    }
    if(!window.opener){
      //console.log('idle_timeout: trigger - ' + idletimeoutlogout + ' minute(s)');
      rcmail.display_message(rcmail.gettext('idletimeoutwarning', 'idle_timeout').replace('%s', idletimeoutlogout), 'warning');
      rcmail.env.idle_logout = window.setTimeout('idle_timeout.perform_logout()', parseInt(idletimeoutlogout) * 60 * 1000);
    }
  }
  
  this.perform_logout = function(){
    if(rcmail.env.action == 'compose'){
      rcmail.command('savedraft');
    }
    if(!rcmail.env.composenewwindow){
      var href = document.location.href;
      //console.log('idle_timeout: logout ... href: ' + href);
      while(href.indexOf('&') > -1){
        href = href.replace('&','|');
      }
      rcmail.http_post('plugin.idle_timeout_logout', '_href='+href);
    }
  }
}

var idle_timeout = new idle_timeout();

$(document).ready(function() {
  rcmail.addEventListener('init', function(evt) {
    //console.log('idle_timeout: init');
    idle_timeout.trigger_idle();
    if(window.opener){
      window.onbeforeunload = function(){
        window.opener.rcmail.env.composenewwindow = false;
      };
    }
    var events = new Array('mousedown','mousemove','keypress','scroll');
    for(var i in events){
      $(document).bind(events[i], function(){
        idle_timeout.trigger_idle();
        if(window.opener && window.opener.idle_timeout)
          window.opener.idle_timeout.trigger_idle();
        for (var ii=0;ii<window.frames.length;ii++){
          if(window.frames[ii].idle_timeout && window.frames[ii].idle_timeout.trigger_idle){
            window.frames[ii].idle_timeout.trigger_idle();
          }
        }
      });
    }
    $(window).bind('scroll', function(){
      idle_timeout.trigger_idle();
    });
  });
  // try to init if rcmail init is not fired
  window.setTimeout('idle_timeout.trigger_idle()',5000);
});