//Control Textarea version 2.0.1 - author Ryan Johnson <ryan@livepipe.net> - copyright 2007 LivePipe LLC - package Control.TextArea - license MIT - http://livepipe.net/projects/control_textarea/
if(typeof (Control)=="undefined"){Control={}}Control.TextArea=Class.create();Object.extend(Control.TextArea.prototype,{onChangeTimeoutLength:500,element:false,onChangeTimeout:false,initialize:function(A){this.element=$(A);$(this.element).observe("keyup",this.doOnChange.bindAsEventListener(this));$(this.element).observe("paste",this.doOnChange.bindAsEventListener(this));$(this.element).observe("input",this.doOnChange.bindAsEventListener(this));if(!!document.selection){$(this.element).observe("mouseup",this.saveRange.bindAsEventListener(this));$(this.element).observe("keyup",this.saveRange.bindAsEventListener(this))}},doOnChange:function(A){if(this.onChangeTimeout){window.clearTimeout(this.onChangeTimeout)}this.onChangeTimeout=window.setTimeout(function(){if(this.notify){this.notify("change",this.getValue())}}.bind(this),this.onChangeTimeoutLength)},saveRange:function(){this.range=document.selection.createRange()},getValue:function(){return this.element.value},getSelection:function(){if(!!document.selection){return document.selection.createRange().text}else{if(!!this.element.setSelectionRange){return this.element.value.substring(this.element.selectionStart,this.element.selectionEnd)}else{return false}}},replaceSelection:function(C){var B=this.element.scrollTop;if(!!document.selection){this.element.focus();var A=(this.range)?this.range:document.selection.createRange();A.text=C;A.select()}else{if(!!this.element.setSelectionRange){var D=this.element.selectionStart;this.element.value=this.element.value.substring(0,D)+C+this.element.value.substring(this.element.selectionEnd);this.element.setSelectionRange(D+C.length,D+C.length)}}this.doOnChange();this.element.focus();this.element.scrollTop=B},wrapSelection:function(A,B){this.replaceSelection(A+this.getSelection()+B)},insertBeforeSelection:function(A){this.replaceSelection(A+this.getSelection())},insertAfterSelection:function(A){this.replaceSelection(this.getSelection()+A)},injectEachSelectedLine:function(C,A,B){this.replaceSelection((A||"")+$A(this.getSelection().split("\n")).inject([],C).join("\n")+(B||""))},insertBeforeEachSelectedLine:function(C,A,B){this.injectEachSelectedLine(function(E,D){E.push(C+D);return E},A,B)}});if(typeof (Object.Event)!="undefined"){Object.Event.extend(Control.TextArea)}Control.TextArea.ToolBar=Class.create();Object.extend(Control.TextArea.ToolBar.prototype,{textarea:false,container:false,initialize:function(A,B){this.textarea=A;if(B){this.container=$(B)}else{this.container=$(document.createElement("ul"));this.textarea.element.parentNode.insertBefore(this.container,this.textarea.element)}},attachButton:function(A,B){A.onclick=function(){return false};$(A).observe("click",B.bindAsEventListener(this.textarea))},addButton:function(C,F,D){var A=document.createElement("li");var B=document.createElement("a");B.href="#";this.attachButton(B,F);A.appendChild(B);Object.extend(B,D||{});if(C){var E=document.createElement("span");E.innerHTML=C;B.appendChild(E)}this.container.appendChild(A)}})

//Create Artician Editor
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,textarea+'_toolbar');
		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));
		}
		//Toolbar Buttons
		this.toolbar.attachButton($(textarea+'_bold'),function(){this.wrapSelection('[b]','[/b]');});
		this.toolbar.attachButton($(textarea+'_quote'),function(){this.wrapSelection('[quote]','[/quote]');});
		this.toolbar.attachButton($(textarea+'_code'),function(){this.wrapSelection('[code]','[/code]');});
		this.toolbar.attachButton($(textarea+'_italic'),function(){this.wrapSelection('[i]','[/i]');});
		this.toolbar.attachButton($(textarea+'_underline'),function(){this.wrapSelection('[u]','[/u]');});
		this.toolbar.attachButton($(textarea+'_left'),function(){this.wrapSelection('[left]','[/left]');});
		this.toolbar.attachButton($(textarea+'_center'),function(){this.wrapSelection('[center]','[/center]');});
		this.toolbar.attachButton($(textarea+'_right'),function(){this.wrapSelection('[right]','[/right]');});
		this.toolbar.attachButton($(textarea+'_indent'),function(){this.wrapSelection('[indent]','[/indent]');});
	
		//Modals
		new Control.Modal($(textarea+'_insert_image'),{
			opacity: 0.30,
			width:300,
			height:130,
			position: 'relative',
			containerClassName: 'interact',
			overlayClassName: 'interactd'
		});	
		new Control.Modal($(textarea+'_insert_media'),{
			opacity: 0.30,
			width:300,
			height:132,
			position: 'relative',
			containerClassName: 'interact',
			overlayClassName: 'interactd'
		});	
		new Control.Modal($(textarea+'_insert_link'),{
			opacity: 0.30,
			width:300,
			height:166,
			position: 'relative',
			containerClassName: 'interact',
			overlayClassName: 'interactd'
		});	
	}
});

//Editor Inserts
function insertMedia(type,fieldValue,ta) {
	ta = new Control.TextArea(ta);
	if(type == 'YouTube') {
		ta.replaceSelection('[youtube]'+fieldValue+'[/youtube]');
	}
	else if(type == 'Metacafe') {
		ta.replaceSelection('[metacafe]'+fieldValue+'[/metacafe]');
	}
	else if(type == 'Stage6') {
		ta.replaceSelection('[stage6]'+fieldValue+'[/stage6]');
	}
}
//Insert Link
function insertLink(URI,siteLabel,ta) {
	ta = new Control.TextArea(ta);
	if(siteLabel.length>0) {
		ta.replaceSelection('[url=' + URI + ']'+siteLabel+'[/url]');
	}
	else {
		ta.replaceSelection('[url]' + URI + '[/url]');
	}
}
//Change Media
function changeMedia(type,ta) {
	$(ta+'_mediaValueField').show();
	if(type == 'YouTube') {
		$(ta+'_mediaValueLabel').innerHTML = "YouTube Video ID";
	}
}
//Insert Text
function InsertText(myField, myValue) {
	myField = getElement(myField);
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	}
	else {
		myField.value += myValue;
	}
}
//Wrap Selection
function wrapSelection(ta,wa,wb) {
	ta = new Control.TextArea(ta);
	ta.wrapSelection(wa,wb);
}

//Wrap Selection
function txreplaceSelection(ta,text) {
	ta = new Control.TextArea(ta);
	ta.replaceSelection(text);
}

//
function getPalette(){
	Effect.toggle('editor_palette', 'blind', {duration: 0.4});

	if($('editor_palette').innerHTML == ''){
		new Ajax.Updater('editor_palette', '/modules/forum/palette.php',{asynchronous:true, evalScripts:true });	
	}
}

function getEmotes(){
	Effect.toggle('emotes','appear', {duration: 0.5}); 
	
	if($('emotes').innerHTML == ''){
		new Ajax.Updater('emotes', '/modules/forum/emotes.php',{asynchronous:true, evalScripts:true });
	}
}