﻿/*
 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 */
(function(){var c={getSelection:function(){var e=this.jquery?this[0]:this;return(('selectionStart'in e&&function(){var l=e.selectionEnd-e.selectionStart;return{start:e.selectionStart,end:e.selectionEnd,length:l,text:e.value.substr(e.selectionStart,l)}})||(document.selection&&function(){e.focus();var r=document.selection.createRange();if(r==null){return{start:0,end:e.value.length,length:0}}var a=e.createTextRange();var b=a.duplicate();a.moveToBookmark(r.getBookmark());b.setEndPoint('EndToStart',a);return{start:b.text.length,end:b.text.length+r.text.length,length:r.text.length,text:r.text}})||function(){return{start:0,end:e.value.length,length:0}})()},replaceSelection:function(){var e=this.jquery?this[0]:this;var a=arguments[0]||'';return(('selectionStart'in e&&function(){e.value=e.value.substr(0,e.selectionStart)+a+e.value.substr(e.selectionEnd,e.value.length);return this})||(document.selection&&function(){e.focus();document.selection.createRange().text=a;return this})||function(){e.value+=a;return this})()}};jQuery.each(c,function(i){jQuery.fn[i]=this})})();

	function bubb(twoc,loldkey,key1){
		if(twoc.toLowerCase()=="цх" && (loldkey==67 || loldkey==99)) if(key1<97) twoc="Ч"; else twoc="ч";
		else if(twoc.toLowerCase()=="сх" && (loldkey==83 || loldkey==115)) if(key1<97) twoc="Ш"; else twoc="ш";
		else if(twoc.toLowerCase()=="ыа" && (loldkey==89 || loldkey==121)) if(key1<97) twoc="Я"; else twoc="я";
		else if(twoc.toLowerCase()=="ыо" && (loldkey==89 || loldkey==121)) if(key1<97) twoc="Ё"; else twoc="ё";
		else if(twoc.toLowerCase()=="ыэ" && (loldkey==89 || loldkey==121)) if(key1<97) twoc="Е"; else twoc="е";
		else if(twoc.toLowerCase()=="ыу" && (loldkey==89 || loldkey==121)) if(key1<97) twoc="Ю"; else twoc="ю";
		
		else if(twoc.toLowerCase()=="ии" && (loldkey==73 || loldkey==105)) if(key1<97) twoc="ИЙ"; else twoc="ий";
		else if(twoc.toLowerCase()=="аи" && (loldkey==65 || loldkey==97)) if(key1<97) twoc="АЙ"; else twoc="ай";
		else if(twoc.toLowerCase()=="эи" && (loldkey==69 || loldkey==101)) if(key1<97) twoc="ЭЙ"; else twoc="эй";
		else if(twoc.toLowerCase()=="уи" && (loldkey==85 || loldkey==117)) if(key1<97) twoc="УЙ"; else twoc="уй";
		else if(twoc.toLowerCase()=="үи" && (loldkey==87 || loldkey==119)) if(key1<97) twoc="ҮЙ"; else twoc="үй";
		else if(twoc.toLowerCase()=="ои" && (loldkey==79 || loldkey==111)) if(key1<97) twoc="ОЙ"; else twoc="ой";
		else if(twoc.toLowerCase()=="оь" && (loldkey==79 || loldkey==111)) if(loldkey<97) twoc="Ө"; else twoc="ө";
		else if(twoc.toLowerCase()=="уь" && (loldkey==85 || loldkey==117)) if(loldkey<97) twoc="Ү"; else twoc="ү";		
		else if(twoc=="Шь") if(loldkey<97) twoc="Щ"; else twoc="Щ";
		else if(twoc=="шь") if(loldkey<97) twoc="щ"; else twoc="щ";
		else if(twoc=="ьь") if(loldkey<97) twoc="Ь"; else twoc="Ь";
		else if(twoc=="Ьь") if(loldkey<97) twoc="'"; else twoc="'";
		else if(twoc=="ъъ") if(loldkey<97) twoc="Ъ"; else twoc="Ъ";
		else if(twoc=="Ъъ") if(loldkey<97) twoc="\""; else twoc="\"";
		
		return twoc;
	}
	
	$.fn.insertAtCaret = function (e) {
			if(typeof( oldkey ) == 'undefined') oldkey="";			
			var key = e.charCode ? e.charCode:e.which;
                        if(key==244) key=113; 	if(key==212) key=81;
			if(key==246) key=119; 	if(key==214) key=87;
			if(key==243) key=101; 	if(key==211) key=69;
			if(key==230) key=114; 	if(key==198) key=82;
			if(key==253) key=116; 	if(key==221) key=84;
			if(key==237) key=121; 	if(key==205) key=89;
			if(key==227) key=117; 	if(key==195) key=85;
			if(key==248) key=105; 	if(key==216) key=73;
			if(key==191) key=111; 	if(key==175) key=79;
			if(key==231) key=112; 	if(key==199) key=80;
			if(key==233) key=97; 	if(key==201) key=65;
			if(key==251) key=115; 	if(key==219) key=83;
			if(key==225) key=100; 	if(key==193) key=68;
			if(key==186) key=102; 	if(key==170) key=70;
			if(key==224) key=103; 	if(key==192) key=71;
			if(key==245) key=104; 	if(key==213) key=72;
			if(key==240) key=106; 	if(key==208) key=74;
			if(key==238) key=107; 	if(key==206) key=75;
			if(key==235) key=108; 	if(key==203) key=76;
			if(key==239) key=39; 	if(key==207) key=34;
			if(key==255) key=122; 	if(key==223) key=90;
			if(key==247) key=120; 	if(key==215) key=88;
			if(key==184) key=99; 	if(key==168) key=67;
			if(key==241) key=118; 	if(key==209) key=86;
			if(key==236) key=98; 	if(key==204) key=66;
			if(key==232) key=110; 	if(key==200) key=78;
			if(key==242) key=109; 	if(key==210) key=77;

                        if(key==229) key=45; 	if(key==197) key=95;
			if(key==249) key=61; 	if(key==217) key=43;
			if(key==234) key=91; 	if(key==202) key=123;
			if(key==250) key=93; 	if(key==218) key=125;
			if(key==228) key=59; 	if(key==196) key=58;
			if(key==252) key=44; 	if(key==220) key=60;
			if(key==226) key=46; 	if(key==194) key=62;
			if(key==254) key=47; 	if(key==222) key=63;
			if(e.ctrlKey) return true;
			var enstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var mnstr = "АБЦДЭФГХИЖКЛМНОПӨРСТУВҮХЫЗ";
			if((key>=65 && key<=90) || (key>=97 && key<=122) || key==34 || key==39 || key==45 || key==95 || key==61 || key==43 || key==91 || key==123 || key==93 || key==125 || key==59 || key==58 || key==44 || key==60 || key==46 || key==62 || key==47 || key==63){
				if(key==34) myValue="ъ" ;
				else if(key==39) myValue="ь";
                                else if(key==45) myValue="-";
				else if(key==61) myValue="=";
				else if(key==91) myValue="[";
				else if(key==93) myValue="]";
				else if(key==59) myValue=";";
				else if(key==44) myValue=",";
				else if(key==46) myValue=".";
				else if(key==47) myValue="/";
				else if(key==95) myValue="_";
				else if(key==43) myValue="+";
				else if(key==123) myValue="{";
				else if(key==125) myValue="}";
				else if(key==58) myValue=":";
				else if(key==60) myValue="<";
				else if(key==62) myValue=">";
				else if(key==63) myValue="?";
				else if(key<97) myValue=mnstr.charAt(key-65);
				else myValue=mnstr.toLowerCase().charAt(key-97);
				
				this.each(function(){
						//IE support						
						if (document.selection) {
								this.focus();
								sel = document.selection.createRange();	
								sel.text = myValue;	
								sel.select();								
								sel.moveStart("character",-2); sel.moveEnd("character",0);								

								if(sel.text.toLowerCase()=="цх" && (oldkey==67 || oldkey==99)) if(key<97) sel.text="Ч"; else sel.text="ч";
								else if(sel.text.toLowerCase()=="сх" && (oldkey==83 || oldkey==115)) if(key<97) sel.text="Ш"; else sel.text="ш";
								else if(sel.text.toLowerCase()=="ыа" && (oldkey==89 || oldkey==121)) if(key<97) sel.text="Я"; else sel.text="я";
								else if(sel.text.toLowerCase()=="ыо" && (oldkey==89 || oldkey==121)) if(key<97) sel.text="Ё"; else sel.text="ё";
								else if(sel.text.toLowerCase()=="ыэ" && (oldkey==89 || oldkey==121)) if(key<97) sel.text="Е"; else sel.text="е";
								else if(sel.text.toLowerCase()=="ыу" && (oldkey==89 || oldkey==121)) if(key<97) sel.text="Ю"; else sel.text="ю";
								
								else if(sel.text.toLowerCase()=="ии" && (oldkey==73 || oldkey==105)) if(key<97) sel.text="ИЙ"; else sel.text="ий";
								else if(sel.text.toLowerCase()=="аи" && (oldkey==65 || oldkey==97)) if(key<97) sel.text="АЙ"; else sel.text="ай";
								else if(sel.text.toLowerCase()=="эи" && (oldkey==69 || oldkey==101)) if(key<97) sel.text="ЭЙ"; else sel.text="эй";
								else if(sel.text.toLowerCase()=="уи" && (oldkey==85 || oldkey==117)) if(key<97) sel.text="УЙ"; else sel.text="уй";
								else if(sel.text.toLowerCase()=="үи" && (oldkey==87 || oldkey==119)) if(key<97) sel.text="ҮЙ"; else sel.text="үй";
								else if(sel.text.toLowerCase()=="ои" && (oldkey==79 || oldkey==111)) if(key<97) sel.text="ОЙ"; else sel.text="ой";
								else if(sel.text.toLowerCase()=="оь" && (oldkey==79 || oldkey==111)) if(oldkey<97) sel.text="Ө"; else sel.text="ө";
								else if(sel.text.toLowerCase()=="уь" && (oldkey==85 || oldkey==117)) if(oldkey<97) sel.text="Ү"; else sel.text="ү";
								else if(sel.text=="Шь") if(oldkey<97) sel.text="Щ"; else sel.text="Щ";
								else if(sel.text=="шь") if(oldkey<97) sel.text="щ"; else sel.text="щ";
								else if(sel.text=="ьь") if(oldkey<97) sel.text="Ь"; else sel.text="Ь";
								else if(sel.text=="Ьь") if(oldkey<97) sel.text="'"; else sel.text="'";
								else if(sel.text=="ъъ") if(oldkey<97) sel.text="Ъ"; else sel.text="Ъ";
								else if(sel.text=="Ъъ") if(oldkey<97) sel.text="\""; else sel.text="\"";								

								if(sel.text.length==2) {sel.moveStart("character",2); sel.moveEnd("character",0);}
								//sel.select();								
								this.focus();								
						}
						//MOZILLA/NETSCAPE support
						else if (this.selectionStart || this.selectionStart == '0') {
								this.focus();
								var startPos = this.selectionStart;
								var endPos = this.selectionEnd;
								var scrollTop = this.scrollTop;
								seltext=this.value.substring(startPos-1,startPos);
								seltextlow=seltext+myValue;
								myValue=bubb(seltextlow,oldkey,key);
								if(startPos>0) startPos--;
								this.value = this.value.substring(0, startPos)+myValue+ this.value.substring(endPos,this.value.length);
								
								this.selectionStart = startPos + myValue.length;
								this.selectionEnd = startPos + myValue.length;
								this.scrollTop = scrollTop;
						} else {
								this.value += myValue;
								this.focus();
						}
				});
				oldkey=key;
				return false;
			} else { oldkey=key; return true;	}
	};