// JavaScript for auto-linking of comments

// This version does not use LP page comments but assumes a hand coded DL.


function makeCommentLinks() {
	if( document.getElementById ) {
		if (comments = document.getElementById('page-comments')) {
			while (node = comments.getElementsByTagName("DD")[0]) {
				// We're removing the first node each time through, so the next one will always be first!
				
				// "Close comment" link
				var newPara = document.createElement('P');
				var newLink = document.createElement('A');
				addEvent(newLink,'click',closeComment);
				newLink.href = '#' + node.id;
				var newText = document.createTextNode('[close this comment]');
				newLink.appendChild(newText);
				newPara.appendChild(newLink);
				node.appendChild(newPara);
				
				document.body.appendChild(node);
				node.style.display = "none";
				node.style.position = "absolute";
				node.className = "popup";
			}
			comments.style.display = "none";

			anchors = document.getElementsByTagName("A");
			for (i=0;i<anchors.length;i++) {
				if (anchors[i].rel == "comment") {
					if (href = anchors[i].href) {
						// Only apply actions to links with hrefs, i.e. links to valid comments terms
						defId = href.substring(href.indexOf('#') + 1);
						if (document.getElementById(defId)) {
							addEvent(anchors[i],'click',showComment);
							//addEvent(anchors[i],'mouseout',hideComment);
						}
					}
				}
			}
		}
	}
}

function findComment(evt) {
	if (!evt) evt = window.evt // for IE/Win
	node = findEventOwner(evt);
	commentId = node.href.substring(node.href.indexOf('#') + 1);
	comment = document.getElementById(commentId);
	return comment;
}

function showComment(evt) {
	comment = findComment(evt);

	// record pointer position; get values for both Netscape and IE DOMs
	scr = getScrollXY();
	var x = evt.clientX + scr.x + 10;
	var y = evt.clientY + scr.y + 15;
	if ((x > window.innerWidth - 300)) x = window.innerWidth - 300;
	// if ((window.innerHeight) && (y > window.innerHeight - 100)) y = window.innerHeight + scrollY - 100;

	// set comment position accordingly and display
	comment.style.top = y + "px";
	comment.style.left = x + "px";
	comment.style.display = "block";

	return false;
}

function hideComment(evt) {
	comment = findComment(evt);
	comment.style.display = "none";
	return;
}

function closeComment(evt) {
	if (!evt) evt = window.evt // for IE/Win
	node = findEventOwner(evt);
	while( node.nodeName != 'DD' ) {
		node = node.parentNode;
	}
	node.style.display = 'none';
	return;
	
}





