/**
 * @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('Kursiv',function(){
			this.wrapSelection('*','*');
		},{
			id: textarea+'_markdown_italics_button'
		});
		$(textarea+'_markdown_italics_button').writeAttribute("title", "Kursiv" );
		$(textarea+'_markdown_italics_button').writeAttribute("class", "markdown_italics_button" );
		
		
		this.toolbar.addButton('Fett',function(){
			this.wrapSelection('**','**');
		},{
			id: textarea+'_markdown_bold_button'
		});
		$(textarea+'_markdown_bold_button').writeAttribute("title", "Fett" );
		$(textarea+'_markdown_bold_button').writeAttribute("class", "markdown_bold_button" );
		
		
		/*this.toolbar.addButton('Titel',function(){
			var selection = this.getSelection();
			if(selection == '')
				selection = 'Titel';
			var str = '';
			(Math.max(5,selection.length)).times(function(){
				str += '-';
			});
			this.replaceSelection("\n" + selection + "\n" + str + "\n");
		},{
			id: textarea+'_markdown_heading_button'
		});
		$(textarea+'_markdown_heading_button').writeAttribute("title", "Ueberschrift" );
		$(textarea+'_markdown_heading_button').writeAttribute("class", "markdown_heading_button" );*/
		
		
		this.toolbar.addButton('Link',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Link URL','');
			if(response == null)
				return;
			this.replaceSelection('[' + (selection == '' ? 'Link Text' : selection) + '](' + (response == '' ? 'http://link_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ')');
		},{
			id: textarea+'_markdown_link_button'
		});
		$(textarea+'_markdown_link_button').writeAttribute("title", "Link einfuegen" );
		$(textarea+'_markdown_link_button').writeAttribute("class", "markdown_link_button" );
		
		
		/*this.toolbar.addButton('Bild',function(){
			var selection = this.getSelection();
			var response = prompt('Enter Image URL','');
			if(response == null)
				return;
			this.replaceSelection('![' + (selection == '' ? 'Image Alt Text' : selection) + '](' + (response == '' ? 'http://bild_url/' : response).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ')');
		},{
			id: textarea+'_markdown_image_button'
		});
		$(textarea+'_markdown_image_button').writeAttribute("title", "Bild einfuegen" );
		$(textarea+'_markdown_image_button').writeAttribute("class", "markdown_image_button" );*/
		
		/*this.toolbar.addButton('Horizontale Linie',function(){
			var selection = this.getSelection();
			this.replaceSelection("\n___\n");
		},{
			id: textarea+'_markdown_hr_button'
		});
		$(textarea+'_markdown_hr_button').writeAttribute("title", "Horizontale Linie einfuegen" );
		$(textarea+'_markdown_hr_button').writeAttribute("class", "markdown_hr_button" );*/
		
		this.toolbar.addButton('Ungeordnete Liste',function(event){  
			this.collectFromEachSelectedLine(function(line){  
				return event.shiftKey ? (line.match(/^\*{2,}/) ? line.replace(/^\*/,'') : line.replace(/^\*\s/,'')) : (line.match(/\*+\s/) ? '*' : '* ') + line;  
			});  
		},{  
			id: textarea+'_markdown_unordered_list_button'
		});
		$(textarea+'_markdown_unordered_list_button').writeAttribute("title", "Ungeordnete Liste" );
		$(textarea+'_markdown_unordered_list_button').writeAttribute("class", "markdown_unordered_list_button" );
		
		this.toolbar.addButton('Geordnete Liste',function(event){  
			var i = 0;  
			this.collectFromEachSelectedLine(function(line){  
				if(!line.match(/^\s+$/)){  
					++i;  
					return event.shiftKey ? line.replace(/^\d+\.\s/,'') : (line.match(/\d+\.\s/) ? '' : i + '. ') + line;  
				}  
			});  
		},{  
			id: textarea+'_markdown_ordered_list_button'  
		});
		$(textarea+'_markdown_ordered_list_button').writeAttribute("title", "Geordnete Liste" );
		$(textarea+'_markdown_ordered_list_button').writeAttribute("class", "markdown_ordered_list_button" );
		
		this.toolbar.addButton('Zitat',function(event){  
			this.collectFromEachSelectedLine(function(line){  
				return event.shiftKey ? line.replace(/^\> /,'') : '> ' + line;  
			});  
		},{  
			id: textarea+'_markdown_quote_button'  
		});
		$(textarea+'_markdown_quote_button').writeAttribute("title", "Zitat" );
		$(textarea+'_markdown_quote_button').writeAttribute("class", "markdown_quote_button" );
		
		/*this.toolbar.addButton('Vorformatiert',function(event){  
			this.collectFromEachSelectedLine(function(line){  
				return event.shiftKey ? line.replace(/    /,'') : '    ' + line;  
			});  
		},{  
			id: textarea+'_markdown_code_button'  
		});
		$(textarea+'_markdown_code_button').writeAttribute("title", "Vorformatiert" );
		$(textarea+'_markdown_code_button').writeAttribute("class", "markdown_code_button" );*/
		
		this.toolbar.addButton('Help',function(){
			window.open('/url_handmade/markdownhelp.php','Hilfe','width=800,height=600,top=200,left=300,scrollbars=yes');
		},{
			id: textarea+'_markdown_help_button'
		});
		$(textarea+'_markdown_help_button').writeAttribute("title", "Die Hilfe-Seite aufrufen" );
		$(textarea+'_markdown_help_button').writeAttribute("class", "markdown_help_button" );
	}
});