// Copyright 2003-9 Richard W. Adams

var autostart;
var nextPage;
var pageMidiFile;
var playCalled = false;
var prevPage;
var root;

//------------------------------------------------
function arrows(position)
{
// PURPOSE	Draw navigational arrows to home, previous & next pages
// ENTRY	Init() has been called

	var className = "navigation";
	if (position != null) {
		className += ' ' + position;
	}
	write('<p class="' + className + '"><a href="' + prevPage + '" onMouseOver="return stat(\'Go to previous page\')" onMouseOut="return erase()"><img src="' + root + '/img/a/ar-left.gif" title=" Back " width="30" height="30"></a> ');
	write('<a href="' + root + 'index.htm#lk" onMouseOver="return stat(\'Go to Cyber Hymnal home page\')" onMouseOut="return erase()"><img src="' + root + 'img/a/ar-toc.gif" title=" Home " width="30" height="30" /></a> ');
	write('<a href="' + nextPage + '" onMouseOver="return stat(\'Go to next page\')" onMouseOut="return erase()"><img src="' + root + '/img/a/ar-right.gif" title=" Next " width="30" height="30"></a></p>');
}
//------------------------------------------------
function begin (
	quote, 	// Scripture quotation
	verse	// Chapter & verse
)
// PURPOSE  Draw the top of the page
// ENTRY	 Init() has been called
{
	setDefaultLanguage();
	if (playCalled == false) {
		error("No call to play()");
		location.href = root + "index.htm";
	}
	if (argsOk(arguments.callee, true, false)) {
		arrows("top");
		write('<marquee>&ldquo;' + quote + '&rdquo; ' + verse + '</marquee><hr />')
	}
}
//------------------------------------------------ 
function bio(text, file)  { 
// PURPOSE		Draw link to a biography page 
// ENTRY		Init() has been called 
// ARGUMENTS	text  Link text to display 
//				file  Biography file page (without extension, which must be .htm) 

	if (argsOk(arguments.callee, true, false)) 	{
		write('<a href="' + root + 'bio/' + file + '.htm" target="_blank" ');
		write('onMouseOver="return stat(\'See biography (opens new window/tab)\')" ');
		write('onMouseOut="return erase()">' + text + '</a>');
	}
} 
//------------------------------------------------
function doUnload() {
	closeCitation();
	closeChild();
	return true;
}
//------------------------------------------------
function end() {
// PURPOSE Draw end of the page
// ENTRY	Init() has been called
	arrows();
	write('<p class="footer" title="When this page last changed">' + document.lastModified + '</p>');
}
//----------------------------------------------------
function getAutostartCookie() {
	var cookieValue = "true";
	var prefix = AUTOSTART_COOKIE + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex != -1) {
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1) {
			cookieEndIndex = document.cookie.length;
		}
		cookieValue = unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}
	return cookieValue;
}
//------------------------------------------------
function init(level, midi, prev, next)
{
// PURPOSE		Page initialization
// ARGUMENTS	level = Number of levels below site root
//					midi = MIDI file name (without extension, which must be .mid)
//					prev = Previous page's URL (without extension, which must be .htm)
//					next = Next page's URL (without extension, which must be .htm)

	if (argsOk(arguments.callee, true, false)) {
		if (window != top) {
			top.location.href = location.href;
		}
	  	root = "";
	  	for (i = 0; i < level; ++i) {
		  	root += "../"
		}
		pageMidiFile = root + "mid/" + midi + ".mid";
		prevPage = prev + ".htm";
		nextPage = next + ".htm";
		write('<link href="' + root + 'sty/tch-hymn.css" rel="stylesheet" type="text/css" />');
		autostart = getAutostartCookie();
		if (autostart == "true") {
			write('<bgsound src="' + pageMidiFile + '" loop="infinite">');
		}
		delay = location.search.toString();
		if (delay != "") {
			delay = delay.substring(1, delay.length);
			write('<meta http-equiv="refresh" content="' + delay + ';url=' + nextPage + '?' + delay + '">');
		}
	}
}
//------------------------------------------------
function insertMusicLinks(
	midi			,	// Full MIDI file name, including extension
	scoreBase		,	// Score file name, down from score directory (no extension)
	scoreExtension	,	// Extension of score file name (must be same as score directory name)
	scoreType			// Decscription of score file type
)
// PURPOSE	Insert insert links for a MIDI & a score file.
// ENTRY		Init() has been called
{
	if (argsOk(arguments.callee, true, false)) {
		var score = root + scoreExtension + '/' + scoreBase + '.' + scoreExtension;
		output = '<span class="musicLinks"> (<a href="' + midi + '" onmouseover="return stat(\'Open MIDI file\')" onmouseout="return erase()">MI&shy;DI</a>, ';
		output += '<a href="' + score + '"';
		if (scoreExtension == 'pdf') {
			output += ' target="_blank"';
		}
		output += ' onmouseover="return stat(\'See sheet music (' + scoreType + ' format)\')" onmouseout="return erase()">score</a>)</span>';
		write(output);
	}
}
//------------------------------------------------
function lmn(
	score	// Base name of score file, with path down from nwc directory (no extension, which must be "nwc")
)
// PURPOSE	Insert links for a page's main MIDI file & NoteWorthy Composer score file
// ENTRY		Init() has been called
{
	if (argsOk(arguments.callee, true, false)) {
		lmn2(pageMidiFile, score);
	}
}
//------------------------------------------------
function lmn2(
	midi,	// Full path to MIDI file, or path down from MIDI directory. If not full path, should not have extension, which must be "mid".
	score	// Score file name, down from the "nwc" directory (no extension, which must be "nwc")
)
// PURPOSE	Insert links for a MIDI file & NoteWorthy Composer score file. This function is called from lmn(),
//			and is also used for "alternate tune" links on hymn pages.
{
	if (argsOk(arguments.callee, true, false)) {
		if (midi.indexOf(root) == -1) {								// If MIDI lacks full path
			midi = root + 'mid/' + midi + ".mid";					// Add it
		}
		insertMusicLinks(midi,score,"nwc","NoteWorthy Composer");
	}
}
//------------------------------------------------
function lmp(
	score	// Base name of score file, with path down from pdf directory (no extension, which must be "pdf")
)
// PURPOSE	Insert links for a page's main MIDI file & PDF score file
// ENTRY		Init() has been called
{
	if (argsOk(arguments.callee, true, false))
		lmp2(pageMidiFile, score);
}
//------------------------------------------------
function lmp2(
	midi,	// Full path to MIDI file, or path down from MIDI directory. If not full path, should not have extension, which must be "mid".
	score	// Score file name, down from the "pdf" directory (no extension, which must be "pdf")
)
// PURPOSE	Insert links for a MIDI file & an Adobe PDF score file. This function is called from lmp(),
//				and is also used for "alternate tune" links on hymn pages.
{
	if (argsOk(arguments.callee, true, false)) {
		if (midi.indexOf(root) == -1) {						// If MIDI lacks full path
			midi = root + 'mid/' + midi + ".mid";			// Add it
		}
		insertMusicLinks(midi, score, "pdf", "Adobe PDF");
	}
}
//------------------------------------------------
function music() {
	var midi, nwc, pdf;
	switch (arguments.length) {
		case 2 :
			midi = pageMidiFile;
			nwc  = arguments[0];
			pdf  = arguments[1];
			break;
		case 3 :
			midi = root + "mid/" + arguments[0]+ ".mid";
			nwc  = arguments[1];
			pdf  = arguments[2];
			break;
	}
	if (nwc == null) {
		error("Bad music argument count (" + arguments.length + ')');
	} else {
		var output = '<span class="musicLinks"> (';
		output += '<a href="' + midi + '" onmouseover="return stat(\'Open MIDI file\')" onMouseOut="return erase()">MI&shy;DI</a>, ';
		output += '<a href="' + root + 'nwc/' + nwc + '.nwc"' + ' onmouseover="return stat(\'See sheet music (Noteworthy Composer format)\')" onmouseout="return erase()">NWC</a>';
		if (pdf != null) {		
			output += ', <a href="' + root + 'pdf/' + pdf + '.pdf"' + ' onmouseover="return stat(\'See sheet music (Adobe PDF format)\')" onmouseout="return erase()" target="_blank">PDF</a>';
		}
		output += ')</span>';
		write(output);
	}
}
//------------------------------------------------
// PURPOSE	Insert embedded MIDI object for browsers that need it, & insert autoplay HTML when appropriate.
// ENTRY	Init() has been called
function play() {
	if (playCalled) {
		error("Play() called multiple times");
	} else {
		playCalled = true;
		if (navigator.appName == "Netscape" && autostart == "true") {
 			write('<embed src="' + pageMidiFile + '" autostart="true" height="18" loop="true" repeat="true">');
 		}
 		if (location.search.toString() != "") {
 			write('<p class="autoplay">AUTOPLAY</p>');
 		}
	}
}
//------------------------------------------------
function start(
	title, 		// Page title
	quote, 		// Scripture quotation
	verse,		// Chapter & verse
	alternate	// Alternate title (optional)
){
	window.onunload =  function() { doUnload() }
	play();
	write("<h1>" + title + "</h1>");
	if (alternate != null) {
		write("<h2>" + alternate + "</h2>");
	}
	begin(quote, verse);
}
//------------------------------------------------
