// ==UserScript==
// @name           Stoofoo Tags
// @namespace      http://www.lmnopc.com/greasemonkey/
// @description    Improves Stoofoo's posting interface a little ... 
// @include        http://*.stoofoo.net/chatty.x*
// @include        http://stoofoo.net/chatty.x*
// ==/UserScript==

/*
	Author: ThomW
	Contributors: dobob, naabster, awun

	www.lmnopc.com
*/

function debug_log(str)
{
	// GM_log(str);
}

if (!unsafeWindow.tw_inserttag)
{
	unsafeWindow.tw_inserttag = unsafeWindow.inserttag;
	
	unsafeWindow.inserttag = function(opentag, closetag)
	{
		// Get TextArea and remember the scrollbar position
		var textarea = document.getElementsByTagName('textarea')[0];
		
		var oldScrollTop = textarea.scrollTop;
		debug_log('very berry scrolltop: ' + textarea.scrollTop);
		
		var startPos = textarea.selectionStart; 
		var endPos = textarea.selectionEnd; 
		debug_log('startPos and endPos are: ' + startPos + ', ' + endPos + '.');
		
		var input = ''; 
		
		// If text is selected, we're going to be adding the tags around it
		if (endPos - startPos > 0)
		{
			input = textarea.value.substring(startPos, endPos);
		}
		// If no text is selected, use prompt() to let the user enter text 
		else
		{
			input = prompt("Type in the text you want to be " + opentag + " in here " + closetag + ".", "");
	
			if (!input)	// if user presses Escape and/or no input is caught
			{
				textarea.focus();
				return;
			}
		}
	
		// If input is blank at this point, exit
		if (input.length == 0)
		{
			textarea.focus();
			return; 
		}
		
		// Trim the input; remember leading and trailing whitespace
		var wsBef = /^\s\s*/.test(input);
		var wsAft = /\s\s*$/.test(input);
		debug_log('wsBef: ' + wsBef + ', wsAft: ' + wsAft );
		input = input.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
		// Update the value of textarea with the outcome; put whitespace outside of tags
		textarea.value = textarea.value.substring(0, startPos)
			+ (wsBef ? ' ' : '')
			+ opentag
			+ input
			+ closetag
			+ (wsAft ? ' ' : '')
			+ textarea.value.substring(endPos, textarea.value.length);
		
		// Place the cursor back in the textarea
		var offset = wsBef ? 1 : 0;

		debug_log('input.length	= ' + input.length);
		debug_log('offset = ' + offset);
		debug_log('opentag.length = ' + opentag.length);
		debug_log('closetag.length = ' + closetag.length);
		
		// Keep text selected
		if (endPos - startPos > 0)
		{
			offset += startPos + opentag.length;
			textarea.setSelectionRange(offset, offset + input.length);
			
			debug_log('selection area is now : ' + offset + ', ' + (offset + input.length) + '.');
			
		}
		// Move the cursor after inserted tags
		else
		{
			offset += startPos + input.length + opentag.length + closetag.length; 
			offset += wsAft? 1 : 0;
			textarea.setSelectionRange(offset, offset);
		}
		textarea.focus();
		
		debug_log('scrollTop: ' + textarea.scrollTop);
		debug_log('scrollHeight: ' + textarea.scrollHeight);
		textarea.scrollTop = oldScrollTop;
	}
}


	function getElementByClassName(ele, strTagName, strClassName) 
	{
		var a = ele.getElementsByTagName(strTagName);
		for (var i = 0; i < a.length; i++) 
		{
			if (a[i].className.indexOf(strClassName) != -1) 
			{
				return a[i];
			}
		}
	}


// replace the tags list with something with all of them 
var postBox = getElementByClassName(document, 'table', 'tbl-box');

var tr = document.createElement('tr');
var td = document.createElement('td');
td.id = 'tw-tags';
GM_addStyle(<><![CDATA[
	td#tw-tags td {
		width: 10em; 
		text-align: center; 
		font-size: 12px; 
	}
]]></>);

td.innerHTML = <><![CDATA[
<table>
	<tr>
		<td>
      e[<a href="javascript:inserttag('e[',']e')" class="olive">Olive</a>]e<br />
      f[<a href="javascript:inserttag('f[',']f')" class="fuchsia">Fuchsia</a>]f<br />
      l[<a href="javascript:inserttag('l[',']l')" class="lime">Lime</a>]l<br />
    </td>
    <td>
      n[<a href="javascript:inserttag('n[',']n')" class="orange">Orange</a>]n<br />
      p[<a href="javascript:inserttag('p[',']p')" class="pink">Pink</a>]p<br />
      q[<a href="javascript:inserttag('q[',']q')" class="quote">Quote</a>]q<br />
    </td>
    <td>
      _[<u><a href="javascript:inserttag('_[',']_')" class="link">Underline</a></u>]_<br />
      /[<i><a href="javascript:inserttag('/[',']/')" class="link">Italic</a></i>]/<br />
      w[<span class="sub-ctrl"><a href="javascript:inserttag('w[',']w')" class="link">W e i r d</a></span>]w<br />
   </td>
	</tr>
</table>
]]></>;

tr.appendChild(td);
postBox.appendChild(tr);
