/*
Code Translit
*/
(function ($) {
	var ageent_time_stamp = new String();
	var ageent_max_key = 0;

	$.fn.Code_translit = function (settings) {
		settings = jQuery.extend({
			rules: 1,
			obect_now : "",
			warning : true
		},
		settings);

		if(settings.rules==1) {
			/* ISO 9-95 */
			en_to_ru={"YE":"Є", "I":"І", "G":"Ѓ", "i":"і", "#":"№", "ye":"є", "g":"ѓ", "A":"А", "B":"Б", "V":"В", "G":"Г", "D":"Д", "E":"Е", "YO":"Ё", "ZH":"Ж", "Z":"З", "I":"И", "J":"Й", "K":"К", "L":"Л", "M":"М", "N":"Н", "O":"О", "P":"П", "R":"Р", "S":"С", "T":"Т", "U":"У", "F":"Ф", "X":"Х", "C":"Ц", "CH":"Ч", "SH":"Ш", "SHH":"Щ", "\'":"Ъ", "Y":"Ы", "":"Ь", "E":"Э", "YU":"Ю", "YA":"Я", "a":"а", "b":"б", "v":"в", "g":"г", "d":"д", "e":"е", "yo":"ё", "zh":"ж", "z":"з", "i":"и", "j":"й", "k":"к", "l":"л", "m":"м", "n":"н", "o":"о", "p":"п", "r":"р", "s":"с", "t":"т", "u":"у", "f":"ф", "x":"х", "c":"ц", "ch":"ч", "sh":"ш", "shh":"щ", "":"ъ", "y":"ы", "":"ь", "e":"э", "yu":"ю", "ya":"я", "":"«", "":"»", "-":"—"};
		} else if(settings.rules==2) {
			/* ГОСТ 16876-71 */ 
			en_to_ru={"EH":"Є", "I":"І", "i":"і", "#":"№", "eh":"є", "A":"А", "B":"Б", "V":"В", "G":"Г", "D":"Д", "E":"Е", "JO":"Ё", "ZH":"Ж", "Z":"З", "I":"И", "JJ":"Й", "K":"К", "L":"Л", "M":"М", "N":"Н", "O":"О", "P":"П", "R":"Р", "S":"С", "T":"Т", "U":"У", "F":"Ф", "KH":"Х", "C":"Ц", "CH":"Ч", "SH":"Ш", "SHH":"Щ", "'":"Ъ", "Y":"Ы", "":"Ь", "EH":"Э", "YU":"Ю", "YA":"Я", "a":"а", "b":"б", "v":"в", "g":"г", "d":"д", "e":"е", "jo":"ё", "zh":"ж", "z":"з", "i":"и", "jj":"й", "k":"к", "l":"л", "m":"м", "n":"н", "o":"о", "p":"п", "r":"р", "s":"с", "t":"т", "u":"у", "f":"ф", "kh":"х", "c":"ц", "ch":"ч", "sh":"ш", "shh":"щ", "":"ъ", "y":"ы", "":"ь", "eh":"э", "yu":"ю", "ya":"я", "":"«", "":"»", "-":"—", "h":"х","H":"Х"};
		}
		// get count max key in array. only once 
		if(ageent_max_key==0) {
			for (x in en_to_ru) {
				if(ageent_max_key < x.length) {
					ageent_max_key = x.length;
				}
			}
		}

		text_obect = settings.obect_now.value;
		is_translate = check_en_alphabet(text_obect);

		if(is_translate) {
			if(settings.warning) {
				if(confirm("Текст сообщения состоит из латинских символов. Применить обратную транслитерацию?")) {
					text_obect = code_get_translate(en_to_ru, text_obect, 2, 3);
					text_obect = code_get_translate(en_to_ru, text_obect, 1, 2);
					text_obect = code_get_translate(en_to_ru, text_obect, 0, 1);
					settings.obect_now.value = text_obect;
					alert('Транслитерация произведена. Проверте, пожалуйста, получившееся сообщение');
					return false;
				}
			} else {
				text_obect = code_get_translate(en_to_ru, text_obect, 2, 3);
				text_obect = code_get_translate(en_to_ru, text_obect, 1, 2);
				text_obect = code_get_translate(en_to_ru, text_obect, 0, 1);
				settings.obect_now.value = text_obect;
			}
		}
		return true;
	};

	/**
		* Function for translate, all web browser but not IE
		* evnt object events
		* en_to_ru  array  rules
		* obect object 
	*/
	function code_get_translate(en_to_ru, text_obect, simbol, len) {

		if(len == 3) {
			if(text_obect.substring(simbol, simbol+1)) {
				txt = text_obect.substring(simbol-2, simbol-1)+text_obect.substring(simbol-1, simbol)+text_obect.substring(simbol, simbol+1);
				if(code_array_key_exists(txt,en_to_ru)) {
					newtext = text_obect.substring(0, simbol-2) + en_to_ru[txt] + text_obect.substring(simbol+1);
					text_obect = code_get_translate(en_to_ru, newtext, simbol, len);
				} else {
					text_obect = code_get_translate(en_to_ru, text_obect, simbol+1, len);
				}
			}
		} else if(len==2) {
			if(text_obect.substring(simbol, simbol+1)) {
				txt = text_obect.substring(simbol-1, simbol)+text_obect.substring(simbol, simbol+1);
				if(code_array_key_exists(txt,en_to_ru)) {
					newtext = text_obect.substring(0, simbol-1) + en_to_ru[txt] + text_obect.substring(simbol+1);
					text_obect = code_get_translate(en_to_ru, newtext, simbol, len);
				} else {
					text_obect = code_get_translate(en_to_ru, text_obect, simbol+1, len);
				}
			}
		} else if(len==1) {
			if(text_obect.substring(simbol, simbol+1)) {
				txt = text_obect.substring(simbol, simbol+1);
				if(code_array_key_exists(txt,en_to_ru)) {
					newtext = text_obect.substring(0, simbol) + en_to_ru[txt] + text_obect.substring(simbol+1);
					text_obect = code_get_translate(en_to_ru, newtext, simbol, len);
				} else {
					text_obect = code_get_translate(en_to_ru, text_obect, simbol+1, len);
				}
			}
		}

		return text_obect;
	}

	function check_en_alphabet(text_obect) {
		en_alphabet=Array("A","a","B","b","C","c","D","d","E","e","F","f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y","y","Z","z");
		count = 0;
		for(i=0; i<text_obect.length; i++) {
			for(j=0;j<52; j++) {
				if(text_obect.substring(i, i+1)==en_alphabet[j]) {
					count=count+1;
					break;
				}
			}
		}

		if((count*2)>=text_obect.length && text_obect.length>0) {
			return true;
		} else {
			return false;
		}
	}

	/**
		* Checking for the presence of key
	*/
	function code_array_key_exists ( key, search ) {   // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		if( !search || (search.constructor !== Array && search.constructor !== Object) ){
			return false;
		}
		return search[key] !== undefined;
	}
})(jQuery);