/**
 * @author Ryan Johnson <ryan@livepipe.net>
 * @copyright 2007 LivePipe LLC
 * @package Control.TextArea.ToolBar.Markdown
 * @license MIT
 * @url http://livepipe.net/projects/control_textarea/
 * @version 1.0.1
 */

Control.TextArea.ToolBar.Markdown = Class.create();
Object.extend(Control.TextArea.ToolBar.Markdown.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));
		}

		//buttons
		this.toolbar.addButton('Italics',function(){
			this.wrapSelection('[i]','[/i]');
		},{
			id: 'markdown_italics_button'
		});

		this.toolbar.addButton('Bold',function(){
			this.wrapSelection('[b]','[/b]');
		},{
			id: 'markdown_bold_button'
		});

		this.toolbar.addButton('Underline',function(){
			this.wrapSelection('[u]','[/u]');
		},{
			id: 'markdown_underline_button'
		});

		this.toolbar.addButton('smail_1',function(){
			this.wrapSelection(':))','');
		},{
			id: 'markdown_1_smyle'
		});

		this.toolbar.addButton('smail_2',function(){
			this.wrapSelection(';:)','');
		},{
			id: 'markdown_2_smyle'
		});

		this.toolbar.addButton('smail_3',function(){
			this.wrapSelection(':)','');
		},{
			id: 'markdown_3_smyle'
		});

		this.toolbar.addButton('smail_4',function(){
			this.wrapSelection(';)','');
		},{
			id: 'markdown_4_smyle'
		});

		this.toolbar.addButton('smail_5',function(){
			this.wrapSelection(':\(','');
		},{
			id: 'markdown_5_smyle'
		});

		this.toolbar.addButton('smail_6',function(){
			this.wrapSelection(':~','');
		},{
			id: 'markdown_6_smyle'
		});

		this.toolbar.addButton('smail_7',function(){
			this.wrapSelection(':ff','');
		},{
			id: 'markdown_7_smyle'
		});

		this.toolbar.addButton('smail_8',function(){
			this.wrapSelection(':o','');
		},{
			id: 'markdown_8_smyle'
		});

		this.toolbar.addButton('smail_9',function(){
			this.wrapSelection(':ups','');
		},{
			id: 'markdown_9_smyle'
		});

		this.toolbar.addButton('smail_10',function(){
			this.wrapSelection('B-)','');
		},{
			id: 'markdown_10_smyle'
		});


	}
});