var bbcode_dir = "http://www.bohlenworld.de/images/bbcode/";
var fontoptions = new Array("Arial", "Arial Black", "Arial Narrow", "Book Antiqua", "Century Gothic", "Comic Sans MS", "Courier New", "Fixedsys", "Franklin Gothic Medium", "Garamond", "Georgia", "Impact", "Lucida Console", "Lucida Sans Unicode", "Microsoft Sans Serif", "Palatino Linotype", "System", "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana");
var sizeoptions = new Array(10, 12, 14, 16, 18, 20, 22, 24, 26, 28);
var justifyoptions = new Array('left','right','justify','center');
var coloroptions = new Array();
	coloroptions = {
	'#000000' : 'Black',
	'#A0522D' : 'Sienna',
	'#556B2F' : 'DarkOliveGreen',
	'#006400' : 'DarkGreen',
	'#483D8B' : 'DarkSlateBlue',
	'#000080' : 'Navy',
	'#4B0082' : 'Indigo',
	'#2F4F4F' : 'DarkSlateGray',
	'#8B0000' : 'DarkRed',
	'#FF8C00' : 'DarkOrange',
	'#808000' : 'Olive',
	'#008000' : 'Green',
	'#008080' : 'Teal',
	'#0000FF' : 'Blue',
	'#708090' : 'SlateGray',
	'#696969' : 'DimGray',
	'#FF0000' : 'Red',
	'#F4A460' : 'SandyBrown',
	'#9ACD32' : 'YellowGreen',
	'#2E8B57' : 'SeaGreen',
	'#48D1CC' : 'MediumTurquoise',
	'#4169E1' : 'RoyalBlue',
	'#800080' : 'Purple',
	'#808080' : 'Gray',
	'#FF00FF' : 'Magenta',
	'#FFA500' : 'Orange',
	'#FFFF00' : 'Yellow',
	'#00FF00' : 'Lime',
	'#00FFFF' : 'Cyan',
	'#00BFFF' : 'DeepSkyBlue',
	'#9932CC' : 'DarkOrchid',
	'#C0C0C0' : 'Silver',
	'#FFC0CB' : 'Pink',
	'#F5DEB3' : 'Wheat',
	'#FFFACD' : 'LemonChiffon',
	'#98FB98' : 'PaleGreen',
	'#AFEEEE' : 'PaleTurquoise',
	'#ADD8E6' : 'LightBlue',
	'#DDA0DD' : 'Plum',
	'#FFFFFF' : 'White'
};
var lastcolor = '#000000';

var smiliewindow_x = 240;
var smiliewindow_y = 280;

phrase['enter_image_url']	= "Bitte gib die URL zu deiner Grafik ein:";
phrase['enter_tag_option']	= "Bitte gib den Parameter für das %1$s Tag ein:";
phrase['enter_email_link']	= "Bitte gib die E-Mail-Adresse ein:";
phrase['enter_link_url']	= "Bitte gib die URL ein:";
phrase['enter_link_text']	= "Gib einen Beschreibungstext für die URL ein (optional):";
phrase['enter_list_type']	= "Listenart wählen: Gib eine '1' für eine nummerierte Liste ein, ein 'a' für eine alphabetische Liste oder lasse das Feld leer für eine unsortierte Liste:";
phrase['enter_list_item']	= "Gib einen Listeneintrag ein.\r\nLasse das Feld leer oder klicke auf 'Abbrechen', um die Liste zu beenden:";


function Text_Editor(editorid)
{
	this.editorid = editorid;
	this.initialized = false;
	this.disabled = false;
	this.popupmode = true;
	
	this.buttons = new Array();
	this.popups = new Array();

	this.textobj = fetch_object(this.editorid + '_textarea');
	this.toolbar = fetch_object(this.editorid + '_toolbar');
	this.smiliebox = fetch_object(this.editorid + '_smiliebox');
	this.codeobj = fetch_object('confirmation_code');

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init = function()
	{
		if (this.initialized)
		{
			return;
		}
		
		if(this.textobj != null)
		{
			this.textobj.disabled = false;
			this.set_editor_contents();
			this.set_editor_functions();
			this.init_controls();

			this.init_smilies(this.smiliebox);

			if (typeof smilie_window != 'undefined' && !smilie_window.closed)
			{
				this.init_smilies(smilie_window.document.getElementById('smilietable'));
			}
		}

		if(this.codeobj != null)
		{
			this.init_confirmation_code();
		}

		this.initialized = true;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.check_focus = function()
	{
		if (!this.editwin.hasfocus)
		{
			this.editwin.focus();
			if (is_opera)
			{
				this.editwin.focus();
			}
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_controls = function()
	{
		if (this.toolbar == null)
		{
			return;
		}

		var buttons = fetch_tags(this.toolbar, 'div');

		for (var i = 0; i < buttons.length; i++)
		{
			if(buttons[i].id)
			{
				if (buttons[i].id.indexOf(this.editorid + '_cmd_resize') != -1)
				{
					this.init_resize_button(buttons[i]);
				}
				else if (buttons[i].id.indexOf(this.editorid + '_cmd_') != -1)
				{
					this.init_command_button(buttons[i]);
				}
				else if (buttons[i].id.indexOf(this.editorid + '_popup_') != -1)
				{
					this.init_popup_button(buttons[i]);
				}
			}
			else if (buttons[i].className == 'separator')
			{
				this.init_separator(buttons[i]);
			}
		}

		set_unselectable(this.toolbar);
		
		this.toolbar.style.display = '';
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_smilies = function(smilie_container)
	{
		if (smilie_container != null)
		{
			var smilies = fetch_tags(smilie_container, 'img');
			for (var i = 0; i < smilies.length; i++)
			{
				if (smilies[i].id && smilies[i].id.indexOf('_smilie_') != false)
				{
					smilies[i].style.cursor = pointer_cursor;
					smilies[i].editorid = this.editorid;
					smilies[i].onclick = Text_Editor_Events.prototype.smilie_onclick;
					smilies[i].unselectable = 'on';
				}
			}
			smilie_container.style.display = '';
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_separator = function(obj)
	{
		var image = document.createElement('img');

		image.src = bbcode_dir + 'separator.gif';
		image.style.width = '5px';
		image.style.height = '20px';
		image.style.margin = '0px';
		obj.appendChild(image);
		obj.style.width = image.style.width;
		obj.style.height = image.style.height;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_resize_button = function(obj)
	{
		obj.cmd = obj.id.substr(obj.id.indexOf('_cmd_') + 5);
		obj.editorid = this.editorid;
		this.buttons[obj.cmd] = obj;

		obj.state = false;
		obj.mode = 'normal';
		
		var image = document.createElement('img');
		
		image.src = bbcode_dir + obj.id.substr(obj.id.indexOf('_cmd_') + 5,8) + '.gif';
		image.style.width = '21px';
		image.style.height = '9px';
		image.style.margin = '0px';
		obj.appendChild(image);
		
		obj.unselectable = 'on';
		obj.style.cursor = pointer_cursor;
		obj.className = 'button_normal';
		obj.style.lineHeight = '9px';
		obj.style.width = image.style.width;
		obj.style.height = image.style.height;
	
		obj.onclick = obj.onmousedown = obj.onmouseover = obj.onmouseout = Text_Editor_Events.prototype.command_button_onmouseevent;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_command_button = function(obj)
	{
		obj.cmd = obj.id.substr(obj.id.indexOf('_cmd_') + 5);
		obj.editorid = this.editorid;
		this.buttons[obj.cmd] = obj;

		obj.state = false;
		obj.mode = 'normal';
		
		var image = document.createElement('img');
		
		image.src = bbcode_dir + obj.cmd.replace(/(insert|justify)_/,"") + '.gif';
		image.style.width = '21px';
		image.style.height = '20px';
		image.style.margin = '0px';
		obj.appendChild(image);
		
		obj.unselectable = 'on';
		obj.style.cursor = pointer_cursor;
		obj.className = 'button_normal';
		obj.style.width = image.style.width;
		obj.style.height = image.style.height;
	
		obj.onclick = obj.onmousedown = obj.onmouseover = obj.onmouseout = Text_Editor_Events.prototype.command_button_onmouseevent;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_popup_button = function(obj)
	{
		obj.cmd = obj.id.substr(obj.id.indexOf('_popup_') + 7);

		if (this.popupmode)
		{
			menu.register(obj.id, true);
			menu.menus[obj.id].open_steps = 5;
			
			obj.editorid = this.editorid;
			obj.state = false;
			this.buttons[obj.cmd] = obj;
			obj.className = 'popup_normal';
			obj.style.cursor = 'default';
			obj.style.height = '20px';
			
			var menupop = document.createElement('img');
			menupop.src = bbcode_dir + 'menupop.gif';
			menupop.style.margin = '1px';
			menupop.style.cursor = pointer_cursor;

			if (obj.cmd == 'fontname')
			{
				this.font_menupop = fetch_object(this.editorid + '_font_menupop');
				this.font_menupop.appendChild(menupop);
				
				this.fontout = fetch_object(this.editorid + '_font_out');
				this.fontout.innerHTML = obj.title;
				this.fontoptions = {'' : this.fontout};

				for (var option in fontoptions)
				{
					var div = document.createElement('div');
					div.id = this.editorid + '_fontoption_' + fontoptions[option];
					div.style.width = this.fontout.style.width;
					div.style.display = 'none';
					div.innerHTML = fontoptions[option];
					this.fontoptions[fontoptions[option]] = this.fontout.parentNode.appendChild(div);
				}
			}
			else if (obj.cmd == 'fontsize')
			{
				this.size_menupop = fetch_object(this.editorid + '_size_menupop');
				this.size_menupop.appendChild(menupop);
				
				this.sizeout = fetch_object(this.editorid + '_size_out');
				this.sizeout.innerHTML = obj.title;
				this.sizeoptions = {'' : this.sizeout};

				for (var option in sizeoptions)
				{
					var div = document.createElement('div');
					div.id = this.editorid + '_sizeoption_' + sizeoptions[option];
					div.style.width = this.sizeout.style.width;
					div.style.display = 'none';
					div.style.textAlign = 'center';
					div.innerHTML = sizeoptions[option];
					this.sizeoptions[sizeoptions[option]] = this.sizeout.parentNode.appendChild(div);
				}
			}
			else if (obj.cmd == 'forecolor')
			{
				this.forecolor_menupop = fetch_object(this.editorid + '_forecolor_menupop');
				this.forecolor_menupop.appendChild(menupop);
			
				this.forecolor_out = fetch_object(this.editorid + '_forecolor_out');
				this.forecolor_bar = fetch_object(this.editorid + '_forecolor_bar');
				
				var image = document.createElement('img');
				image.src = bbcode_dir + 'forecolor.gif';
				image.style.width = '21px';
				image.style.height = '16px';
				image.style.margin = '0px';
				image.style.display = 'block';
				this.forecolor_out.appendChild(image);
				
				var image = document.createElement('img');
				image.src = bbcode_dir + 'blank.gif';
				image.style.width = '21px';
				image.style.height = '4px';
				image.style.margin = '0px';
				image.style.display = 'block';
				image.style.backgroundColor = '#000000';
				image.id = this.editorid + '_forecolor_bar';
				this.forecolor_bar.appendChild(image);
			}
			else if (obj.cmd == 'justify')
			{
				this.justify_menupop = fetch_object(this.editorid + '_justify_menupop');
				this.justify_menupop.appendChild(menupop);
				
				this.justifyout = fetch_object(this.editorid + '_justify_out');
				this.justifyoptions = {'' : this.justifyout};
				
				var image = document.createElement('img');
				image.src = bbcode_dir + 'left.gif';
				image.style.width = '21px';
				image.style.height = '20px';
				image.style.margin = '0px';
				image.style.display = 'block';
				this.justifyout.appendChild(image);

				for (var option in justifyoptions)
				{
					var div = document.createElement('div');
					div.id = this.editorid + '_justifyoption_' + justifyoptions[option];
					div.style.width = this.justifyout.style.width;
					div.style.display = 'none';
					div.style.textAlign = 'center';
					div.innerHTML = justifyoptions[option];
					this.justifyoptions[justifyoptions[option]] = this.justifyout.parentNode.appendChild(div);
				}
			}
			
			obj._onmouseover = obj.onmouseover;
			obj._onclick = obj.onclick;
			obj.onmouseover = obj.onmouseout = obj.onclick = Text_Editor_Events.prototype.popup_button_onmouseevent;

			menu.menus[obj.id]._show = menu.menus[obj.id].show;
			menu.menus[obj.id].show = Text_Editor_Events.prototype.popup_button_show;
		}
		else
		{
			this.build_select(obj);
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.build_select = function(obj)
	{
		var sel = document.createElement('select');
		sel.id = this.editorid + '_select_' + obj.cmd;
		sel.editorid = this.editorid;
		sel.cmd = obj.cmd;

		var opt = document.createElement('option');
		opt.value = '';
		opt.text = obj.title;
		sel.add(opt, is_ie ? sel.options.length : null);

		var opt = document.createElement('option');
		opt.value = '';
		opt.text = ' ';
		sel.add(opt, is_ie ? sel.options.length : null);

		switch (obj.cmd)
		{
			case 'fontname':
			{
				for (var i = 0; i < fontoptions.length; i++)
				{
					var opt = document.createElement('option');
					opt.value = fontoptions[i];
					opt.text = (fontoptions[i].length > 10 ? (fontoptions[i].substr(0, 10) + '...') : fontoptions[i]);
					sel.add(opt, is_ie ? sel.options.length : null);
				}

				sel.onchange = Text_Editor_Events.prototype.formatting_select_onchange;
				break;
			}

			case 'fontsize':
			{
				for (var i = 0; i < sizeoptions.length; i++)
				{
					var opt = document.createElement('option');
					opt.value = sizeoptions[i];
					opt.text = sizeoptions[i];
					sel.add(opt, is_ie ? sel.options.length : null);
				}

				sel.onchange = Text_Editor_Events.prototype.formatting_select_onchange;
				break;
			}

			case 'forecolor':
			{
				for (var i in coloroptions)
				{
					var opt = document.createElement('option');
					opt.value = coloroptions[i];
					opt.text = PHP.trim((coloroptions[i].length > 5 ? (coloroptions[i].substr(0, 5) + '...') : coloroptions[i]).replace(new RegExp('([A-Z])', 'g'), ' $1'));
					opt.style.backgroundColor = i;
					sel.add(opt, is_ie ? sel.options.length : null);
				}

				sel.onchange = Text_Editor_Events.prototype.formatting_select_onchange;
				break;
			}
			
			case 'justify':
			{
				for (var i = 0; i < justifyoptions.length; i++)
				{
					var opt = document.createElement('option');
					opt.value = justifyoptions[i];
					opt.text = justifyoptions[i];
					sel.add(opt, is_ie ? sel.options.length : null);
				}

				sel.onchange = Text_Editor_Events.prototype.formatting_select_onchange;
				break;
			}
		}

		while (obj.hasChildNodes())
		{
			obj.removeChild(obj.firstChild);
		}

		this.buttons[obj.cmd] = obj.appendChild(sel);
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_popup_menu = function(obj)
	{
		if (this.disabled)
		{
			return;
		}

		switch (obj.cmd)
		{
			case 'fontname':
			{
				var menu = this.init_menu_container('fontname', '180px', '250px', 'auto');
				this.build_fontname_popup(obj, menu);
				break;
			}
			case 'fontsize':
			{
				var menu = this.init_menu_container('fontsize', '50px', 'auto', 'auto');
				this.build_fontsize_popup(obj, menu);
				break;
			}
			case 'forecolor':
			{
				var menu = this.init_menu_container('forecolor', 'auto', 'auto', 'visible');
				this.build_forecolor_popup(obj, menu);
				break;
			}
			case 'justify':
			{
				var menu = this.init_menu_container('justify', 'auto', 'auto', 'auto');
				this.build_justify_popup(obj, menu);
				break;
			}
		}

		this.popups[obj.cmd] = this.toolbar.appendChild(menu);

		set_unselectable(menu);
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_menu_container = function(cmd, width, height, overflow)
	{
		var menu = document.createElement('div');

		menu.id = this.editorid + '_popup_' + cmd + '_menu';
		menu.className = 'popup_menu';
		menu.style.display = 'none';
		menu.style.cursor = 'default';
		menu.style.width = width;
		menu.style.height = height;
		menu.style.overflow = overflow;

		return menu;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.build_fontname_popup = function(obj, menu)
	{
		for (var n in fontoptions)
		{
			var option = document.createElement('div');
			option.innerHTML = '<span style="font-family:' + fontoptions[n] + ';font-size:14px">' + fontoptions[n] + '</span>';
			option.className = 'popup_menuitem_normal';
			option.style.padding = '0px 3px 0px 3px';
			option.style.lineHeight = '20px';
			option.style.height = '20px';
			option.style.verticalAlign = 'middle';
			option.style.textAlign = 'left';
			option.title = fontoptions[n];
			option.cmd = obj.cmd;
			option.controlkey = obj.id;
			option.editorid = this.editorid;
			option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = Text_Editor_Events.prototype.menuoption_onmouseevent;
			option.onclick = Text_Editor_Events.prototype.formatting_option_onclick;
			menu.appendChild(option);
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.build_fontsize_popup = function(obj, menu)
	{
		for (var n in sizeoptions)
		{
			var option = document.createElement('div');
			option.innerHTML = '<span style="font-family:verdana;font-size:' + sizeoptions[n] + 'px">' + sizeoptions[n] + '</span>';
			option.style.textAlign = 'center';
			option.className = 'popup_menuitem_normal';
			option.style.padding = '0px 3px 0px 3px';
			option.title = sizeoptions[n];
			option.cmd = obj.cmd;
			option.controlkey = obj.id;
			option.editorid = this.editorid;
			option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = Text_Editor_Events.prototype.menuoption_onmouseevent;
			option.onclick = Text_Editor_Events.prototype.formatting_option_onclick;
			menu.appendChild(option);
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.build_forecolor_popup = function(obj, menu)
	{
		var colorout = fetch_object(this.editorid + '_forecolor_out');
		colorout.editorid = this.editorid;
		colorout.onclick = Text_Editor_Events.prototype.colorout_onclick;

		var table = document.createElement('table');
		table.cellPadding = 0;
		table.cellSpacing = 0;
		table.border = 0;

		var i = 0;
		for (var hex in coloroptions)
		{
			if (i % 8 == 0)
			{
				var tr = table.insertRow(-1);
			}
			i++;

			var div = document.createElement('div');
			div.style.backgroundColor = hex;
			div.style.width = '10px';
			div.style.height = '10px';
			div.style.fontSize = '1px';
			div.style.border = '1px solid #666666';
			div.style.cursor = pointer_cursor;

			var option = tr.insertCell(-1);
			option.style.textAlign = 'center';
			option.className = 'popup_menuitem_normal';
			option.style.padding = '2px';
			option.appendChild(div);
			option.cmd = obj.cmd;
			option.editorid = this.editorid;
			option.controlkey = obj.id;
			option.colorhex = hex;
			option.id = this.editorid + '_color_' + coloroptions[hex];
			option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = Text_Editor_Events.prototype.menuoption_onmouseevent;
			option.onclick = Text_Editor_Events.prototype.coloroption_onclick;
		}

		menu.appendChild(table);
	}
	
	this.build_justify_popup = function(obj, menu)
	{
		for (var n in justifyoptions)
		{
			var option = document.createElement('div');
			
			var image = document.createElement('img');
			image.src = bbcode_dir + justifyoptions[n] + '.gif';
			image.style.width = '21px';
			image.style.height = '20px';
			image.style.margin = '0px';
			image.style.display = 'block';
			option.appendChild(image);
			
			option.className = 'popup_menuitem_normal';
			option.style.padding = '0px 3px 0px 3px';
			option.title = justifyoptions[n];
			option.cmd = obj.cmd;
			option.controlkey = obj.id;
			option.editorid = this.editorid;
			option.justify = justifyoptions[n];
			option.onmouseover = option.onmouseout = option.onmouseup = option.onmousedown = Text_Editor_Events.prototype.menuoption_onmouseevent;
			option.onclick = Text_Editor_Events.prototype.justify_option_onclick;
			menu.appendChild(option);
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.menu_context = function(obj, state)
	{
		if (this.disabled)
		{
			return;
		}

		switch (obj.state)
		{
			case true:
			{
				this.set_control_style(obj, 'button', 'down');
				break;
			}

			default:
			{
				switch (state)
				{
					case 'mouseout':
					{
						this.set_control_style(obj, 'button', 'normal');
						break;
					}
					case 'mousedown':
					{
						this.set_control_style(obj, 'popup', 'down');
						break;
					}
					case 'mouseup':
					case 'mouseover':
					{
						this.set_control_style(obj, 'button', 'hover');
						break;
					}
				}
			}
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.button_context = function(obj, state, controltype)
	{
		if (this.disabled)
		{
			return;
		}

		if (typeof controltype == 'undefined')
		{
			controltype = 'button';
		}

		switch (obj.state)
		{
			case true:
			{
				switch (state)
				{
					case 'mouseover':
					case 'mousedown':
					case 'mouseup':
					{
						this.set_control_style(obj, controltype, 'down');
						break;
					}
					case 'mouseout':
					{
						this.set_control_style(obj, 'button', 'selected');
						break;
					}
				}
				break;
			}

			default:
			{
				switch (state)
				{
					case 'mouseover':
					case 'mouseup':
					{
						this.set_control_style(obj, controltype, 'hover');
						break;
					}
					case 'mousedown':
					{
						this.set_control_style(obj, controltype, 'down');
						break;
					}
					case 'mouseout':
					{
						this.set_control_style(obj, controltype, 'normal');
						break;
					}
				}
				break;
			}
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.set_control_style = function(obj, controltype, mode)
	{
		if (obj.mode != mode)
		{
			obj.mode = mode;
			obj.className = controltype + '_' + obj.mode;
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.format = function(e, cmd, arg)
	{
		e = do_an_e(e);

		if (this.disabled)
		{
			return false;
		}

		if (cmd.substr(0, 6) == 'resize')
		{
			this.resize_editor(parseInt(cmd.substr(9), 10) * (parseInt(cmd.substr(7, 1), 10) == '1' ? 1 : -1));
			return;
		}

		this.check_focus();

		if (cmd.substr(0, 4) == 'wrap')
		{
			var ret = this.wrap_tags(cmd.substr(6), (cmd.substr(4, 1) == '1' ? true : false));
		}
		else if (this[cmd])
		{
			var ret = this[cmd](e);
		}
		else
		{
			try
			{
				var ret = this.apply_format(cmd, false, (typeof arg == 'undefined' ? true : arg));
			}
			catch(e)
			{
				this.handle_error(cmd, e);
				var ret = false;
			}
		}

		this.set_context(cmd);

		this.check_focus();

		return ret;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.wrap_tags = function(tagname, useoption, selection)
	{
		tagname = tagname.toUpperCase();

		if (typeof selection == 'undefined')
		{
			selection = this.get_selection();
			if (selection === false)
			{
				selection = '';
			}
			else
			{
				selection = new String(selection);
			}
		}

		if (useoption === true)
		{
			var option = this.show_prompt(construct_phrase(phrase['enter_tag_option'], ('[' + tagname + ']')), '');
			if (option = this.verify_prompt(option))
			{
				var opentag = '[' + tagname + '=' + option + ']';
			}
			else
			{
				return false;
			}
		}
		else if (useoption !== false)
		{
			var opentag = '[' + tagname + '=' + useoption + ']';
		}
		else
		{
			var opentag = '[' + tagname + ']';
		}

		var closetag = '[/' + tagname + ']';
		var text = opentag + selection + closetag;

		this.insert_text(text, opentag.str_length(), closetag.str_length());

		return false;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.handle_error = function(cmd, e)
	{
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.show_prompt = function(dialogtxt, defaultval)
	{
		return PHP.trim(new String(prompt(dialogtxt, defaultval)));
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.verify_prompt = function(str)
	{
		switch(str)
		{
			case 'http://':
			case 'null':
			case 'undefined':
			case 'false':
			case '':
			case null:
			case false:
				return false;
			default:
				return str;
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.resize_editor = function(change)
	{
		var newheight = parseInt(this.editbox.style.height, 10) + change;

		if (newheight >= 100)
		{
			this.editbox.style.height = newheight + 'px';

			if (change % 99 != 0)
			{
				set_cookie('editor_height', newheight);
			}
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.destroy_popup = function(popupname)
	{
		this.popups[popupname].parentNode.removeChild(this.popups[popupname]);
		this.popups[popupname] = null;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.destroy = function()
	{
		for (var i in this.buttons)
		{
			this.set_control_style(this.buttons[i], 'button', 'normal');
		}

		for (var menu in this.popups)
		{
			this.destroy_popup(menu);
		}

		if (this.fontoptions)
		{
			for (var i in this.fontoptions)
			{
				if (i != '')
				{
					this.fontoptions[i].parentNode.removeChild(this.fontoptions[i]);
				}
			}
			this.fontoptions[''].style.display = '';
		}

		if (this.sizeoptions)
		{
			for (var i in this.sizeoptions)
			{
				if (i != '')
				{
					this.sizeoptions[i].parentNode.removeChild(this.sizeoptions[i]);
				}
			}
			this.sizeoptions[''].style.display = '';
		}
	};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Standard editor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.write_editor_contents = function(text)
	{
		this.textobj.value = text;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.set_editor_contents = function(initial_text)
	{
		this.editwin = this.textobj;
		this.editdoc = this.textobj;
		this.editbox = this.textobj;
		this.spellobj = this.textobj;

		if (typeof initial_text != 'undefined')
		{
			this.write_editor_contents(initial_text);
		}

		this.editdoc.editorid = this.editorid;
		this.editwin.editorid = this.editorid;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.set_editor_functions = function()
	{
		if (this.editdoc.addEventListener)
		{
			this.editdoc.addEventListener('keypress', Text_Editor_Events.prototype.editdoc_onkeypress, false);
		}
		else if (is_ie)
		{
			this.editdoc.onkeydown = Text_Editor_Events.prototype.editdoc_onkeypress;
		}

		this.editwin.onfocus = Text_Editor_Events.prototype.editwin_onfocus;
		this.editwin.onblur = Text_Editor_Events.prototype.editwin_onblur;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.set_context = function()
	{
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.apply_format = function(cmd, dialog, argument)
	{
		switch (cmd)
		{
			case 'bold':
			case 'italic':
			case 'underline':
			case 'strike':
			{
				this.wrap_tags(cmd.substr(0, 1), false);
				return;
			}
			
			case 'justify_left':
			case 'justify_center':
			case 'justify_right':
			case 'justify_justify':
			{
				this.wrap_tags(cmd.substr(8), false);
				return;
			}
			
			case 'justify':
			{
				this.wrap_tags(argument,false);
				return;
			}

			case 'fontname':
			{
				this.wrap_tags('font', argument);
				return;
			}

			case 'fontsize':
			{
				this.wrap_tags('size', argument);
				return;
			}

			case 'forecolor':
			{
				this.wrap_tags('color', argument);
				return;
			}

			case 'link':
			{
				var sel = this.get_selection();
				if (sel)
				{
					this.wrap_tags('url', argument);
				}
				else
				{
					this.wrap_tags('url', argument, argument);
				}
				return;
			}

			case 'image':
			{
				this.wrap_tags('img', false, argument);
				return;
			}

			case 'removeformat':
			{
				return;
			}
			
			default:
			{
				if(cmd !== 'unlink')
				{
					this.wrap_tags(cmd, argument);
				}
				return;
			}
		}

		//alert('cmd :: ' + cmd + '\ndialog :: ' + dialog + '\nargument :: ' + argument);
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.strip_simple = function(tag, str, iterations)
	{
		var opentag = '[' + tag + ']';
		var closetag = '[/' + tag + ']';

		if (typeof iterations == 'undefined')
		{
			iterations = -1;
		}

		while ((startindex = PHP.stripos(str, opentag)) !== false && iterations != 0)
		{
			iterations --;
			if ((stopindex = PHP.stripos(str, closetag)) !== false)
			{
				var text = str.substr(startindex + opentag.length, stopindex - startindex - opentag.length);
				str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
			}
			else
			{
				break;
			}
		}

		return str;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.strip_complex = function(tag, str, iterations)
	{
		var opentag = '[' + tag + '=';
		var closetag = '[/' + tag + ']';

		if (typeof iterations == 'undefined')
		{
			iterations = -1;
		}

		while ((startindex = PHP.stripos(str, opentag)) !== false && iterations != 0)
		{
			iterations --;
			if ((stopindex = PHP.stripos(str, closetag)) !== false)
			{
				var openend = PHP.stripos(str, ']', startindex);
				if (openend !== false && openend > startindex && openend < stopindex)
				{
					var text = str.substr(openend + 1, stopindex - openend - 1);
					str = str.substr(0, startindex) + text + str.substr(stopindex + closetag.length);
				}
				else
				{
					break;
				}
			}
			else
			{
				break;
			}
		}

		return str;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.removeformat = function(e)
	{
		var simplestrip = new Array('b', 'i', 'u', 's');
		var complexstrip = new Array('font', 'color', 'size');

		var str = this.get_selection();
		if (str === false)
		{
			return;
		}

		for (var tag in simplestrip)
		{
			str = this.strip_simple(simplestrip[tag], str);
		}

		for (var tag in complexstrip)
		{
			str = this.strip_complex(complexstrip[tag], str);
		}

		this.insert_text(str);
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.insert_link = function(e, url)
	{
		this.prompt_link('url', url, phrase['enter_link_url'], 'http://');
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.unlink = function(e)
	{
		var sel = this.get_selection();
		sel = this.strip_simple('url', sel);
		sel = this.strip_complex('url', sel);
		this.insert_text(sel);
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.insert_image = function(e, img)
	{
		if (typeof img == 'undefined')
		{
			img = this.show_prompt(phrase['enter_image_url'], 'http://');
		}
		if (img = this.verify_prompt(img))
		{
			return this.apply_format('image', false, img);
		}
		else
		{
			return false;
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.insert_email = function(e, email)
	{
		this.prompt_link('email', email, phrase['enter_email_link'], '');
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.insert_smilie = function(e, smilietext)
	{
		this.check_focus();
		
		smilietext = ' '+smilietext+' ';
		return this.insert_text(smilietext, smilietext.length, 0);
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.prompt_link = function(tagname, value, phrase, iprompt)
	{
		if (typeof value == 'undefined')
		{
			value = this.show_prompt(phrase, iprompt);
		}
		if ((value = this.verify_prompt(value)) !== false)
		{
			if (this.get_selection())
			{
				this.apply_format('unlink');
				this.wrap_tags(tagname, value);
			}
			else
			{
				this.wrap_tags(tagname, value, value);
			}
		}
		return true;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.get_editor_contents = function()
	{
		return this.editdoc.value;
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.get_selection = function()
	{
		if (typeof(this.editdoc.selectionStart) != 'undefined')
		{
			return this.editdoc.value.substr(this.editdoc.selectionStart, this.editdoc.selectionEnd - this.editdoc.selectionStart);
		}
		else if (document.selection && document.selection.createRange)
		{
			return document.selection.createRange().text;
		}
		else if (window.getSelection)
		{
			return window.getSelection() + '';
		}
		else
		{
			return false;
		}
	};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.init_confirmation_code = function()
	{
		this.codeobj.onclick = Text_Editor_Events.prototype.codeobj_onclick;
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.insert_text = function(text, movestart, moveend)
	{
		this.check_focus();

		if (typeof(this.editdoc.selectionStart) != 'undefined')
		{
			var opn = this.editdoc.selectionStart + 0;
			var scrollpos = this.editdoc.scrollTop;

			this.editdoc.value = this.editdoc.value.substr(0, this.editdoc.selectionStart) + text + this.editdoc.value.substr(this.editdoc.selectionEnd);

			if (movestart === false)
			{
				// do nothing
			}
			else if (typeof movestart != 'undefined')
			{
				this.editdoc.selectionStart = opn + movestart;
				this.editdoc.selectionEnd = opn + text.str_length() - moveend;
			}
			else
			{
				this.editdoc.selectionStart = opn;
				this.editdoc.selectionEnd = opn + text.str_length();
			}
			this.editdoc.scrollTop = scrollpos;
		}
		else if (document.selection && document.selection.createRange)
		{
			var sel = document.selection.createRange();
			sel.text = text.replace(/\r?\n/g, '\r\n');

			if (movestart === false)
			{
				// do nothing
			}
			else if (typeof movestart != 'undefined')
			{
				sel.moveStart('character', -text.str_length() +movestart);
				sel.moveEnd('character', -moveend);
			}
			else
			{
				sel.moveStart('character', -text.str_length());
			}
			sel.select();
		}
		else
		{
			this.editdoc.value += text;
		}
	};
	this.init();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Editor event handler functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Text_Editor_Events()
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.codeobj_onclick = function(e)
{
	var now = new Date();
	this.src = this.src + '?' + now.getTime();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.smilie_onclick = function(e)
{
	Editor[this.editorid].insert_smilie(e,
		this.alt,
		this.src,
		this.id.substr(this.id.lastIndexOf('_') + 1)
	);

	if (typeof smilie_window != 'undefined' && !smilie_window.closed)
	{
		smilie_window.focus();
	}

	return false;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.command_button_onmouseevent = function(e)
{
	e = do_an_e(e);

	if (e.type == 'click')
	{
		Editor[this.editorid].format(e, this.cmd, false, true);
	}

	Editor[this.editorid].button_context(this, e.type);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.popup_button_onmouseevent = function(e)
{
	e = do_an_e(e);

	if (e.type == 'click')
	{
		this._onclick(e);
		Editor[this.editorid].menu_context(this, 'mouseover');
	}
	else
	{
		Editor[this.editorid].menu_context(this, e.type);
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.popup_button_show = function(obj, instant)
{
	if (typeof Editor[obj.editorid].popups[obj.cmd] == 'undefined' || Editor[obj.editorid].popups[obj.cmd] == null)
	{
		Editor[obj.editorid].init_popup_menu(obj);
	}
	this._show(obj, instant);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.formatting_select_onchange = function(e)
{
	var arg = this.options[this.selectedIndex].value;
	if (arg != '')
	{
		Editor[this.editorid].format(e, this.cmd, arg);
	}
	this.selectedIndex = 0;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.menuoption_onmouseevent = function(e)
{
	e = do_an_e(e);
	Editor[this.editorid].button_context(this, e.type, 'popup_menuitem');
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.formatting_option_onclick = function(e)
{
	Editor[this.editorid].format(e, this.cmd, this.firstChild.innerHTML);
	menu.hide();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.justify_option_onclick = function(e)
{
	Editor[this.editorid].format(e, this.cmd, this.justify);
	menu.hide();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.coloroption_onclick = function(e)
{
	lastcolor = this.colorhex;
	fetch_object(this.editorid + '_forecolor_bar').style.backgroundColor = this.colorhex;
	Editor[this.editorid].format(e, this.cmd, this.colorhex);
	menu.hide();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.colorout_onclick = function(e)
{
	e = do_an_e(e);
	Editor[this.editorid].format(e, 'forecolor', lastcolor);
	return false;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.editwin_onfocus = function(e)
{
	this.hasfocus = true;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.editwin_onblur = function(e)
{
	this.hasfocus = false;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.editdoc_onmouseup = function(e)
{
	Editor[this.editorid].set_context();
	if (Editor[this.editorid].popupmode)
	{
		menu.hide();
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.editdoc_onkeyup = function(e)
{
	Editor[this.editorid].set_context();
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Text_Editor_Events.prototype.editdoc_onkeypress = function(e)
{
	if (!e)
	{
		e = window.event;
	}

	if (e.ctrlKey)
	{
		var code = e.charCode ? e.charCode : e.keyCode;
		switch (String.fromCharCode(code).toLowerCase())
		{
			case 'b': cmd = 'bold'; break;
			case 'i': cmd = 'italic'; break;
			case 'u': cmd = 'underline'; break;
			case 's': cmd = 'strike'; break;
			default: return;
		}

		e = do_an_e(e);
		Editor[this.editorid].apply_format(cmd, false, null);
		return false;
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
