/*
Texteditor

Werkt alleen in IE, eventueel aan te passen voor FF.
*/

var textarea_topleft_boundingleft;
var textarea_topleft_boundingtop;

initTextarea();

function isCursorInTextarea()
{
	if (document.selection)
	{ // IE
		document.editpageform.content.focus();
		sel = document.selection.createRange();
		return ((textarea_topleft_boundingleft!=sel.boundingLeft)
		 	 || (textarea_topleft_boundingtop!=sel.boundingTop)
		 	 || (sel.boundingWidth > 0));
	}
	else
	{ // FireFox
		var $tb = document.getElementById("content");
		return (typeof $tb.selectionStart != 'undefined');
	}
}

function insertAtCursor(myValue, disableCheck)
{
	if (document.selection)
	{ // IE
		document.editpageform.content.focus();
		sel = document.selection.createRange();
		if ((textarea_topleft_boundingleft!=sel.boundingLeft)
		 || (textarea_topleft_boundingtop!=sel.boundingTop)
		 || (sel.boundingWidth > 0)
		 || (disableCheck))
		{
			myTxt = myValue.replace(/%s/, sel.text);
			sel.text = myTxt;
		}
	}
	else
	{ // FireFox
		var $tb = document.getElementById("content");
		
		if (typeof $tb.selectionStart != 'undefined')
		{
			var $before, $after, $selection, $myTxt;
			$before = $tb.value.substring(0, $tb.selectionStart)
			$selection = $tb.value.substring($tb.selectionStart, $tb.selectionEnd)
			$after = $tb.value.substring($tb.selectionEnd, $tb.value.length)
		
			$myTxt = myValue.replace(/%s/, $selection);

			$tb.value= String.concat($before, $myTxt, $after);
		}
		$tb.focus();
	}
}

function getSelectedText()
{
	if (document.selection)
	{ // IE
		document.editpageform.content.focus();
		sel = document.selection.createRange();
		return sel.text;
	}
	else
	{ // FireFox
		var $tb = document.getElementById("content");
		
		if (typeof $tb.selectionStart != 'undefined')
		{
			return $tb.value.substring($tb.selectionStart, $tb.selectionEnd);
		}
		else
		{
			return "";
		}
	}
}

function insertInternalLinkAtCursor(sInternalLink)
{
	sID = sInternalLink.substring(1);
	if (sInternalLink.substring(0,1)=="c")
		insertAtCursor("[cat="+sID+"]%s[/cat]");
	else
		insertAtCursor("[page="+sID+"]%s[/page]");
}

function insertFileAtCursor(sFile)
{
	if (getSelectedText().length > 0)
		insertAtCursor("[file="+sFile+"]%s[/file]");
	else
		insertAtCursor("[file]"+sFile+"[/file]%s");
}

function insertImgAtCursor(sFile)
{
	insertAtCursor("[img]"+sFile+"[/img]%s");
}

function cbxChange(obj)
{
	var s = "";
	var disableCheck = false;
	if (obj.selectedIndex <= 0) return;
	if (obj.name=="kop")
	{
		disableCheck = true;
		s = "[h=" + obj.selectedIndex + "]%s[/h]";
	}
	else if (obj.name=="kleur")
	{
		switch (obj[obj.selectedIndex].text)
		{
		case "rood":	s = "[color=red]%s[/color]"; break;
		case "groen":	s = "[color=green]%s[/color]"; break;
		case "blauw":	s = "[color=blue]%s[/color]"; break;
		case "paars":	s = "[color=magenta]%s[/color]"; break;
		case "anders":	s = "[color=naam]%s[/color]"; break;
		default:
			alert("Niet ondersteunde kleur.");
			return;
		}
	}
	else if (obj.name=="grootte")
	{
		s = "[size=" + obj[obj.selectedIndex].text + "]%s[/size]";
	}
	else
		return;
	obj.selectedIndex = 0;
	insertAtCursor(s, disableCheck);
}

function initTextarea()
{
	if (document.selection)
	{ // IE
		if (document.editpageform != null)
		{
			document.editpageform.content.focus();
			sel = document.selection.createRange();
			textarea_topleft_boundingleft = sel.boundingLeft;
			textarea_topleft_boundingtop = sel.boundingTop;
		}
	}
}

