//This library governed by the GNU Lesser General Public License
//JavaScript WebVoyage(TM) Timer v2.2
//by Jim Robinson, Tarrant County College Libraries http://library.tccd.edu/code/

// User Settings
// the number of minutes until timeout
var minutes       = 15;

// remaining number of seconds when the popup window appears	
var remindTime    = 60;	

// the relative URI of the search page (dialog box 2, Start New Search button)
var newSearchURL  = "/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=First";

// the relative URI of the login page (dialog box 2, New Session Login button)
var newLoginURL   = "/Pwebrecon.cgi?DB=local&PAGE=hbSearch";

// the relative URI of the home/reset session page (dialog box 1, New Session button)
var newSessionURL = "/cgi-bin/Pwebrecon.cgi?DB=local&PAGE=First";

// warning text
//var warningText = "Warning: Your MadCat session will end soon.<br />To continue with this session, click Continue Session<br />To begin a new session, click New Session";
var warningText = "Warning: Your MadCat session will end soon.<br />To continue with this session, click <kbd>Continue Session</kbd>.";

// timed-out text
//var timeoutText = "Your session has ended.<br />To start a new search, click Start New Search.<br />To begin a new session, click New Session Login.";
var timeoutText = "Your session has ended.<br />To start a new search, click <kbd>Start New Search</kbd>.";

// wait message
var waitMessage = "One moment please...";

// timer countdown display location
// Some browsers (IE7) are no longer allowing javascript control of the status
// bar unless the user specifically allows it. However, most modern browsers 
// still allow javascript control of the title bar. Setting titlebar to true 
// will append the countdown to the end of the page title in the titlebar.
var statusbar = true; // Show the counter in the status bar
var titlebar = true; // Show the counter in the title bar

/********************************* Timer functions *******************************/
var secondsRemaining=minutes*60,seconds=0,isCounting=true,isCSS,isW3C,isIE4,isNN4,isIE6CSS,title=document.title;

function initTimer() {
	var oDraggable=new zDraggable(document.getElementById("warningDiv"),zDraggable.DRAG_X | zDraggable.DRAG_Y);if(document.images) {isCSS=(document.body && document.body.style)?true:false;isW3C=(isCSS && document.getElementById)?true:false;isIE4=(isCSS && document.all)?true:false;isNN4=(document.layers)?true:false;isIE6CSS=(document.compatMode && document.compatMode.indexOf("CSS1") >= 0)?true:false;startTimer();}
}

function startTimer() {

	minutes = Math.floor(secondsRemaining / 60);
	seconds = secondsRemaining%60;
	
	if(seconds < 10) {
		seconds = "0" + seconds;
	}
	
	if(secondsRemaining < 0) {
		
		if (statusbar) window.status = "MadCat session has timed out";
		
		if (titlebar) document.title=title + "  -- Session has timed out.";
		
		if (document.warningForm && document.warningForm.timeRemaining) document.warningForm.timeRemaining.value=formatTime("MadCat session has timed out");
		
		if (isCounting) {
		
			setButtons();
			isCounting = false;
			
		}
		
	}
	
	else if(secondsRemaining > 0 && secondsRemaining <= remindTime) {
	
		showTimer();
		
		if (statusbar) window.status = formatTime("MadCat session timeout in: "+minutes+":"+seconds);
		
		if (titlebar) document.title = title + formatTime("  -- Session timeout in "+minutes+":"+seconds);
		
		if (document.warningForm && document.warningForm.timeRemaining)document.warningForm.timeRemaining.value=formatTime("MadCat session timeout in: "+minutes+" minutes, "+seconds+" seconds"); 
		
		}
		
	else {
		
		if(statusbar) window.status = formatTime("MadCat session timeout in: "+minutes+":"+seconds);
			
		if(titlebar) document.title = title + formatTime("  -- Session timeout in "+minutes+":"+seconds);
			
		if(document.warningForm && document.warningForm.timeRemaining) document.warningForm.timeRemaining.value=formatTime("MadCat session timeout in: "+minutes+" minutes, "+seconds+" seconds");
			
	}
		
	var interval=1;
		
	setTimeout("startTimer()",1000*interval);
		
	secondsRemaining-=interval;
	
}

function formatTime(x) {

	var pattern=new RegExp("&#([0-9]+);"),result;
	
	while((result=pattern.exec(x))!=null) {
		x=x.replace(result[0],String.fromCharCode(RegExp.$1));
	}
	
	x=x.replace(/&nbsp;/g,' ');
	
	return x;
	
}

function seekLayer(doc,name) {var theObj;for(var i=0;i<doc.layers.length;i++) {if(doc.layers[i].name==name) {theObj=doc.layers[i];break;}if(doc.layers[i].document.layers.length>0) {theObj=seekLayer(document.layers[i].document,name);}}return theObj;}

function getRawObject(obj) {var theObj;if(typeof obj=="string") {if(isW3C) {theObj=document.getElementById(obj);}else if(isIE4) {theObj=document.all(obj);} else if (isNN4) {theObj=seekLayer(document,obj);}}else{theObj=obj;}return theObj;}

function getObject(obj) {var theObj=getRawObject(obj);if(theObj && isCSS) {theObj=theObj.style;}return theObj;}

function shiftTo(obj,x,y) {var theObj=getObject(obj);if(theObj) {if(isCSS) {var units=(typeof theObj.left=="string")?"px":0;theObj.left=x+units;theObj.top=y+units;}else if(isNN4) {theObj.moveTo(x,y)}}}

function show(obj) {var theObj=getObject(obj);if(theObj) {theObj.visibility="visible";}}

function hide(obj) {var theObj=getObject(obj);if(theObj) {theObj.visibility="hidden";}}

function showTimer() {setButtons();if(document.getElementById("warningDiv") && document.getElementById("warningDiv").style.visibility!="visible") {show(document.getElementById("warningDiv"));}}

function setButtons() {var button1='<input type="button" name="refresh" id="refresh" value="Continue Session" onclick="refreshPage()">';var button2='<input type="button" name="restart" id="restart" value="New Session" onclick="newSearch(newSessionURL)">';var warningH3='<h4 align="center">'+warningText+'</h4>';if(secondsRemaining<=0) {button1='<input type="button" name="refresh" id="refresh" value="Start New Search" onclick="newSearch(newSearchURL)">';button2='<input type="button" name="restart" id="restart" value="New Session Login" onclick="newSearch(newLoginURL)">';warningH3=timeoutText;}document.getElementById("refreshButton").innerHTML=button1;document.getElementById("warningHeader").innerHTML=warningH3;}

function refreshPage() {secondsRemaining=remindTime+15;var warningH3=waitMessage;if(document.getElementById) {document.getElementById('refreshButton').style.visibility='hidden';document.getElementById('timeRemaining').style.visibility='hidden';document.getElementById("warningHeader").innerHTML=warningH3;}document.location.replace(document.location.href);}

function newSearch(url) {secondsRemaining=remindTime+15;var warningH3='<h4 align="center">'+waitMessage+'</h4>';if(document.getElementById) {document.getElementById('refreshButton').style.visibility='hidden';document.getElementById('timeRemaining').style.visibility='hidden';document.getElementById("warningHeader").innerHTML=warningH3;}document.location.replace(url);}

function getWarning() {var myDiv='<div id="warningDiv">';myDiv+='<form name="warningForm">';myDiv+='<table id="warningTable" class="timer">';myDiv+='<tr><td id="warningHeader" class="timer" nowrap="nowrap"></td></tr>';myDiv+='<tr><td>';myDiv+='<input type="text" name="timeRemaining" id="timeRemaining" size="50" class="timer">';myDiv+='</td></tr><tr>';myDiv+='<td id="refreshButton"></td>';myDiv+='</tr></table></form></div>';return myDiv;}