/*
	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(obj,v)
{
	obj=Id(obj);
	this.GetSelectedText=function()
	{
		return this.iesel ? this.iesel.text : (this.start>=0 && this.end>this.start) ? obj.value.substring(this.start,this.end) : "";
	}

	this.SetSelectedText=function(text, secondtag)
	{
		if(this.iesel)
		{
			if(typeof(secondtag)=="string")
			{
				var l=this.iesel.text.length;
				this.iesel.text=text+this.iesel.text+secondtag;
				this.iesel.moveEnd("character", -secondtag.length);
				this.iesel.moveStart("character", -l);
			}
			else
				this.iesel.text=text;
			this.iesel.select();
		}
		else if(this.start>=0&&this.end>=this.start)
		{
			var left=obj.value.substring(0,this.start);
			var right=obj.value.substr(this.end);
			var scont=obj.value.substring(this.start, this.end);
			if(typeof(secondtag)=="string")
			{
				obj.value=left+text+scont+secondtag+right;
				this.end=obj.selectionEnd=this.start+text.length+scont.length;
				this.start=obj.selectionStart=this.start+text.length;
			}
			else
			{
				obj.value=left+text+right;
				this.end=obj.selectionEnd=this.start+text.length;
				this.start=obj.selectionStart=this.start+text.length;
			}
			obj.scrollTop=this.scroll;
			obj.focus();
		}
		else
		{
			obj.value+=text + ((typeof(secondtag)=="string") ? secondtag : "");
			if(this.scroll>=0)
				obj.scrollTop=this.scroll;
		}
	}

	this.GetText=function()
	{
		return obj.value;
	}

	this.SetText=function(text)
	{
		obj.value=text;
	}

	this.TextAreaEvent=function()
	{
		act_editor=this.carretHandler;
		EDITOR.ActivateEditor(this.id);
		if(document.selection)
			this.carretHandler.iesel = document.selection.createRange().duplicate();
		else if(typeof(this.selectionStart)!="undefined")
		{
			this.carretHandler.start=this.selectionStart;
			this.carretHandler.end=this.selectionEnd;
			this.carretHandler.scroll=this.scrollTop;
		}
		else
			this.carretHandler.start=this.carretHandler.end=-1;
	}

	this.Bold=function()
	{
		this.SetSelectedText("[b]","[/b]");
		return false;
	}

	this.Strike=function()
	{
		this.SetSelectedText("[s]","[/s]");
		return false;
	}

	this.Italic=function()
	{
		this.SetSelectedText("[i]","[/i]");
		return false;
	}

	this.Li=function()
	{
		this.SetSelectedText("[*]");
		return false;
	}

	this.Ol=function()
	{
		this.SetSelectedText("[ol]\r[*]\r[*]\r[*]\r","[/ol]");
		return false;
	}

	this.Ul=function()
	{
		this.SetSelectedText("[ul]\r[*]\r[*]\r[*]\r","[/ul]");
		return false;
	}

	this.UnderLine=function()
	{
		this.SetSelectedText("[u]","[/u]");
		return false;
	}

	this.Hr=function()
	{
		this.SetSelectedText("[hr]");
		return false;
	}

	this.Tab=function()
	{
		this.SetSelectedText("\t");
		return false;
	}

	this.Left=function()
	{
		this.SetSelectedText("[left]","[/left]");
		return false;
	}

	this.Right=function()
	{
		this.SetSelectedText("[right]","[/right]");
		return false;
	}

	this.Center=function()
	{
		this.SetSelectedText("[center]","[/center]");
		return false;
	}

	this.Justify=function()
	{
		this.SetSelectedText("[justify]","[/justify]");
		return false;
	}

	this.C=function()
	{
		this.SetSelectedText("[c]");
		return false;
	}

	this.R=function()
	{
		this.SetSelectedText("[r]");
		return false;
	}

	this.Nobb=function()
	{
		this.SetSelectedText("[nobb]","[/nobb]");
		return false;
	}

	this.Tm=function()
	{
		this.SetSelectedText("[tm]");
		return false;
	}

	this.Url=function()
	{
		var text = this.GetSelectedText();
		var link = prompt(el_lang['enter_adress'],"http://");
		if (link==null)
			return false;
		text = prompt(el_lang['link_text'],text);
		if (text==null)
			return false;
		this.SetSelectedText("[url="+link+"]"+text+"[/url]");
		return false;
	}

	this.Img=function()
	{
		var link = this.GetSelectedText();
		if (!link)
			link = prompt(el_lang['enter_image_addr'],link);
		if (link==null)
			return false;
		this.SetSelectedText("[img]"+link+"[/img]");
		return false;
	}

	this.Mail=function()
	{
		var link=this.GetSelectedText();
		var link=prompt(el_lang['enter_adress'],link);
		if (link==null)
			return false;
		if (!IsEmail(link) && !confirm(el_lang['wrong_email']))
			return this.Mail();
		text=prompt(el_lang['link_text'],link);
		if (text==null)
			return false;
		this.SetSelectedText("[email="+link+"]"+text+"[/email]");
		return false;
	}

	this.Color=function(cn)
	{
		if (!cn)
			return false;
		this.SetSelectedText("[color="+cn+"]","[/color]");
	}

	this.BackGround=function(cn)
	{
		if (!cn)
			return false;
		this.SetSelectedText("[background="+cn+"]","[/background]");
	}

	this.Size=function(s)
	{
		if (!s)
			return false;
		this.SetSelectedText("[size="+s+"]","[/size]");
	}

	this.Font=function(s)
	{
		if (!s)
			return false;
		this.SetSelectedText("[font="+s+"]","[/font]");
	}


	this.Plus=function()
	{
		obj.rows+=5;
		return false;
	}

	this.Minus=function()
	{
		if (obj.rows>=10)
			obj.rows-=5;
		return false;
	}

	this.Preview=function()
	{
		var area=Id('prev_'+v);
		Ajax(
			'-1',
			{
				'type':'bbpreview',
				'service':EDITOR.serid,
				'text':this.GetText(),
				'key':v
			},
			function(result)
			{
				$(area).html(result).show();
			},
			false
		);
		return false;
	}

	this.HidePrev=function()
	{
		$('#prev_'+v).hide();
	}

	obj.carretHandler=this;
	obj.onchange=obj.onclick=obj.onkeyup=obj.onfocus=this.TextAreaEvent;
	if(!document.selection)
		obj.onSelect=this.TextAreaEvent;
	this.start=-1;
	this.end=-1;
	this.scroll=-1;
	this.iesel=null;
	EDITOR.NewEditor(
						obj.id,
						function(text)
						{
							if(text.indexOf('][/')==-1)
								obj.carretHandler.SetSelectedText(text);
							else
							{
								var arr=text.split('][/');
								obj.carretHandler.SetSelectedText(arr[0]+']','[/'+arr[1]);
							}
						},
						function(text){obj.value=text},
						function(){return obj.value}
	);
}

function HotKeyHandler(ev)
{
	var evt, key;
	if(!(evt=ev ? ev : window.event))
		return;
	key = evt.keyCode ? evt.keyCode : evt.charCode;
	key=HotKeyHandler.convertIEKey[String(key)] ? HotKeyHandler.convertIEKey[String(key)] : key;
	if (evt && evt.ctrlKey)
	{
		key=evt.shiftKey ? String.fromCharCode(key).toUpperCase() : String.fromCharCode(key).toLowerCase();
		if(typeof(HotKeyHandler.keys[key]) == "function")
		{
			HotKeyHandler.keys[key](evt);
			evt.cancelBubble = true;
			evt.returnValue = false;
			if(evt.preventDefault)
				evt.preventDefault();
			if(evt.stopPropagation)
				evt.stopPropagation();
			return false;
		}
	}
	return true;
}

HotKeyHandler.WindowInit=function()
{
	if(document.addEventListener && (GetOperaVersion()>6||GetOperaVersion()==0))
		document.addEventListener("keypress", HotKeyHandler, false);
	else
		document.onkeydown = HotKeyHandler;
}
HotKeyHandler.convertIEKey={"1":65,"2":66,"4":68,"12":76,"16":80,"19":83,"20":84,"21":85,"26":90};
HotKeyHandler.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();
	}
};
HotKeyHandler.WindowInit();

function IsEmail(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))
};

function GetOperaVersion()
{
	var opver=navigator.userAgent.match(/Opera\s*([0-9.]+)/i);
	return (opver && opver.length>1) ? parseFloat(opver[1]) : 0;
}
