// JavaScript Document
// --------- index handling --------
function openClose(e) {
evt = e || window.event;
evtTarget = evt.target || evt.srcElement;
tid=evtTarget.id;
divid=tid.substring(1);
 if (document.getElementById(divid).style.display == "block") {
     document.getElementById(divid).style.display = "none";
     document.getElementById(tid).innerHTML = "+"; 
	 }
 else {
     document.getElementById(divid).style.display = "block";
     document.getElementById(tid).innerHTML = "-"; 
	 } 
}
	 
function showDetail(e) {
evt = e || window.event;
evtTarget = evt.target || evt.srcElement;
secclass=evtTarget.className;
// test if section is available
if (secclass.substring(0,4)!='noty'){
// set tab title
document.getElementById("panel").innerHTML = "<p class='load'>Loading</p>";
tab=evtTarget.innerHTML;
y=document.getElementById("cont");
y.innerHTML=tab;
// fetch text to display
txt='/Texts/Manuscript/'+tab+'.php';
makeRequest(txt);
TabbedPanels1.showPanel(2);}
}

function setupTabs() {
ai=document.getElementById('mindx');
ab=ai.getElementsByTagName('span');
  for (i=0;i<ab.length;i++){
	  stag=ab[i].innerHTML;
	  if (stag!='+'){
		  ab[i].onclick=showDetail;
		  }
		else {
			ab[i].onclick=openClose;
		}
  }
}
// ------------ end of index handling ---------

// --------- collapsible section handling ---------
function findPos(obj){
var curt = 0;
if (obj.offsetParent){
	while (obj.offsetParent){
		curt += obj.offsetTop;
		obj=obj.offsetParent;
	}
}
  return(curt);
}

function popUp(){
	tid=this.id;
	el=document.getElementById(tid); // span
	gl=document.getElementById('e'+tid); // gloss
	hi=gl.offsetHeight; // height of gloss
	elp=findPos(this); // element position
	ypos=elp+20;
	gl.style.top=ypos+'px';
	gl.style.display="block";
}

function popHide(){
	tid='e'+this.id;
	gl=document.getElementById(tid);
	gl.style.top='-500px';
	g1.style.display="none";
}

function ocPanel(e) {
evt = e || window.event;
evtTarget = evt.target || evt.srcElement;
tid=evtTarget.id;
divid=tid.substring(1);
 if (document.getElementById(divid).style.display == "block") {
     document.getElementById(divid).style.display = "none";
     document.getElementById(tid).innerHTML = "more.."; 
     document.getElementById(tid).style.color = "blue"; 
	 }
 else {
     document.getElementById(divid).style.display = "block";
     document.getElementById(tid).innerHTML = "close"; 
     document.getElementById(tid).style.color = "red"; 
	 } 
}

function setupCsects(){
ai=document.getElementById('csect');
ab=ai.getElementsByTagName('span');
for (i=0;i<ab.length;i++){
	stag=ab[i].innerHTML;
	if (stag=='more..'){
	  ab[i].onclick=ocPanel;
	  ab[i].style.cursor = 'pointer';
	  // hide panel
	  tid=(ab[i].id).substring(1);
	  document.getElementById(tid).className = 'note';
		}
	else {
		ab[i].style.cursor='pointer';
		ab[i].className='gloss';
		tid=ab[i].id;
		tid='e'+tid;
		document.getElementById(tid).className='gtxt';
		ab[i].onmouseover=popUp;
		ab[i].onmouseout=popHide;
		}
	}
}

// -- end of collapsible section handling ---------

// Cross-browser function to load a text (or HTML) file
// Call with file name to be loaded
function makeRequest(url) {
	xhr = null; // global for text file object
//	alert(url);
if (window.XMLHttpRequest) {
	xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
	xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xhr!=null) {
		xhr.onreadystatechange = function(){showContents(xhr)};
		xhr.open("GET", url, true); // async request
		xhr.send(null);
	}
else {
	alert("Cannot read this section");
	}
}
// listener function, triggered when request completes
// displays the contents of the requested file
function showContents(xhr) {
	var outMsg = "";
	if (xhr.readyState == 4) {
		switch (xhr.status) {
		case 200:
//		testtxt = testtxt+xhr.readyState+'-'+xhr.status+'-'+xhr.statusText+'<br/>';
			outMsg = xhr.responseText;
			break;
		case 404:
			outMsg = "<i>Not available at present<i>";
			break;
		default:
			outMsg = "<p>Cannot access the information requested at this time</p>";
			break;
		}
		document.getElementById("panel").innerHTML = outMsg;
//		alert(outMsg);
	}
	setupCsects();
	return;
}
