

function transliterate(word, isUrl) {

    var tr = {"À": "A", "à": "a", "Á": "B", "á": "b",
        "Â": "V", "â": "v", "Ã": "G", "ã": "g",
        "Ä": "D", "ä": "d", "Å": "E", "å": "e",
        "¨": "E", "¸": "e", "Æ": "Zh", "æ": "zh",
        "Ç": "Z", "ç": "z", "È": "I", "è": "i",
        "É": "Y", "é": "y", "ÊÑ": "X", "êñ": "x",
        "Ê": "K", "ê": "k", "Ë": "L", "ë": "l",
        "Ì": "M", "ì": "m", "Í": "N", "í": "n",
        "Î": "O", "î": "o", "Ï": "P", "ï": "p",
        "Ð": "R", "ð": "r", "Ñ": "S", "ñ": "s",
        "Ò": "T", "ò": "t", "Ó": "U", "ó": "u",
        "Ô": "F", "ô": "f", "Õ": "H", "õ": "h",
        "Ö": "Ts", "ö": "ts", "×": "Ch", "÷": "ch",
        "Ø": "Sh", "ø": "sh", "Ù": "Sch", "ù": "sch",
        "Û": "Y", "û": "y", "Ü": "'", "ü": "'",
        "Ý": "E", "ý": "e", "Ú": "'", "ú": "'",
        "Þ": "Yu", "þ": "yu", "ß": "Ya", "ÿ": "ya"};


    var result = "";

    result = word.split('').map(function(char) {
        return tr[char] || char;
    }).join("");

    if (isUrl == 'yes') {
        result = result
                .toLowerCase() // change everything to lowercase
                .replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces
                .replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen
                .replace(/[^a-z\u0400-\u04FF0-9-]+/g, "") // remove all non-cyrillic, non-numeric characters except the hyphen
                .replace(/[-]+/g, "-") // replace multiple instances of the hyphen with a single instance
                .replace(/^-+|-+$/g, "") // trim leading and trailing hyphens
                .replace(/[-]+/g, "-")
    }
    return result;
}

function InitTransliterate() {
    if ($nc("[data-type='transliterate']").length) {
        $nc.each($nc("[data-type='transliterate']"), function() {
            $nc(this).after('<span class="nc-transliterate-action nc-icon nc--refresh" title="Òðàíñëèòåðàöèÿ íàçâàíèÿ"></span>');
            $nc(this).next('.nc-transliterate-action').click(function(e) {
                e.preventDefault();
                elemName = $nc(this).prev().attr('data-from');
                isUrl = $nc(this).prev().attr('data-is-url');
                $nc(this).prev().val(transliterate($nc('[name="' + elemName + '"]').val(), isUrl));
            });
        });
    }
}
$nc(document).ready(function() {
    InitTransliterate();
});