// 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 = "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);
							//addEvent(anchors[i],'mouseout',hideComment);
						}
					}
				}
			}
		}
	}
}

function findComment(evt) {
	if (!evt) evt = window.evt // for IE/Win
	node = findEventOwner(evt);
	while( node.nodeName != 'A' ) {
		// Make sure we find the link
		node = node.parentNode;
	}
	commentId = node.href.substring(node.href.indexOf('#') + 1);
	comment = document.getElementById(commentId);
	return comment;
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if ( isSafari ) {
		scrOfX = 0;
		scrOfY = 0;
	}
	else if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return { x : scrOfX, y : scrOfY };
}

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




