function listInjector(lines, line)
{
	lines.push((line.match(/^[\s\t]*<li>/i) ? '' : '<li>') + line + (line.match(/<\/li>[\s\t]*$/i) ? '' : '</li>'));
	return lines;
}

var textarea = new Array();
var toolbar = new Array();
var lastControl = -1;
var menuOptions = {
	width: 'auto',
	border: 1,
	radius: 1,
	stem: 'topMiddle',
	hook: { tip: 'topMiddle', target: 'bottomMiddle' },
	showOn: 'click',
	hideOn: 'click'
};
var num = 0;

var color_button = new Array();
var emoticon_button = new Array();
var font_button = new Array();
var size_button = new Array();
function FrutToolbarMinimal(toolbar)
{
	toolbar.addButton('', function(){
		this.wrapSelection('<b>','</b>');
	},{ className: 'bold_button', title: getText('bold') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<i>','</i>');
	},{ className: 'italic_button', title: getText('italic') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<u>','</u>');
	},{ className: 'underline_button', title: getText('underline') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<s>','</s>');
	},{ className: 'strikethrough_button', title: getText('strikethrough') });
	
	num = font_button.length;
	toolbar.container.insert('<li><a href="#" id="font_button' + num + '" class="font_button" title="' + getText('text_font') + '" onclick="return false;" /></li>');
	font_button[num] = $('font_button' + num);
	new Tip(font_button[num],
		'<a class="list_cell" style="font-family:serif;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-family:serif;&quot;>\',\'</span>\');font_button[' + num + '].prototip.hide();return false;">Sans</a>' +
		'<a class="list_cell" style="font-family:sans-serif;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-family:sans-serif;&quot;>\',\'</span>\');font_button[' + num + '].prototip.hide();return false;">Serif</a>' +
		'<a class="list_cell" style="font-family:cursive;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-family:cursive;&quot;>\',\'</span>\');font_button[' + num + '].prototip.hide();return false;">Cursive</a>' +
		'<a class="list_cell" style="font-family:fantasy;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-family:fantasy;&quot;>\',\'</span>\');font_button[' + num + '].prototip.hide();return false;">Fantasy</a>' +
		'<a class="list_cell" style="font-family:monospace;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-family:monospace;&quot;>\',\'</span>\');font_button[' + num + '].prototip.hide();return false;">Mono</a>' +
		'<div class="clear"></div>'
	, menuOptions);
	
	num = size_button.length;
	toolbar.container.insert('<li><a href="#" id="size_button' + num + '" class="size_button" title="' + getText('text_size') + '" onclick="return false;" /></li>');
	size_button[num] = $('size_button' + num);
	new Tip(size_button[num],
		'<a class="list_cell" style="font-size:xx-small;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:xx-small;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">XXS</a>' +
		'<a class="list_cell" style="font-size:x-small;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:x-small;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">XS</a>' +
		'<a class="list_cell" style="font-size:small;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:small;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">S</a>' +
		'<a class="list_cell" style="font-size:medium;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:medium;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">M</a>' +
		'<a class="list_cell" style="font-size:large;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:large;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">L</a>' +
		'<a class="list_cell" style="font-size:x-large;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:x-large;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">XL</a>' +
		'<a class="list_cell" style="font-size:xx-large;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;font-size:xx-large;&quot;>\',\'</span>\');size_button[' + num + '].prototip.hide();return false;">XXL</a>' +
		'<div class="clear"></div>'
	, menuOptions);
	
	num = color_button.length;
	toolbar.container.insert('<li><a href="#" id="color_button' + num + '" class="color_button" title="' + getText('text_color') + '" onclick="return false;" /></li>');
	color_button[num] = $('color_button' + num);
	new Tip(color_button[num],
		'<a class="palette_cell" style="background-color:#000;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#000;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#00f;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#00f;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#0f0;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#0f0;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#0ff;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#0ff;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="palette_cell" style="background-color:#f00;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#f00;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#f0f;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#f0f;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#f90;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#f90;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#999;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#999;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="palette_cell" style="background-color:#666;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#666;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#66f;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#66f;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#6f6;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#6f6;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#6ff;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#6ff;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="palette_cell" style="background-color:#f66;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#f66;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#f6f;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#f6f;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#ff6;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#ff6;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="palette_cell" style="background-color:#fff;" href="#" onclick="textarea[' + lastControl + '].wrapSelection(\'<span style=&quot;color:#fff;&quot;>\',\'</span>\');color_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>'
	, menuOptions);
	
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		var response = prompt(getText('link_prompt') + ':', 'http://');
		if (response == null)
			return;
		this.replaceSelection(
			'<a href="' + (response == '' ? '#' : (response.match(/^([a-z]+:\/\/|#)/i) ? '' : 'http://') + response) + '">' +
			(selection == '' ? '' : selection) + '</a>'
		);
	},{ className: 'link_button', title: getText('link') });
	
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		var response = prompt(getText('image_prompt') + ':', 'http://');
		if (response == null)
			return;
		this.replaceSelection(
			'<img src="' + (response == '' ? '#' : (response.match(/^([a-z]+:\/\/|#)/i) ? '' : 'http://') + response) + '" alt="' +
			(selection == '' ? '' : selection) + '" />'
		);
	},{ className: 'image_button', title: getText('image') });
	
	num = emoticon_button.length;
	toolbar.container.insert('<li><a href="#" id="emoticon_button' + num + '" class="emoticon_button" title="' + getText('emoticon') + '" onclick="return false;" /></li>');
	emoticon_button[num] = $('emoticon_button' + num);
	new Tip(emoticon_button[num],
		'<a class="button_cell" href="#" id="emot_smile_button" title=":)" onclick="textarea[' + lastControl + '].replaceSelection(\' :)\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_sad_button" title=":(" onclick="textarea[' + lastControl + '].replaceSelection(\' :(\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_grin_button" title=":D" onclick="textarea[' + lastControl + '].replaceSelection(\' :D\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_glasses_button" title="8)" onclick="textarea[' + lastControl + '].replaceSelection(\' 8)\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="button_cell" href="#" id="emot_surprise_button" title=":o" onclick="textarea[' + lastControl + '].replaceSelection(\' :o\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_wink_button" title=";)" onclick="textarea[' + lastControl + '].replaceSelection(\' ;)\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_crying_button" title=";(" onclick="textarea[' + lastControl + '].replaceSelection(\' ;(\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_plain_button" title=":|" onclick="textarea[' + lastControl + '].replaceSelection(\' :|\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="button_cell" href="#" id="emot_kiss_button" title=":*" onclick="textarea[' + lastControl + '].replaceSelection(\' :*\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_happy_button" title=":P" onclick="textarea[' + lastControl + '].replaceSelection(\' :P\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_devil_button" title="(devil)" onclick="textarea[' + lastControl + '].replaceSelection(\' (devil)\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="emot_angel_button" title="(angel)" onclick="textarea[' + lastControl + '].replaceSelection(\' (angel)\');emoticon_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>'
	, menuOptions);
}

var heading_button = new Array();
function FrutToolbarStandard(toolbar)
{
	toolbar.addButton('', function(){
		this.wrapSelection('<div style="text-align:left">\n','\n</div>');
	},{ className: 'align_left_button', title: getText('align_left') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<div style="text-align:center">\n','\n</div>');
	},{ className: 'align_center_button', title: getText('align_center') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<div style="text-align:right">\n','\n</div>');
	},{ className: 'align_right_button', title: getText('align_right') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<div style="text-align:justify">\n','\n</div>');
	},{ className: 'align_justify_button', title: getText('align_justify') });
	
	num = heading_button.length;
	toolbar.container.insert('<li><a href="#" id="heading_button' + num + '" class="heading_button" title="' + getText('heading') + '" onclick="return false;" /></li>');
	heading_button[num] = $('heading_button' + num);
	new Tip(heading_button[num],
		'<a class="button_cell" href="#" id="heading_1_button" title="' + getText('heading') + ' 1" onclick="textarea[' + lastControl + '].wrapSelection(\'<h1>\',\'</h1>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="heading_2_button" title="' + getText('heading') + ' 2" onclick="textarea[' + lastControl + '].wrapSelection(\'<h2>\',\'</h2>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="heading_3_button" title="' + getText('heading') + ' 3" onclick="textarea[' + lastControl + '].wrapSelection(\'<h3>\',\'</h3>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>' +
		'<a class="button_cell" href="#" id="heading_4_button" title="' + getText('heading') + ' 4" onclick="textarea[' + lastControl + '].wrapSelection(\'<h4>\',\'</h4>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="heading_5_button" title="' + getText('heading') + ' 5" onclick="textarea[' + lastControl + '].wrapSelection(\'<h5>\',\'</h5>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<a class="button_cell" href="#" id="heading_6_button" title="' + getText('heading') + ' 6" onclick="textarea[' + lastControl + '].wrapSelection(\'<h6>\',\'</h6>\');heading_button[' + num + '].prototip.hide();return false;"></a>' +
		'<div class="clear"></div>'
	, menuOptions);
	
	toolbar.addButton('', function(event){
		this.injectEachSelectedLine(listInjector, '<ul>\n', '\n</ul>');
	},{ className: 'unordered_list_button', title: getText('unordered_list') });
	
	toolbar.addButton('', function(event){
		this.injectEachSelectedLine(listInjector, '<ol>\n', '\n</ol>');
	},{ className: 'ordered_list_button', title: getText('ordered_list') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<div style="margin-left:10px">\n','\n</div>');
	},{ className: 'indent_add_button', title: getText('indent_add') });
}

function FrutToolbarAdvanced(toolbar)
{
	toolbar.addButton('', function(){
		this.wrapSelection('<sup>','</sup>');
	},{ className: 'superscript_button', title: getText('superscript') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<sub>','</sub>');
	},{ className: 'subscript_button', title: getText('subscript') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<span style="text-transform:uppercase">','</span>');
	},{ className: 'allcaps_button', title: getText('allcaps') });
	
	toolbar.addButton('', function(){
		this.wrapSelection('<span style="font-variant:small-caps">','</span>');
	},{ className: 'smallcaps_button', title: getText('smallcaps') });
}

function FrutToolbarPost(toolbar)
{
	toolbar.addButton('', function(){
		if ((postBlurb != null))
		{
			postBlurb.value = this.getSelection();
			this.replaceSelection('');
			postBlurb.focus();
		}
	},{ className: 'cut_button', title: getText('cut') });
}

function FrutToolbarExtra(toolbar)
{
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		if (selection == '')
		{
			selection = prompt(getText('user_prompt') + ':', '');
			if (selection == null)
				return;
		}
		this.replaceSelection('<user>' + selection + '</user>');
	},{ className: 'user_link_button', title: getText('user_link') });
	
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		if (selection == '')
		{
			selection = prompt(getText('video_prompt') + ':', 'http://');
			if (selection == null)
				return;
		}
		this.replaceSelection('<video>' + selection + '</video>');
	},{ className: 'frut_video_button', title: getText('frut_video') });
	
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		if (selection == '')
		{
			selection = prompt(getText('video_prompt') + ':', 'http://');
			if (selection == null)
				return;
		}
		this.replaceSelection('<youtube>' + selection + '</youtube>');
	},{ className: 'youtube_video_button', title: getText('youtube_video') });
	
	toolbar.addButton('', function(){
		var selection = this.getSelection();
		if (selection == '')
		{
			selection = prompt(getText('video_prompt') + ':', 'http://');
			if (selection == null)
				return;
		}
		this.replaceSelection('<vimeo>' + selection + '</vimeo>');
	},{ className: 'vimeo_video_button', title: getText('vimeo_video') });
}

function FrutNewTextArea(element)
{
	lastControl++;
	textarea[lastControl] = new Control.TextArea(element);
	toolbar[lastControl] = new Control.TextArea.ToolBar(textarea[lastControl]);
	toolbar[lastControl].container.className = 'textarea_toolbar';
}

function FrutMiniTextArea(element)
{
	FrutNewTextArea(element);
	FrutToolbarMinimal(toolbar[lastControl]);
	FrutToolbarExtra(toolbar[lastControl]);
}

function FrutChatTextArea(element)
{
	FrutNewTextArea(element);
	var temp = menuOptions;
	menuOptions.stem = 'bottomMiddle';
	menuOptions.hook = { tip: 'bottomMiddle', target: 'topMiddle' };
	FrutToolbarMinimal(toolbar[lastControl]);
	menuOptions = temp;
	//FrutToolbarExtra(toolbar[lastControl]);
}

function FrutMessageTextArea(element)
{
	FrutNewTextArea(element);
	FrutToolbarMinimal(toolbar[lastControl]);
	FrutToolbarStandard(toolbar[lastControl]);
	FrutToolbarAdvanced(toolbar[lastControl]);
}

function FrutTextArea(element)
{
	FrutNewTextArea(element);
	FrutToolbarMinimal(toolbar[lastControl]);
	FrutToolbarStandard(toolbar[lastControl]);
	FrutToolbarAdvanced(toolbar[lastControl]);
	FrutToolbarExtra(toolbar[lastControl]);
}

function FrutPostTextArea(element)
{
	FrutNewTextArea(element);
	FrutToolbarMinimal(toolbar[lastControl]);
	FrutToolbarStandard(toolbar[lastControl]);
	FrutToolbarAdvanced(toolbar[lastControl]);
	FrutToolbarPost(toolbar[lastControl]);
	FrutToolbarExtra(toolbar[lastControl]);
}