/**
* Редактор для habra version 0.03
*/

var Editor = jsfw.Class({
	__constructor:function(o)
	{
		this.dom = $(o.el);
		this.isAutoPreview = $('changeAutoPreview').checked;
		this.isTipograf = $('changeTipograf').checked;
		this.isColer = $('changeColer').checked; 
		this.isHyphenWords = false;
		this.isNumberCode = false;
		if(!this.dom) this.dom = $.create('div',{className:'Editor'});
		this.controlBar = new ControlBar({
			el:$.getTag('div','controlBar',this.dom)[0]
		})
		this.controlBar.addEvent('onclick',$d(this,this.eClickConrolBarButton));
		this.textarea = $.getTag('textarea','*',this.dom)[0];
		this.textarea.onkeyup = $d(this,this.eKeyUp);
		if(jsfw.browser.opera)
			this.textarea.onkeypress = $d(this,this.eKeyDown);
		else
			this.textarea.onkeydown = $d(this,this.eKeyDown);
		this.preview = $('preview');
		document.body.onkeyup = $d(this,this.eKeyUpBody);
	},
	changeAutoPreview:function(b){
		this.isAutoPreview = b.getDom().checked;
		return true;
	},
	changeColer:function(b){
		this.isColer = b.getDom().checked;
		return true;
	},
	changeTipograf:function(b)
	{
		this.isTipograf = b.getDom().checked;
		return true;
	},
	eClickConrolBarButton:function(cb,cmd,b)
	{
		return this.execute(cmd,b);
	},
	clearText:function()
	{
		this.textarea.value = '';
	},
	insertTag:function(tag)
	{
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		this.textarea.focus();
		var cursor = this.getCaretPos();
		var text = this.textarea.value;
		var prevText = text.substr(0,cursor.start);
		var selectText = text.substr(cursor.start,cursor.length);
		var nextText = text.substr(cursor.start+cursor.length);
		var sp = '\n' + (" ".repeat(this.firsLineCountSpace(prevText)));
		var tagOpen = tag.open.replace(/\{tab\}/gm,Editor.tab).replace(/\n/gm,sp);
		var tagClose = tag.close.replace(/\{tab\}/gm,Editor.tab).replace(/\n/gm,sp);
		prevText += tagOpen;
		nextText = tagClose + nextText;
		this.textarea.value = prevText + selectText + nextText;
		this.setCaretPos(prevText.length,selectText.length);
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
	},
	insertText:function(text,selected)
	{
		if (!jsfw.browser.mozilla && !jsFW.browser.safari) // opera && ie
		{
			var s = document.selection;
			var sel=s.createRange();
			if(sel.parentElement()==this.textarea)
			{
				sel.text = text;
			}
		}
		else
		{
			var oldtext = this.textarea.value;
			var start = this.textarea.selectionStart;
			var end = this.textarea.selectionEnd;
			var newText = oldtext.substr(0,start)+text+oldtext.substr(end);
			this.textarea.value = newText;
			this.textarea.selectionStart=start+text.length;
			this.textarea.selectionEnd=end+text.length;
		}
	},
	getSelectText:function()
	{
		if (!jsfw.browser.mozilla && !jsFW.browser.safari) // opera && ie
		{
			var s = document.selection;
			var sel=s.createRange();
			if(sel.parentElement()==this.textarea) return sel.text;
		}
		else
		{
			return this.textarea.value.substring(this.textarea.selectionStart,this.textarea.selectionEnd);
		}
		return '';
	},
	getCaretPos:function()
	{
		var pos = 0,len=0;
		if('setSelectionRange' in this.textarea)
		{
			pos = this.textarea.selectionStart;
			len = this.textarea.selectionEnd-this.textarea.selectionStart;
		}
		else
		{
			var sel = document.selection.createRange();
			if(sel.parentElement()==this.textarea)
			{
				var clone = sel.duplicate();
				len = sel.text.length;
				sel.collapse(true);
				clone.moveToElementText(this.textarea);
				clone.setEndPoint('EndToEnd', sel);
				pos = clone.text.length;
			}
		}
		return {start:pos,length:len};
	},
	setCaretPos:function(start,len,ta)
	{
		ta = ta || this.textarea;
		this.textarea.focus();
		if('setSelectionRange' in ta)
		{
			ta.selectionStart = start;
			ta.selectionEnd = start+len;
		}
		else
		{
			var sel = document.selection.createRange();
			if(sel.parentElement()==ta)
			{
				sel.moveToElementText(ta);
				sel.collapse(true);
				sel.moveEnd("character",start+len);
				sel.moveStart("character",start);
				sel.select();
			}
		}
	},
	insertTagParam:function(tag)
	{
		this.winParam = $.create('div',{className:'Win'});
		this.winParam.p = this.getCaretPos();
		var form = $.create('form',this.winParam);
		form.innerHTML = jsFW.tmpl('<table><%;for(var i=0,l=params.length;i<l;i++){var pa = params[i];%><tr><th><%=pa.title%>: </th><td><input type="text" value="<%=pa.def||""%>"/></td></tr><%}%><tr><th colspan="2"><input type="submit" value="OK"/></th></tr></table>',tag);
		document.body.appendChild(this.winParam);
		jsFW.displayBlock({opacity:'0.05',backgroundColor:'#C1D9F0'});
		var first = form.elements[0];
		if(first)
		{
			$.setCursor(first,0,first.value.length);
			first.focus();
		}
		var _this = this;
		form.onsubmit=function(){
			var open = tag.open;
			var close = tag.close;
			for(var i=0,l=this.elements.length;i<l;i++)
			{
				open = open.replace('{'+i+'}',this.elements[i].value);
				close = close.replace('{'+i+'}',this.elements[i].value);
			}
			_this.closeWinParam();
			try{
			_this.insertTag({open:open,close:close});
			}catch(er){}
			return false;
		}; 
	},
	closeWinParam:function(){
		if(this.winParam)
		{
			$.remove(this.winParam);
			jsFW.displayUnblock();
			var p = this.winParam.p;
			if(p) this.setCaretPos(p.start,p.length);
			this.winParam = null;
		}
	},
	execute:function(cmd,b)
	{
		var ret = false;
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		var tag = Editor.insert_tag[cmd];
		if(tag)
		{
			if(tag.params)
			{
				this.insertTagParam(tag);
			}
			else
			{
				this.insertTag(tag);
			}
		}
		else if(cmd in this)
		{
			ret = this[cmd](b);
		}
		else if(cmd.indexOf('text-code-')>=0)
		{
			var lang = cmd.replace('text-code-','');
			var tag = Editor.insert_tag['text-code-color'];
			this.insertTag({open:(tag.open.replace('{class}',lang)),close:tag.close,params:tag.params});
		}
		if(this.isAutoPreview) this.previewShow();
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return ret;
	},
	insertTab:function(e)
	{
		var cursor = this.getCaretPos();
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		if(cursor.length==0)
		{
			this.insertText(Editor.tab);
		}
		else
		{
			var text = this.textarea.value;
			var first = text.lastIndexOf('\n',cursor.start)+1;
			var end = text.indexOf('\n',cursor.start+cursor.length);
			var prevText = text.substr(0,first);
			if(end<0)
			{
				var selectLine = text.substr(first);
				var nextText = '';
			}else
			{
				var selectLine = text.substring(first,end);
				var nextText = text.substr(end);
			}
			var t = selectLine.split('\n');
			selectLine = t.map('c->"'+Editor.tab+'"+c').join('\n');
			this.textarea.value = prevText + selectLine + nextText;
			var start = cursor.start+Editor.tab.length;
			
			var end = cursor.length+Editor.tab.length*(t.length-1);
			if(jsfw.browser.msie)
			{
				start--;
				end--;
			}
			this.setCaretPos(start,end)
		}
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	deleteTab:function()
	{
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		var cursor = this.getCaretPos();
		var text = this.textarea.value;
		var start = cursor.start,len = Editor.tab.length;
		if(cursor.length==0)
		{
			if(cursor.length==0)
			{
				start-=len;
			}
			var txt = text.substr(start,len);
			if(txt == Editor.tab)
			{
				this.textarea.value = text.substr(0,start)+text.substr(start+len);
			}
			else
			{
				var i = txt.lastIndexOf('\n')+1;
				if(i>0)
				{
					start+= (i);
				}
			}
			this.setCaretPos(start,0);
		}
		else
		{
			var first = text.lastIndexOf('\n',cursor.start)+1;
			var end = text.indexOf('\n',cursor.start+cursor.length);
			var prevText = text.substr(0,first);
			if(end<0)
			{
				var selectLine = text.substr(first);
				var nextText = '';
			}else
			{
				var selectLine = text.substring(first,end);
				var nextText = text.substr(end);
			}
			var t = selectLine.split('\n');
			selectLine = t.map(function(c){
				var txt = c.substr(0,len);
				if(txt == Editor.tab)
				{
					return c.substr(len);
				}
				return c;
			}).join('\n');
			text = prevText + selectLine + nextText;
			if(text != this.textarea.value)
			{
				this.textarea.value = text;
				var start = cursor.start-Editor.tab.length;
				var end = cursor.length-Editor.tab.length*(t.length-1);
				if(jsfw.browser.msie)
				{
					start--;
					end--;
				}
				this.setCaretPos(start,end)
			}
		}
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	firsLineCountSpace:function(text)
	{
		var i = text.lastIndexOf('\n')+1;
		var line = text.substr(i);
		var i=0;
		for(var l=line.length;i<l;i++)
		{
			if(line.charAt(i)!=' ') break;
		}
		return i;
	},
	enterTab:function()
	{
		var cursor = this.getCaretPos();
		var prevText = this.textarea.value.substr(0,cursor.start);
		var nextText = this.textarea.value.substr(cursor.start+cursor.length);
		var i = this.firsLineCountSpace(prevText);
		if(i>0)
		{
			var text = prevText + '\n'+(" ".repeat(i)) + nextText;
			i += cursor.start+1;
			if(jsfw.browser.opera || jsfw.browser.safari) i++;
			if(jsfw.browser.msie) i -= (prevText.split('\n').length-1);
			this.textarea.value = text;
			this.setCaretPos(i,0);
			return false;
		}
		return true;
	},
	eKeyUpBody:function(s,e)
	{
		if(e.keyCode == $e.keys.esc)this.closeWinParam();
	},
	eKeyUp:function()
	{
		if(this.isAutoPreview) this.previewShow();
	},
	getHotkey:function(e)
	{
		var hotkey = Editor.hotkey;
		if(e.ctrlKey)
		{
			hotkey = hotkey.ctrl;
			if(e.altKey)
			{
				hotkey = hotkey.alt;
			}else if(e.shiftKey)
			{
				hotkey = hotkey.shift;
			}
		}
		else if(e.altKey)
		{
			hotkey = hotkey.alt;
			if(e.shiftKey)
			{
				hotkey = hotkey.shift;
			}
		}else if(e.shiftKey)
		{
			hotkey = hotkey.shift;
		}
		return hotkey;
	},
	eKeyDown:function(s,e)
	{
		var code = $e.keysId[e.keyCode] || String.fromCharCode(e.keyCode);
		var hotkey = this.getHotkey(e);
		hotkey = hotkey[code];
		if(hotkey)
		{
			if('cmd' in hotkey && hotkey.cmd in this) 
			{
				if(!this[hotkey.cmd].apply(this,(hotkey.params||[]).concat(e))){
					e.cancelBubble = true;
					e.returnValue = false;
					if(e.preventDefault) e.preventDefault();
					if(e.stopPropagation) e.stopPropagation();
					return false;
				}
			}
		}
	},
	lineUp:function(){
		var text = this.textarea.value;
		var cursor = this.getCaretPos();
		var start = text.lastIndexOf('\n',cursor.start-1);
		if(start>=0)
		{
			var subText,prevText='',nextText='';
			var end = text.indexOf('\n',cursor.start);
			prevText = text.substr(0,start);
			if(end>=0) 
			{
				subText = text.substring(start,end);
				nextText = text.substr(end);
			}
			else 
			{
				subText = text.substr(start);
			}
			var i = prevText.lastIndexOf('\n',prevText.length-1);
			if(i>0)
			{
				this.textarea.value = prevText.substr(0,i)+subText+prevText.substr(i)+nextText;
				this.setCaretPos(i+1,0);
			}
			else
			{
				this.textarea.value = subText.substr(1)+'\n' + prevText +nextText;
				this.setCaretPos(0,0);
			}
		}
		return false;
	},
	lineDown:function(){
		var text = this.textarea.value;
		var cursor = this.getCaretPos();
		var end = text.indexOf('\n',cursor.start);
		if(end>=0)
		{
			var subText,prevText='',nextText='';
			var start = text.lastIndexOf('\n',cursor.start-1);
			nextText = text.substr(end);
			if(start>=0) 
			{
				prevText = text.substr(0,start);
				subText = text.substring(start,end);
			}
			else 
			{
				subText = text.substr(0,end);
			}
			var i = nextText.indexOf('\n',1);
			if(i>0)
			{
				this.textarea.value = prevText + nextText.substr(0,i) + subText + nextText.substr(i);
				this.setCaretPos(prevText.length+i+1,0);
			}
			else
			{
				this.textarea.value = prevText + nextText + subText;
				this.setCaretPos(prevText.length+nextText.length+1,0);
			}
		}
		return false;
	},
	previewShow:function(text,f)
	{
		if(!this.previewContent) this.previewContent = this.preview.contentWindow.document.getElementById('content');
		var text = text || this.textarea.value;
		text = text.replace(/<p[^>]*>((\s|\S)*?)<\/p>/g,"$1<br/>").replace(/>(\s)+</gm,'/><').replace(/<code[^>]*>([\s\S]*?)<\/code>/igm,function(a,code)
		{
			return "<code>"+code.replace(/\t/gm,'    ').replace(/ /gm,'&nbsp;').replace(/</gm,'&lt;').replace(/>/gm,'&gt;')+"</code>";
		}).replace(/\n/gm,'<br/>');
		this.previewContent.innerHTML = text;
	},
	getResulText:function()
	{
		var text = this.textarea.value;
		this.isTipograf;
		this.isColer;
		jsFW.displayBlock({opacity:'0.30',backgroundColor:'#C1D9F0'});
		var ajaxLoader = $.create('div',{className:'AjaxLoader'},document.body);
		$.create('img',{src:'/images/empty.gif'},ajaxLoader);
		var _this = this;
		try{
			jsFW.HttpRequest.submit({
				url:'/habraeditor/getResultText.aspx',
				param:{
					isTipograf:this.isTipograf,
					isColer:this.isColer,
					isNumberCode:this.isNumberCode,
					isHyphenWords:this.isHyphenWords,
					text:text
				},
				onload:function(text,success)
				{
					if(success)
					{
						_this.showResultText(text)
					}
					else
						jsFW.displayUnblock();
					$.remove(ajaxLoader);
				}
			});
		}catch(e){
			$.remove(ajaxLoader);
			jsFW.displayUnblock();
		}
	},
	showResultText:function(text){
		text = text.replace(/<!--.*\n.*$/g,'');
		this.winParam = $.create('div',{className:'Win'});
		$.create('h5',{innerHTML:'Скопируйте текст и вставте на хабре.'},this.winParam);
		var ta = $.create('textarea',{value:text},$.create('div',this.winParam));
		var b = $.create('input',{value:'OK',type:'button'},$.create('center',this.winParam));
		var _this = this;
		b.onclick = function(){
			_this.closeWinParam();
		};
		ta.onmousedown = function()
		{
			_this.setCaretPos(0,this.value.length,this);
			this.focus();
			return false;
		};
		document.body.appendChild(this.winParam);
		jsfw.fx.opacity(this.winParam,0,1,{time:200});
	},
	previewShowResult:function()
	{
		var _this=this;
		if(!this.previewContent) this.previewContent = this.preview.contentWindow.document.getElementById('content');
		var ajaxLoader = $.create('div',{className:'AjaxLoader'},document.body);
		$.create('img',{src:'/images/empty.gif'},ajaxLoader);
		try
		{
		jsFW.HttpRequest.submit({
				url:'/habraeditor/getResultText.aspx',
				param:{
					isTipograf:this.isTipograf,
					isColer:this.isColer,
					isHyphenWords:this.isHyphenWords,
					isNumberCode:this.isNumberCode,
					text:this.textarea.value
				},
				onload:function(text,success)
				{
					if(success)
					{
						/* var t = text.split('<code');
						if(t.length>1)
						{
							var res = [t[0].replace(/\n/mg,'<br/>')];
							var code,t2;
							for(var i=1,l=t.length;i<l;i++)
							{
								var c = t[i];
								var ind = c.indexOf('>')+1;
								if(ind>0 && (code = c.substr(ind)) && (t2 = code.split('</code>')) && t2.length>1)
								{
									res.push("<code>" + t2[0] +"</code>" + t2[1].replace(/\n/mg,'<br/>'));
								}
								else
									res.push("<code"+c.replace(/\n/mg,'<br/>'));
							}
							text = res.join('');
						} */
						_this.previewContent.innerHTML = text.replace(/>(\s)+</gm,'/> <').replace(/\n/gm,'<br/>');
					}
					$.remove(ajaxLoader);
				}
			});
		} catch(er){
			$.remove(ajaxLoader);
		}
	}
},jsfw.Object);
Editor.tab = '    ';
Editor.insert_tag = {
	'format-text-bold':{open:'<strong>',close:'</strong>'},
	'format-text-italic':{open:'<em>',close:'</em>'},
	'format-text-underline':{open:'<u>',close:'</u>'},
	'format-text-strikethrough':{open:'<strike>',close:'</strike>'},
	'format-text-sup':{open:'<sup>',close:'</sup>'},
	'format-text-sub':{open:'<sub>',close:'</sub>'},
	'header-h1':{open:'<h1>',close:'</h1>'},
	'header-h2':{open:'<h2>',close:'</h2>'},
	'header-h3':{open:'<h3>',close:'</h3>'},
	'header-h4':{open:'<h4>',close:'</h4>'},
	'header-h5':{open:'<h5>',close:'</h5>'},
	'header-h6':{open:'<h6>',close:'</h6>'},
	'text-paragraf':{open:'<p>\n{tab}',close:'\n</p>'},
	'text-ul':{open:'<ul>\n{tab}<li>',close:'</li>\n</ul>'},
	'text-ol':{open:'<ol>\n{tab}<li>',close:'</li>\n</ol>'},
	'text-li':{open:'<li>',close:'</li>'},
	'text-blockquote':{open:'<blockquote>\n',close:'\n</blockquote>'},
	'text-br':{open:'<br/>\n',close:''},
	'text-pre':{open:'<pre>\n{tab}',close:'\n</pre>'},
	'text-code':{open:'<code>\n',close:'\n</code>'},
	'text-code-color':{open:'<code class="{class}">\n',close:'\n</code>'},
	'text-link':{open:'<a href="{0}" title="{1}">',close:'</a>',params:[{title:'Сылка',def:'http://'},{title:'Описание'}]},
	'text-image':{open:'<img src="{0}" alt="{1}"/>',close:'',params:[{title:'Источник',def:'http://'},{title:'Альтернативный текст'}]},
	'text-table':{open:'<table>\n{tab}<tr>\n{tab}{tab}<td>',close:'</td>\n{tab}</tr>\n</table>\n'},
	'text-table-tr':{open:'<tr>\n{tab}<td>',close:'</td>\n</tr>'},
	'text-table-td':{open:'<td>',close:'</td>'},
	'text-table-th':{open:'<th>',close:'</th>'},
	/* спец символы */
	'spec-sym-euro':{open:'€',close:''},
	'spec-sym-copy':{open:'©',close:''},
	'spec-sym-r':{open:'®',close:''},
	'spec-sym-tm':{open:'™',close:''},
	'spec-sym-amp':{open:'&amp;',close:''},
	'spec-sym-p':{open:'¶',close:''},
	'spec-sym-short-tere':{open:'–',close:''},
	'spec-sym-tere':{open:'—',close:''},
	'spec-sym-quotes':{open:'«',close:'»'},
	'text-table-lt':{open:'&lt;',close:''},
	'text-table-gt':{open:'&gt;',close:''},
	/*Хабра*/
	'habra-habracut':{open:'<habracut text="{0}"/>\n',close:'',params:[{title:'Читать дальше -> ',def:'Читать дальше'}]},
	'habra-user':{open:'<hh user="{0}"/>\n',close:'',params:[{title:'Имя хабраюзера'}]},
	'habra-video':{open:'<video>',close:'</video>'}
};
Editor.block_tag = ['div','ul','ol','p','h1','h2','h3','h4','h5','h6'];

Editor.hotkey = {
	'tab':{
		cmd:'insertTab'
	},
	enter:{
		cmd:'enterTab'
	},
	ctrl:{
		B:{
			cmd:'execute',
			params:['format-text-bold']
		},
		I:{
			cmd:'execute',
			params:['format-text-italic']
		},
		U:{
			cmd:'execute',
			params:['format-text-underline']
		},
		alt:{
			
		},
		shift:{
			up:{
				cmd:'lineUp'
			},
			down:{
				cmd:'lineDown'
			}
		}
	},
	alt: {
		'1':{
			cmd:'execute',
			params:['header-h1']
		},
		'2':{
			cmd:'execute',
			params:['header-h2']
		},
		'3':{
			cmd:'execute',
			params:['header-h3']
		},
		'4':{
			cmd:'execute',
			params:['header-h4']
		},
		'5':{
			cmd:'execute',
			params:['header-h5']
		},
		'6':{
			cmd:'execute',
			params:['header-h6']
		},
		P:{
			cmd:'execute',
			params:['text-paragraf']
		},
		U:{
			cmd:'execute',
			params:['text-ul']
		},
		O:{
			cmd:'execute',
			params:['text-ol']
		},
		L:{
			cmd:'execute',
			params:['text-li']
		},
		N:{
			cmd:'execute',
			params:['text-pre']
		},
		A:{
			cmd:'execute',
			params:['text-link']
		},
		T:{
			cmd:'execute',
			params:['text-table']
		},
		R:{
			cmd:'execute',
			params:['text-table-tr']
		},
		D:{
			cmd:'execute',
			params:['text-table-td']
		},
		H:{
			cmd:'execute',
			params:['text-table-th']
		},
		Q:{
			cmd:'execute',
			params:['text-blockquote']
		},
		C:{
			cmd:'execute',
			params:['text-code']
		},
		shift: {
			H:{
				cmd:'execute',
				params:['habra-habracut']
			},
			U:{
				cmd:'execute',
				params:['habra-user']
			},
			V:{
				cmd:'execute',
				params:['habra-video']
			}
		}
	},
	shift: {
		tab:{
			cmd:'deleteTab'
		},
		enter:{
			cmd:'execute',
			params:['text-br']
		}
	}
};

var ControlBar = jsfw.Class({
	__constructor:function(o)
	{
		this.dom = $(o.el);
		if(!this.dom) this.dom = $.create('div',{className:'controlBar'});
		this.domButtons = $.getTag('ul','Buttons',this.dom)[0];
		var _this = this;
		if(this.domButtons){
			this.tabs = $.getTag('a','Button',this.domButtons,function(e)
			{
				var id = e.hash.substr(1);
				var b = new Button({el:e,id:id});
				var panel = $(id);
				if(!panel) panel = $.create('div',{className:'Panel'},_this.dom);
				else
				{
					var buttons = $.getTag('div','group',panel,function(buttons){
						var bs = [];
						var e;
						for(var i=buttons.childNodes.length;i--;)
						{
							e = buttons.childNodes[i];
							if($.ifClass(e,'Button'))
							{
								var b = new Button({el:e,id:e.id});
							}
							else if($.ifClass(e,'ButtonList'))
							{
								var b = new ButtonList({el:e,id:e.id});
							}
							if(b)
							{
								b.addEvent('onclick',$d(_this,_this.clickButton));
								bs.push(b);
								b=null;
							}
						}
						if(bs.length>0) $.addClass(bs[0].getDom(),'last');
						return bs;
					});
				}
				panel.style.display = 'none';
				var tab = {
					button:b,
					panel:panel
				};
				b.addEvent('onclick',$d(_this,_this.selectTab,[tab]));
				return tab;
			});
		}
		else{
			this.domButtons = $.create('ul',{className:'Buttons'});
		}
		this.selectTab(null,null,this.tabs[0]);
	},
	clickButton:function(button,e)
	{
		return this.callEvent('onclick',[button.id,button]);
	},
	selectTab:function(button,e,tab)
	{
		if(this.sTab!=tab)
		{
			if(this.sTab)
			{
				this.sTab.button.unselect();
				this.sTab.panel.style.display = 'none';
			}
			tab.button.select();
			tab.panel.style.display = '';
			this.sTab=tab;
			this.callEvent('onselecttab',[tab]);
		}
		return false;
	}
},jsfw.Object);

var Button = jsfw.Class(function(){
	var count_id = 0;
return {
	__constructor:function(o)
	{
		this.dom = $(o.el);
		this.id = o.id || count_id++;
		if(!this.dom) this.dom = $.create('a',{className:'controlBar'});
		this.dom.onclick = $d(this,function(s,e){return this.callEvent('onclick',[e]);});
	},
	select:function()
	{
		$.addClass(this.dom,'selected');
	},
	unselect:function()
	{
		$.removeClass(this.dom,'selected');
	},
	getDom:function()
	{
		return this.dom;
	}
}},jsfw.Object);

var ButtonList = jsfw.Class(function(){
	var count_id = 0;
return {
	__constructor:function(o)
	{
		this.dom = $(o.el);
		if(!this.dom) this.dom = $.create('a',{className:'controlBar'});
		this.list = this.dom.getElementsByTagName('ul')[0];
		//this.list.style.display = 'none';
		this.toggleList = $.getTag('a','toggleList',this.dom)[0];
		this.toggleList.onclick = $d(this,this.eToogleList);
		var dSelectItem = $d(this,this.eSelectItem);
		$.getTag('a','Button',this.dom)[0].onclick = $d(this,function(){this.hideList();this.callEvent('onclick')});
		this.id = $.getTag('a','Button',this.list,function(e){
			e.onclick = dSelectItem;
			return e;
		})[0].id;
	},
	eSelectItem:function(a)
	{
		this.id = a.id;
		this.hideList();
		this.callEvent('onclick');
		return false;
	},
	eToogleList:function()
	{
		if(this.isShow) this.hideList(); else this.showList();
		return false;
	},
	showList:function()
	{
		if(!this.isShow)
		{
			$.addClass(this.dom,'show');
			jsFW.displayBlock({opacity:'0.1',onclick:$d(this,this.hideList)});
			jsfw.fx.opacity(this.list,0,1,{time:200});
		}
		this.isShow = true;
	},
	hideList:function()
	{
		if(this.isShow)
		{
			$.opacity(this.list,0);
			var _this = this;
			jsfw.fx.opacity(this.list,1,0,{
				time:100,
				oncalbackEnd:function()
				{
					$.removeClass(_this.dom,'show');
					jsfw.displayUnblock();
				}
			});
		}
		this.isShow = false;
	},
	select:function()
	{
		$.addClass(this.dom,'selected');
	},
	unselect:function()
	{
		$.removeClass(this.dom,'selected');
	},
	getDom:function()
	{
		return this.dom;
	}
}},jsfw.Object);

jsfw.ready(function(){
	window.habraeditor = new Editor({
		el:'editor'
	});
});