﻿var LATIN_MAP = {
    'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE', 'Ç':
    'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I', 'Î': 'I',
    'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö':
    'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U', 'Ű': 'U',
    'Ý': 'Y', 'Þ': 'TH', 'ß': 'ss', 'à':'a', 'á':'a', 'â': 'a', 'ã': 'a', 'ä':
    'a', 'å': 'a', 'æ': 'ae', 'ç': 'c', 'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e',
    'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó':
    'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u',
    'û': 'u', 'ü': 'u', 'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
};

var LATIN_SYMBOLS_MAP = {
    '©':'(c)'
};

var RUSSIAN_MAP = {
    'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e', 'ё':'yo', 'ж':'zh',
    'з':'z', 'и':'i', 'й':'j', 'к':'k', 'л':'l', 'м':'m', 'н':'n', 'о':'o',
    'п':'p', 'р':'r', 'с':'s', 'т':'t', 'у':'u', 'ф':'f', 'х':'h', 'ц':'c',
    'ч':'ch', 'ш':'sh', 'щ':'sh', 'ъ':'', 'ы':'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', 'Х':'H', 'Ц':'C',
    'Ч':'Ch', 'Ш':'Sh', 'Щ':'Sh', 'Ъ':'', 'Ы':'Y', 'Ь':'', 'Э':'E', 'Ю':'Yu',
    'Я':'Ya'
};

var ALL_DOWNCODE_MAPS = new Array();
ALL_DOWNCODE_MAPS[0] = LATIN_MAP;
ALL_DOWNCODE_MAPS[1] = LATIN_SYMBOLS_MAP;
ALL_DOWNCODE_MAPS[2] = RUSSIAN_MAP;

var Downcoder = new Object();
Downcoder.Initialize = function() {
    if (Downcoder.map) { // already made
        return ;
    }
    Downcoder.map = {};
    Downcoder.chars = '' ;
    for (var i in ALL_DOWNCODE_MAPS) {
        var lookup = ALL_DOWNCODE_MAPS[i];
        for (var c in lookup) {
            Downcoder.map[c] = lookup[c] ;
            Downcoder.chars += c ;
        }
    }
    Downcoder.regex = new RegExp('[' + Downcoder.chars + ']|[^' + Downcoder.chars + ']+','g') ;
};

downcode= function(slug) {
    Downcoder.Initialize();
    
    var downcoded = "";
    var pieces = slug.match(Downcoder.regex);
    if (pieces) {
        for (var i = 0 ; i < pieces.length ; i++) {
            if (pieces[i].length == 1) {
                var mapped = Downcoder.map[pieces[i]] ;
                if (mapped != null) {
                    downcoded+=mapped;
                    continue ;
                }
            }
            downcoded+=pieces[i];
        }
    } else {
        downcoded = slug;
    }
    return downcoded;
};

function URLify(s, num_chars) {
    s = downcode(s);
    var removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from",
                      "is", "in", "into", "like", "of", "off", "on", "onto", "per",
                      "since", "than", "the", "this", "that", "to", "up", "via",
                      "with"];
    r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi');
    s = s.replace(r, '');

    s = s.replace(/[^-\w\s]/g, '');
    s = s.replace(/^\s+|\s+$/g, '');
    s = s.replace(/[-\s]+/g, '_');
    s = s.toLowerCase();
    
    if (typeof num_chars !== 'undefined') {
        return s.substring(0, num_chars);
    }
    return s;
}

function showInlinePopup(url) {
    var options = {
        closeOnEscape: true,
        closeOnClick: true,
        showCloseButton: true
    };

    $.nmManual(url, options);
    return false;
}

function showPopup(url, width, height, name) {
    if (typeof name == 'undefined' || name === '') {
        name = 'popupwnd';
    } else {
        name = URLify(name);
    }

    var top = (screen.height - height) / 2;
    var left = (screen.width - width) / 2;    
    
    var params = 'toolbar=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,fullscreen=no,resizable=no,' +
                 'width=' + width  +','+
                 'height='+ height +','+
                 'top='   + top    +','+
                 'left='  + left   +'';

    var popupwnd = window.open(url, name, params, true);
    if (popupwnd) {
        popupwnd.focus();
    }
    return false;
}

