// ==UserScript==
// @name          tabs
// @namespace     http://www.alasal.be
// @description   this adds tabs on textareas
// @include       *
// ==/UserScript==
 
window.a_u = true;
// true = standaard staat de tab's functie aan
// false = standaard staat de tab's functie uit
 
window.a_u_key = 18;
// hier kan je de nummer ingeven van snelkoppeling om de tab's functie aan of uit te zetten
// momenteel staat hij op altGr
// als je dus deze knop + TAB inhoudt, kan je switchen tussen aan en uit.
 
window.checkbox = false;
// true: er komt boven de textarea een knop om te switchen tussen aan en uit.
// false: er gebeurt niets.
window.checkbox_text = "Gebruik tab's in de textarea. (shortcut = AltGr + TAB)";
// Hier zet je de text die na de checkbox moet komen.
 
//////////////////////////// versie 1.1: Tab's in een textarea //////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
//made by Haytjes //////////// http://www.alasal.be /////////////////////////////////////
 
window.startPos = "";
window.endPos = "";
window.sel = "";
window.altGr = false;
window.input_id = "";
 
window.position = function(e) 
{
	if (document.selection) 
	{
		this.focus();
		sel = document.selection.createRange();
	}
	if (this.selectionStart || this.selectionStart == '0') 
	{
		startPos = this.selectionStart;
		endPos = this.selectionEnd;
	}
	var key = (typeof e 
!= 'undefined' && typeof e
.which 
!= 'undefined') ? e
.which 
: 	(typeof e != 'undefined' && typeof e.keyCode != 'undefined') ? e.keyCode :
	(typeof window.event != 'undefined' && typeof event.keyCode != 'undefined') ? event.keyCode :
	null;
	{
		{
			altGr = true;
			return true;
		}
		{
			if(altGr)
			{
				(a_u)?a_u=false:a_u=true;
				altGr = false;
				if(checkbox)
					input_id.checked = a_u;
				return false;
			}
			if(a_u)
			{
				insertAtCursor(this,"\t");
				e.preventDefault();
			}
		}
	}
};
window.insertAtCursor = function(myField, myValue) 
{
	//IE support
	if (document.selection) 
	{
		myField.focus();
		sel.text = myValue;
		sel.moveStart('character', 0);
		sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		myField.value = myField.value.substring(0, startPos)
							 + myValue
							 + myField.value.substring(endPos, myField.value.length);
		myField.selectionStart = startPos+myValue.length;
		myField.selectionEnd = startPos+myValue.length;
	} 
	else
	{
		myField.value += myValue;
	}
};
 
 
window.start = function()
{
	var oTextArea = document.getElementsByTagName("textarea");
	var y = oTextArea.length;
	for(var x=0;x<y;x++)
	{
		if(!oTextArea.item(x).getAttribute("wysiwyg"))
		{
			if(checkbox)
			{
				var oDiv = document.createElement("DIV");
				var oInput=document.createElement("INPUT");
				var oTextNode = document.createTextNode(checkbox_text);
				oInput.type = "checkbox";
				oInput.checked = a_u;
				oInput.addEventListener('click', function(event){a_u = event.target.checked = !a_u;}, true);
				oDiv.appendChild(oInput);
				oDiv.appendChild(oTextNode);
				input_id = oDiv.firstChild;
				oTextArea.item(x).parentNode.insertBefore(oDiv,oTextArea.item(x));
			}
			oTextArea.item(x).addEventListener('keydown', position, true);
			oTextArea.item(x).addEventListener('keyup'  , function(){ return altGr = false;}, true);
		}
	}
};
start();