/**
 * @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.CommentMenu = Class.create();
Object.extend(Control.TextArea.ToolBar.CommentMenu.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,
			emoticons: true,
			photo: true,
			video: true
		};
		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('B',function(){
			this.wrapSelection('[b]','[/b]');
		},{
			id: 'comment_bold'
		});

		this.toolbar.addButton('I',function(){
			this.wrapSelection('[i]','[/i]');
		},{
			id: 'comment_italic'
		});
		
		this.toolbar.addButton('U',function(){
			this.wrapSelection('[u]','[/u]');
		},{
			id: 'comment_underline'
		});*/
			
		this.toolbar.addButton('Bold',function(){
			clearCommentTextarea($('comment_body'));
			var selection = this.getSelection();
			if(selection.length == 0) {
				selection = prompt('Enter Your Text','');
				if(selection.length == 0) return;
			}
			this.replaceSelection('[b]' + selection + ['[/b]']);
		},{
			id: 'comment_bold'
		});
		
		this.toolbar.addButton('Italic',function(){
			clearCommentTextarea($('comment_body'));
			var selection = this.getSelection();
			if(selection.length == 0) {
				selection = prompt('Enter Your Text','');
				if(selection.length == 0) return;
			}
			this.replaceSelection('[i]' + selection + ['[/i]']);
		},{
			id: 'comment_italic'
		});
			
		this.toolbar.addButton('Underline',function(){
			clearCommentTextarea($('comment_body'));
			var selection = this.getSelection();
			if(selection.length == 0) {
				selection = prompt('Enter Your Text','');
				if(selection.length == 0) return;
			}
			this.replaceSelection('[u]' + selection + ['[/u]']);
		},{
			id: 'comment_underline'
		});
		
		this.toolbar.addButton('Link',function(){
			clearCommentTextarea($('comment_body'));
			var selection = this.getSelection();
			if(selection.length == 0) {
				selection = prompt('Enter Visible Link Text','');
				if(selection.length == 0) return;
			}
			var url = prompt('Enter Link URL','');
			if(url == null) return;
			this.replaceSelection('[url=' + (url == '' ? 'http://link_url/' : url).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ']' + selection + ['[/url]']);
		},{
			id: 'comment_link'
		});
		
		if(this.options.photo) {
			this.toolbar.addButton('Photo',function(){
				clearCommentTextarea($('comment_body'));
				var selection = this.getSelection();
				if(selection.length == 0) {
					selection = prompt('Enter Photo URL','');
					if(selection == null) return;
				}
				this.replaceSelection('[img]' + (selection == '' ? 'http://link_url/' : selection).replace(/^(?!(f|ht)tps?:\/\/)/,'http://') + ['[/img]']);
			},{
				id: 'comment_photo'
			});
		}
		
		if(this.options.video) {
			this.toolbar.addButton('Video',function(){
				clearCommentTextarea($('comment_body'));
				var selection = this.getSelection();
				if(selection.length == 0) {
					selection = prompt('Enter Video Embed Code','');
					if(selection.length == 0) return;
					this.replaceSelection('[embed]' + selection + ['[/embed]']);
				}
			},{
				id: 'comment_video'
			});
		}
		
		if(this.options.emoticons) {
			this.toolbar.addButton('Emoticons',function(){
				clearCommentTextarea($('comment_body'));
				var selection = this.getSelection();
				Element.show('emoticons');
				},{
				id: 'comment_emoticons'
			});	
		}
	}
});