var popup_window;
var popup_window_is_open = false;
var help_window_is_open = false;

function acceptscookies()
{
var accepts_cookies=true;
var cookie_monster = document.cookie;

if ((cookie_monster == "") || parseInt(cookie_monster.indexOf('accepts_cookies')) < 1)
	{
	document.cookie='accepts_cookies=true;';
	var cookie_monster = document.cookie;

	if ((cookie_monster == "") || parseInt(cookie_monster.indexOf('accepts_cookies')) < 1)
		{
		accepts_cookies = false;
		} // end of if

	} // end of if

return accepts_cookies;

} // end of acceptscookies()


function get_cookie(name)

{

var result = null;
var the_cookie = " " + document.cookie + ";";
var search_name = " " + name + "=";
var start = the_cookie.indexOf(search_name);
var end;

if (start != -1)
	{ // Fetch value from cookie (if found)
	start += search_name.length;
	end = the_cookie.indexOf(";",start);
	result = unescape(the_cookie.substring(start,end));
	} // end of if

return result;
} // end of get_cookie()

function set_cookie(name, value)

{
// Okay, yeah, this creates a "year 2005 problem" :)  Remember this when it breaks!
document.cookie = name + "=" + escape(value) + "; expires=Tuesday, 31-Dec-04 23:59:00 GMT; path=/";

} // end of set_cookie()

function delete_cookie(name)

{  // Expiring is as simple as placing a date in the past into the cookie
var now = new Date("31-Dec-99");
document.cookie = name + "=expired" + "; expires=" + now;
} // end of delete_cookie()

//
// Okay, the hard stuff starts
// Basically we want a window to "pop up", stay for time X, then go away for time Y
// The hard part is remembering to check to see if the window still exists, or if 
// the user has hidden it or...you get the picture!
//

function start_popup()

{
accepts = parseInt(acceptscookies());
if (accepts < 1)
	{  // Since they asked to start pop-up, tell them (again) it requires cookies
	this.location="cookie_error.html";
	} // end of if

// Start popup, make stay_away false if set

set_cookie("stay_away","no");
load_popup();

} // end of start_popup()


function load_popup()
{

if (get_cookie("stay_away") == null)
	{  // Set if cookie not found
	set_cookie("stay_away","no");
	} // end of if

	// Don't show if stay_away is true!
if (get_cookie("stay_away") == "no")
	{  // If enabled, check to see if on top
	if (self != top)
		{  // ...and make visible if not on top
		top.location = self.location;
		} // end of if

	if (get_cookie("onscreen") == null)
		{  // Set default timeout (yeah, should be a constant :P)
		set_cookie("onscreen",30000);
		} // end of if
	
	if (get_cookie("delay") == null)
		{  // Set default time away
		set_cookie("delay",30000);
		} // end of if

	// Create window
	
	popup_window = window.open("popup/popup0.cfm","PopUp_Reagan","width=210,height=210,menubar=0,scrollbar=0,resizable=1,toolbar=0,directories=0,location=0,status=0,menu=0");
	popup_window_is_open = true;
	timer = setTimeout("blur_popup()",get_cookie("onscreen"));

	} // end of if

} // end of load_popup()


//
// We need a function to tell us when the help window is open
// so we don't disturb someone using help with a change in the 
// pop up window
//

function check_help_window()

{
var done = false;

if (help_window_is_open == true)
	{ // wait for help window to close before regaining focus
	done = setTimeout("check_help_window()",10000);
	} // end of while

return true;

} // end of check_help_window()


function blur_popup()

{

if (popup_window_is_open == true)
	{
	if (help_window_is_open == false)
		{ // blur focus only if help window is closed
		popup_window.blur();
		} // end of if

	// Now go away for the chosen duration

	timer = setTimeout("focus_popup()",get_cookie("delay"));

	} // end of if

} // end of blur_popup()


function focus_popup()

{ // Pick a new "information" window from the 39 (yeah, should be a constant) defined windows
var new_location = "popup/popup"+ Math.round(Math.random()*39).toString() + ".html";

if (popup_window_is_open == true)
	{
	if (help_window_is_open == false)
		{ // change focus only if help is closed
		popup_window.location = new_location;
		popup_window.focus();
		} // end of if

	//  Stay in focus for chosen duration
	
	time = setTimeout("blur_popup()",get_cookie("onscreen"));

	} // end of if

} // end of focus_popup()


function stop_popup()

{

// Close everythind down and let everyone know

popup_window.close();
popup_window_is_open = false;

} // end of stop_popup()

