/*	post-load markup for KJBP Bible chapter pages.
	http://av1611.com/kjbp/kjv-bible-text/index.html
*/

function highlightVerses()
{
	var hv = /\?vhl=(\d+)-?(\d+)?/i;
	var match = hv.exec(window.location.href);
	var startVerse;
	var endVerse;
	var elem;
	var scrollElem = 0;
	if (match) {
		startVerse = parseInt(match[1], 10);
		if (match[2]) {
			endVerse = parseInt(match[2], 10);
		} else {
			endVerse = startVerse;
		}
		if ((startVerse	>= 1) && (endVerse >= startVerse) && (endVerse <= 255)) {
			// highlights verse divs when URL contains ?vhl=startVerse[-endVerse]
			var v = 0;
			var vid = "";
			for (v = parseInt(startVerse, 10); v <= endVerse; v += 1) {
				vid = "verseNo" + v;
				elem = document.getElementById(vid);
				if (elem) {
					scrollElem = scrollElem || elem;
					elem.className = 'verseHighlighted';
				}
			}
		}
		if (scrollElem) {
			$.scrollTo(scrollElem,800,{offset:-200});
		}
	}
}

function addLoadEvent(f)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = f;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
		f();
		};
	}
}

//do highlighting when page is done loading.		
addLoadEvent(highlightVerses);

