var g_sec;
var g_min;
var g_hr;
var sessionid;
var portalpath;
var initialtext = "";
var reset_time = false;

function setSessionID(sessid)
{
  sessionid = sessid;
}

function setPortalpath(path)
{
  portalpath = path;
}

function setInitialText(text)
{
  initialtext = text;
}

function set_title_time(time) {
	g_hr = Math.floor(time / 3600);
	time %= 3600;
	g_min = Math.floor(time / 60);
	time %= 60;
	g_sec = time;
	reset_time = true;
}

function title_time(time, pagetitle) {
	if (sessionid == "") return;
	hr = Math.floor(time / 3600);
	time %= 3600;
	min = Math.floor(time / 60);
	time %= 60;
	sec = time;

	if (reset_time) {
		sec = g_sec;
		min = g_min;
		hr = g_hr;
		reset_time = false;
	}

	sec--;

	if (sec < 0) {
		if ((min > 0) || (hr > 0)) {
			sec = 59;
			min--;
		}
	}

	if (min < 0) {
		if (hr > 0) {
			min = 59;
			hr--;
		}
	}

	seconds_left = sec + (min * 60) + (hr * 60 * 60);
	
	if (pagetitle.indexOf(" - Time Out") == -1) {
  	if (seconds_left > 0) {
  		clocktext = initialtext + " - Time Out " +  hr + ":" + ((min < 10) ? "0" : "") + min + ":" + ((sec < 10) ? "0" : "") + sec; 
  		top.document.title = pagetitle + clocktext;
  		if (seconds_left == 300) {
        var newWindow = window.open(portalpath + '/index2.php?option=com_sso&section=popup&sessionid=' + sessionid, 'Timeout Warning - SauceOpen', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=150');
      }
  	} else {
  		top.document.title = pagetitle + '- Timed Out 0:00:00';
  	}	
  	setTimeout("title_time(" + seconds_left + ",'" + pagetitle.replace("\'", "\\'") + "')", 1000);
	}
}


