/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym. See addons/copyrights/info.txt for more information.
	=================
	Based on TextAreaSelectionHelper by Sardar <Sardar@vingrad.ru>
	http://forum.vingrad.ru/forum/topic-35775.html
	http://forum.vingrad.ru/forum/topic-84449.html
*/
var act_editor;
function EleanorBBEditor(id)
{	var th=this,		textarea=$("#div_"+id).find('a').each(function(){			var m=this.className.match(/bbe_([a-z0-9\-]+)/),
				f;
			if(!m)
				return;
			switch(m[1])
			{				case 'bold':
					f=function(){th.Bold();return false}
				break;				case 'italic':
					f=function(){th.Italic();return false}
				break;
				case 'uline':
					f=function(){th.UnderLine();return false}
				break;
				case 'strike':
					f=function(){th.Strike();return false}
				break;
				case 'left':
					f=function(){th.Left();return false}
				break;
				case 'center':
					f=function(){th.Center();return false}
				break;
				case 'right':
					f=function(){th.Right();return false}
				break;
				case 'justify':
					f=function(){th.Justify();return false}
				break;
				case 'hr':
					f=function(){th.Hr();return false}
				break;
				case 'url':
					f=function(){th.Url();return false}
				break;
				case 'mail':
					f=function(){th.Mail();return false}
				break;
				case 'img':
					f=function(){th.Img();return false}
				break;
				case 'ul':
					f=function(){th.Ul();return false}
				break;
				case 'ol':
					f=function(){th.Ol();return false}
				break;
				case 'li':
					f=function(){th.Li();return false}
				break;
				case 'tm':
					f=function(){th.Tm();return false}
				break;
				case 'c':
					f=function(){th.C();return false}
				break;
				case 'r':
					f=function(){th.R();return false}
				break;
				case 'tab':
					f=function(){th.Tab();return false}
				break;
				case 'nobb':
					f=function(){th.Nobb();return false}
				break;
				case 'preview':
					f=function(){th.Preview();return false}
				break;
				case 'splus':
					f=function(){th.Plus();return false}
				break;
				case 'sminus':
					f=function(){th.Minus();return false}
				break;
				case 'font':
					new DropDown(this,"right-bottom","click","#div_"+id+" .bb_fonts");
				break;
			}
			if(f)
				$(this).click(f);
		}).end().find('select').each(function(){			var m=this.className.match(/bbe_([a-z0-9\-]+)/),
				f;
			if(!m)
				return;
			switch(m[1])
			{
				case 'color':
					f=function(){if(this.value){th.Color(this.value);$('option:first',this).attr('selected',true)}}
				break;
				case 'font':
					f=function(){if(this.value){th.Font(this.value);$('option:first',this).attr('selected',true)}}
				break;
				case 'background':
					f=function(){if(this.value){th.BackGround(this.value);$('option:first',this).attr('selected',true)}}
				break;
				case 'size':
					f=function(){if(this.value){th.Size(this.value);$('option:first',this).attr('selected',true)}}
				break;
			}
			if(f)
				$(this).change(f);		}).end().find('textarea:first').get(0);
		this.textarea=textarea;
	this.GetSelectedText=function()
	{
		return document.selection ? document.selection.createRange().text : textarea.value.substring(textarea.selectionStart,textarea.selectionEnd);
	}

	this.SetSelectedText=function(tag,secondtag,textfunc)
	{		var texta=$("textarea:first").get(0);
		if(document.selection)
		{
			var iesel=document.selection.createRange();
			if(typeof(secondtag)=="string")
			{
				var text=typeof textfunc=="function" ? textfunc(iesel.text) : iesel.text;
				var l=text.replace(/\n/g,'').length;
				iesel.text=tag+text+secondtag;
				iesel.moveEnd("character",-secondtag.length);
				iesel.moveStart("character",-l);
			}
			else
				iesel.text=(typeof textfunc=="function" ? textfunc(text) : tag);
			iesel.select();
		}
		else if(texta.selectionStart<=texta.selectionEnd)
		{
			var left=texta.value.substring(0,texta.selectionStart);
			var right=texta.value.substr(texta.selectionEnd);
			var content=texta.value.substring(texta.selectionStart,texta.selectionEnd);
			if(typeof textfunc=="function")
				content=textfunc(content);
			if(typeof(secondtag)=="string")
			{
				var start=texta.selectionStart;
				texta.value=left+tag+content+secondtag+right;
				texta.setSelectionRange(start+tag.length,start+tag.length+content.length);
			}
			else
			{
				if(typeof tag!="string")
					tag=content;
				var start=texta.selectionStart;
				texta.value=left+tag+right;
				if(texta.selectionStart!=texta.selectionEnd)
					texta.setSelectionRange(start,start+tag.length);
				else
					texta.setSelectionRange(start+tag.length,start+tag.length);
			}
		}
		else
			texta.value+=tag + ((typeof(secondtag)=="string") ? secondtag : "");
	}

	this.GetText=function()
	{
		return textarea.value;
	}

	this.SetText=function(text)
	{
		textarea.value=text;
	}

	this.Bold=function()
	{		this.SetSelectedText("[b]","[/b]");
	}

	this.Strike=function()
	{
		this.SetSelectedText("[s]","[/s]");
	}

	this.Italic=function()
	{
		this.SetSelectedText("[i]","[/i]");
	}

	this.Li=function()
	{
		this.SetSelectedText("[*]");
	}

	this.Ol=function()
	{
		this.SetSelectedText("[ol]","[/ol]",function(t){return t ? "\n[*]"+t.replace(/\n/g,"\n[*]")+"\n" : "\n[*]\n[*]\n[*]\n"});
	}

	this.Ul=function()
	{
		this.SetSelectedText("[ul]","[/ul]",function(t){return t ? "\n[*]"+t.replace(/\n/g,"\n[*]")+"\n" : "\n[*]\n[*]\n[*]\n"});
	}

	this.UnderLine=function()
	{
		this.SetSelectedText("[u]","[/u]");
	}

	this.Hr=function()
	{
		this.SetSelectedText("[hr]");
	}

	this.Tab=function()
	{
		this.SetSelectedText("\t");
	}

	this.Left=function()
	{
		this.SetSelectedText("[left]","[/left]");
	}

	this.Right=function()
	{
		this.SetSelectedText("[right]","[/right]");
	}

	this.Center=function()
	{
		this.SetSelectedText("[center]","[/center]");
	}

	this.Justify=function()
	{
		this.SetSelectedText("[justify]","[/justify]");
	}

	this.C=function()
	{
		this.SetSelectedText("[c]");
	}

	this.R=function()
	{
		this.SetSelectedText("[r]");
	}

	this.Nobb=function()
	{
		this.SetSelectedText("[nobb]","[/nobb]");
	}

	this.Tm=function()
	{
		this.SetSelectedText("[tm]");
	}

	this.Url=function()
	{
		var text=this.GetSelectedText();
		var link="http://";
		if(text.match(/^([a-z]{3,10}:\/\/[a-zа-я0-9\/\._\-:]+\.[a-z]{2,5}\/)?(?:[^\s@{}]*)?$/))
			link=text;
		link=prompt(el_lang['enter_adress'],link);
		if (link==null)
			return;
		text=prompt(el_lang['link_text'],text);
		if (text==null)
			return;
		this.SetSelectedText("[url="+link+"]"+text+"[/url]");
	}

	this.Img=function()
	{
		var link = this.GetSelectedText();
		if (!link)
			link = prompt(el_lang['enter_image_addr'],link);
		if (link==null)
			return;
		this.SetSelectedText("[img]"+link+"[/img]");
	}

	this.Mail=function()
	{
		var link=this.GetSelectedText();
		var link=prompt(el_lang['enter_adress'],link);
		if (link==null)
			return;
		if (!this.IsEmail(link) && !confirm(el_lang['wrong_email']))
			return this.Mail();
		text=prompt(el_lang['link_text'],link);
		if (text==null)
			return;
		this.SetSelectedText("[email="+link+"]"+text+"[/email]");
	}

	this.Color=function(cn)
	{		if (!cn)
			return;
		this.SetSelectedText("[color="+cn+"]","[/color]");
	}

	this.BackGround=function(cn)
	{
		if (!cn)
			return;
		this.SetSelectedText("[background="+cn+"]","[/background]");
	}

	this.Size=function(s)
	{
		if (!s)
			return;
		this.SetSelectedText("[size="+s+"]","[/size]");
	}

	this.Font=function(s)
	{
		if (!s)
			return;
		this.SetSelectedText("[font="+s+"]","[/font]");
	}


	this.Plus=function()
	{		textarea.rows+=5;	}

	this.Minus=function()
	{		if(textarea.rows>=10)
			textarea.rows-=5;	}

	this.Preview=function()
	{		Ajax(
			'-1',
			{				'type':'bbpreview',
				'service':EDITOR.serid,
				'text':this.GetText()
			},
			function(result)
			{				var pr=$("#div_"+id+" .preview:first");					button_hide=$("<br /><div align=\"center\"><input type=\"button\" class=\"button\" value=\""+el_lang['hide']+"\" /></div>").find("input").click(function(){					pr.empty().hide();				}).end();				pr.html(result).append(button_hide).show();
			},
			false
		);
	}

	this.IsEmail=function(C)
	{
		var A=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)"),B=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
		return(!A.test(C) && B.test(C))
	}

	textarea.onfocus=function(){EDITOR.ActivateEditor(this.id);act_editor=th}
	EDITOR.NewEditor(
						id,
						function(text){th.SetSelectedText(text)},
						function(text){textarea.value=text},
						function(){return textarea.value},
						function(pre,after,func){th.SetSelectedText(pre,after,func)}
	);
}

(function()
{	this.convertIEKey={"1":65,"2":66,"4":68,"12":76,"16":80,"19":83,"20":84,"21":85,"26":90};
	this.keys=
	{
		"b":function()
		{			if (act_editor)
				act_editor.Bold();
		},
		"i":function()
		{
			if (act_editor)
				act_editor.Italic();
		},
		"u":function()
		{
			if (act_editor)
				act_editor.UnderLine();
		},
		"t":function()
		{
			if (act_editor)
				act_editor.Tab();
		},
		"l":function()
		{
			if (act_editor)
				act_editor.Url();
		},
		"e":function()
		{
			if (act_editor)
				act_editor.Mail();
		},
		"I":function()
		{
			if (act_editor)
				act_editor.Img();
		},
		"S":function()
		{
			if (act_editor)
				act_editor.Srike();
		},
		"L":function()
		{
			if (act_editor)
				act_editor.Left();
		},
		"M":function()
		{
			if (act_editor)
				act_editor.Center();
		},
		"R":function()
		{
			if (act_editor)
				act_editor.Right();
		},
		"J":function()
		{
			if (act_editor)
				act_editor.Justify();
		},
		"h":function()
		{
			if (act_editor)
				act_editor.Hr();
		}
	};

	function Handler(ev)
	{
		var evt,key;
		if(!(evt=ev ? ev : window.event))
			return;
		key = evt.keyCode ? evt.keyCode : evt.charCode;
		key=this.convertIEKey[String(key)] ? this.convertIEKey[String(key)] : key;
		if (evt && evt.ctrlKey)
		{			key=evt.shiftKey ? String.fromCharCode(key).toUpperCase() : String.fromCharCode(key).toLowerCase();
			if(typeof(this.keys[key]) == "function")
			{
				this.keys[key](evt);
				evt.cancelBubble=true;
				evt.returnValue=false;
				if(evt.preventDefault)
					evt.preventDefault();
				if(evt.stopPropagation)
					evt.stopPropagation();
				return false;
			}
		}
		return true;
	}
	$(document).keypress(function(e){Handler(e)});
})();