// JavaScript Document
var isTiming = false;


function popVerse(svid, evid, ver) 
{
	terms = window.open("http://bibletools.org/index.cfm/fuseaction/Bible.popVerse/sVerseID/" + svid + "/eVerseID/" + evid + "/version/" + ver, "verse", "scrollbars=yes,resizable=yes,status=yes,width=450,height=350");
			self.verse = terms
}

function emailFriend(circuit,efa,ct,k,rtd,id,topic,sverseid,everseid,version,opt)
{
	var page = 'http://bibletools.org/index.cfm/fuseaction/Email.showEmailToFriend/circuit/' + circuit + '/efa/' + efa + '/CT/' + ct + '/k/' + k + '/rtd/' + rtd + '/id/' + id + '/topic/' + topic + '/sverseid/' + sverseid + '/everseid/' + everseid + '/version/' + version + '/opt/' + opt;
	var windowprops = "width=290,height=460,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no";
	
	window.open(page,"",windowprops);

}

function getCookie(name) {var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else begin += 2; var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length; return unescape(dc.substring(begin + prefix.length, end));
}


function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + '=' + escape(value) + ((expires) ? '; expires=' + expires.toGMTString() : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
	document.cookie = curCookie;
}




function setClickPath() {
}

function loadPageJS()
{
	bereanpopup();
	loadTypeAheadSuggest();
	//injectSuggestBehavior()
}

function bereanpopup() 
{		
	if (getCookie("popup") != "popped")
	{
		var expire = new Date((new Date()).getTime() + 1000 * 24 * 3600000);
		setCookie("accept", "oreo", expire, "/", "", false); 
	
		if (getCookie("accept"))
		{
			
			if(typeof showAd != "undefined")
			{
				showAd();
				setCookie("popup", "popped", expire, "/", "", false);
			}
			
		}
	
	}
}


function chanout() {
}


function showAd() {
	document.getElementById('bereanad').style.visibility = 'visible';
}

function hideAd() {
	document.getElementById('bereanad').style.visibility = 'hidden';
}

function showElement(element)
{
	var elem = document.getElementById(element);
	elem.style.display = 'block';
    elem.style.visibility = 'visible';
}

function hideElement(element)
{
	var elem = document.getElementById(element);
	elem.style.display = 'none';
   // elem.style.visibility = 'hidden';

}

function alignElements(bottomElement, topElement, xoffset, yoffset) {
	var element1 = document.getElementById(bottomElement);
	var element2 = document.getElementById(topElement);
	var selectedPosX = 0;
    var selectedPosY = 0;
       
    var element2Height = element2.offsetHeight;
	var element2Width = element2.offsetWidth;
	
    while(element2 != null){
          selectedPosX += element2.offsetLeft;
		  selectedPosY += element2.offsetTop;
		  element2 = element2.offsetParent;
     }
	element1.style.left = selectedPosX - xoffset + 'px';
	element1.style.top = selectedPosY - yoffset + 'px';
}

function changeChapterLeft() {
	var inputs = [];
    $('#BibleVerseInput :input', this).each(function() { inputs.push(this.name + '=' + escape(this.value)); })
	jQuery.ajax({type: "GET", data: inputs.join('&'), dataType: "html", url: "indexfb4.cfm?fuseaction=bible.showleft", timeout: 2000, error: function() {console.log("Failed to reach the server");}, success: function(r) {document.getElementById('chapterleftcontents').innerHTML = r;}})
}

function changeVerseAJAX() {
	document.getElementById('commentarycontents').innerHTML = '';
	var ajaxForm = document.getElementById('BibleVerseInput');
	var postData = '';
    postData = formData2QueryString(ajaxForm);
	var ajax = new Ajax();
	var commentaryhand = function(str) {
		document.getElementById('commentarycontents').innerHTML = str;
	}
		ajax.doGet('http://bibletools.org/indexfb4.cfm?fuseaction=Bible.showResourceForVerseAJAX&' + postData, commentaryhand);
	ajax = null;
	var ajax = new Ajax();
	var chapterhand = function(str) {
		document.getElementById('chaptercontents').innerHTML = str;
	}
		ajax.doGet('http://bibletools.org/indexfb4.cfm?fuseaction=Bible.showChapterForVerseAJAX&' + postData, chapterhand);
	
	hideElement('verseselectmenu');
	ajaxForm = null;
	ajax = null;
	chapterhand = null;
	commentaryhand = null;
	postData = null;
}

function showBibleVersionsMenu() {
	hideElement('concordancemenu');
	hideElement('verseselectmenu');
	alignElements('bibleversionsmenu', 'chapterleftnav', 30, 120);
	showElement('bibleversionsmenu');
}

function showConcordanceMenu() {
	hideElement('verseselectmenu');
	hideElement('bibleversionsmenu');
	alignElements('concordancemenu', 'chapternav', 55, 120);
	showElement('concordancemenu');
	document.getElementById('concordanceinput').focus();
}

function showResourceForVerse(verseID, opt, RTD) {
	
	$("#verseDetails"+verseID).load("indexfb4.cfm",{fuseaction:"Bible.showResourceForVerseAJAX",sVerseID:verseID,opt:opt,RTD:RTD}).show("slow");
	//showElement('verseDetails'+verseID);
	return false;
}

function showVerseOptionsMenu() {
	
	alignElements('verseoptionsmenu', 'commentarynav', 107, 105);
	showElement('verseoptionsmenu');
}

function HideBibleVersions(){
	hideElement('bibleversionsmenu');
	EraseTimeout();
}

function EraseTimeout(){
	clearTimeout(isTiming);
	isTiming = false;
}

function StartTimeoutBibleVersions(){
	if(isTiming)
		EraseTimeout();
	isTiming = setTimeout("HideBibleVersions()", 1000);
}
function showKeywords() {
	$("div#sermonKeywords").show();
}
function toggleDescription() {
	if ($('#descriptionLink').html() == 'show') {
		$("div#sermonDescription").show();
		$('#descriptionLink').html('hide');
	}
	else {
		$("div#sermonDescription").hide();
		$('#descriptionLink').html('show');
	}
}
function toggleKeywords() {
	if ($('#keywordsLink').html() == 'show') {
		$("div#sermonKeywords").show();
		$('#keywordsLink').html('hide');
	}
	else {
		$("div#sermonKeywords").hide();
		$('#keywordsLink').html('show');
	}
}