
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('Bold',function(){
			this.wrapSelection('[bf:',']');
		},{
			id: 'markdown_bold_button'
		});

		this.toolbar.addButton('Italics',function(){
			this.wrapSelection('[em:',']');
		},{
			id: 'markdown_italics_button'
		});
		
		this.toolbar.addButton('Underline',function(){
			this.wrapSelection('[u:',']');
		},{
			id: 'markdown_underline_button'
		});
		
		this.toolbar.addButton('Delete',function(){
			this.wrapSelection('[del:',']');
		},{
			id: 'markdown_delete_button'
		});
		
		this.toolbar.addButton('Sub',function(){
			this.wrapSelection('[sub:',']');
		},{
			id: 'markdown_sub_button'
		});
		
		this.toolbar.addButton('Sup',function(){
			this.wrapSelection('[sup:',']');
		},{
			id: 'markdown_sup_button'
		});
		
		this.toolbar.addButton('Link',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Link URL','');
			if(response == null)
				return;
			this.replaceSelection('[' + (response == '' ? 'http://link_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ':' + (selection == '' ? 'Link Text' : selection) + ']');
		},{
			id: 'markdown_link_button'
		});
		
		this.toolbar.addButton('Heading',function(){
			this.injectEachSelectedLine(function(lines,line){
				lines.push((event.shiftKey ? (line.match(/^\*{2,}/) ? line.replace(/^\*/,'') : line.replace(/^\*\s/,'')) : (line.match(/\*+\s/) ? '-' : '- ') + line));
				return lines;
			});
		},{
			id: 'markdown_heading_button'
		});
		
		
		this.toolbar.addButton('Unordered List',function(event){
			this.injectEachSelectedLine(function(lines,line){
				lines.push((event.shiftKey ? (line.match(/^\-{2,}/) ? line.replace(/^\-/,'') : line.replace(/^\-\s/,'')) : (line.match(/\-+\s/) ? '-' : '- ') + line));
				return lines;
			});
		},{
			id: 'markdown_unordered_list_button'
		});
		
		this.toolbar.addButton('Block Quote',function(event){
			this.wrapSelection('>>\n','\n<<');
		},{
			id: 'markdown_quote_button'
		});
		
		this.toolbar.addButton('Code Block',function(){
			this.wrapSelection('>||\n','\n||<');
		},{
			id: 'markdown_code_button'
		});
		
		this.toolbar.addButton('Footnote',function(){
			this.wrapSelection('((','))');
		},{
			id: 'markdown_footnote_button'
		});
		
		this.toolbar.addButton('Help',function(){
			window.open('http://worstman.net/blog/');
		},{
			id: 'markdown_help_button'
		});
	}
});
