//  C.McDonald April 2003
//
//=============================================================================

// Set constants.

var emmanuelEmail="contact-us1" + String.fromCharCode(64) +
			"salisburyemmanuel.org.uk";

var emmanuelRoot="http://www.salisburyemmanuel.org.uk";
if ( self.location.protocol == "file:" )
    emmanuelRoot="file:///c:/web-site";

// Ensure that content pages are displayed within the frame layout.

if ( !top.emmanuelFrameset )
    top.location.replace( emmanuelRoot + "/index.html?" +
				self.location.pathname );

// Set variable so that we return to the current page if the
// frameset is covered.

if ( top.contentFrame )
    top.contentRef=top.contentFrame.location.pathname;

// Set frameset title.

if ( document.title && self.location!=top.location )
	top.document.title = document.title;

//=============================================================================
//
//  Function to invoke the online bible for a given scripture reference.
//
//  book 	 - bible book name.
//  chapterVerse - reference in format Chapter:FirstVerse-LastVerse
//
function bibleRef( book, chapterVerse ) {

    var cv, vrange, i, href, refWindow, paras, curloc, ec;

    // Construct hyperlink reference from book name, chapter & first verse.

    cv = chapterVerse.split(":");
    href = emmanuelRoot + "/main/bible/" + book + cv[0] +".html";

    // Remove any previous verse highlights from this chapter.

    if ( top.bibleRefInfo && !top.bibleRefInfo.refWindow.closed ) {
	refWindow = top.bibleRefInfo.refWindow;
	curloc = refWindow.location.href;
	ec = curloc.indexOf('#');
	if ( ec >= 0 ) curloc = curloc.substring(0,ec);
	if ( curloc == href ) {
	    paras = refWindow.document.getElementsByTagName("p");
	    for ( i=0; i<paras.length; i++ ) {
	    	if ( paras[i] && paras[i].className &&
		    paras[i].className == "selectedVerse" )
			paras[i].className=null;
	    }
	}
    }

    // Display bible chapter in reference window.

    if ( cv.length > 1 ) {
	vrange = cv[1].split("-");
	href = href + "#v" + vrange[0];
    }
 
    refWindow = window.open( href, "BibleRef" );

    // Store details and queue function to highlight the verses for
    // the given reference.

    if ( refWindow.document.getElementById && cv.length > 1 ) {
	if ( vrange.length == 1 ) vrange[1]=vrange[0];

	top.bibleRefInfo = { refWindow:refWindow, href:href, retry:0,
	    firstVerse:parseInt(vrange[0]), lastVerse:parseInt(vrange[1]) };

	setTimeout( "bibleRefHighlight()", 500 );
    }

    refWindow.focus();
}

//=============================================================================
//
// Function to highlight a range of verses in an online bible chapter.
// This function is queued to run after the chapter has been loaded - it
// may need to be tried several times. 
//
function bibleRefHighlight() {

    var bibleRefInfo, refWindow, i, v, done;

    if ( !top.bibleRefInfo ) return;

    bibleRefInfo = top.bibleRefInfo;
    refWindow = bibleRefInfo.refWindow;
    if ( refWindow.closed ) return;

    if ( refWindow.location.href != bibleRefInfo.href ) {
	done = 0;
    } else {

        done = 1;

        for ( i=bibleRefInfo.firstVerse; i<=bibleRefInfo.lastVerse; i++ ) {
	    v = refWindow.document.getElementById("v"+i);
	    if ( v ) v.className = "selectedVerse";
	    else done = 0;
    	}
    }

    bibleRefInfo.retry++;
    if ( !done && bibleRefInfo.retry < 20 )
		window.setTimeout( "bibleRefHighlight()", 500 );
}

//=============================================================================
//
// Function to pop-up a bible text embedded in the document.
//
// evt		- mouse over event.
// refNo	- reference number in this document.
//
function bibleRefPopUp( evt, refNo ) {

    window.refTimer = null;

    if ( !document.bibleref ) {
	if ( !document.createElement ) return;
	document.bibleref = document.createElement( 'div' )
        document.bibleref.className = 'bibleTextPopUp';
    	document.bibleref.style.visibility = "hidden";
	document.body.insertBefore(document.bibleref,document.body.firstChild);
    }

    if ( document.bibleref.style.visibility == "visible" ||
	!window.bibleRefTexts || !window.bibleRefTexts[refNo] ) return;

    document.bibleref.innerHTML = window.bibleRefTexts[refNo];

    if ( evt.screenY > screen.availHeight*0.75 ) {
    	document.bibleref.style.top =
	    	( evt.clientY+document.body.scrollTop -
		document.bibleref.offsetHeight-10 ) + "px";
    } else {
    	document.bibleref.style.top =
	    	( evt.clientY+document.body.scrollTop+15 ) + "px";
    }

    document.bibleref.style.left = Math.max( 2,
		evt.clientX + document.body.scrollLeft
			- document.bibleref.offsetWidth+50) + "px";

    window.refTimer = setTimeout(
		'document.bibleref.style.visibility = "visible"', 500 );
}

//=============================================================================
//
// Function to remove pop-up a bible text.
//
function bibleRefPopDown() {

    if ( document.bibleref ) {
    	document.bibleref.style.visibility = "hidden";
	if ( window.refTimer ) clearTimeout( window.refTimer );
    }
}

