
// FOR NAV BUTTONS
var loading = "LOADING.................................................................";
var lTemp = "LOADING";
var loadingInterval = 0;
var insertRow = new CallBackObject();
var whomWhere = '';
insertRow.OnComplete = insertRow_Complete;
insertRow.OnLoading  = insertRow_Loading;
insertRow.OnError    = insertRow_Error;

function insertRow_Complete(responseText, responseXML) {
	document.getElementById('insertCell').innerHTML = responseText;
	clearInterval(loadingInterval);
	return;
}

function insertRow_Error(status, statusText, responseText) {
//	alert(responseText);
}

function insertRow_Loading() {
	loadingInterval = setInterval("Loading()", 150);
	setTimeout("document.getElementById('insertRow').style.display = '';", 150);
	return;
}

/*
	Cheap, but fancy Loading... to place in the insertRow until the real data arrives
*/
function Loading() {
	var theTemp = '<p class="loading">';
	if(lTemp.length > 20) lTemp = 'LOADING';
	lTemp += loading.charAt(lTemp.length);
	theTemp += lTemp;
	document.getElementById('insertCell').innerHTML = theTemp;
	return;
}


function pageLoader() {
//initialize our DHTML history
	dhtmlHistory.initialize();
	if(debug) log("<b>dhtmlHistory initialized</b>");

//subscribe to DHTML history change events
	dhtmlHistory.addListener(historyChange);
	if(debug) log("<b>dhtmlHistory listener created</b>");

// Bookmark handling
	var currentLocation = dhtmlHistory.getCurrentLocation();

	if(currentLocation !== "") {
// We are simply loading the basic page and being asked to anchor at #verse
		if(currentLocation.search(/\//) < 0 && currentLocation.replace(/[^\d]/g, '') !== '') {
			var anchorPoint = 'verse_'+currentLocation;
			document.getElementById(anchorPoint).scrollIntoView(true);
// We were also given a URL.i for insertWhat
			if(insertWhat !== "" && insertWhat !== "none") {
				startInsertRowCallback(insertWhat, currentLocation);
				historyStorage.put(currentLocation, insertWhat+'/'+currentLocation);
			}

// We are simply loading the basic page and being asked to anchor at #top
		} else if(currentLocation === 'top') {
			window.scroll(0,0);
// We are loading up from a bookmark i.e. #comm/16
		} else {
			var whom = currentLocation.split('/')[0];
			var where = currentLocation.split('/')[1];
			if(insertWhatList.search(whom) >= 0) {
				whomWhere = whom+'/'+where;
				startInsertHandler(whom, where);
			}
		}
	} else if(currentLocation == "" && insertWhat !== "" && insertWhat !== "none") {
			startInsertRowCallback(insertWhat, insertAtVerse);
			historyStorage.put(currentLocation, insertWhat+'/'+insertAtVerse);
	}
}

//  Form handler for the 'Change Version' drop-down on the Bibel pages
function chkVrsnChange(selObj) {

	if(selObj.options[selObj.selectedIndex].value.length <= 1) {
		selObj.selectedIndex=0;
		return false;
	}
	selObj.form.submit();
}

/*basic DHTML logging for our test page*/
function log(msg) {
	var logNode = document.getElementById("logWin");
	var content = "<p>" + msg + "</p>" + logNode.innerHTML;
	logNode.innerHTML = content;
}

function historyChange(newLocation, historyData) {
	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSON(historyData)
		: historyData
	);

	var msg = "<b>A history change has occured:</b> | newLocation=" + newLocation + " | historyData=" + historyMsg + " |";
	if(debug) log(msg);
	whomWhere = newLocation;
	if(historyData !== null) {
		var whom = historyData.split('/')[0];
		var where = historyData.split('/')[1];
		if(whom == 'fnt' || whom == 'fnv') { startFootNoteHandler(whom, where.split("_")[0], where.split("_")[1]); }
		else { startInsertRowCallback(whom, where); }
	} else {
		closeInserts('insertRow');
		var anchorPoint = 'verse_'+newLocation.replace(/[^\d]/g, '');
		try {
				document.getElementById(anchorPoint).scrollIntoView(true);
		} catch(e) { ; }

//		var whom = newLocation.split('/')[0];
//		var where = newLocation.split('/')[1];
//		startInsertRowCallback(whom, where);
	}
}

/*
	Our event handler when a user follows a footnote navigation
*/
startFootNoteHandler = function(whom, where, fn) {
	whomWhere = whom+'/'+where+'_'+fn;
	var anchorPoint = 'fnt_'+where+'_'+fn;
	if(whom=='fnv') anchorPoint = 'verse_'+where;
	document.getElementById(anchorPoint).scrollIntoView(true);
	dhtmlHistory.add(whomWhere, whomWhere);

	return false;
}

/*
	Our event handler to add history change events when a user
	clicks a nav button
*/
startInsertHandler = function(whom, where) {
	whomWhere = whom+'/'+where;
	startInsertRowCallback(whom, where);

	dhtmlHistory.add(whomWhere, whomWhere);

	var msg = "<b>A history event has been added:</b> [newLocation=" + whomWhere + " | historyData=" + whomWhere + "]";
	if(debug) log(msg);

	return false;
}

/*
	Move the hidden insertRow into place and begin the AJAX call to get fresh data
*/
function startInsertRowCallback(whom, where) {
	var link='/BibleAJAX/'+whom+'.cfm?b='+ShortName+'&c='+Chapter+'&v='+where+'&t='+Translation;

	if(whom == 'none') { return closeInserts('insertRow'); }

	moveToInsertRow(where);
	insertRow.DoCallBack('POST', link, '&this=that');
}

/*
	Hide the insertRow, move it to the new place in the table,
	and prepare the timer for onLoading() to handle
*/
function moveToInsertRow(where) {
	var tableRows = document.getElementById('table_bible').rows;
	var tbLength = tableRows.length;
	var anchorPoint = 'verse_'+where;
	var i=0;
	clearInterval(loadingInterval);
	document.getElementById('insertRow').style.display = 'none';
	document.getElementById(anchorPoint).scrollIntoView(true);
	for(i=0;i<tbLength;i++)
		if(tableRows[i].id == 'insertRow')
			document.getElementById("table_bible").moveRow(i, parseInt(where, 10)+wherePlus);
	lTemp = 'LOADING';
}

/*
	Just shut down the insertRow and change teh history item for the row
*/
function closeInserts(id) {
	document.getElementById(id).style.display='none';
	dhtmlHistory.add(whomWhere, null);
	return false;
}


// Form checking for the NAV button forms
function chkDictForm(form) {
  var objToChk = form.Dict.options[form.Dict.selectedIndex].value;
  if(objToChk == 0) return false;
  return true;
}
function chkImgForm(form) {
  var objToChk = form.Comm.options[form.Comm.selectedIndex].value;
  if(objToChk == 0) return false;
  location.href=objToChk;
  return false;
}
function chkCommForm(theForm) {
  var theValue = theForm.Comm.options[theForm.Comm.selectedIndex].value;
  if(theValue == 0) return false;
  if(theValue.search(/^AuthorID=[0-9]+/) == 0) { location.href='/audio_video/comm_topic.cfm?'+theValue; return false; }
  if(theValue.search(/^(\/Comm\/|\/study\/)/i) == 0) { location.href=theValue; return false; }
  return true;
}



// FOR TENSE DEFINITION
var tdObj = new CallBackObject();
tdObj.OnComplete = tdObj_Complete;
tdObj.OnError    = tdObj_Error;function tdObj_Complete(responseText, responseXML) {
	var workingObj = document.getElementById('tenseDefinition');
	workingObj.innerHTML = responseText;
	workingObj.style.display='';
}

function tdObj_Error(status, statusText, responseText) {
//	alert(responseText);
}

function getTenseDef(MyLink, MyData) {
	var link = MyLink;
	var theData = MyData;
	tdObj.DoCallBack('POST', link, theData);
	return false;
}

// FOR POINTED HEBREW DISPLAY OPTIONS
var HebObj = new CallBackObject();
HebObj.OnComplete = HebObj_Complete;
HebObj.OnError    = HebObj_Error;

function HebObj_Complete(responseText, responseXML) {
	var workingObj = document.getElementById('HebrewText');
	workingObj.innerHTML = responseText;
}

function HebObj_Error(status, statusText, responseText) {
//	alert(responseText);
}

function getHebDisplayOptions(MyLink, MyData) {
	var link = MyLink;
	var theData = MyData;
	HebObj.DoCallBack('POST', link, theData);
	return false;
}

document.body.onclick = function() {
	if(shrinkTimer != null)	setTimeout("shrinkInterval = setInterval(\"shrinkCopyOptions()\", 1)", 50);
	return true;
};
