// Copyright 2003-9 Richard W. Adams

var AUTOSTART_COOKIE = "tchAutoStart";
var childIsOpen;
var citeWindow;
var parentWindowName = "parentWindow";

//-----------------------------------------------------
/*
	PURPOSE	Ensures a function received all required arguments, no extra 
			arguments & (if so specified) no arguments are empty.
	RETURNS	True if the arguments validate, else false.
*/
function argsOk(
	functionCallee	,	// Caller's callee object
	allArgsRequired	,	// True = All arguments are required
	emptyArgsAllowed	// True = Empty arguments are allowed
){
	var ok = true;
	for (var i = 0; i < 1; ++i) {
		var functionName = functionCallee.toString().split(' ')[1].split('(')[0];
		var args = functionCallee.arguments;
		var expectedArgCount = functionCallee.length;
		var actualArgCount  = args.length;
		if ((allArgsRequired && actualArgCount < expectedArgCount) ||
			(actualArgCount > expectedArgCount)) {
			error("Function " + functionName + " expected " + expectedArgCount + " arguments, but received " + actualArgCount + ".");
			ok = false;
			break;
		}
		if (emptyArgsAllowed) {
			break;
		}
		for (var j = 0; j < args.length; ++j) {
			if (args[j] != null && args[j].length == 0) {
				error("Function " + functionName + "() received an empty argument.");
				ok = false;
				break;
			}
		}
	}
	return ok;
}
//------------------------------------------------
function ask(message) {
	write('<p class="request maintain">' + message + ', would you <a href="' + root + 'misc/mail.htm" onmouseover="return stat(\'Get our e-mail address\')" onmouseout="return erase()">send us an e-mail</a>? Thanks!</p>');
}
//------------------------------------------------
function cite (
	name	,	// Source name (will appear on displayed page)
	file	,	// Name of file with bibliographic info (no path or extension)
	suffix	,	// Page number(s), volume, etc.
	omitPage	// True = Don't show page number(s); optional, default = false
)
// PURPOSE	Writes HTML for a source citation link
// ENTRY	The variable root is defined, giving the path to the Web site root (no trailing slash)
{
	if (omitPage == null) {
		omitPage = false;
	}
	var prefix = "";
	if (root == null) {
		error("Root variable undefined");
	} else if (argsOk(arguments.callee, false, true)) {
		write('<a onmouseover="return stat(\'See bibliographic citation\')" onmouseout="return erase()" onclick="return openCitation(\'' + root + 'bib/' + file + '.htm\')" href="' + root + 'js/nojs.htm">' + name + '</a>')
		if (suffix.length > 0) {
			if (omitPage == false) {					// if Including page numbes
				if (suffix.search("p.") == -1 &&		// If page number not given
					suffix.indexOf('-') == -1 &&
					suffix.indexOf(',') == -1)			// If just a single page
					prefix = "p. ";						// Use this
				else										// If multiple pages
					prefix = "pp. ";						// Use this
			}
			write(", " + prefix + suffix);
		}
	}
}
//------------------------------------------------
function closeChild() {
	if (childIsOpen)
	  childWindow .close();
}
//------------------------------------------------
function closeCitation() {
	if (citeWindow && citeWindow != null) {
		citeWindow.close();
	}
}
//------------------------------------------------
function erase() {
	return stat('');
}
//------------------------------------------------
function error(message) {
	var prefix = "SCRIPT ERROR\n\nPlease report this message and URL to the Cyber Hymnal staff (see error reporting link on home page).\n\nMessage:\n\n\t";
	alert(prefix + message + "\n\nURL:\n\n\t" + location.href);
}
//------------------------------------------------
// PURPOSE Write a request for photo(s)
// ENTRY	Init() has been called
function fot(name, better) {
	image("pho&shy;to", name, better == null ? false : better);
}
//-----------------------------------------------------
function illustrate
// PURPOSE	Draw a link to an illustration file
// ENTRY	Init() has been called
(
	caption		,	// Image caption
	file		,	// File name, without path (if optional extension is omitted, name must not have any periods; default extension is "jpg")
	width		,	// Image width (pixels)
	height		,	// Image height (pixels)
	rightAligned,	// True = Align image at right edge of screen (optional; default is false)
	credit		,	// Byline (optional)
	url			 	// URL for byline link (optional)
){
	if (argsOk(arguments.callee, false, false)) {
		if (file.indexOf('.') == -1)
			file += ".jpg";
		var output = "";
		var	tableClass = rightAligned ? 'tableRight' : 'inner';
		output += '<table class="' + tableClass + '"><tr><td class="enclosingImage">';
		output += '<img src="' + root + 'img/' + file + '" class="illustration" title="' + caption + '" width="' + width + '" height="' + height + '" />';
		output += '<br /><div class="imageCaption">' + caption + '</div>';
		if (credit != null) 	{
			var bylineHint;
			output += '<div class="credit" width="' + width + '">' ;
			if (url != null) {
			  if (url.indexOf("mailto:") == -1 && url.indexOf("http:") == -1)
			  	url = "http://" + url;
				if (url.indexOf("mailto:") != -1)
					bylineHint = "Send e-mail";
				else
					bylineHint = "Visit external Web site (opens new window/tab)";
				output += '<a href="' + url + '" target="_blank" onmouseover="return stat(\'' + bylineHint + '\')" onmouseout="return erase()">';
			}
			if (credit.indexOf("&copy;") == -1)
				credit = "Courtesy of " + credit;
			output += credit;
			if (url != null)
				output += '</a>';
			output += '</div>';
		}
		output += '</td></tr></table>';
		write(output)	;		
	}
}//------------------------------------------------
function image(item, name, better)
// PURPOSE Write a request for picture or photo
// ENTRY		Init() has been called
{
	var betterString = '';
	if (better != null && better == true)
		betterString = 'better ';
	if (argsOk(arguments.callee, true, false))
		write('<p class="request maintain">If you have ac&shy;cess to a ' + betterString + item + ' of ' + name + ' that we could put on&shy;line, please <a href="' + root + 'misc/pics.htm" onmouseover="return stat(\'Learn how to send us images\')" onmouseout="return erase()">click here</a>.</p>');
}
//------------------------------------------------
function lk(text, hint, url, key) {
	if (url.indexOf('#') == -1)
		url += ".htm";
	write("<a href='" + url + "' onmouseover='return stat(\"" + hint + "\")' onmouseout='return erase()'>" + text + "</a>" );
}
//------------------------------------------------
function mail() {
	write(', would you <a href="' + root + 'misc/mail.htm" onmouseover="return stat(\'Get our e-mail address\')" onmouseout="return erase()">send us an e-mail</a>? Thanks!');
}
//------------------------------------------------
function mark(text) { // Prevents warnings (XHTML doesn't allow <marquee> tag)
	write('<marquee>' + text + '</marquee>');
}
//------------------------------------------------
function openChild(page, width, height)
{
	childIsOpen = true;
	childWindow = open(page, "ChildWnd", "toolbar=no, status=no, menubar=no, resizable=yes, scrollbars=yes, height=" + height + ", width=" + width);
	return true;
}
//------------------------------------------------
function openCitation(child) {
	window.name = parentWindowName;
	citeWindow = open(child, "citeWindow", "toolbar=no, status=no, menubar=yes, resizable=yes, scrollbars=yes, height=20, width=500");
	return false;
}
//------------------------------------------------
function pic(name, better)
// PURPOSE	Write a request for pictures(s)
// ENTRY		Init() has been called
{
	if (better == null)
		better = false;
	image("pic&shy;ture", name, better);
}
//-----------------------------------------------------
function portrait
// PURPOSE	Inserts a portrait
// ENTRY	Init() has been called
(
	caption		,	// Image caption (shown as tool tip on screen, as text below picture on printouts)
	file		,	// File name, without path (if optional extension is omitted, name must not have any periods; default extension is "jpg")
	width		,	// Image width (pixels)
	height		,	// Image height (pixels)
	rightAligned,	// True = Align image at right edge of screen (optional; default is false)
	credit		,	// Byline (optional)
	url			,	// URL for byline link (optional)
	imageClass		// Image class (optional; default is "portrait")
){
	if (argsOk(arguments.callee, false, false))
	{
		if (file.indexOf('.') == -1)
			file += ".jpg";
		var output = "";
		if (imageClass == null)
			imageClass = 'portrait';
		var tableClass;
		if (rightAligned == true) {
			tableClass = 'tableRight';
		}
		output += '<table class="' + tableClass + '"><tr><td width="' + width + '" class="enclosingImage">';
		output += '<img src="' + root + 'img/' + file + '" class="' + imageClass + '" title="' + caption + '" width="' + width + '" height="' + height + '" />';
		output += '<br /><div class="imageCaption">' + caption + '</div>';
		if (credit != null) {
			var bylineHint;
			output += '<div class="credit" width="' + width + '">Courtesy of ';
			if (url != null) {
			  if (url.indexOf("mailto:") == -1 && url.indexOf("http:") == -1)
			  	url = "http://" + url;
				if (url.indexOf("mailto:") != -1)
					bylineHint = "Send e-mail";
				else
					bylineHint = "Visit external Web site (opens new window/tab)";
				output += '<a href="' + url + '" target="_blank" onmouseover="return stat(\'' + bylineHint + '\')" onmouseout="return erase()">';
			}
			output += credit;
			if (url != null)
				output += '</a>';
			output += '</div>';
		}
		output += '</td></tr></table>';
		write(output);
	}
}
//------------------------------------------------
function redirect(page) {
	parentWindow = window.open('', parentWindowName);
	parentWindow.location = page;
	closeCitation();
	return false;
}
//------------------------------------------------
/*
	Sets the default language for the body of the document. This ensures that text
	in the document which is in a different language than the rest of the document
	is italicized (see style sheet). Call this method only AFTER creating the
	<body> element.
*/
function setDefaultLanguage() {
	nodes = document.getElementsByTagName("body");
	if (nodes == null) {
		error("Couldn't find <body> element in document tree.");
	} else {
		nodes.item(0).setAttribute("lang","en");
	}
}
//------------------------------------------------
function stat(message) {
	window.status = message;
	return true;
}
//------------------------------------------------
function write(string) {
	document.write(string);
}