// 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(window.location.protocol + '//' + window.location.hostname + "/esv/default.aspx?ScriptureRef=" + sWork);
}

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;
	if (document.getElementById("Scripture").style.display == "block") 
	{
		$("#ScriptureCont").html("<br>&nbsp;Retrieving passage. <img src='/esv/WaitCirc.gif'><br></span>&nbsp;");
		$("#Scripture").redrawShadow();
	}
	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 (navigator.userAgent.indexOf('Firefox'))
		{
			xmlhttp.onload = xmlhttp.onerror = xmlhttp.onabort = function()
			{
				state_Change();
			}
		}
	}
	else
	{
		document.getElementById("ScriptureCont").innerHTML = "Your browser does not support JavaScript XMLHTTP.";
		$("#Scripture").redrawShadow();
	}
}

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) 
		{
			if (navigator.userAgent.indexOf('Firefox'))
			{
				MouseY = 0 + MouseY - 90;
			}
			else
			{
				MouseY = 0 + (MouseY - parseInt(oDiv.offsetHeight)) - 15;
			}
		}
		oDiv.style.left = MouseX + 'px';
		oDiv.style.top = MouseY + 'px';
		//oDiv.style.display = "block";	
		$("#Scripture").removeShadow();
		$("#Scripture").fadeIn();
		$("#Scripture").dropShadow({top:-4,left:-4,opacity:0.4,blur:3});

	}
}

function ScripClose()
{
	$("#Scripture").fadeOut("slow");
	$("#Scripture").removeShadow();
}

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;
}
//-->
