Control.TextArea.ToolBar.Html = Class.create();
Object.extend(Control.TextArea.ToolBar.Html.prototype,{
	textarea: false,
	toolbar: false,
	options: {},
	initialize: function(textarea,options){
		this.textarea = new Control.TextArea(textarea);
		this.toolbar = new Control.TextArea.ToolBar(this.textarea);
		this.converter = (typeof(Showdown) != 'undefined') ? new Showdown.converter : false;
		this.options = {
			preview: false,
			afterPreview: Prototype.emptyFunction
		};
		Object.extend(this.options,options || {});
		if(this.options.preview){
			this.textarea.observe('change',function(textarea){
				if(this.converter){
					$(this.options.preview).update(this.converter.makeHtml(textarea.getValue()));
					this.options.afterPreview();
				}
			}.bind(this));
		}
		this.toolbar.addButton('Bold',function(){
			this.wrapSelection('<strong>','</strong>');
		},{
			id: 'bold_button'
		});
		this.toolbar.addButton('Italics',function(){
			this.wrapSelection('<em>','</em>');
		},{
			id: 'italics_button'
		});
		this.toolbar.addButton('Unordered List',function(event){
			this.wrapSelection('<ul>','</ul>');
		},{
			id: 'unordered_list_button'
		});
		this.toolbar.addButton('Ordered List',function(event){
			this.wrapSelection('<ol>','</ol>');
		},{
			id: 'ordered_list_button'
		});
		this.toolbar.addButton('Bullet',function(event){
			this.wrapSelection('<li>','</li>');
		},{
			id: 'bullet_button'
		});
		this.toolbar.addButton('Link',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Link URL','');
			if(response == null)
				return;
			this.replaceSelection('<a href="' + (response == '' ? 'http://link_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + '">' + (selection == '' ? 'Link Text' : selection) + '</a>');
		},{
			id: 'link_button'
		});
	}
});