// *************************************************************
// Functions for "pop-in" windows (DHTML simulations of pop-up windows)
// *************************************************************


// *************************************************************
// The functions below implement a simulated "pop-in" window 
// for glossary definitions, web links, etc.

// Convenience variables to keep track of the DOM elements we need to manipulate

// This will get set to the popInWindow we're currently moving when EngagePopInWindow_ebook is called
// It has to be set to something so that ShiftPopInWindow works
var popInWindow_ebook;

// Convenience variables for structures in the "main" pop-in window
var popInContent_ebook;
var popInTitle_ebook;
var popInLinkHolder_ebook;
var popInLink_ebook;

// function can be called when Disengage_ebook() is called
var disengageFunction_ebook;

// Global variables for moving the window around
var startX_ebook, startY_ebook;		// position of mouse the last time we executed MovePopInWindow_ebook
var mouseX_ebook, mouseY_ebook;		// current position of mouse
var lastScrollY_ebook = 0;	// scroll position the last time we moved
var popInWindowX_ebook, popInWindowY_ebook;	// current position of the popInWindow div

// Default width for pop-in window, and default background color
var defaultWidth_ebook = 325;

// Add the following constant to the width value sent in to ShowPopInWindow_ebook
// for an image
popinMarginOffset_ebook = 10;

// Hold a function to run when the pop-in window is closed
var cleanUpFunction_ebook = null;

// Return a handle to the "main" popin window (as opposed to sticky notes and potentially
// other future pop-ins)
function MainPopInWindow() {
	return main.document.getElementById("popInWindow_n");
}

// Return a handle to the popin window appearing in a supplemental window, for certain books
function SuppPopInWindow() {
        //alert("SuppPopInWindow function");
	if (document.getElementById("popInWindow") != null) {
        	return document.getElementById("popInWindow");
	} else {	
		alert("popInWindow is null");
	}
}


// Initialize the def window.  This has to be done every time we load a new section
function InitializePopInWindow_ebook(inSuppWindow) {
	// We're now writing the PopIn window dynamically (see WritePopInWindow_ebook below).
	// But we can't be sure yet that old sections files with the pop-in window
	// hard-coded don't exist.  So for now all the id's have _n pasted onto the end.
 	if (inSuppWindow == true) {
		popInContent_ebook = document.getElementById("popInContent");
		popInTitle_ebook = document.getElementById("popInTitle");
		popInLinkHolder_ebook = document.getElementById("popInLinkHolder");
		popInLink_ebook = document.getElementById("popInLink");
    } else {
		popInContent_ebook = main.document.getElementById("popInContent_n");
		popInTitle_ebook = main.document.getElementById("popInTitle_n");
		popInLinkHolder_ebook = main.document.getElementById("popInLinkHolder_n");
		popInLink_ebook = main.document.getElementById("popInLink_n");
	}

	popInWindowX_ebook = 10;	// initial values set according to the hard-coded DIV values
	popInWindowY_ebook = 10;	// PW 9/2007: Now overwritten in EngagePopInWindow_ebook, but keep these here for now...

	// Set up event handlers to allow the window to be dragged
	if (popInTitle_ebook != null) {
		if ( typeof(inSuppWindow) != 'undefined' && inSuppWindow == true) {
			popInTitle_ebook.onmousedown = EngagePopInWindow_ebook;
		} else {
			popInTitle_ebook.onmousedown = EngagePopInWindow_ebook;
		}

	}
	if (inSuppWindow == null) {
                main.document.onmouseup = Disengage_ebook;
    } else if (inSuppWindow == true) {
                document.onmouseup == Disengage_ebook;
    }
	
	
}


// Shift the popInWindow.  If absolute = true, go to x, y (but don't shift to a -1 location). 
// If absolute = false or undefined, move by x, y
function ShiftPopInWindow_ebook(x, y, absolute, adjusting) {
	var oldLeft = popInWindowX_ebook;
	var oldTop = popInWindowY_ebook;

	if (absolute) {
		if (x != -1) {
			popInWindowX_ebook = x;
		}
		if (y != -1) {
			popInWindowY_ebook = y;
		}
	} else {
		popInWindowX_ebook += x;
		popInWindowY_ebook += y;
	}
	
	// Now calculate dx/dy, even if we are doing things absolutely
	var dx = popInWindowX_ebook - oldLeft;
	var dy = popInWindowY_ebook - oldTop;	// moving from oldTop = 200 to piy = 100, dy is 100-200 = -100
	
	// See where this is going to take us, and
	// Don't let it move off the left/top of the screen
	// offsetLeft/offsetTop are (hopefully) always absolute, not relative
	// to the div the window is attached to.
	var doL = (popInWindow_ebook.offsetLeft + popInWindow_ebook.offsetWidth) + dx;
	var doT = popInWindow_ebook.offsetTop + dy;	// continuing example above, if oT was 10, doT would be 10 + (-100) = -90
	if (doL < 50) {
		popInWindowX_ebook -= (doL - 50);
	}
	if (doT < 0) {
		popInWindowY_ebook -= doT;	// continuing example above, piy becomes 100 + (-90) = 10 -- we move so that offsetTop is 0
	}
	
	// Technically, the style should be "10px", not just "10".
	popInWindow_ebook.style.left = popInWindowX_ebook + "px";
	popInWindow_ebook.style.top = popInWindowY_ebook + "px";

}

// Get the current mouse position, taking account off any scrolling
function SetMousePositions_ebook(e) {
	// Netscape-compatible browsers send the event in as a parameter to the function,
	// but Explorer-compatible browsers require us to get the event.
	// Note that we need the main.window event, which will localize the mouse relative
	// to the main frame
	if (!e) var e = main.window.event;
	
	// See the bottom of http://www.quirksmode.org/index.html?/js/events_compinfo.html
	// for a version of this code that will work with all browsers; we only care about IE, NS, and Safari

	mouseX_ebook = e.clientX + main.document.body.scrollLeft;
	mouseY_ebook = e.clientY + main.document.body.scrollTop;
}

// This function is set as an event handler that's called
// when the user first clicks on the definition window
function EngagePopInWindow_ebook(e, piw, dfn, inSuppWindow) {
	// PW 9/2007: to allow for sticky notes, we now allow an optional piw argument. 
	// If set, popInWindow_ebook is set to this value. If not set, popInWindow_ebook gets the "normal"
	// popin window.
	if (piw != null) {
		popInWindow_ebook = piw;
	} else {
		if (inSuppWindow == true) {
			popInWindow_ebook = SuppPopInWindow();
		} else {
			popInWindow_ebook = MainPopInWindow();
		}
	}
	
	// PW 9/2007: allow for a function to be called on disengage
	disengageFunction_ebook = dfn;
	
	// Register an onmousemove event handler at the document level to move the window
	main.document.onmousemove = MovePopInWindow_ebook;
	
	// Cancel selection on body and title for remainder of move
	DisableSelection_ebook(main.document.body);
	DisableSelection_ebook(popInTitle_ebook);

	// Get the mouse event (see note in SetMousePositions_ebook)
	if (!e) var e = main.window.event;
	
	// set the initial values of startX/Y
	SetMousePositions_ebook(e);
	startX_ebook = mouseX_ebook;
	startY_ebook = mouseY_ebook;
	
	// and popInWindowX_ebook/popInWindowY_ebook
	popInWindowX_ebook = popInWindow_ebook.offsetLeft;
	popInWindowY_ebook = popInWindow_ebook.offsetTop;
	
	// Also set the values for lastScrollY_ebook
	// Note that we don't bother with X scrolling
	if (document.documentElement && document.documentElement.scrollTop) {
		lastScrollY_ebook = main.document.documentElement.scrollTop;
	} else {
		lastScrollY_ebook = main.document.body.scrollTop;
	}
	
	return StopEvent_ebook(e);
}

function DisableSelection_ebook(el) {
    el.onselectstart = function() {
        return false;	// IE
    };
    el.unselectable = "on";	// IE?
    el.style.MozUserSelect = "none";	// mozilla
    el.style.KhtmlUserSelect = "none";	// safari?
}

function EnableSelection_ebook(el) {
    el.onselectstart = null;
    el.unselectable = "off";	// IE?
    el.style.MozUserSelect = "";	// mozilla
    el.style.KhtmlUserSelect = "";	// safari?
}

// MovePopInWindow_ebook: called when the mouse is moved after engaging the window
function MovePopInWindow_ebook(e, inSuppWindow) {
	// Get the mouse event	
	var e;
	if (!e) {
		if (inSuppWindow == true) {
			e = window.event;
		} else {
			e = main.window.event;
		}
	}

	// Get the new mouse position
	SetMousePositions_ebook(e);
	
	// Move the window
	if (inSuppWindow == true) {
		ShiftPopInWindow_ebook(mouseX_ebook - startX_ebook, mouseY_ebook - startY_ebook, false);
	} else {
		ShiftPopInWindow_ebook(mouseX_ebook - startX_ebook, mouseY_ebook - startY_ebook, false);
	}


	// Set startX/Y so that the next time this function is called we'll move appropriately
	startX_ebook = mouseX_ebook;
	startY_ebook = mouseY_ebook;

	return StopEvent_ebook(e);
}


// Disengage_ebook: called when the mouse button is released
function Disengage_ebook() {
	// Clear the onmousemove event handler
	main.document.onmousemove = null;
	
	// Enable text selection
	EnableSelection_ebook(main.document.body);
	EnableSelection_ebook(popInTitle_ebook);
	
	// Call the disengageFunction_ebook if one was specified
	if (disengageFunction_ebook != null) {
		disengageFunction_ebook(popInWindow_ebook);
	}
	
	// Have to reset this function now; otherwise other mouseUps call it too
	disengageFunction_ebook = null;
}

// Stop the event e from bubbling up to higher layers of the DOM
// see http://www.quirksmode.org/index.html?/js/events_compinfo.html
function StopEvent_ebook(e) {
	e.cancelBubble = true;		// IE
	if (e.stopPropagation) 		// everyone else
		e.stopPropagation();
	
	// In NS, we (also?) have to return false to cancel the default action from occurring.
	// StopEvent_ebook returns false so that the caller can say "return StopEvent_ebook(e);"
	// to cover all eventualities.
	return false;
}

// Show something in the main popin window
				// 1	2	3	4    5    6      7      8        9
function ShowPopInWindow_ebook(title, content, link, width, top, left, bgcolor, fn, inSuppWindow) {
	// PW 9/07: make sure popInWindow is not set to a sticky note popIn
 	// KC: for popins in supp windows
	//alert("inSuppWindow in ShowPopInWindow_ebook: " + inSuppWindow);
    if (inSuppWindow == null) {     
             popInWindow_ebook = MainPopInWindow()
    } else if (inSuppWindow == true) {
             popInWindow_ebook = SuppPopInWindow();
    }	

	 // KC if we have ShowFootnote in the content and we're in a supp window, make sure to adjust the call
        if (inSuppWindow == true) {

                content = content.replace(/top.ShowFootnote\(([^\)]*)\)/, "ShowFootnote($1, true)");

        }

	// Remember what we're showing
	popInTitleShowing = title;
	
	// If the popInContent_ebook span hasn't yet been recognized (this is an issue when
	// in IE6, at least), call InitializePopInWindow again
	if (popInContent_ebook == null) {
		InitializePopInWindow_ebook(inSuppWindow);
	}
	// GUARD AGAINST USER CLICKING ON A TERM BEFORE GLOSSARY.JS HAS LOADED??
	
	// Set the title, content, and link
	popInTitle_ebook.innerHTML = "<b>" + title + "</b>";
	popInContent_ebook.innerHTML = content;
	
	// If link is null, there isn't one, so hide the div
	if (link == null) {
		SetDisplay(popInLinkHolder_ebook, "none");
	} else {
		SetDisplay(popInLinkHolder_ebook, "block");
		popInLink_ebook.innerHTML = link;
	}

	// Position the popInWindow in the same position as it was last left,
	// relative to the current scroll position

	// Find out what the current scroll position is
	var currentScrollY;
	if (inSuppWindow == null) {
    	if (main.document.documentElement && main.document.documentElement.scrollTop) {
        	currentScrollY = main.document.documentElement.scrollTop;
        } else {
            currentScrollY = main.document.body.scrollTop;
        }
    } else if (inSuppWindow == true) {
        if (document.documentElement && document.documentElement.scrollTop) {
            currentScrollY = document.documentElement.scrollTop;
        } else {
            currentScrollY = document.body.scrollTop;
        }
    }

	
	// If top/left are set, scroll to that position
	// If left is null set it to -1, meaning that we'll stay wherever we were
	if (left == null) {
		left = -1;
	}
	// If top is null, shift so that it's near the top of the screen, regardless of scrolling
	if (top == null) {
		top = currentScrollY + 25;
	}
	
	ShiftPopInWindow_ebook(left, top, true);

	// Alternatively, we COULD:
	// Shift the window to compensate for any scrolling that happened since
	// the last time the window moved.
	// If, for example, the window was scrolled to 0, 0 before
	// and now is scrolled to 100, 0, we want to move down 100 pixels
	// ShiftPopInWindow_ebook(0, currentScrollY - lastScrollY_ebook);
	
	// Record the current scroll position
	lastScrollY_ebook = currentScrollY;
	
	// Set width to provided or default width
	if (width != null) {
   		if ((width.toString()).indexOf("px") != -1) {
			popInWindow_ebook.style.width = width;
		} else {
			popInWindow_ebook.style.width = width + "px";
		}

	} else {
		popInWindow_ebook.style.width = defaultWidth_ebook + "px";
	}	
	
	// Set background color to provided or default color
	if (inSuppWindow == null) {
    	if (bgcolor != null) {
        	main.document.getElementById('popInBody_n').style.backgroundColor = bgcolor;
         } else {
            main.document.getElementById('popInBody_n').style.backgroundColor = defaultPopInBgColor;        // set in defaults.js
         }
    }

	// Show the popInWindow
	SetVisibility(popInWindow_ebook, "visible");

	// Record the clean up function, if one was sent.
	cleanUpFunction_ebook = fn;
}

// Hide the popin window
function ClosePopInWindow_ebook(inSuppWindow) {

	if (inSuppWindow == null) {
    	SetVisibility(MainPopInWindow(), "hidden");
    } else if (inSuppWindow == true) {
        SetVisibility(SuppPopInWindow(), "hidden");
    }

	// Set lastDefinedTermId to null
	lastDefinedTermId = null;
	
	// Remember that we're not showing any popin window now
	popInTitleShowing = null;

	// run cleanUpFunction_ebook if it was sent
	if (cleanUpFunction_ebook != null) {
		cleanUpFunction_ebook();
	}
}

// -----------------------------------------
// Resize a pop-in window

var popInWindowW_ebook, popInWindowH_ebook;	// current width/height of the popInWindow div
var innerPopInDivW_ebook, innerPopInDivH_ebook, innerPopInDivWComp_ebook, innerPopInDivHComp_ebook;
var innerPopInDiv_ebook;

function EngagePopInResize_ebook(e, piw, innerDiv, innerDivWComp, innerDivHComp, dfn) {
	popInWindow_ebook = piw;
	innerPopInDiv_ebook = innerDiv;
	innerPopInDivWComp_ebook = innerDivWComp;
	innerPopInDivHComp_ebook = innerDivHComp;

	// allow for a function to be called on Disengage_ebook
	disengageFunction_ebook = dfn;
	
	// Register an onmousemove event handler at the document level to resize the window
	main.document.onmousemove = ResizePopInWindow_ebook;

	// Get the mouse event (see note in SetMousePositions_ebook)
	if (!e) var e = main.window.event;
	
	// set the initial values of startX_ebook/Y
	SetMousePositions_ebook(e);
	startX_ebook = mouseX_ebook;
	startY_ebook = mouseY_ebook;
	
	// and popInWindowW_ebook/popInWindowH_ebook
	popInWindowW_ebook = popInWindow_ebook.offsetWidth;
	popInWindowH_ebook = popInWindow_ebook.offsetHeight;
	innerPopInDivW_ebook = innerPopInDiv_ebook.offsetWidth;
	innerPopInDivH_ebook = innerPopInDiv_ebook.offsetHeight;
	
	return StopEvent_ebook(e);
}

function ResizePopInWindow_ebook(e) {
	// Get the mouse event	
	if (!e) var e = main.window.event;

	// Get the new mouse position
	SetMousePositions_ebook(e);
	
	// Resize the window
	popInWindowW_ebook += (mouseX_ebook - startX_ebook);
	popInWindowH_ebook += (mouseY_ebook - startY_ebook);
	
	popInWindow_ebook.style.width = popInWindowW_ebook + "px";
	popInWindow_ebook.style.height = popInWindowH_ebook + "px";

	// And the inner div
	//innerPopInDivW_ebook += (mouseX_ebook - startX_ebook);
	//innerPopInDivH_ebook += (mouseY_ebook - startY_ebook);
	
	innerPopInDiv_ebook.style.width = (popInWindowW_ebook + innerPopInDivWComp_ebook) + "px";
	innerPopInDiv_ebook.style.height = (popInWindowH_ebook + innerPopInDivHComp_ebook) + "px";

	// Set startX_ebook/Y so that the next time this function is called we'll move appropriately
	startX_ebook = mouseX_ebook;
	startY_ebook = mouseY_ebook;

	return StopEvent_ebook(e);
}

//----------------------------------------------------------------
// Functions for particular pop-ins follows
// ---------------------------------------------------------------

// var lastDefinedTermId		// defined in ebookSuppWinAPI.js
// Show the given term's definition in the definition window
function Define(termId) {	
	// Decide on the content
	var content;
	if (T_ID == null) {
		T_ID = glossaryF.T_ID;
	}
	if (T_ID[termId] == null) {
		content = 'Not defined...';
	} else {
		content = T_ID[termId];
	}
	
	// Extract the full term and add other links if possible
	var start = content.indexOf("<b>") + 3;
	var end = content.indexOf("</b>");
	var morelinks;
	if (start != -1 && end != -1) {
		var term = content.substring(start, end);
		// Remove : if there
		term = term.replace(/[:]/g, "");

		// Remove entities if there
		term = term.replace(/\&\w+;/g, "");

		// remove parentheticals
		term = term.replace(/ *\([^)]+\)/g, "");

		// replace spaces with + for google
		var googleTerm = term.replace(/ /g, "+");

		// And add quotes around the whole thing
		googleTerm = '"' + googleTerm + '"';

		// replace spaces with _ for wikipedia
		//var wikiTerm = term.replace(/ /g, "_");
		
		// Add wikipedia and google links	
		// morelinks = ' | <a class="ba" href="http://en.wikipedia.org/wiki/' + wikiTerm + '" target="_blank">Wikipedia</a> | <a class="ba" href=\'http://www.google.com/search?q=' + googleTerm + '\' target="_blank">Google</a>';
		if (bookId == 'theguide8e') {
			morelinks = '';
		} else {
			morelinks = ' | <a class="ba" href=\'http://www.google.com/search?q=' + googleTerm + '\' target="_blank">Google</a>';
		}
	} else {
		morelinks = "";
	}
	
	// definitionWidth can be set in config.js;
	// if it's not set, it will be null, which is fine.
	// windowTop, windowLeft can be set in config.js; set to null in defaults.js
	ShowPopInWindow_ebook("Glossary Definition"
					, content
					, '<a class="ba" href="JavaScript:top.OpenSupp(\'glossary\')">Open full Glossary</a>' +  morelinks
					, definitionWidth
					, null
					, windowLeft
	);

	// Record the term, so that if we jump to the glossary we'll go to this place
	// If we do so, use the first four letters of the termId, in hopes of getting some
	// additional helpful entries.
	// KC: Unfortunately, in IE, &para and &cent in the query string get rendered as a paragraph and cents symbol respectively, throwing an
	// error 
	if (termId.substr(0,4) == "para" || termId.substr(0,4) == "cent") {
		lastDefinedTermId = termId.substr(0, 5);
	} else {
		lastDefinedTermId = termId.substr(0, 4);
	}
}


// Show the given links in the weblink window
function ShowLinks(linkId) {
	// Decide on the content
	var content;
	if (L_ID[linkId] == null) {
		content = 'No link...';
	} else {
		content = L_ID[linkId];
	}
	
	// KC: removed "view all web links" on 12/20/16, since web links are sometimes being linked using bcs now
	
	ShowPopInWindow_ebook("Web Link"
					, content
					);
}

// Show the given footnote in a popin window
function ShowFootnote(footnoteId, inSuppWindow) {
	// Decide on the content
	var content;
	if (F_ID[footnoteId] == null) {
		content = 'No footnote...';
	} else {
		content = F_ID[footnoteId];
	}
	
	ShowPopInWindow_ebook("Footnote", content
                                        , null
                                        , null
                                        , null
                                        , null
                                        , null
                                        , null
                                        , inSuppWindow
	);
}

// Show the given footnote in a popin window
function ShowComment(commentId) {
	// Decide on the content
	var content;
	if (C_ID[commentId] == null) {
		content = 'No comment...';
	} else {
		content = C_ID[commentId];
	}
	
	ShowPopInWindow_ebook("Comment " + commentId
					, content, null, 490, null, null, '#ffffff'
	);
}

// Show the given equation in a popin window
function ShowEquation(eqn) {
	// Decide on the content
	var content = "";
	if (E_ID[eqn] == null) {
		// it may be that, for example, 9.4 was called but what was meant was 9.4a, 9.4b, 9.4c 
		
		if (E_ID[eqn + "a"] != null) {
			content += "<table><tr><td><b>(a)</b></td><td><img src=\"../figures/" + E_ID[eqn + "a"] + "\"></td></tr></table>";
		}
		if (E_ID[eqn + "b"] != null) {
			content += "<table><tr><td><b>(b)</b></td><td><img src=\"../figures/" + E_ID[eqn + "b"] + "\"></td></tr></table>";
		}
		if (E_ID[eqn + "c"] != null) {
			content += "<table><tr><td><b>(c)</b></td><td><img src=\"../figures/" + E_ID[eqn + "c"] + "\"></td></tr></table>";
		}
		// if still no content by this point, make an error
		if (content == null) {
			content = "No equation...";
		}
	} else {
		if (bookId == "physse6e") {
			content = "<img src=\"/physse6e/figures/" + E_ID[eqn] + "\">";
		}
		else
		{
		content = "<img src=\"../figures/" + E_ID[eqn] + "\">";
		}
	}
	
	if (bookId == "physse6e") {
		var nums = eqn.split(".");
		if (nums[0]=='42') {		eqn = 'R.' + nums[1]; }	
	}
	
	ShowPopInWindow_ebook("Equation " + eqn
					, content, null, 490, null, null, '#ffffff'
	);
}

// Show a whatif answer (disco) in the popin window
function ShowWhatIfAnswer(chapter, number) {
	// Decide on the content
	var content;
	if (WDYT[chapter + "_" + number] == null) {
		content = 'No answer...';
	} else {
		content = WDYT[chapter + "_" + number];
	}

	ShowPopInWindow_ebook("What Did You Think?"
					, content
					, null
	);
}


// Show arbitrary stuff in the pop in window
function PopInAlert(title, content, link) {
	// Decide on the content
	var content;
	if (T_ID[termId] == null) {
		content = 'Not defined...';
	} else {
		content = T_ID[termId];
	}
	
	ShowPopInWindow_ebook("Glossary Definition"
					, content
					, '<a class="ba" href="JavaScript:top.OpenSupp(\'glossary\')">Open full Glossary</a>'
	);

	// Record the term, so that if we jump to the glossary we'll go to this place
	// If we do so, use the first four letters of the termId, in hopes of getting some
	// additional helpful entries.
	lastDefinedTermId = termId.substr(0, 4);
}

var popInHTML_ebook = 
	'<div id="popInWindow_n" style="visibility:hidden; position:absolute; left:10; top:10; border:1px solid #000000; background-color:#dddddd; width:325; font-size:12px; text-align:left; z-index:1000">'
	+ '    <table border="0" cellspacing="0" cellpadding="0" width="100%" style="margin:0px; padding:0px"><tr>'
	+ '        <td id="popInTitle_n" class="popInTitleS" style="font-size:11px; font-family:Verdana, sans-serif; padding:2px 3px 4px 3px; cursor:move; width:95%; border-width:0px; ">Title</td>'
	+ '        <td class="popInTitleS" style="font-size:11px; font-family:Verdana, sans-serif; padding:2px 3px 4px 3px; cursor:pointer; text-align:right; border-width:0px"><a href="Javascript:top.ClosePopInWindow_ebook()" class="popInCloseLinkS" style="font-weight:bold; cursor:pointer">[X]</a></td>'
	+ '    </tr></table>'
	+ '    <div style="padding:5px" id="popInBody_n">'
	+ '        <div id="popInContent_n">content</div>'
	+ '        <div id="popInLinkHolder_n" style="margin-top:5px"><b>&raquo;</b> <span id="popInLink_n">link</span></div>'
	+ '    </div>'
	+ '</div>'
	;

function WritePopInWindow_ebook() {
	main.document.write(popInHTML_ebook);
}

// Show a figure in the popin window
// pass the image name (with extension, e.g. "1_1.jpg"), which must be in the figures/ directory
// , the width of the figure, and a caption (optional)
function PopInFigure(img, width, caption, imgwidth, type, captionstyle) {

	if (type == null || type == "") {
		type = "Figure";
	} else {
		type = "Nugget";
	}
	
	if (captionstyle == null) {
		captionstyle = "padding-bottom:5px";
	} else {
		captionstyle = "padding-bottom:5px; " + captionstyle;
	}
	
	
	if (caption == null) {
		caption = "";
	} else {
		caption = "<div style='" + captionstyle + "'>" + caption + "</div>";
	}
	
	if (imgwidth == null || imgwidth == "") {
		imgwidth = width;
	}
	
	ShowPopInWindow_ebook(type, '<center><img src="' + urlStartBookID + 'figures/' + img + '" alt="" width="' + imgwidth + '" border="0" align="center"></center>' + caption, null, width + popinMarginOffset_ebook);	//, top, left
}


// Keep track of the title we're currently showing in the pop-in window
var popInTitleShowing = null;
function PopInShowing_ebook() {
	return popInTitleShowing;
}

// The following is for the Comment-style eBook.  But we call it in the "mainstream" apis, so 
// it has to live in one of the mainstream apis.
function ToolWindowShowing() {
	return (PopInShowing_ebook() == 'eBook Tools');
}

// PW: On 11/18/2008, I replaced all calls to ShowPopInWindow() with ShowPopInWindow_ebook()
// to eliminate a conflict in v3 Portal with the ShowPopInWindow function used by other parts of the portal.
// But just in case some ebook pages are calling ShowPopInWindow directly,
// write alternate versions of the public functions in this file.
// v3 Portal will just overwrite these versions
function ShowPopInWindow(title, content, link, width, top, left, bgcolor, fn) {
	ShowPopInWindow_ebook(title, content, link, width, top, left, bgcolor, fn);
}

function ClosePopInWindow() {
	ClosePopInWindow_ebook();
}

function PopInShowing() {
	PopInShowing_ebook();
}

function WritePopInWindow() {
	WritePopInWindow_ebook();
}

function MovePopInWindow(e) {
	MovePopInWindow_ebook(e);
}

