// JavaScript for auto-linking of comments


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!
				var links = node.getElementsByTagName('A');
				for( var i=0; i<links.length; i++ ) {
					if( links[i].rev == 'comment' ) {
						addEvent(links[i],'click',closeComment);
						links[i].removeAttribute('href');
						var newText = document.createTextNode('close this comment');
						links[i].removeChild(links[i].firstChild);
						links[i].appendChild(newText);
					}
				}
				/*
				var newLink = document.createElement('A');
				var newText = document.createTextNode('close this comment');
				var newSpan = document.createElement('SPAN');
				newLink.appendChild(newSpan);
				newLink.appendChild(newText);
				addEvent(newLink,'click',closeComment);
				var newPara = document.createElement('P');
				newPara.className = 'closePopup';
				newPara.appendChild(newLink);
				node.appendChild(newPara);				
				*/
				
				document.body.appendChild(node);
				node.style.display = "none";
				node.style.position = "absolute";
				node.className = (node.className == '') ? "popup" : 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],'mouseover',showComment);
							if( anchors[i].className.indexOf('image') != -1 ) {
								addEvent(anchors[i],'mouseout',hideComment);
							}
						}
					} 
				}
			}
			
			// Pseudo image maps 
			var divs = document.getElementsByTagName('DIV');
			for( i=0; i<divs.length; i++ ) {
				if( divs[i].className.indexOf('mapped-image') != -1 ) {
					// When the user mouses over the image, reveal all the comment links.
					addEvent(divs[i],'mouseover',showCommentAreas);
					addEvent(divs[i],'mouseout',hideCommentAreas);
					
					// Meanwhile, hide the comment links.
					var anchors = divs[i].getElementsByTagName("A");
					for (j=0; j<anchors.length; j++) {
						if (anchors[j].rel == "comment") {
							if (href = anchors[j].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)) {
									anchors[j].style.display = 'none';
								}
							}
						}
					}
				}
			}
		}
	}
}

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;
}

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;
	
}

function findEventOwnerDiv(evt) {
	if (evt.target) {
		// DOM compliant
		var node = evt.target; 
	}
	else {
		// IE/Win
		var node = evt.srcElement;
	}
	while (node) {
		if ( (node.nodeType == node.ELEMENT_NODE || node.nodeType == 1) && node.nodeName == "DIV" ) {
			// node.ELEMENT_NODE does not seem to work in Safari
			return node;
		}
		node = node.parentNode;
	}
	return null;
}

function showCommentAreas(evt) {
	if (!evt) evt = window.evt // for IE/Win
	var div= findEventOwnerDiv(evt);
	var anchors = div.getElementsByTagName("A");
	for (j=0; j<anchors.length; j++) {
		if (anchors[j].rel == "comment") {
			if (href = anchors[j].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)) {
					anchors[j].style.display = 'block';
				}
			}
		}
	}

	return;
}

function hideCommentAreas(evt) {
	if (!evt) evt = window.evt // for IE/Win
	var div = findEventOwnerDiv(evt);
					var anchors = div.getElementsByTagName("A");
					for (j=0; j<anchors.length; j++) {
						if (anchors[j].rel == "comment") {
							if (href = anchors[j].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)) {
									anchors[j].style.display = 'none';
								}
							}
						}
					}

	return;
}





