// popupTimer.js

/*
		popupTimer.js
		window.popupTimers[key] where key = div's id to show/hide
		each member of which is an object with properties:
			showTime - absolute millisecond time to "Show" the div
			hideTime - "                         to "Hide" the div
			running  - boolean, true when setTimeout("run") is invoked, to false when "run" runs
		methods:
			popupTimerSet(divId, showOrHide="Show|Hide", delay(millis))
				if "show", if not running then sets ShowTime to now + delay, and setTimeOut("run(divId)", delay)
				           else (running) then noop - this means only the first "show" sets the showTime, subsequent shows are noops
				else ("Hide"), sets HideTime to now + delay, and if not running then setTimeOut("run(divId)", delay)
				                                    - this means that every "Hide" advances the time to hide - ie the last hide + delay it actually hides
			popupTimerRun(divId)
				runs in setTimeout, after the desired delay
				compares Showtime vs Hidetime, and whichever is later is the desired state, if one is empty it doesn't count (ie a "show" has occured but no "hide" has occured yet - normal "in" scenario)
				Checks the later time to see if it is "time yet", 
					if time is in future, calculates delta and setTimeout's itself that number of millis
					if time is now or in the past, does appropriate Hide or Show based whichever is later.
				sets running to false
				clears both times
*/

// Initialize

if (!window.popupTimers) {
	window.popupTimers = new Array();
}

// ------------------------------------
function PopupTimer(id) {
	this.id = id;
	this.timerSet = false;
	this.state    = "";
	this.showTime = "";
	this.hideTime = "";
	return this;
}

// ------------------------------------
function popupTimerSet(id, showOrHide, delay, theCaller) {

	var caller = theCaller.id;
	
	var nowMsec = (new Date()).getTime(); // millis since 1970...
	
	var thisEntry = window.popupTimers[id];
	if (!thisEntry) {thisEntry = window.popupTimers[id] = new PopupTimer(id);}
	
	if (showOrHide.toLowerCase() == "show") {
		
		// Show
		
		if (popupTimerDebugging() ) {
			popupWindowTimerDebugLog(
				"<br>Entering " + caller + " Set " + 
				"showOrHide="   + showOrHide          + "; " + 
				"delay="        + delay               + "; " + 
				"timerSet="     + thisEntry.timerSet  + "; " + 
				"state="        + thisEntry.state     + "; " + 
				"hideTime="     + thisEntry.hideTime  + "; " + 
				"showTime="     + thisEntry.showTime
			);
		}
		
		thisEntry.state = "Show"; // always override the state per this event, but only update the time on first "Show"
		
		if (!thisEntry.timerSet) {

			thisEntry.showTime = nowMsec + delay;
			setTimeout("popupTimerRun('" + id + "')" , delay);
			thisEntry.timerSet = true;
		}
		
	} else { 
		
		// Hide 

		if (popupTimerDebugging() ) {
			popupWindowTimerDebugLog(
				"<br>Entering " + caller + " Set " + 
				"showOrHide="   + showOrHide          + "; " + 
				"delay="        + delay               + "; " + 
				"timerSet="     + thisEntry.timerSet  + "; " + 
				"state="        + thisEntry.state     + "; " + 
				"hideTime="     + thisEntry.hideTime  + " " + 
				"showTime="     + thisEntry.showTime
			);
		}
		
		// always override the state per this event, and update the time on every "Hide"
		thisEntry.state = "Hide"; 
		thisEntry.hideTime = nowMsec + delay;

		if (!thisEntry.timerSet) {

			setTimeout("popupTimerRun('" + id + "')" , delay);
			thisEntry.timerSet = true;

		}

	}
	
	if (popupTimerDebugging() ) {
		popupWindowTimerDebugLog(
			"<br>Leaving " + caller + " Set " + 
			"showOrHide="  + showOrHide          + "; " + 
			"delay="       + delay               + "; " + 
			"timerSet="    + thisEntry.timerSet  + "; " + 
			"hideTime="    + thisEntry.hideTime  + "; " + 
			"showTime="    + thisEntry.showTime  + " " + 
		"<br>");
	}

}
	
// ------------------------------------
function popupTimerRun(id) { 
	
	var nowMsec = (new Date()).getTime(); // millis since 1970...
	
	var laterTime = "";
	var desiredState = "";
	
	var thisEntry = window.popupTimers[id];
	if (!thisEntry) {alert("Bad id"); return false;}

	// showTime or hideTime, whichever is later 
	
	// if both are blank, something is wrong
	if (thisEntry.hideTime == "" && thisEntry.showTime == "") {
		if (popupTimerDebugging() ) {
			popupWindowTimerDebugLog(
				"<br>Huh? Run is running before a set has run ?"
			);
		}
	}
	
	// respect whatever the last desired state was
	
	desiredState = thisEntry.state;
	
	if (desiredState == "Show") {laterTime = thisEntry.showTime;}
	else                        {laterTime = thisEntry.hideTime;}
	
	if (popupTimerDebugging() ) {
		popupWindowTimerDebugLog(
			"<br>in Run " + 
			"timerSet="     + thisEntry.timerSet  + "; " + 
			"state="        + thisEntry.state     + "; " + 
			"hideTime="     + thisEntry.hideTime  + "; " + 
			"showTime="     + thisEntry.showTime  + "; " + 
			"desiredState=" + desiredState        + "; " + 
			"laterTime="    + laterTime           + "; " + 
			"nowMsec="      + nowMsec             + " " + 
		"<br>");
	}

	// Check to see if is time yet, if not, then wake back up when it is time
	
	if (laterTime > nowMsec) { 
		var delta = laterTime - nowMsec;
		setTimeout("popupTimerRun('" + id + "')" , delta);
		
		if (popupTimerDebugging() ) {
			popupWindowTimerDebugLog(
				"<br>in Run - resubmitting after delta=" + delta
			);
		}
		
		return;
		
	}
	
	// it is time, hide it or show it, whichever was later
	
	var theDiv = document.getElementById(id);
	if (!theDiv) {alert("Cannot find Div with id '" + id + "'"); return false;}
	
	if (desiredState == "Show") {theDiv.style.display = "";}
	else                        {theDiv.style.display = "none";}
	
	// clean up
	thisEntry.timerSet = false;
	thisEntry.showTime = "";
	thisEntry.hideTime = "";
	
}

// ------------------------------------	
function popupTimerDebugging() {
	if (window.popupTimerDebugWindow) return true;
	return false;
}

// ------------------------------------	
function togglePopupTimerDebugWindow() {

	var xsizex, ysizex, xpos, ypos;
	
	// try to save the original window size and placement, otherwise, create some defaults

	var xsizex = 1000;
	var ysizex = 400;
	var xpos   = 100;
	var ypos   = 100;

	if (window.popupTimerDebugWindow) { // we know about one, but it may have been closed, in which case this is a hanging pointer
		try {
			if (document.all) { // ie - mystery
				/*
				xsizex = window.popupTimerDebugWindow.document.body.offsetWidth;
				xsizex = window.popupTimerDebugWindow.document.body.offsetHeight;
				xpos = window.popupTimerDebugWindow.document.body.offsetLeft;
				ypos = window.popupTimerDebugWindow.document.body.offsetTop;
				*/
			} else { // all others
				xsizex = window.popupTimerDebugWindow.innerWidth;
				ysizex = window.popupTimerDebugWindow.innerHeight;
				xpos = window.popupTimerDebugWindow.screenX;
				ypos = window.popupTimerDebugWindow.screenY;
			}
		} catch (e) {}
	}

	// try to close it, but it might throw an error, don't worry about it
	var closed = true;
	try {
		window.popupTimerDebugWindow.close();
	} catch (e) { closed = false; }

	// open a debug window	
	openPopupTimerDebugWindow(xsizex, ysizex, xpos, ypos);
	
}

function openPopupTimerDebugWindow(desiredXSize, desiredYSize, desiredXPos, desiredYPos) {

	var windowURL = "";
	var windowName = "popupTimerDebugWindowZ";
	var menuInPopup     = "false";
	var locationInPopup = "false";

	window.popupTimerDebugWindow = window.open(windowURL, windowName,"toolbar=no,width=" + desiredXSize + ",height=" + desiredYSize +
                                ",screenX=" + desiredXPos + ",screenY=" + desiredYPos +
                                ",directories=no,status=no,scrollbars=yes,resizable=yes" +
                                ",menubar=" + menuInPopup + ",location=" + locationInPopup);

	popupWindowTimerDebugLog("<h3>PopupTimer Debug Window</h3>");
		
}

// --------------------------------------
function popupWindowTimerDebugLog (str) {

	try {
		window.popupTimerDebugWindow.document.write (str);
	} catch (e) {};
	
}
	