/************************************** 
*  Bible References for Messages - TLIG
*  Ajax Version
*  Joćo Wetternick - TLIG Brazil - Jul/2008
***/


 //  global vars
 var currTopic = null;
 var nPosY = 0;


// Invoque php file to access Bible database table, 
//  using Ajax
//
function BRefGet(book, chpt, vrsI, vrsE) 
{
   var o = document.getElementById("brefMsg");
   var xRef;
   var xmlHttp;
   var sResponse = "Wait...";

   // if only one versicle, copy the first to last
   if (vrsE == null)
      vrsE = vrsI;

   xRef = book+" "+chpt+":"+vrsI;
   if (vrsE != vrsI)
      xRef = xRef + "-"+vrsE;

   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null)
   {
	  alert ("Your browser does not support AJAX!");
	  return;
   } 
   xmlHttp.onreadystatechange=function() 
   {
	  if(xmlHttp.readyState==4)   // 4 : The request is complete
	  {
          // Show the result of database biblical reference search
		  sResponse=xmlHttp.responseText;
          o.innerHTML = fmtBibleRef (xRef, sResponse);
          ShowTopic ("brefMsg");
      }
   }

   // show an wait message inside hint, during database access
   o.innerHTML = fmtBibleRef (sResponse, "");
   ShowTopic ("brefMsg");

   xmlHttp.open("GET","/global/BiblRef.php?book="+book+"&chapter="+chpt+"&vIni="+vrsI+"&vEnd="+vrsE,true);
   xmlHttp.send(null);
}


// formats the hint to show the bible reference
//
function fmtBibleRef (s_ref, s_text) {
   if (s_text.length > 500)
   {  return "<table cellpadding=5 bgcolor=#FFFFCC style='border:1px solid #770000' WIDTH='400'><tr><td align=left><img style='width: 13px; height: 13px; float: right;' alt='close' src='/images/global/msg/close.gif'><IMG style='WIDTH: 102px; HEIGHT: 76px' alt='' hspace=0 src='/images/global/msg/Bible2.gif' align=right border=0><I><font face='arial,helvetica' size='-1'>Biblical reference :</I></font><HR ALIGN='left' SIZE='1' WIDTH='150'><font face='arial,helvetica'><font size='-1'><b>"+s_ref+"</b><br><br><div style='padding:5px; width:384px; height:180px; font-size: 12px; overflow:auto;'  align='justify'>"+s_text+"</div></font></td></tr></table>"
   }  else
   {  return "<table cellpadding=5 bgcolor=#FFFFCC style='border:1px solid #770000' WIDTH='400'><tr><td align=left><img style='width: 13px; height: 13px; float: right;' alt='close' src='/images/global/msg/close.gif'><IMG style='WIDTH: 102px; HEIGHT: 76px' alt='' hspace=0 src='/images/global/msg/Bible2.gif' align=right border=0><I><font face='arial,helvetica' size='-1'>Biblical reference :</I></font><HR ALIGN='left' SIZE='1' WIDTH='150'><font face='arial,helvetica'><font size='-1'><b>"+s_ref+"</b><br><br><div style='font-size: 12px;' align='justify'>"+s_text+"</div></font></td></tr></table>"
   }
}

function fmtFootnote (s_text) {
   return "<table cellpadding=5 bgcolor=#FFFFCC style='border:1px solid #770000' WIDTH='400'><tr><td align=left><img style='width: 13px; height: 13px; float: right;' alt='close' src='/images/global/msg/close.gif'><IMG style='WIDTH: 96px; HEIGHT: 20px' alt='' hspace=0 src='/images/global/msg/lapis.gif' align=right border=0><I><font face='arial,helvetica' size='-1'>Footnote : </I></font><HR ALIGN='left' SIZE='1' WIDTH='150'><font face='arial,helvetica'><font size='-1'><div style='text-align:justify;'>"+s_text+"</div></font></font></td></tr></table>"
}       


// show the footnote 
//
function ftRef(b) {
var o = document.getElementById("brefMsg");

       o.innerHTML = fmtFootnote (document.getElementById(b).innerHTML);
	   ShowTopic ("brefMsg");
}


//---------------------------------------------
//---------------------------------------------
// these functions deal with little hint window

function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	
	return mOffsetTop;
}

//-----------------------------------------
function CloseTopic(topic)
{
    var tela;
	
	if (currTopic != null)
	{   tela = document.getElementById(topic);
	 
       //  tela.style.visibility = 'hidden';
       tela.style.display = 'none';
    }
    currTopic = null;

   // parent.indexfmset.rows="100%,0%";

}

//-----------------------------------------
function ShowTopic(topic, left, top)
{
  //var link = document.getElementById(topic);
  var tela = document.getElementById(topic);

  if (currTopic != null)
	CloseTopic(currTopic);
  currTopic = topic;
  if (left != null && left >= 0) 
	tela.style.left = left;

  if (top != null && top >= 0) {
	tela.style.top = top;
  }
  else {
    // tela.style.top = getOffsetTop(link);
    // // tela.style.visibility = 'visible';
	getCursorPosXY(tela);
	tela.style.top = nPosY;
  }

  tela.style.display = '';

}

//-----------------------------------------

function getCursorPosXY(e)
{
	var posx = 0;
	var posy = 0;

	if (!e) var e = window.event;

	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
		// alert ("document.body.scrollTop = "+document.body.scrollTop);
		// alert ("e.clientY = "+e.clientY);
	}


	// posx and posy contain the mouse position relative to the document
	// Do something with this information
     
    // update global variable nPosY
	nPosY = document.body.scrollTop+120; // parent.msg.document.body.scrollTop+50;

}


// Object XmlHttpRequest : Ajax
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


