// JavaScript Document
var IE = document.all?true:false;
var rv = 0;
if (IE) 
{
	var ua = navigator.userAgent;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null)
	rv = parseFloat( RegExp.$1 );
}
else
{
	document.captureEvents(Event.MOUSEMOVE)
}

document.onmousemove = getMouseXY;
var MouseX = 0;
var MouseY = 0;
var xmlhttp;

function ParseMe()
{
	//Written by Martin Jongerius and Paul Wasilkoff
	//(c) 2005 - United Church of God, an International Association
	var sWork = document.getElementById("Body").innerHTML;
	var oRegEx = /([1-3]\s)?(Genesis|Exodus|Leviticus|Numbers|Deuteronomy|Joshua|Judges|Ruth|Samuel|Kings|Chronicles|Ezra|Nehemiah|Esther|Job|Psalms|Psalm|Proverbs|Ecclesiastes|Song of Solomon|Isaiah|Jeremiah|Lamentations|Ezekiel|Daniel|Hosea|Joel|Amos|Obadiah|Jonah|Micah|Nahum|Habakkuk|Zephaniah|Haggai|Zechariah|Malachi|Matthew|Mark|Luke|John|Acts|Romans|Corinthians|Galatians|Ephesians|Philippians|Colossians|Thessalonians|Timothy|Titus|Philemon|Hebrews|James|Peter|Jude|Revelation)\s+(\d{1,3}:\d{1,3}[-]{0,}\d{0,3})/gi;
	
	sWork = sWork.replace(oRegEx, "<a href='javascript:showScripture(\"$1\",\"$2\",\"$3\");'>$1$2 $3</a>");
	document.getElementById("Body").innerHTML = sWork;
	BuildThem();
}

function showScripture(Numeral, Book, ChapVers)
{
	var sWork = Numeral.replace(/\W{0,}/, "") + Book + " " + ChapVers;
	//getXML("http://www.ucg.org/resources/esv/esv.asp?ScriptureRef=" + Numeral.replace(/\W{0,}/, "") + Book + ChapVers);
	getXML("/esv/default.asp?ScriptureRef=" + sWork);
	//getXML("http://www.esvapi.org/v2/rest/passageQuery?key=62965db9e61802d6&passage=" + Numeral.replace(/\W{0,}/, "") + Book + ChapVers)
}

function getXML(url)
{
	//document.getElementById("ScriptureCont").innerHTML = xmlhttp.responseText;
	//document.getElementById("ScriptureCont").innerHTML = "<div id='Error'><p>&nbsp;</p><p>Temporarily Unavailable</p></div>";
	document.getElementById("Scripture").style.left = MouseX;
	document.getElementById("Scripture").style.top = MouseY;
	document.getElementById("Scripture").style.display = "block";
	document.getElementById("ScriptureCont").innerHTML = "<br>&nbsp;Retrieving passage. <img src='/esv/WaitCirc.gif'><br></span>&nbsp;";
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
		// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
		if (xmlhttp.onreadystatechange == null) state_Change();
	}
	else
	{
		document.getElementById("ScriptureCont").innerHTML = "Your browser does not support JavaScript XMLHTTP.";
	}
}

function state_Change()
{
	var sWork;
	if (xmlhttp.readyState==4)
	{
		// 4 = "loaded"
		if (xmlhttp.status==200)
		{
			// 200 = OK
			sWork = xmlhttp.responseText;
		}
		else
		{
			sWork = "Problem retrieving passage.";
		}
		document.getElementById("ScriptureCont").innerHTML = sWork
		var iWidth, iHeight
		if (navigator.appName.indexOf("Microsoft")!=-1)
		{
			iWidth = document.body.offsetWidth;
			iHeight = document.body.offsetHeight;
		}
		else
		{
			iWidth = window.innerWidth;
			iHeight = window.innerHeight;
		}
		//iWidth = iWidth + document.body.scrollLeft;
		iHeight = iHeight + document.body.scrollTop;
	
		var oDiv = document.getElementById("Scripture");
		oDiv.style.width = '180px';
		if (MouseX + oDiv.offsetWidth > iWidth) MouseX = 0 + (iWidth - parseInt(oDiv.offsetWidth)) - 20;
		if (MouseY + oDiv.offsetHeight > iHeight) MouseY = 0 + (iHeight - oDiv.offsetHeight) - 15;
		oDiv.style.left = MouseX;
		oDiv.style.top = MouseY;
		oDiv.style.display = "block";
	}
}

function ScripClose()
{
	document.getElementById("Scripture").style.display = "none";
}

function BuildThem()
{
	creatediv('Scripture', '', '');
	creatediv('ScriptureClose','','<a href="javascript:ScripClose();" title="Close">X</a>');
	creatediv('ScriptureCont','','');
	ScripClose();
}
	

function creatediv(id, myClass, html)
{ 

	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', id);
	newdiv.setAttribute('className', myClass);
	
	if (html) {newdiv.innerHTML = html;}
	
	if (id!="Scripture") 
	{
		document.getElementById("Scripture").appendChild(newdiv);
	}
	else
	{
		document.body.appendChild(newdiv);
	}
}

function getMouseXY(e)
{
//	if (IE) 
//	{
//		// grab the x-y pos.s if browser is IE
//		if (!e) e = window.event;
//		MouseX = e.clientX + document.body.scrollLeft;
//		MouseY = e.clientY + document.body.scrollTop;
//	}
//	else
//	{
//		// grab the x-y pos.s if browser is NS
//		MouseX = e.pageX;
//		MouseY = e.pageY;
//	}
    e = e || window.event;
    if (e.pageX || e.pageY) {
        MouseX = e.pageX;
        MouseY = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        MouseX = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        MouseY = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
	return true;
}
//-->