// ==UserScript==
// @name           nsftools Show Comments
// @namespace      http://www.captainoblivious.com
// @description    Adds a link below each blog post so that you can click on that link to see the comments for that post on the same page.
// @include        http://*.nsftools.com/blog/*.htm*
// ==/UserScript==


/* This is a Greasemonkey script designed to be used with Julian
Robichaux's blog (http://www.nsftools.com).

If you have no idea what Greasemonkey is, start here:

Firefox users (the original): http://greasemonkey.mozdev.org/

IE users (the copy):
http://www.daishar.com/blog/archives/2005/03/greasemonkey_fo.html

I've only tested this script with Firefox, so I have no idea how well it
does or doesn't work with IE.  I have no intention of support IE as a
platform.  If it works, that's nice.  If it doesn't, so be it.  Get a
real browser.

===========================

The problem with Julian's blog is that he uses a third-party comments
tool (enetation.co.uk), which requires the comments to appear either in a
pop-up window or on a separate page.

What this script does (thanks to the magic of Greasemonkey) is to add a
link at the bottom of each blog entry.  If you click on that link, it
will open a <div> beneath the post that will be populated with the
comments.

===========================

This is version 0.9 of the nsftoolscomments.user.js script by Rob McDonagh ( http://www.CaptainOblivious.com ) April 17, 2005.

You can do anything you'd like with this script, just don't hold me liable for anything, and don't pretend you wrote it yourself (why would you want to?).

Most of the code, and even most of the comments, stolen with glee from Julian's own LDDMonkey script: http://www.OpenNTF.org/projects/pmt.nsf/ProjectLookup/LDDMonkey

===========================

*/ 

(function () {
	
	// the main chunk of code here just steps through all the elements of class
	// blogEntry and blogEntryTitle to find out where to put the << show comments inline >>
	// clickable span.  If there are no comments, the blogEntryTitle will tell us.

	var entryTitles=getElementsByClass("blogEntryTitle");
	var entries=getElementsByClass("blogEntry");
	
	// there is a presumption that there will be the same number of blog Entries as there
	// are titles.  If Julian ever decides to mess with this script, that could conceivably 
	// be a mistake.  Otherwise, there's no reason for this not to be the case.

	for (i=0; i<entryTitles.length; i++) {
	//for (i=0; i<1; i++) {
		var title=entryTitles[i];	//if the structure of the page changes, this will all break
		var myEl=title.childNodes[4];
		var myLittleEl=myEl.childNodes[0];
		var newEl=myLittleEl.childNodes[8];
		var holder=newEl.firstChild.nodeValue;  //element that holds the comments count retrieved from enetation
		var openParen=holder.indexOf("(");
		var closeParen=holder.indexOf(")");
		var numComments=holder.substring(openParen+1, closeParen);  //giving Julian the benefit of the doubt here - assume possible infinite number of comment digits...
		if (numComments>0) {		//don't bother unless there are actually comments to show
			newEl=newEl.nextSibling;
			newEl=newEl.nextSibling;
			holder=newEl.getAttribute("href");	//this is the url to retrieve comments from enetation
			var urlToGet=holder;
			var entry=entries[i];
			var myP=entry.nextSibling;
			var linkDiv=document.createElement("div");
			linkDiv.setAttribute("tag", holder);
			linkDiv.onclick = doMeBaby;
			linkDiv.style.cursor = "pointer";
			linkDiv.style.backgroundColor = "#DDDDBB"
			linkDiv.innerHTML="<<&nbsp &nbsp Show comments inline &nbsp &nbsp>>";
			var div = document.createElement("div");
			div.setAttribute("id", holder);
			div.style.display = "none";
			div.style.border = "1px solid #777777";
			div.style.backgroundColor = "#F7F8F3";
			div.style.padding = "5px";
			//div.style.margin = "5px";
			//div.style.marginLeft = "5px";
			div.style.visibility = "hidden";
			myP.parentNode.insertBefore(linkDiv, myP);	
			myP.parentNode.insertBefore(div, myP);	
			
		}
		
	}


function getElementsByClass(classname) {
	// Julian's so logical that all his blog posts are contained within div tags 
	// of a class 'blogEntry'.  This function returns all elements based on class,
	// similar to the getElementByTag routines.
	var customcollection=new Array();
 	var inc=0;
 	var alltags=document.all? document.all : document.getElementsByTagName("*");
 	for (i=0; i<alltags.length; i++){
   		if (alltags[i].className==classname) {
     		customcollection[inc++]=alltags[i];
 		}
	}
	return customcollection;
}	

function doMeBaby() {
		var tag = this.getAttribute("tag");
		var div = document.getElementById(tag);
		// if we're already showing the div, just hide it and return
		if (div.style.visibility == "visible") {
			div.style.display = "none";
			div.style.height = 0;
			div.style.visibility = "hidden";
			this.innerHTML="<<&nbsp &nbsp Show comments inline &nbsp &nbsp>>";
			return;
		}
		
		// show the div and grab the response message if we haven't already
		div.style.visibility = "visible";
		div.style.display = "block";
		div.style.height = "";
		if (!div.innerHTML) {
			div.innerHTML = "getting comments - this takes a few seconds, because the comments are hosted on a different site...";
			getResponseText(div, tag);
		}
		this.innerHTML="<<&nbsp &nbsp Hide comments &nbsp &nbsp>>";		
}

function getResponseText (div, tag) {
			var bodyText="";
	        GM_xmlhttpRequest({
	        method:"GET",
	        url: tag,
	        onload:function(results)
	        	{
	        		var resp = results.responseText;
	        		var pos = resp.indexOf("<hr noshade");
	        		if (pos > 0) {
						var pos2 = resp.indexOf('<font class="addCommentFont">', pos);
						if (pos2 > 0) {
							bodyText = resp.substring(pos+46, pos2);	//skipping first HR element, we hope
							bodyText = "<br>" + bodyText;		//adding a little spacing
						}					
					}	
	        		div.innerHTML=bodyText;
	        	}
	        });
}
})();

