
/****************************************************/
/* ---------------- kiosk.js ---------------------- */
/*                                                  */
/* Script for redirecting browser to homepage after */
/* a specified number of seconds of inactivity.     */
/*                                                  */
/* Set to work only on a IE browser with customised */
/* useragent string. 				    */
/*                                                  */
/* Also deals with closing popup windows before     */
/* redirecting.                                     */
/*                                                  */
/****************************************************/

try 
{
	var g_iMaxIdleSecs;
	var g_iIdleSecs;
	var g_iCountDownSecs;
	var g_sHomepageURL;
	var g_bIsPopUp;
	var g_iNumOfPopups = 0;
	var g_iKillingPopUps = 0;

	
	if (typeof(g_iMaxIdleSecs) == 'undefined') // check in case script is included twice
	{
		g_iMaxIdleSecs = 120;
		g_iCountDownSecs = 118; //15
		g_iIdleSecs = 0;
		g_iPopUpTimeoutMillisecs = 20000;
		g_iPopUpWaitMillisecs = 50;

		g_sHomepageURL = 'http://www.connected-earth.com/';
		
		// all kiosks should have this in useragent string 
		if (navigator.userAgent.indexOf('ConnectedEarthKiosk') > -1) 
		{
		    	document.write('<style type="text/css">#divSubmityourstory {display:none;}</style>');
    	 	
			window.setInterval('checkForIdle()', 1000);
			//check if this window is a pop up
			if (window.opener && typeof(window.opener.g_iNumOfPopups) != 'unknown' && typeof(window.opener.g_iNumOfPopups) != 'undefined') 
			{
				g_bIsPopUp = true;
				
				window.opener.g_iNumOfPopups = window.opener.g_iNumOfPopups + 1;
				window.onbeforeunload=popupUnload;
				self.focus()
			}	
			document.onmousemove=resetTimer;
			document.onkeydown=resetTimer;
			document.onclick=resetTimer;
		}
	}

	if (window.opener) self.focus();

}
catch (e) 
{
} //ignore errors


/**** Functions *****/


/*** decrement popup counter when this popup unloads ***/	
function popupUnload()
{
	try 
	{
		if (g_bIsPopUp) 
			window.opener.g_iNumOfPopups = window.opener.g_iNumOfPopups - 1;
	}
	catch (e) {} //ignore errors
}


/*** check if idle for specified interval and initiate redirect ***/
function checkForIdle()
{
	try 
	{

		if (g_bIsPopUp) 
		{
			// initiate popup suicide when its time comes
			if (!window.opener || window.opener.g_iKillingPopUps > 0  || typeof(window.opener.g_iNumOfPopups) == 'undefined' || window.opener.g_iNumOfPopups < 1 || window.opener.location.href==g_sHomepageURL ) 
				window.setTimeout('self.close()', 10);
		}
		else	
		{		
			// check if idle time is up
			if (g_iIdleSecs >= g_iMaxIdleSecs)
				redirectAfterPopUpSuicide();
			else	
				g_iIdleSecs++;

			// display countdown to redirection
			if ((g_iIdleSecs > 0) && ((g_iMaxIdleSecs - g_iIdleSecs) < (g_iCountDownSecs+1))) 
				window.status = 'Redirecting to home page in ' + ((g_iMaxIdleSecs - g_iIdleSecs)+1) + ' seconds...'
			else if (window.status.substr(0,8) == 'Redirect')
				window.status = 'Done';
		}
	}
	catch (e) 
	{
	} //ignore errors

}

/*** reset timer after mouse or keyboard event ***/
function resetTimer()
{
	try 
	{
		g_iIdleSecs=0;
		if (g_bIsPopUp) 
		{
			//reset parent window for popup
			window.opener.resetTimer()
		}
		return true;
	}
	catch (e) {} //ignore errors
}


/*** wait for any pop up windows to die before redirecting **/
function redirectAfterPopUpSuicide()
{
	try 
	{
		window.status =  "Waiting for pop up windows to close... " 

		g_iKillingPopUps = g_iKillingPopUps + 1;
		// wait while pop up windows still exist and timeout has not been reached  
		if (g_iNumOfPopups > 0 && g_iKillingPopUps < ((g_iPopUpTimeoutMillisecs/g_iPopUpWaitMillisecs)+1))
			window.setTimeout("redirectAfterPopUpSuicide()", g_iPopUpWaitMillisecs)
		else
			window.document.location = g_sHomepageURL;
	}
	catch (e) {} //ignore errors
}


