function ReactionForm(modStatus)
{
	this.forumImageBASEUrl = "";
	this.reactionForm = null;
	this.reactionFormContent = null;
	this.reactionFormToolbar = null;
	this.smiliePopup = null;
	
	this.init();
}

Object.extend(ReactionForm.prototype,
{
	init: function()
	{
		//check if forumImageBASEUrl isset in document
		if (typeof(forumImageBASEUrl) != 'undefined')
		{
			this.forumImageBASEUrl = forumImageBASEUrl;
		}
			if (document.getElementById('reactieForm'))
			{
				this.reactionForm = document.getElementById('reactieForm');
				if (this.reactionForm)
				{
					this.reactionFormContent = this.reactionForm.elements['reactieFormContent'];
	
					this.reactionFormToolbar = this.createToolbar();
				}
			}
			else if (document.getElementById('addTopicForm'))
			{
				this.reactionForm = document.getElementById('addTopicForm');
				if (this.reactionForm)
				{
					this.reactionFormContent = this.reactionForm.elements['reactieFormContent'];
	
					this.reactionFormToolbar = this.createToolbar();
				}
			}

			addEvent(window, 'unload', this.cleanUp.bind(this));
	},
	cleanUp: function()
	{
		this.reactionForm = null;
		this.reactionFormContent = null;
		this.reactionFormToolbar = null;
		this.errorMessage = null;
		this.smiliePopup = null;
	},
	createToolbar: function()
	{
		var toolbarItems = [
			{img: 'maximize', title: 'Vergroot tekstveld', func: this.resizeReactionForm.bind(this)},
			{img: 'smilies', title: 'Smilies', func: this.showSmilies.bind(this)},
			{img: 'bold', title: 'Tekst vet maken', func: this.rteAction.bind(this, 'bold')},
			{img: 'italic', title: 'Tekst schuin zetten', func: this.rteAction.bind(this, 'italic')},
			{img: 'underline', title: 'Tekst onderlijnen', func: this.rteAction.bind(this, 'underline')},
			{img: 'strike', title: 'Tekst doorstrepen', func: this.rteAction.bind(this, 'strike')},
			{img: 'sub', title: 'Tekst in subschrift zetten', func: this.rteAction.bind(this, 'sub')},
			{img: 'sup', title: 'Tekst in superschrift zetten', func: this.rteAction.bind(this, 'sup')},
			{img: 'small', title: 'Tekst verkleinen', func: this.rteAction.bind(this, 'small')},
			{img: 'list_bullet', title: 'Lijst maken', func: this.rteAction.bind(this, 'list_bullet')},
			{img: 'link', title: 'Link maken', func: this.rteAction.bind(this, 'link')}
		];

		var toolbarContainer = document.createElement('div');
		toolbarContainer.id = 'reactieToolbar';

		var toolbarImg;
		for (var i = 0; i < toolbarItems.length; i++)
		{
			toolbarImg = document.createElement('img');
			toolbarImg.src = this.forumImageBASEUrl+'forum/toolbar/' + toolbarItems[i]['img'] + '.gif';
			toolbarImg.title = toolbarItems[i]['title'];
			addEvent(toolbarImg, 'click', toolbarItems[i]['func']);
			toolbarContainer.appendChild(toolbarImg);
		}
//alert(this.reactionFormContent.parentNode);
		this.reactionFormContent.parentNode.appendChild(toolbarContainer);
		
		if (this.reactionFormContent.createTextRange)
		{
			this.reactionFormContent.cursorPos = null;
			this.reactionFormContent.onkeyup = this.storeCursor;
			this.reactionFormContent.onclick = this.storeCursor;
			this.reactionFormContent.onselect = this.storeCursor;
		}

		if (!is.mac)
		{
			if (this.reactionFormContent.createTextRange)
				this.reactionFormContent.onkeydown = this.shortKey.bind(this);
			else
				this.reactionFormContent.onkeypress = this.shortKey.bind(this);
		}

		return toolbarContainer;
	},
	rteAction: function(type, text)
	{
		if (this.reactionFormContent.cursorPos)
		{
			if (type != 'plain') text = this.reactionFormContent.cursorPos.text;
			this.reactionFormContent.cursorPos.text = this.rteReplace(type, text);
		}
		else if (this.reactionFormContent.selectionStart != undefined)
		{
			// remember scrollposition
			var scrollTop = this.reactionFormContent.scrollTop;

			var sStart = this.reactionFormContent.selectionStart;
			var sEnd = this.reactionFormContent.selectionEnd;
			if (type != 'plain') text = this.reactionFormContent.value.substring(sStart, sEnd);
			text = this.rteReplace(type, text);
			this.reactionFormContent.value = this.reactionFormContent.value.substr(0, sStart) + text + this.reactionFormContent.value.substr(sEnd);
			var nStart = sStart == sEnd ? sStart + text.length : sStart;
			var nEnd = sStart + text.length;
			this.reactionFormContent.setSelectionRange(nStart, nEnd);

			// reset scrollposition
			this.reactionFormContent.scrollTop = scrollTop;
		}
		else
		{
			if (type != 'plain') text = '';
			this.reactionFormContent.value += this.rteReplace(type, text);
		}

		this.reactionFormContent.focus();
		if (this.reactionFormContent.createTextRange) this.reactionFormContent.onselect();
	},
	rteReplace: function(type, text)
	{
		var val = '';
		switch (type)
		{
			case 'plain':
				break;
			case 'bold':
				text = '[b]'+text+'[/b]';
				break;
			case 'italic':
				text = '[i]'+text+'[/i]';
				break;
			case 'underline':
				text = '[u]'+text+'[/u]';
				break;
			case 'strike':
				text = '[s]'+text+'[/s]';
				break;
			case 'sub':
				text = '[sub]'+text+'[/sub]';
				break;
			case 'sup':
				text = '[sup]'+text+'[/sup]';
				break;
			case 'small':
				text = '[small]'+text+'[/small]';
				break;
			case 'list_bullet':
				text = '[list]\r\n[li]'+(text.split(/\r?\n/).join('[/li]\r\n[li]'))+'[/li]\r\n[/list]';
				break;
			case 'link':
				if (/^(http:\/\/|www\.)/i.test(text))
				{
					val = prompt('Voer omschrijving in:', text);
					if (val !== null && val != '') text = '[url='+text.replace(/(["\\])/g,'\\$1')+']'+val+'[/url]';
				}
				else
				{
					val = prompt('Voer de URL in:','http:\/\/');
					if (val !== null && val != 'http:\/\/')
					{
						if (text == '') text = '[url]'+val+'[/url]';
						else text = '[url='+val.replace(/(["\\])/g,'\\$1')+']'+text+'[/url]';
					}
				}
				break;
			case 'quote':
				text = '[quote]\r\n'+text+'\r\n[/quote]';
				break;
		}

		return text;
	},
	shortKey: function(e)
	{
		e = e || event;

		var key = 0;
		if (e.keyCode) key = e.keyCode;
		else if (e.which) key = e.which - 32;

		if (e.ctrlKey && !e.shiftKey)
		{
			switch (key)
			{
				case 66:
					this.rteAction('bold');
					return cancelEvent(e);
				case 73:
					this.rteAction('italic');
					return cancelEvent(e);
				case 83:
					this.rteAction('strike');
					return cancelEvent(e);
				case 85:
					this.rteAction('underline');
					return cancelEvent(e);
			}
		}

		return true;
	},
	resizeReactionForm: function(e)
	{
		e = e || event;
		var img = e.target || e.srcElement;

		if (hasClass(this.reactionFormContent, 'large'))
		{
			removeClass(this.reactionFormContent, 'large');
			img.src = this.forumImageBASEUrl+'forum/toolbar/maximize.gif';
			img.title = 'Vergroot tekstveld';
		}
		else
		{
			addClass(this.reactionFormContent, 'large');
			img.src = this.forumImageBASEUrl+'forum/toolbar/minimize.gif';
			img.title = 'Verklein tekstveld';
		}

		if (window.utracker_stretch)
			utracker_stretch();
	},
	storeCursor: function()
	{
		this.cursorPos = document.selection.createRange().duplicate();
	},
	showSmilies: function()
	{
		if (this.smiliePopup === null)
			this.smiliePopup = this.createSmiliePopup();

		if (this.smiliePopup)
		{
			if (this.smiliePopup.style.display == 'block')
			{
				this.smiliePopup.style.display = 'none';
			}
			else
			{
				setRelativePosition(this.smiliePopup, this.reactionFormToolbar, 0, 26);
				this.smiliePopup.style.display = 'block';
			}
		}
	},
	createSmiliePopup: function()
	{
		var smilies = {
		 ":)" : ["smile.gif",15,15],
		 ":(" : ["frown.gif",15,15],
		 ":o" : ["redface.gif",15,15],
		 ":D" : ["biggrin.gif",15,15],
		 ";)" : ["wink.gif",15,15],
		 ":+" : ["clown.gif",15,15],
		 "}>" : ["devil.gif",15,15],
		 ":'(" : ["cry.gif",15,15],
		 ":P" : ["puh2.gif",15,15],
		 ":9" : ["yummie.gif",15,15],
		 ":*)" : ["shiny.gif",15,15],
		 "O+" : ["heart.gif",15,15],
		 ":O" : ["yawnee.gif",15,15],
		 "}:O" : ["rc5.gif",18,18],
		 ":Y)" : ["vork.gif",20,15],
		 ":z" : ["sleephappy.gif",32,15],
		 ";(" : ["sadley.gif",15,15],
		 "8-)" : ["coool.gif",15,15],
		 ":?" : ["confused.gif",19,15],
		 "|:(" : ["frusty.gif",30,15],
		 ":9~" : ["kwijl.gif",15,15],
		 ":>" : ["puh.gif",15,15],
		 //":\/" : ["nosmile.gif",15,15],
		 ":|" : ["nosmile2.gif",15,15],
		 ":X" : ["shutup.gif",15,16],
		 "8)7" : ["bonk.gif",30,17],
		 "O-)" : ["hypocrite.gif",15,20],
		 "_\/-\\o_" : ["worshippy.gif",29,15]
		};

		if (smilies)
		{
			var smilieHTML = [], options;
			for (var smilie in smilies)
			{
				if (smilies.hasOwnProperty(smilie))
				{
					options = smilies[smilie];
					smilieHTML.push({n:'span',a:{title:smilie,onclick:this.rteAction.bind(this,'plain',' '+smilie+' ')},c:[{n:'img',a:{src:this.forumImageBASEUrl+'smilies/'+options[0],style:'width:'+options[1]+'px;'+'height:'+options[2]+'px;'+'margin-left:'+Math.floor((32-options[1])/2)+'px;'+'margin-top:'+Math.floor((22-options[2])/2)+'px;'}}]});
				}
			}

			var smilieContainer = HTMLBuilder().built({n:'div',a:{id:'smilies'},c:smilieHTML});

			document.body.appendChild(smilieContainer);

			return smilieContainer;
		}

		return false;
	}
});