var effect=
{
	textarea:
	{
		active:function(ev)
		{
			var code = (ev.keyCode ? ev.keyCode : ev.charCode);
		
			if(code != 9)	return;
			if(ev.ctrlKey)	return;
			
			ev.preventDefault();
			ev.stopPropagation();
			
			var selSt = this.selectionStart;
			var selSp = this.selectionEnd;
			var shift = ev.shiftKey;
			
			var value = this.value;
			var stt = 0
			var stp = 0;
			var x	= value.substring(selSt,selSp);
			var top = this.scrollTop||0;
			
			if(shift)
			{
				this.value = value.substring(0,selSt).replace(/((?:.*\r?\n)*)([^\r\n]+$)/gi,function(a,b,c)
				{
					x = c+x;
					return b;
				});
				
				if(x.substring(0,1)=="\t")
				stt++;
				
				this.value += x.replace(/(\r?\n|^)(\t+)/gi,function(a,b,c)
				{
					stp++;
					return b+c.substring(1);
				})+value.substring(selSp);
					
				this.selectionStart	= selSt-stt;
				this.selectionEnd	= selSp-stp;
				this.scrollTop		= top;
				return;
			}
			
			if(selSt == selSp)
			{
				this.value = this.value.substring(0,selSt)+"\t"+this.value.substring(selSt);
				this.selectionStart	= selSt+1;
				this.selectionEnd	= selSp+1;
				this.scrollTop		= top;
				return;
			}
			
			x = value.substring(selSt,selSp);
			
			this.value = value.substring(0,selSt).replace(/((?:.*\r?\n)*)([^\r\n]+$)/gi,function(a,b,c)
			{
				x = c+x;
				return b;
			});
			
			this.value += x.replace(/(\r?\n|^)/gi,function(a,b)
			{
				stp++;
				return b+"\t";
			})+value.substring(selSp);
				
			this.selectionStart	= selSt+1;
			this.selectionEnd	= selSp+stp;
			this.scrollTop		= top;
		}
	},
	//
	inserttag:function(txtarea)
	{
		var txtarea = $(txtarea);
		
		txtarea.focus();
		theSelection = false;
		
		if ((clientVer >= 4) && is_ie && is_win)
		{
			theSelection = document.selection.createRange().text;
			if (theSelection)
			{
				document.selection.createRange().text = usertags[tagnumber] + theSelection + usertags[tagnumber+1];
				txtarea.focus();
				theSelection = '';
				return;
			}
		}
		else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
		{
			mozWrap(txtarea, usertags[tagnumber], usertags[tagnumber+1]);
			return;
		}
		
		txtarea.value += usertags[tagnumber] + usertags[tagnumber+1];
		
		if (!((clientVer >= 4) && is_ie && is_win))
		{
			newpos = txtarea.value.length-usertags[tagnumber+1].length;
			txtarea.setSelectionRange(newpos, newpos);
		}
		
		txtarea.focus();
	},
	mozWrap:function(txtarea, open, close)
	{
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;
		
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + open + s2 + close + s3;
		newpos = txtarea.value.length-close.length;
		txtarea.setSelectionRange(newpos, newpos);
		return;
	},
	
	close:
	{
		init:function(obj)
		{
			var item = new effect.close.listener(obj);
			
			return item;
		},
		
		listener:function(obj)
		{
			this.obj=obj;
			this.changed=false;
			
			var inp = obj.getElementsByTagName('input');
			var txt = obj.getElementsByTagName('textarea');
			
			this.change = function()
			{
				console.log(this.tagName);
				self.changed=true;
			}
			
			for(var i=0;i<inp.length;i++)
			{
				this.change.attach('change',inp[i]);
			}
			
			for(var i=0;i<txt.length;i++)
			{
				this.change.attach('change',txt[i]);
			}
		}
		
	}
};

(function()
{
	var textarea=[];
	var is_ie=(navigator.userAgent.indexOf("MSIE") != -1);
	
	var init=function(name, max_height)
	{
		var name = $(name);
		if (!name) return;
		
		for(var i=0,num=textarea.length;i<num;i++)
		{
			if (textarea[i]['obj']==name)
			return textarea[i];
		}
		
		var obj = {};
		obj.obj				= name;
		obj.first_height	= null;
		obj.cur_height		= null;
		obj.max_height		= max_height;
		obj.HasFocus		= false;
		
		//(function(){obj.HasFocus=true}).attach('focus',obj.obj);
		//(function(){obj.HasFocus=false}).attach('blur',obj.obj);
		
		textarea.push(obj);
		
		return textarea[i];
	};
	
	var defaults=function(editor)
	{
		if(!editor.first_height) return;	//размер не меняли
		
		//var frame = getFrame(editor);
		//if(!frame)	return;
		
		editor.obj.style.height=editor.first_height+'px';
		
		//frame.style.height=(frame.height=editor.first_height)+'px';
		
		editor.cur_height=null;
	}
	
	var getHeight=function(editor)
	{
		var tmp=editor.obj;
		
		var h1=tmp.scrollHeight;
		var h2=tmp.offsetHeight;
		var h3=tmp.clientHeight;
		
		if (!editor.first_height)
		editor.first_height=h2;
		
		if(is_ie) return (h1>editor.max_height ? editor.max_height : h1);
		
		if (h1>(h2+1))		h2 = h1-1;
		else if (h1==h2)	h2 -= 20;
		
		return (h2>editor.max_height ? editor.max_height : h2);
	}
	
	var setHeight=function(name,max_height)
	{
		var editor=init(name, max_height);
		
		if(!editor) return;
		
		var height = getHeight(editor);
		
		//if(!editor.HasFocus)	//не в фокусе, делаем стандартные размеры
		//return;// defaults(editor);
		
		if (!height)	return;
		
		if(editor.cur_height == height) return;
		
		if(height<=editor.first_height) return defaults(editor);
		
		editor.obj.style.height=(editor.cur_height=height)+'px';
	}
	
	window.effect.textarea.height=function(name, max_height)
	{
		setInterval(function(){try{setHeight(name, max_height);}catch(e){}},10);
	};
	
})();
