// ==UserScript==
// @name           Shack - Dipslay Author ID
// @namespace      http://www.lmnopc.com/greasemonkey/
// @description    Displays Shackers' ID numbers next to their names
// @include        http://*.shacknews.com/laryn.x?*      
// @include        http://shacknews.com/laryn.x?*        
// @include        http://*.shacknews.com/frame_laryn.x?*
// @include        http://shacknews.com/frame_laryn.x?* 
// ==/UserScript==
/*

	Amazon Wish List: http://amazon.com/gp/registry/1YRBQ22VGN9PR

	------------------------------------------------------------

	Shack User ID 
	Author: Thom Wetzel - www.lmnopc.com
	(C)2007 Thom Wetzel

	REVISIONS:
	
	2007-06-22 : Initial release


*/

(function() {

	// grab start time of script
	var benchmarkTimer = null;
	var scriptStartTime = getTime();

	// UTILITY FUNCTIONS
	function getTime() { benchmarkTimer = new Date(); return benchmarkTimer.getTime(); }
	
	// ThomW: I took getElementsByClassName and stripped it down to just what's needed by this script
	function getElementByClassName(oElm, strTagName, strClassName)
	{
		var arrElements = oElm.getElementsByTagName(strTagName);
		var oElement;
		for(var i=0; i < arrElements.length; i++)
		{
			oElement = arrElements[i];
			if (oElement.className.indexOf(strClassName) == 0)
			{
				return oElement;
			}
		}
	}

	// install shackerId functions
	function installShackerId(threadId)
	{
		var dbg = false;

		// this makes the script run on the right document when called from the iframe
		if (unsafeWindow != unsafeWindow.top)
		{
			document = unsafeWindow.top.document;
		}
		
		// find threadId
		var t = document.getElementById('item_' + threadId);
		if (!t)
		{
			if (dbg) { GM_log('COULD NOT FIND root_' + threadId); }
			return false;
		}
		
		// find fpauthor_#
		var auth = getElementByClassName(t, 'div', 'fullpost');
		auth = String(auth.className).split(' ');
		for (var i = 0; i < auth.length; i++)
		{
			if (auth[i].indexOf('fpauthor_') == 0)
			{
				auth = auth[i].split('_')[1];
				break;
			}
		}

		// find div.postmeta
		var pm = getElementByClassName(t, 'span', 'author');
		if (!pm)
		{
			if (dbg) { GM_log('getElementsByClassName could not locate span.author'); }
			return false;
		}
		
		// create [lol] button
		var d = document.createElement('div');
		d.style.display = 'inline';
		d.style.float = 'none';
		d.style.paddingLeft = '10px';
		d.style.fontSize = '14px';

		d.appendChild(document.createTextNode('#' + auth));

		// add d to pm
		pm.appendChild(d);
	}


	// handle iframe calls
	if (String(location.href).indexOf('frame_laryn.x') != -1)
	{
		// override standard show_item_fullpost with one that supports this script
		if (!unsafeWindow.shackid_show_item_fullpost)
		{
			unsafeWindow.shackid_show_item_fullpost = unsafeWindow.show_item_fullpost;
			unsafeWindow.show_item_fullpost = function(root_id, article_id, fullpost_element)
			{
				// call original function
				unsafeWindow.shackid_show_item_fullpost(root_id, article_id, fullpost_element);

				// embed videos in updated parent window
				installShackerId(article_id);
			}
		}
		
		// override function used for Refresh Thread button
		if (!unsafeWindow.shackid_replace_whole_element_from_iframe)
		{
			unsafeWindow.shackid_replace_whole_element_from_iframe = unsafeWindow.replace_whole_element_from_iframe;
			unsafeWindow.replace_whole_element_from_iframe = function(id)
			{
				unsafeWindow.shackid_replace_whole_element_from_iframe(id);
				installShackerId(String(id).substr(5));
			}
		}
	}

	// all other pages
	else
	{
		if (!unsafeWindow.shackid_show_item_fullpost)
		{
			// override standard show_item_fullpost with one that supports this script
			unsafeWindow.shackid_show_item_fullpost = unsafeWindow.show_item_fullpost;
			unsafeWindow.show_item_fullpost = function(root_id, article_id, fullpost_element)
			{
				// call original function
				unsafeWindow.shackid_show_item_fullpost(root_id, article_id, fullpost_element);

				installShackerId(article_id);
			}
		}

		// find all the fullposts on the page
		var items = document.evaluate("//div[contains(@class, 'fullpost')]/..", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		for (item = null, i = 0; item = items.snapshotItem(i); i++)
		{
			var threadId = item.id.substr(5);

			installShackerId(threadId);
		}
	}



	// log execution time
	if (GM_log)
	{
		GM_log((getTime() - scriptStartTime) + 'ms');
	}



})();
