/* 
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 JS file dynamically adds the appropriate CSS file link to the DOM (essay.css)

This js can be used only for pages which has only one type of links to popdownsi.e.either TEXTLINKS or IMAGES
*/

insertStyleSheet("/lls/assets/styles/showlinks.css");
var curEvt = new universalEvent();

function showComment(e) {
	curEvt.refresh(e);
	closeAll();
	var n,p,d;
	while (curEvt.evtSrc.nodeName.toLowerCase() != "span") {
		if (curEvt.evtSrc.parentNode.nodeName.toLowerCase() == "body") return false;
		curEvt.evtSrc = curEvt.evtSrc.parentNode;
		curEvt.evtSrcId = curEvt.evtSrc.id;
	}
	n=parseInt(curEvt.evtSrcId.substr(curEvt.evtSrcId.lastIndexOf("-")+1));
	if (curEvt.evtSrcId=="comment-ref-" + n) //highlight only if that is text link
		highlightRef(n,true);
	if (curEvt.evtSrc)
	{
		p=findTopInlineNode(curEvt.evtSrc);
		var inPos = document.getElementById('insertComments');
		// check if the place to dispaly the comments is mentioned in the Xml doc or not ...
		if (inPos)
			p.parentNode.insertBefore(d=document.getElementById("comment-" + n),inPos);
		else	
			p.parentNode.insertBefore(d=document.getElementById("comment-" + n),p);
	}
	d.style.display="block";

}

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 (r)
	{
		if (on) 
		{
			r.className = "essay-comment-ref-on";		
		}		
		else 
		{
			r.className = "essay-comment-ref";
		}
	}
}

function newCloseGraphic() {
	var im = document.createElement("img");
	im.setAttribute("src","/lls/assets/images/close.gif");
	im.setAttribute("width","14");
	im.setAttribute("height","14");
	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.width="85%";
	d.className="essay-comment";
	d.insertBefore(newCloseGraphic(),d.firstChild);
}


i=0;
while ((d=document.getElementById("comment-Image-" + ++i)) != null) {
	d.className="essay-comment";
	
	d.title=(o=document.getElementById("comment-" + i).getElementsByTagName("h3")).length > 0 ? o[0].innerHTML + " - click to display": "Click to display comment";
	var dd=d.firstChild;
	//alert (dd.alt);
	if (!(dd.alt)) // if no alt text is mentioned in the xml/html page; then add the click to alt text
		dd.alt=d.title;
	//newText = document.createTextNode(" "); 
	//d.insertBefore(newText,d.firstChild);
	d.onclick=showComment;
}


