/* essay.js
Creates dynamically appearing Div's linked from Span tags in DOM.

Span tags format: <span id="comment-ref-3"> (where 3 is the
corresponding number in the div id)

Div tag format: <div id="comment-3">	(note: number matches span
above, therefore these two elements will be associated on the page)

This code is based strongly (read: 99%) on the essay.js script
The only difference is where the popup div is inserted

This JS file dynamically adds the appropriate CSS file link to the DOM (showlinks.css)
*/

insertStyleSheet("/lls/llonline/assets/styles/showlinks.css");
var curEvt = new universalEvent();

function showComment(e) {
	curEvt.refresh(e);
	closeAll();
	var n,p,d;
	if (curEvt.evtSrc.nodeName.toLowerCase() == "img") {
		curEvt.evtSrc = curEvt.evtSrc.parentNode;
		curEvt.evtSrcId = curEvt.evtSrc.id;
	}
	curEvt.evtSrcId = curEvt.evtSrc.id;
	var n=parseInt(curEvt.evtSrcId.substr(curEvt.evtSrcId.lastIndexOf("-")+1));
	highlightRef(n,true);
	p=findTopInlineNode(curEvt.evtSrc);
	// insert div after the element
	
	if (inPos)
	{
		insertAfter(d=document.getElementById("comment-" + n),inPos);
	}
	else
	{
		//alert (p.nodeName);
		//insertAfter(d=document.getElementById("comment-" + n),p);
		insertAfter(d=document.getElementById("comment-" + n),curEvt.evtSrc);
	}
	
	d=document.getElementById("comment-" + n);
	d.style.display="block";
	//if (d.scrollIntoView) d.scrollIntoView(false);
}

function closeDiv(e) {
	curEvt.refresh(e);
	var p;
	(p=curEvt.evtSrc.parentNode).style.display="";
	highlightRef(parseInt(p.id.substr(p.id.lastIndexOf("-")+1)),false);
}

function highlightRef (num, on) {
	var r = document.getElementById("comment-ref-" + num);
	if (on) {
		r.className = "essay-comment-ref-on";	
	}		
	else {
		r.className = "essay-comment-ref";
	}
}

function newCloseGraphic() {
	var im = document.createElement("img");
	im.setAttribute("src","assets/images/close.gif");
	im.setAttribute("width","14");
	im.setAttribute("height","14");
	im.setAttribute("alt","Close box");
	im.onclick=closeDiv;
	im.className = "close-but";
	return im;
}
function closeAll() {
	var i=0;
	while ((d=document.getElementById("comment-" + ++i)) != null) {
		d.style.display="";
		highlightRef(i,false);
	}
}

var i=0,d,o,sp;

while ((d=document.getElementById("comment-" + ++i)) != null) {
	d.style.display="";
	d.style.width="600px";		
	if (!d.className) d.className="essay-comment";
	d.insertBefore(newCloseGraphic(),d.firstChild);
	sp=document.getElementById("comment-ref-" + i);
	p=findTopInlineNode(sp);
	var inPos = document.getElementById('insertComments');
	// check if the place to dispaly the comments is mentioned in the Xml doc or not ...
	if (inPos)
	{
		
		insertAfter(d,inPos);
	}
	else
	{
		// By default insert the comment below the link element
		
		insertAfter(d,p);
		//insertAfter(d,sp);
	}
}
i=0;
while ((d=document.getElementById("comment-ref-" + ++i)) != null) {
	d.className="essay-comment-ref";
	d.title=(o=document.getElementById("comment-" + i).getElementsByTagName("h1")).length > 0 ? o[0].innerHTML + " - click to display": "Click to display comment";
	var current_page	=	location.href;
	//alert (current_page.indexOf("accessible=true"));
	var accessible_version	=	current_page.indexOf("accessible=true");
	if (accessible_version>0)
	{
		var cmt = document.getElementById("comment-" + i);
		// you won't find any element with the id 'justBelowLink' in the html/xml page; that's good for this
		var accesPos	=	document.getElementById("justBelowLink"); 
		if (cmt)
			d.insertBefore(cmt,accesPos);
	}
	else
		d.onclick=showComment;
}