// ==UserScript==
// @name           cn_ubb_enhance
// @namespace      http://shutterfreak.net/xtra/greasemonkey/ubb_enhance
// @description    Enhance CN forums by adding short links to individual posts in flat mode. It works in the live forums and in the forum archives.
// @author         Olivier Biot
// @version        0.2.1
// @include        http://www.cloudynights.com/ubbthreads/showflat.php*
// @include        http://www.cloudynights.com/ubbarchive/showflat.php*
// ==/UserScript==

function cn_forum_base_url()
{
	if ( document.location.href.search( 'http://www.cloudynights.com/ubbarchive' ) == 0 ) {
		return 'http://www.cloudynights.com/ubbarchive';
	} else if ( document.location.href.search( 'http://www.cloudynights.com/ubbthreads' ) == 0 ) {
		return 'http://www.cloudynights.com/ubbthreads';
	}
	return null;
}

function isNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function ubbThreadedPostLink(postId, id_prefix)
{
	var link = document.createElement('a');
	link.href=cn_forum_base_url() + '/showthreaded.php/Number/' + postId;
	link.setAttribute('id', id_prefix + '-' + postId); 
	return link;
}

function UbbLinkPopup(postId, title)
{
    node = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + '<html><head>' + '<title>Create link to post #' + postId +': ' + title + '</title>' + '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />' + '<link rel="stylesheet" href="/ubbthreads/stylesheets/template.css" type="text/css" />' + '</head><body>' + '<textarea name="linkfield-' + postId + '" id="linkfield-' + postId + '" cols="60" rows="5">' + '[link=' + ubbThreadedPostLink(postId, 'post_link_popup') + ']' + title + '[/link]' + '</textarea>' + '<p>Copy the text above and paste it in a post if you want to link to this post.</p>' + '<p><a href="javascript:self.close()">Close this Window</a></p>' + '</body></html>';
    popup = window.open("", "Create link to post #" + postId, "resizeable,width=520,height=200,status=0,toolbar=0,scrollbars=1");
    popup.document.write(node);
    popup.document.close();
}


var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
	if (links[i].name && links[i].name[0] == '#') {
		// Get the Post ID: it's encoded as an anchor starting with '#'
		// followed with digits representing the post ID (the post number):
		var postId = links[i].name.slice(1);
		if ( isNumeric(postId) ) { // Valid Post ID anchor
			// Retrieve the parent element of this link (an HTML Font Element):
			var parent_node = document.getElementsByTagName('a')[i].parentNode;
			parent_node.appendChild(document.createTextNode(' | '));
			// The subject of the post (post title) is contained in the 1st SPAN sibling of this parent:
			var post_subject = parent_node.parentNode.getElementsByTagName('span')[0].innerHTML;
			// Create a link element that will contain the short, quick link to the post in threaded mode:
			var link = ubbThreadedPostLink(postId, 'post_link');
			link.setAttribute('target', '_blank');
			// Add an image icon for the link:
			var img = document.createElement('img');
			img.setAttribute('src',"http://www.cloudynights.com/ubbthreads/images/threaded.gif");
			img.setAttribute('border', "0");
			// First add the icon, then the text:
			link.appendChild(img);
			link.appendChild(document.createTextNode("linky"));
			// Finally add the link:
			parent_node.appendChild(link);
			i++; // Skip the link we just created */

			// Now create a 'UBB link popup' link:
			parent_node.appendChild(document.createTextNode(' | '));
			var popup = document.createElement('a');
			popup.href='#';
			popup.setAttribute('onclick', 'javascript:UbbLinkPopup(' + postId + ", '" + post_subject + "'); return false;");
			popup.setAttribute('id', 'popup-' + postId);
			// Add a link text to the popup link:
			popup.appendChild(document.createTextNode('UBB link'));
			
			parent_node.appendChild(popup);
			i++; // Skip the link we just created */
		}
	}
}


