// ==UserScript==
// @name           Shacknews : Read More Web 3.0 Shit
// @namespace      http://lmnopc.com/greasemonkey/
// @include        http://www.shacknews.com/laryn.x*
// @include        http://shacknews.com/laryn.x*
// @include        http://www.shacknews.com/
// @include        http://shacknews.com/
// ==/UserScript==

// ©2010 THOM WETZEL 
// DON'T COPY THAT FLOPPY!  

// 2010-02-25 	: initial release
// 2010-02-26 	: fixed to work on more stories than just latest chatty
//				 	: my </div> parsing included an extra </div> at the end that is now removed
// 2010-03-01	: now works on the front page too! watch out because javascript-driven stuff won't work... :(  
					

(function() {

	function getElementByClassName(oElm, strTagName, strClassName)
	{
		var arrElements = oElm.getElementsByTagName(strTagName);
		for(var i=0; i < arrElements.length; i++)
		{
			if (arrElements[i].className.indexOf(strClassName) == 0)
			{
				return arrElements[i];
			}
		}
	}
	
	function isFrontPage() { return (location.href.substr(-14) == 'shacknews.com/'); }
	
	function isCommentsPage() { return (location.href.indexOf('laryn.x') != -1); }

	
	if (isCommentsPage())
	{
		// find div.news 
		var newses = document.evaluate("//div[@class='news']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	
		if (newses.snapshotLength != 1) { return false };
		var news = newses.snapshotItem(0);
		
		// find a.readmore
		var anchor = getElementByClassName(news, 'a', 'readmore');   
		if (anchor == null) { return; }
	
		// attach event listener to the anchor's click event 
		anchor.addEventListener('click', webthreepointohbullshit, false);
	}
	else if (isFrontPage())
	{
		// find div.news 
		var items = document.evaluate("//div[@class='story']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		
		for (item = null, i = 0; item = items.snapshotItem(i); i++) 
		{
			// find a.readmore
			var anchor = getElementByClassName(item, 'a', 'readmore');
			if (anchor == null) { continue; }
		
			// attach event listener to the anchor's click event 
			anchor.addEventListener('click', webthreepointohbullshit, false);
		}
	}	


	// function that handles the web 3.0 bullshit 
	function webthreepointohbullshit(e)
	{
		if (!e) { var e = window.event; }

		// stop event from bubbling up 
		e.stopPropagation(); 
		e.preventDefault(); 
		
	  	GM_xmlhttpRequest({ method:"GET",
	  			url: e.target.href,
			    onload:function(result)
			    {
					if (result.readyState == 4) 
					{
						if (result.status == 200) 
						{
							// get the html of the shack
							var doc = result.responseText;
							
							// UNFORTUNATELY I have to parse the HTML as a string...  
							
							// find the start of div.news 
							var tgt = '<div class="news">'; 
							var pos = doc.indexOf(tgt); 
							if (pos == -1) { return; }
	
							// Start of news's innerHTML 
							var newsStart =  pos + tgt.length;
							
							// tricky part.  Count start of divs and end of divs until end of divs == 0 to find div.news's partner
							var divs = 1; 
							pos = newsStart; 
							while (divs > 0)
							{
								var nextOpen =  doc.indexOf('<div', pos); 
								var nextClose = doc.indexOf('</div', pos); 
								
								if (nextOpen < nextClose)
								{
									divs++;
									pos = nextOpen + 4;  
								}
								else
								{
									divs--; 
									pos = doc.indexOf('>', nextClose);
									if (pos != -1) { pos++; }
								}
								
								if (pos == -1) { return false; }
							}

							// truncate the last </div> found at the end of div.news
							var newsEnd = pos - 6;
							
							// find news (again ... but this time in the web 3.0 bullshit function)
							var target = null; 
							if (isCommentsPage())
							{ 
								var newses = document.evaluate("//div[@class='news']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
								if (newses.snapshotLength != 1) { return false };
								target = newses.snapshotItem(0);
							}
							else if (isFrontPage())
							{
								// Find the target anchor's parent div.story
								target = e.target; 
								do 
								{
									target = target.parentNode;
									
									if (target.className == 'story') { break; } 
								}
								while (target != null)
							}
							
							// replace div's innerHTML wth the parsed story
							if (target != null)
							{
								target.innerHTML = doc.substring(newsStart, newsEnd) + '<p style="text-align: center; background-color: #a00; color: #fff; padding: 0.5em 0;"><strong>OH SNAP! STRAIGHT UP WEB 3.0 BULLSHIT!</strong></p>';
							}
							
						} 
						else 
						{
							alert('There was a problem with the request.');
						}
					}
			    }
	  		});
  	}
})();

