//document.onmousemove = captureMousePosition;
// windowResize seems to hang IE
//window.onresize = windowResize;
// Global variables
var xMousePos = 0; // Horizontal position of the mouse on the screen
var yMousePos = 0; // Vertical position of the mouse on the screen
var xMousePosMax = 0; // Width of the page
var yMousePosMax = 0; // Height of the page
var thisBook = new String("");
var extension = ".html";
var currentChapter = "1";
var userAgent =navigator.userAgent.toLowerCase();
var isGecko = (userAgent.indexOf('gecko') != -1);
var isIe = (userAgent.indexOf("msie")!=-1);
var darkBackground = "#3578bc";
var lightBackground = "#81addc";
var XHTML_NS = "http://www.w3.org/1999/xhtml";
var ieVersion = -1;
if (isIe)
{
    ieVersion = Number(userAgent.substr(userAgent.indexOf("msie") + 5, 3));
}

function docElement(elementName)
{
    var element = null;
    if (isIe == true)
    {
        var eName = 'element = document.all.' + elementName + ';';
        eval(eName);
        
    }
    else if (document.getElementById)
    {
        element = document.getElementById(elementName);
    }
    return element;
}

function setAttribute(element, attribute, value)
{
    if (isIe == true)
    {
        eval(element + "." + "attribute" + "=" + value + ";");
    }
    else
    {
        element.setAttribute(attribute,value);
    }
}

function getWindowWidth()
{
  var width = 0;
  if (top.window.innerWidth)
  {
        width = top.window.innerWidth;
  }
  else
  {
      width = parent.document.body.clientWidth;
  }
  return width;
}

function getWindowHeight()
{
  var height = 0;
  if (top.window.innerHeight)
  {
          height = top.window.innerHeight;
  }
  else
  {
      height = parent.document.body.clientHeight;
  }
  return height;
}

function captureMousePosition(e)
{
	try
	{
	    if (document.layers)
	    {
	        // When the page scrolls in Netscape, the event's mouse position
	        // reflects the absolute position on the screen. innerHight/Width
	        // is the position from the top/left of the screen that the user is
	        // looking at. pageX/YOffset is the amount that the user has
	        // scrolled into the page. So the values will be in relation to
	        // each other as the total offsets into the page, no matter if
	        // the user has scrolled or not.
	        xMousePos = e.pageX;
	        yMousePos = e.pageY;
	        xMousePosMax = window.innerWidth + window.pageXOffset;
	        yMousePosMax = window.innerHeight + window.pageYOffset;
	    }
	    else if (document.all)
	    {
	        // When the page scrolls in IE, the event's mouse position
	        // reflects the position from the top/left of the screen the
	        // user is looking at. scrollLeft/Top is the amount the user
	        // has scrolled into the page. clientWidth/Height is the height/
	        // width of the current page the user is looking at. So, to be
	        // consistent with Netscape (above), add the scroll offsets to
	        // both so we end up with an absolute value on the page, no
	        // matter if the user has scrolled or not.
			if (window)
			{
		        xMousePos = window.event.x + document.body.scrollLeft;
		        yMousePos = window.event.y + document.body.scrollTop;
		        xMousePosMax = document.body.clientWidth + document.body.scrollLeft;
		        yMousePosMax = document.body.clientHeight + document.body.scrollTop;
			}
	    }
	    else if (document.getElementById)
	    {
	        // Netscape 6 behaves the same as Netscape 4 in this regard
	        xMousePos = e.pageX;
	        yMousePos = e.pageY;
			if (window)
			{
		        xMousePosMax = window.innerWidth + window.pageXOffset;
		        yMousePosMax = window.innerHeight + window.pageYOffset;
			}
	    }
	}
	catch (exception)
	{
	}
}

// Strips space out of string
function stripSpace(book)
{
    var i;
    var spaceCount = 0;
    var striped = book;
    for (i=0; i<striped.length; i++)
    {
        if (striped.substring(i,i+1) == ' ')
        {
            spaceCount++;
            striped = striped.substring(0,i) + striped.substring(i+1,striped.length);
        }
    }
    return striped.substring(0,book.length - spaceCount);
}

// called by body.onload
function loaded(book)
{
  thisBook = book;
  chapterOnLoad();
}

// called from timeout to set location
function setLocation(location)
{
        window.location = location;
}

// called when window size changes to readjust position of components
function windowResize()
{
    // only use this if it is a bible book page
    if (docElement("chapters"))
    {
        if (isIe == true)
        {
            var chapterHeight = getWindowHeight() - 115;
            docElement("chapters").style.height = chapterHeight + "px";  
        }
    }
}
/*
function gotoBook(book)
{
    if (book == 'Contents')
    {
        window.location = 'index.html';
    }
    else if (book == 'Search')
    {
        if (isJsp())
        {
            window.location = '../NIVBibleSearch.html';
        }
        else
        {   
            window.location = '../SearchTop.html';
        }
    }
    else
    {
        window.location = stripSpace(book) + extension;
    }
    setBookSelected();
}*/

function gotoChapter(chapter,myChapter)
{
  currentChapter = chapter;
  setCurrentChapter(myChapter);
  showChapter(chapter);
  window.location = '#ch' + chapter;
  hideChapterList();
}

function gotoVerse(chapter, verse)
{
  showChapter(chapter);
  var chapNum = chapter;
  // work around for bug giving wrong chapter
  var oldLocation = new String(window.location);
  var extIndex = oldLocation.indexOf(".html");
  if (extIndex > -1)
  {
    chapNum = new Number(oldLocation.substring(extIndex - 3, extIndex));
    if (chapNum < 1) chapNum = chapter;
  }
  // end of work around
  //window.location = '#' + chapNum + ':' + verse;
  window.location = '#ch' + chapNum + 'v' + verse;
  hideVerseList(chapter);
  if (isIe && ieVersion > 6)
  {// correction done in css for ff
    document.documentElement.scrollTop -= 50;
  }
}

function toggleItem(item)
{
	if (item)
	{
	    if (item.style.display == 'none')
	    {
	        item.style.display = '';
	    }
	    else 
	    {
	        item.style.display = 'none';
	    }
	}
}

function showList(listId, buttonId)
{
    var list = document.getElementById(listId);
    var button = document.getElementById(buttonId);
    if (button && list)
    {
      if (list.style.display == 'none')
      {
          list.style.display = '';
          //button.style.borderStyle = 'inset'; 
          button.style.borderStyle = 'none';
          button.style.borderTopStyle = 'solid'; 
          button.style.borderBottomStyle = 'solid';
      }
    }
}

function toggleList(listId, buttonId)
{
    var list = document.getElementById(listId);
    var button = document.getElementById(buttonId);
    if (button && list)
    {
      if (list.style.display == 'none')
      {
          list.style.display = '';
          //button.style.borderStyle = 'inset'; 
          button.style.borderStyle = 'none';
          button.style.borderTopStyle = 'solid'; 
          button.style.borderBottomStyle = 'solid';
      }
      else 
      {
          list.style.display = 'none';
          //button.style.borderStyle = 'outset'; 
          button.style.borderStyle = 'none';
          button.style.borderBottomStyle = 'solid'; 
          button.style.borderTopStyle = 'none'; 
      }   
    }
}

function showChapter(chapter)
{
  var para = document.getElementById('ch' + chapter + 'para');
  if (para) para.style.display = '';
}

function toggleChapterDisplay(chapter)
{
  var para = document.getElementById('ch' + chapter + 'para');
  if (para) toggleItem(para);
}

function chooseBook() 
{
  showList('bookList','book');
  hideChapterList();
  hideVerseList(currentChapter);
  hideVersionList();
  hideSearch();
}

function chooseChapter() 
{
  showList('chapterList','chapter');
  hideBookList();
  hideVerseList(currentChapter);
  hideVersionList();
  hideSearch();
}

function hideSearch()
{
	hideList("searchForm","showSearch");
}
function showSearch()
{
  showList("searchForm","showSearch");
  hideBookList();
  hideVerseList(currentChapter);
  hideVersionList();
}

function chooseVerse() 
{
  showList('ch' + currentChapter + 'verses','verse');
  showList('verseList','verse');
  hideBookList();
  hideChapterList();
  hideVersionList();
  hideSearch();
}

function chooseVersion(bibleVer)
{
  showList('versionList','chooseVersion');
  var location = new String(window.location);
  var hashIndex = location.indexOf('#');
  var versions = ntVersions;
  if (location.indexOf('OT') > -1)
  {
    versions = otVersions;
  }
  if (hashIndex > -1)
  {
  	var verseRef = location.substring(hashIndex);
	for (var i = 0; i < versions.length; i++)
	{
		var versionLink = document.getElementById(versions[i]);
		if (versionLink)
		{
			var oldLink = versionLink.href;
			if (oldLink.indexOf('#')  > -1)
			{
				oldLink = oldLink.substring(0,oldLink.indexOf('#'));
			}
			versionLink.href = oldLink + verseRef;
		}
	}
	for (var j = 0; j < versions.length; j++)
	{
		var versionLink = document.getElementById(versions[j]);
		if (versionLink)
		{
			var oldLink = versionLink.href;
			if (oldLink.indexOf('#')  > -1)
			{
				oldLink = oldLink.substring(0,oldLink.indexOf('#'));
			}
			versionLink.href = oldLink + verseRef;
		}
	}
  }
  hideBookList();
  hideChapterList();
  hideVerseList(currentChapter);
  hideSearch();
}

function createVersionList(bibleVer, verName, book)
{
  var location = new String(window.location);
  var versionSuffix = "/html";
  if (location.indexOf(versionSuffix) == -1)
  {
  	versionSuffix = "&";
  }
  var thisVersion = bibleVer + versionSuffix;
  var versions = ntVersions;
  var names = ntVersionNames;
  var testament = "NT";
  if (location.indexOf('OT') > -1)
  {
    versions = otVersions;
    names = otVersionNames;
    testament = "OT";
  }
  
  var thisChapter = 1;
  var searchRoot = "";
  if (versionSuffix.length > 1)
  {
  	var bookStart = location.indexOf('html/') + 8; // include OT/ or NT/
  	var bookEnd = location.indexOf('/',bookStart);
  	var verseEnd = location.indexOf('.html');
  	thisChapter = new Number(location.substring(verseEnd - 3, verseEnd));
  	searchRoot = "../../../../";
  }
  else
  {
  	thisChapter = new Number(bibleUtils.getParameter("chapter"));
  }
  if (thisChapter <= 0) thisChapter = 1;
  var aAttrib = "onmouseover='mouseOverLink(this)' onmouseout='mouseOutLink(this)' ";
  for (var i = 0; i < versions.length; i++)
  {
    if (versions[i] != bibleVer)
    {
    	if (versionSuffix.length > 1)
    	{
      		var versionLocation = location.replace(thisVersion, versions[i] + versionSuffix);    
      		document.write("<a href='" + versionLocation + "' id='" + 
                     versions[i] + "' " + aAttrib + ">" + names[i] + "</a><br/>");
        }
        else
        {
        	document.write("<a href='bible.php?version=" + versions[i] +
        		"&amp;testament=" + testament + "&amp;book=" + book +
        		"&amp;chapter=" + thisChapter +
        		"' " + aAttrib + ">" + names[i] + "</a><br/>");
        }
      	document.write("<a href='javascript:chooseParallel(\"" + versions[i] + "\",\"" + bibleVer + "\");' " 
            + aAttrib + ">" + names[i] + "-" + verName + "</a><br/>");
    }
  }
  
  var bookRef = location.substring(bookStart, bookEnd) + "." + thisChapter;
  
  if (isLocal())
  {
    document.writeln("<form id='parallel' action='../../../../parallel.html' method='get'>");
  }
  else
  {
    document.writeln("<form id='parallel' action=" + searchRoot + "'search/parallel.php' method='post'>");
  }
  document.writeln("<input type='hidden' name='ref' value='" + bookRef + "'/>");
  document.writeln("<input type='hidden' name='leftVersion' value='Judson'/>");
  document.writeln("<input type='hidden' name='rightVersion' value='NIV'/>");
  document.writeln("<input type='hidden' name='ref' value='" + book + "." + thisChapter+ "'/>");
  document.writeln("</form>");
}


function hideLists()
{
    hideVersionList();
    hideBookList();
    hideChapterList();
    hideVerseList(currentChapter);
    // don't hide search onclick, since it interferes with search button
}

function hideList(listId, buttonId)
{
  var list = document.getElementById(listId);
  if (list) list.style.display = 'none';
  var button = document.getElementById(buttonId);
  if (button) {
    //button.style.borderStyle = 'outset'; 
    button.style.borderStyle = 'none';
    button.style.borderTopStyle = 'none'; 
    button.style.borderBottomStyle = 'solid'; 
  }
}

function hideVersionList()
{
  hideList('versionList','chooseVersion');
}

function hideBookList()
{
  hideList('bookList','book');
}

function hideChapterList()
{
  hideList('chapterList','chapter');
}

function hideVerseList(chapter)
{
  hideList('ch' + chapter + 'verses','verse');
  hideList('verseList','verse');
}


/*
function gotoChapter(book, chapter)
{
    window.location = thisBook + extension + '#' + chapter;
}

function gotoVerse(book, chapter, verse)
{
    var ch = new String(chapter);
    if (chapter == "-") ch = '0';
    //window.open(thisBook + extension + '#' + ch + ':' + verse,'_top');
    window.location = thisBook + extension + '#' + ch + ':' + verse;
}
*/

// called by onmouseover event of a bible verse
function setCurrentVerse(verse)
{
    var currentDisplay = document.getElementById('currentRef');
    var displayVerse = verse;
    if (verse.substring(0,2) == '0:')
    {
        displayVerse = verse.substring(2,verse.length);
    }
    if (currentDisplay)
    {
        while (currentDisplay.hasChildNodes())
        {
            currentDisplay.removeChild(currentDisplay.lastChild);
        }
        var currentText = document.createTextNode(displayVerse);
        currentDisplay.appendChild(currentText);
        if (typeof myUnicode != "undefined")
            myUnicode.parseNodeIfNeeded(currentDisplay);
    }
    window.status= thisBook + ' ' + displayVerse;
}

function setCurrentChapter(chapter)
{
    var currentDisplay = document.getElementById('currentChapter');
    var displayChapter = chapter;
    if (currentDisplay)
    {
        while (currentDisplay.hasChildNodes())
        {
            currentDisplay.removeChild(currentDisplay.lastChild);
        }
        var currentText = document.createTextNode(displayChapter);
        currentDisplay.appendChild(currentText);
    }
}

// called by onmouseover event of a footnote
function showFootnote(id)
{
  if (document.getElementById(id))
  {
    if (document.getElementById(id).style.display == 'none')
    {
        document.getElementById(id).style.display = '';
        // hide copyright to stop it getting confused with footnote
        //document.getElementById("footer").style.display = 'none';
    }
    else
    {
      document.getElementById(id).style.display = 'none';
    }
    setTimeout("hideFootnote('" + id + "')", 15000);
  }
}

// called by timeout on footer display
function hideFootnote(id)
{
    if (document.getElementById(id))
    {
        document.getElementById(id).style.display = 'none';
        //document.getElementById("footer").style.display = '';
    }
}

// ie version of changeChapter
function changeIeChapter(book,newChapter,newVerse)
{   
    var verse;
    var options = "<option selected='' value='-'>-</option>\n";

    for (verse =1; verse<=chapterArray[newChapter-1]; verse++)
    {
        if (verse == newVerse)
        {
            options = options + "<option selected='true' value='" + verse + "'>" + verse + "</option>";
        }
        else
        {
            options = options + "<option value='" + verse + "'>" + verse + "</option>";
        }
    }
    document.all.sSelectVerse.innerHTML = 
        "<select onchange='gotoVerse(document.all.selectBook.value, document.all.selectChapter.value,this.value,document)' id='selectVerse' class='selectVerse'>" + options + "</select>";
}

// function called from the chapter list box onChange event
function changeChapter(book,newChapter,newVerse)
{
    var verse;
    var newOption;
    var optionText;
    var verseList = document.getElementById("selectVerse");
    if (isIe == true)
    {
        changeIeChapter(book,newChapter,newVerse)
    }
    else
    {
        while (verseList.lastChild != null)
        {
            verseList.removeChild(verseList.lastChild);
        }
        newOption = document.createElement("option");
        newOption.setAttribute("value","-");
        optionText = document.createTextNode("-");
        //newOption.setAttribute("selected","");
        newOption.appendChild(optionText);
        verseList.appendChild(newOption);

        for (verse =1; verse<=chapterArray[newChapter-1]; verse++)
        {
            newOption = document.createElement("option");
            newOption.setAttribute("value",verse);
            if (verse == newVerse)
            {
                newOption.setAttribute("selected","");
            }
            optionText = document.createTextNode(verse);
            newOption.appendChild(optionText);
            verseList.appendChild(newOption);
        }
    }
    gotoChapter(book,newChapter);
}

// Function called by onload to set contents of chapter list
function setChapterList()
{
    var theDoc = document;
    // now change the chapter list
    // first remove the old verse list
    verseList = theDoc.getElementById("selectVerse");
    while (verseList.lastChild != null)
    {
        verseList.removeChild(verseList.lastChild);
    }
    // remove the old chapter list
    chapterList = theDoc.getElementById("selectChapter");
    while (chapterList.lastChild != null)
    {
        chapterList.removeChild(chapterList.lastChild);
    }
    // general chapter option stuff
    newOption = theDoc.createElement("option");
    optionText = theDoc.createTextNode("-");
    newOption.setAttribute("value","-");
    newOption.setAttribute("selected","");
    newOption.appendChild(optionText);
    chapterList.appendChild(newOption);
    // general verse option stuff
    newOption = theDoc.createElement("option");
    optionText = theDoc.createTextNode("-");
    newOption.setAttribute("value","-");
    newOption.setAttribute("selected","");
    newOption.appendChild(optionText);
    verseList.appendChild(newOption);
    // only create list if more that one chapter
    if (chapterArray.length>1)
    {
        for (var chapter =1; chapter<=chapterArray.length; chapter++)
        {
            newOption = theDoc.createElement("option");
            newOption.setAttribute("value",chapter);
            optionText = theDoc.createTextNode(chapter);
            newOption.appendChild(optionText);
            chapterList.appendChild(newOption);
        }
    }
    else if (chapterArray.length==1)
    {
        // if no chapters need to set verse options now
        for (verse =1; verse<=chapterArray[0]; verse++)
        {
            newOption = theDoc.createElement("option");
            newOption.setAttribute("value",verse);
            optionText = theDoc.createTextNode(verse);
            newOption.appendChild(optionText);
            verseList.appendChild(newOption);
        }
    }
}

// sets the array of the number of verses per chapter
// the length of the array gives the number of chapters
// the values in the array are the number of verses for a given chapter index
function setChapterArray(book)
{
    // while loop used as a means to escape once a match is found
    // without using if else. It is never actually looped.
    while (true)
    {
        if (book == "1Chronicles") {chapterArray = new Array(54,55,24,43,26,81,40,40,44,14,47,40,14,17,29,43,27,17,19,8,30,19,32,31,31,32,34,21,30); break;}
        if (book == "1Corinthians") {chapterArray = new Array(31,16,23,21,13,20,40,13,27,33,34,31,13,40,58,24); break;}
        if (book == "1John") {chapterArray = new Array(10,29,24,21,21); break;}
        if (book == "1Kings") {chapterArray = new Array(53,46,28,34,18,38,51,66,28,29,43,33,34,31,34,34,24,46,21,43,29,53); break;}
        if (book == "1Peter") {chapterArray = new Array(25,25,22,19,14); break;}
        if (book == "1Samuel") {chapterArray = new Array(28,36,21,22,12,21,17,22,27,27,15,25,23,52,35,23,58,30,24,42,15,23,29,22,44,25,12,25,11,31,13); break;}
        if (book == "1Thessalonians") {chapterArray = new Array(10,20,13,18,28); break;}
        if (book == "1Timothy") {chapterArray = new Array(20,15,16,16,25,21); break;}
        if (book == "2Chronicles") {chapterArray = new Array(17,18,17,22,14,42,22,18,31,19,23,16,22,15,19,14,19,34,11,37,20,12,21,27,28,23,9,27,36,27,21,33,25,33,27,23); break;}
        if (book == "2Corinthians") {chapterArray = new Array(24,17,18,18,21,18,16,24,15,18,33,21,14); break;}
        if (book == "2John") {chapterArray = new Array(); chapterArray[0]=13; break;}
        if (book == "2Kings") {chapterArray = new Array(18,25,27,44,27,33,20,29,37,36,21,21,25,29,38,20,41,37,37,21,26,20,37,20,30); break;}
        if (book == "2Peter") {chapterArray = new Array(21,22,18); break;}
        if (book == "2Samuel") {chapterArray = new Array(27,32,39,12,25,23,29,18,13,19,27,31,39,33,37,23,29,33,43,26,22,51,39,25); break;}
        if (book == "2Thessalonians") {chapterArray = new Array(12,17,18); break;}
        if (book == "2Timothy") {chapterArray = new Array(18,26,17,22); break;}
        if (book == "3John") {chapterArray = new Array(); chapterArray[0]=14; break;}
        if (book == "Acts") {chapterArray = new Array(26,47,26,37,42,15,60,39,43,48,30,25,52,28,40,40,34,28,41,38,40,30,35,26,27,32,44,31); break;}
        if (book == "Amos") {chapterArray = new Array(15,16,15,13,27,14,17,14,15); break;}
        if (book == "Colossians") {chapterArray = new Array(29,23,25,18); break;}
        if (book == "Daniel") {chapterArray = new Array(21,49,30,37,31,28,28,27,27,21,45,13); break;}
        if (book == "Deuteronomy") {chapterArray = new Array(46,37,29,49,33,25,26,20,29,22,32,32,18,29,23,22,20,22,21,20,23,30,25,22,19,19,26,68,29,20,30,52,29,12); break;}
        if (book == "Ecclesiastes") {chapterArray = new Array(18,26,22,16,20,12,29,17,18,20,10,14); break;}
        if (book == "Ephesians") {chapterArray = new Array(23,22,21,32,33,24); break;}
        if (book == "Esther") {chapterArray = new Array(22,23,15,17,14,14,10,17,32,3); break;}
        if (book == "Exodus") {chapterArray = new Array(22,25,22,31,23,30,25,32,35,29,10,51,22,31,27,36,16,27,25,26,36,31,33,18,40,37,21,43,46,38,18,35,23,35,35,38,29,31,43,38); break;}
        if (book == "Ezekiel") {chapterArray = new Array(28,10,27,17,17,14,27,18,11,22,25,28,23,23,8,63,24,32,14,49,32,31,49,27,17,21,36,26,21,26,18,32,33,31,15,38,28,23,29,49,26,20,27,31,25,24,23,35); break;}
        if (book == "Ezra") {chapterArray = new Array(11,70,13,24,17,22,28,36,15,44); break;}
        if (book == "Galatians") {chapterArray = new Array(24,21,29,31,26,18); break;}
        if (book == "Genesis") {chapterArray = new Array(31,25,24,26,32,22,24,22,29,32,32,20,18,24,21,16,27,33,38,18,34,24,20,67,34,35,46,22,35,43,55,32,20,31,29,43,36,30,23,23,57,38,34,34,28,34,31,22,33,26); break;}
        if (book == "Habakkuk") {chapterArray = new Array(17,20,19); break;}
        if (book == "Haggai") {chapterArray = new Array(15,23); break;}
        if (book == "Hebrews") {chapterArray = new Array(14,18,19,16,14,20,28,13,28,39,40,29,25); break;}
        if (book == "Hosea") {chapterArray = new Array(11,23,5,19,15,11,16,14,17,15,12,14,16,9); break;}
        if (book == "Isaiah") {chapterArray = new Array(31,22,26,6,30,13,25,22,21,34,16,6,22,32,9,14,14,7,25,6,17,25,18,23,12,21,13,29,24,33,9,20,24,17,10,22,38,22,8,31,29,25,28,28,25,13,15,22,26,11,23,15,12,17,13,12,21,14,21,22,11,12,19,12,25,24); break;}
        if (book == "James") {chapterArray = new Array(27,26,18,17,20); break;}
        if (book == "Jeremiah") {chapterArray = new Array(19,37,25,31,31,30,34,22,26,25,23,17,27,22,21,21,27,23,15,18,14,30,40,10,38,24,22,17,32,24,40,44,26,22,19,32,21,28,18,16,18,22,13,30,5,28,7,47,39,46,64,34); break;}
        if (book == "Job") {chapterArray = new Array(22,13,26,21,27,30,21,22,35,22,20,25,28,22,35,22,16,21,29,29,34,30,17,25,6,14,23,28,25,31,40,22,33,37,16,33,24,41,30,24,34,17); break;}
        if (book == "Joel") {chapterArray = new Array(20,32,21); break;}
        if (book == "John") {chapterArray = new Array(51,25,36,54,47,71,53,59,41,42,57,50,38,31,27,33,26,40,42,31,25); break;}
        if (book == "Jonah") {chapterArray = new Array(17,10,10,11); break;}
        if (book == "Joshua") {chapterArray = new Array(18,24,17,24,15,27,26,35,27,43,23,24,33,15,63,10,18,28,51,9,45,34,16,33); break;}
        if (book == "Jude") {chapterArray = new Array(); chapterArray[0]=25; break;}
        if (book == "Judges") {chapterArray = new Array(36,23,31,24,31,40,25,35,57,18,40,15,25,20,20,31,13,31,30,48,25); break;}
        if (book == "Lamentations") {chapterArray = new Array(22,22,66,22,22); break;}
        if (book == "Leviticus") {chapterArray = new Array(17,16,17,35,19,30,38,36,24,20,47,8,59,57,33,34,16,30,37,27,24,33,44,23,55,46,34); break;}
        if (book == "Luke") {chapterArray = new Array(80,52,38,44,39,49,50,56,62,42,54,59,35,35,32,31,36,43,48,47,38,71,55,53); break;}
        if (book == "Malachi") {chapterArray = new Array(14,17,18,6); break;}
        if (book == "Mark") {chapterArray = new Array(45,28,35,41,43,56,36,38,48,52,32,44,37,72,46,20); break;}
        if (book == "Matthew") {chapterArray = new Array(25,23,17,25,48,34,29,34,38,42,30,50,58,36,39,28,26,34,30,34,46,46,38,51,46,75,66,20); break;}
        if (book == "Micah") {chapterArray = new Array(16,13,12,13,15,16,20); break;}
        if (book == "Nahum") {chapterArray = new Array(15,13,19); break;}
        if (book == "Nehemiah") {chapterArray = new Array(11,20,32,23,19,19,73,18,38,39,36,47,31); break;}
        if (book == "Numbers") {chapterArray = new Array(54,34,51,49,31,27,89,26,23,36,35,16,33,45,41,50,13,32,22,29,35,41,30,25,18,65,23,31,40,16,54,42,56,29,34,13); break;}
        if (book == "Obadiah") {chapterArray = new Array(); chapterArray[0]=21; break;}
        if (book == "Philemon") {chapterArray = new Array(); chapterArray[0]=25; break;}
        if (book == "Philippians") {chapterArray = new Array(30,30,21,23); break;}
        if (book == "Proverbs") {chapterArray = new Array(33,22,35,27,23,35,27,36,18,32,31,28,25,35,33,33,28,24,29,30,31,29,35,34,28,28,27,28,27,33,31); break;}
        if (book == "Psalms") {chapterArray = new Array(6,12,8,8,12,10,17,9,20,18,7,8,6,7,5,11,15,50,14,9,13,31,6,10,22,12,14,9,11,12,24,11,22,22,28,12,40,22,13,17,13,11,5,26,17,11,9,14,20,23,19,9,6,7,23,13,11,11,17,12,8,12,11,10,13,20,7,35,36,5,24,20,28,23,10,12,20,72,13,19,16,8,18,12,13,17,7,18,52,17,16,15,5,23,11,13,12,9,9,5,8,28,22,35,45,48,43,13,31,7,10,10,9,8,18,19,2,29,176,7,8,9,4,8,5,6,5,6,8,8,3,18,3,3,21,26,9,8,24,13,10,7,12,15,21,10,20,14,9,6); break;}
        if (book == "Revelation") {chapterArray = new Array(20,29,22,11,14,17,17,13,21,11,19,17,18,20,8,21,18,24,21,15,27,21); break;}
        if (book == "Romans") {chapterArray = new Array(32,29,31,25,21,23,25,39,33,21,36,21,14,23,33,26); break;}
        if (book == "Ruth") {chapterArray = new Array(22,23,18,22); break;}
        if (book == "SongOfSolomon") {chapterArray = new Array(17,17,11,16,16,13,13,14); break;}
        if (book == "Titus") {chapterArray = new Array(16,15,15); break;}
        if (book == "Zechariah") {chapterArray = new Array(21,13,10,14,11,15,14,23,17,12,17,14,9,21); break;}
        if (book == "Zephaniah") {chapterArray = new Array(18,15,20); break;}
        if (book == "Contents") {chapterArray = new Array(); break;}
        // default to avoid loop
        chapterArray = new Array();
        window.system = "Unknown book: " + book;
        break;
    }
}

function setParallelRef(ref)
{
  var theForm = document.forms[0];
  theForm.ref.value = ref;
  theForm.submit();
}

function isJsp()
{
    // assume that if this is being run off a web server then it is the jsp
    // version. Ofcourse if its not a jsp server then this will produce
    // invalid results but the search will be very slow if run from a normal
    // server anyway
    var location = new String(window.location);
    if (location.substring(0,4) == "http") return true;
    return false;
}

function indexLoaded()
{   
    if (!isJsp())
    {
        var search = document.getElementById("search");
        search.style.display="none";
        document.getElementById("searchLink").href="../SearchTop.html";
    }
}

function isLocal()
{
	var pageUrl = new String(window.location);
	if (pageUrl.indexOf('file') > -1)
		return true;
	return false;
}

function chapterOnLoad()
{
  if (isLocal())
  {
       var theForm = document.getElementById('search');
       if (theForm)
       {
         theForm.setAttribute('action', "../../../../bibleSearch.html");
         theForm.setAttribute('method', "get");
       }
       var theBibleIndex = document.getElementById('biblesIndex');
       if (theBibleIndex)
       {
         theBibleIndex.setAttribute('href','../../../../index.html');
       }
  }
  if (isIe && ieVersion > 6)
  {// fixed header offset for ie, ff uses css
    var loc = new String(window.location);
    var hash = loc.indexOf("#ch");
    if (hash > -1)
    {
        setTimeout("document.documentElement.scrollTop -= 50;", 100);
    }
  }
}

function onloadMainIndex()
  {
    // hack to fix links in offline version
    if (isLocal())
    {
      var padaukLink = '../installers/PadaukOT-2.1.exe';
      var doulosLink = '../installers/DoulosSIL4.0.14.r1.exe';
      var firefoxLink = '../installers/firefox-1.5.0.6-gr2.en-US.win32.installer.exe';
      var homeLink = '../readme.html';
      var fixLink = document.getElementById("padaukLink1");
      if (fixLink) fixLink.setAttribute('href',padaukLink);
      fixLink = document.getElementById("padaukLink2");
      if (fixLink) fixLink.setAttribute('href',padaukLink);
      fixLink = document.getElementById("padaukLink3");
      if (fixLink) fixLink.setAttribute('href',padaukLink);
      fixLink = document.getElementById("firefoxLink1");
      if (fixLink) fixLink.setAttribute('href',firefoxLink);
      fixLink = document.getElementById("firefoxLink2");
      if (fixLink) fixLink.setAttribute('href',firefoxLink);
      fixLink = document.getElementById("homeLink");
      if (fixLink) fixLink.setAttribute('href',homeLink);
      fixLink = document.getElementById("doulosLink1");
      if (fixLink) fixLink.setAttribute('href',doulosLink);

    }
  }

function indexOnLoad()
{
  // change the form action to be java script not php
  //var pageUrl = new String(window.location);
  //if (pageUrl.indexOf('file') > -1)
  if (isLocal())
  {
    var theForm = document.getElementById('search');
    if (theForm)
    {
      theForm.setAttribute('action', "../../bibleSearch.html");
      theForm.setAttribute('method', "get");
    }
	// several links also need fixing
    var theLink = document.getElementById('searchLink');
    if (theLink)
    {
      theLink.setAttribute('href', "../../bibleSearch.html");
    }
    theLink = document.getElementById('chooseVersion');
    if (theLink)
    {
      theLink.setAttribute('href', "../../index.html");
    }
    theLink = document.getElementById('titleLink');
    if (theLink)
    {
      theLink.setAttribute('href', "../../index.html");
    }
    theLink = document.getElementById('biblesIndex');
    if (theLink)
    {
      theLink.setAttribute('href', "../../index.html");
    }
  }
}


function feedback(textLink)
{
  // some obfuscation to reduce spam
  document.writeln("<a href='ma" + "ilto:feedback" 
                   + "@" + 
                   "myanmar" + "bible.com' class='feedback'>" 
                   + textLink + "</a>");
}


function appendCharacter(inputId, newChar)
{
  // append a character to the input box
  var inputElement = document.getElementById(inputId);
  var oldText = inputElement.value;
  inputElement.value = oldText + newChar; 
}

function chooseParallel(lVersion, rVersion)
{
  // we may have 2 forms on the page
  var theForm = document.forms[0];
  if (theForm && (! theForm.leftVersion))
  {
    theForm = document.forms[1];
  }
  if (theForm)
  {
    if (isLocal())
    {
		var pf = document.getElementById('parallelForm');
		if (pf) 
		{
			pf.setAttribute('action','parallel.html');
			pf.setAttribute('method','get');
		}
        document.location = theForm.action + "#leftVersion=" + lVersion + "&rightVersion=" +
            rVersion + "&ref=" + theForm.ref.value;
	}
    else
    {
        theForm.leftVersion.value = lVersion;
        theForm.rightVersion.value = rVersion;
        theForm.submit();
    }
  }
}

function fixLinks()
{
	if (isLocal())
	{
		var theLink = document.getElementById('titleLink');
		if (theLink) theLink.setAttribute('href','index.html');
		theLink = document.getElementById('returnLink');
		if (theLink) theLink.setAttribute('href','index.html');
	}
}

function removeFootnotes()
{
    //try
    {
        var spanNodes = document.getElementsByTagName("span");
        var i = 0;
		
        for (i = 0; i<spanNodes.length; i++)
        {
            var span = spanNodes.item(i);
			//alert(span.getAttribute("class"));
            if (String(span.getAttribute("class")).indexOf("footnote") > -1 ||
			    String(span.getAttribute("className")).indexOf("footnote") > -1)
            {
                var star = span.previousSibling;
                var sParent = span.parentNode;
				star.innerHTML = "";
				span.innerHTML = "";
                sParent.removeChild(star);
                sParent.removeChild(span);
                // the removal invalidates the nodelist, so re load it
                i = 0;
                spanNodes = document.getElementsByTagName("span");
            }
        }
        var removeLink = document.getElementById("removeFootnotes");
        if (removeLink)
        {
            var removeLinkParent = removeLink.parentNode;
            removeLinkParent.removeChild(removeLink);
        }
    }
    //catch (e)
    {}
}

function showOsisChapter(osisId)
{
    if (document.getElementById(osisId))
    {
	    document.getElementById(osisId).style.display = "";
	    document.getElementById(osisId + "plus").style.display = "none";
	    document.getElementById(osisId + "minus").style.display = "";
	}
}

function scaleView()
{
    var rect = bibleUtils.windowSize();
    var body = document.getElementById("chBody");
    if (body)
    {
        var bodyRect = bibleUtils.getPos(body);
        var oldHeight = body.style.height;
        var margin = 115;// includes padding on chBody
        var scrollTitleHeight = 20;
        var sideMargin = 75;
        var newHeight = (rect.height - margin) + "px";
        if (oldHeight != newHeight)
        {
            body.style.height = newHeight;
            var newHeight = (rect.height - sideMargin) + "px";
            // slighlly smaller for scrollable lists excluding title
            var bookDiv = document.getElementById("bookDiv");
            if (bookDiv) bookDiv.style.height = newHeight;
            var versionDiv = document.getElementById("versionDiv");
            if (versionDiv) versionDiv.style.height = newHeight;
	        newHeight = (rect.height - sideMargin - scrollTitleHeight) + "px";
            var versionDivScroll = document.getElementById("versionScroll");
            if (versionDivScroll) versionDivScroll.style.height = newHeight;
            var bookDivScroll = document.getElementById("bookScroll");
            if (bookDivScroll) bookDivScroll.style.height = newHeight;
        }
    }
}


function checkRefShowing()
{
    var url = new String(document.location);
    var osisStart = url.indexOf("#");
    if (osisStart)
    {
        var osisRef = url.substring(osisStart + 1);
        var dot1 = osisRef.indexOf(".");
        var dot2 = osisRef.lastIndexOf(".");
        if (dot1 != dot2)
        {
            showOsisChapter(osisRef.substring(0, dot2));
            var verseTag = document.getElementById(osisRef);
            if (verseTag)
            {
                //verseTag.style.backgroundColor = "green";
            }
            //document.location = url;
        }
        else
        {
            //alert(url.substring(osisStart + 1));
            showOsisChapter(osisRef);
        }
    }
}

function xmlVersionList(thisVersion, testament, bookFile)
{
  var listDiv = document.getElementById("versionScroll");
  var versions = ntVersions;
  for (var i = 0; i < versions.length; i++)
  {
    if (versions[i] != thisVersion)
    {
      var versionLocation = "../../../" + versions[i] + "/osis/" + testament + 
      	"/" + bookFile + ".xml";
      //new String(location).replace(thisVersion, versions[i]);
      
      var a, br;
      if (document.createElementNS) a = document.createElementNS(XHTML_NS,"a");
      else a = document.createElement("a");
      var parallelJs = "javascript:chooseParallel('" + versions[i] + "','" + thisVersion + "');";
      a.setAttribute("href",parallelJs);
      a.setAttribute("title",versions[i] + " - " + thisVersion);
      a.appendChild(document.createTextNode("||"));
      listDiv.appendChild(a);
      
      listDiv.appendChild(document.createTextNode(" "));
      if (document.createElementNS) a = document.createElementNS(XHTML_NS,"a");
      else a = document.createElement("a");
      a.appendChild(document.createTextNode(versions[i]));
      a.setAttribute("href",versionLocation);
      a.setAttribute("title",versions[i]);
      listDiv.appendChild(a);
      //if (document.createElementNS) br = document.createElementNS(XHTML_NS,"br");
      //else br = document.createElement("br");
      //listDiv.appendChild(br);
      //listDiv.appendChild(document.createTextNode(" "));
      if (document.createElementNS) br = document.createElementNS(XHTML_NS,"br");
      else br = document.createElement("br");
      listDiv.appendChild(br);
      
    }
  }
}

function mouseOverLink(l)
{
	l.style.backgroundColor = "white";
}

function mouseOutLink(l)
{
	l.style.backgroundColor = lightBackground;
}

