// Copyright (c)2008 Daniel Pupius, http://www.endoflow.com
// Pseudo localization scripts.

var pseudo = {};

/**
 * Maps characters to an array of potential candidates that can be used for
 * psuedo localization.
 * @type {Object}
 */
pseudo.CHAR_TABLE = {
  'a': ['\u00e2', '\u00e5', '\u04d3'],
  'b': ['\u042C', '\u0184', '\u044a', '\u044c', '\u0463'],
  'c': ['\u00e7', '\u00a2', '\u0481', '\u04ab'],
  'd': ['\u010f', '\u0111', '\u0500', '\u0501', '\u0502'],
  'e': ['\u00ea', '\u0435', '\u0450', '\u04bd', '\u04d7'],
  'f': ['\u0192', '\u0493'],
  'g': ['\u011d', '\u01e5', '\u0581'],  //, '\u0648'
  'h': ['\u0127', '\u045b', '\u056b', '\u0570'],
  'i': ['\u00a1', '\u00ee', '\u0457'],
  'j': ['\u0575'],
  'k': ['\u0137', '\u045c', '\u049b', '\u049d', '\u049f', '\u04a1'],
  'l': ['\u026d'],
  'm': ['\u0271', '\u028d', '\u043c', '\u04ce'],
  'n': ['\u0439', '\u0564', '\u0572', '\u0578', '\u057c', '\u05d7', '\u05ea'],
  'o': ['\u047b', '\u04eb', '\u0585', '\u05e1'],
  'p': ['\u01bf', '\u0440', '\u0562', '\u0569', '\u0584'],
  'q': ['\u0563', '\u0566'],
  'r': ['\u0155', '\u0157', '\u0211', '\u0453'],
  's': ['\u015b', '\u0219', '\u078e'], // makes it rtl: '\u0649'
  't': ['\u0165', '\u0167', '\u01ab', '\u0442', '\u0565', '\u0567'],
  'u': ['\u00b5', '\u0446', '\u045f', '\u0574', '\u0576', '\u057d'],
  'v': ['\u04b1'],
  'w': ['\u0175', '\u026f', '\u0461', '\u047d', '\u0561'],
  'x': ['\u00d7', '\u04b3'],  // RTL: '\u05d0'
  'y': ['\u00ff', '\u0177', '\u01b4', '\u045e', '\u04ee', '\u04f0', '\u04f1', '\u04f2', '\u05e2'],
  'z': ['\u01b6', '\u017a', '\u017c', '\u0225'],
  'A': ['\u00c0', '\u00c3', '\u00c5', '\u0104', '\u0466', '\u04d2'],
  'B': ['\u00df', '\u0181', '\u0412', '\u0432', '\u0545'],
  'C': ['\u00c7', '\u010C', '\u0187', '\u0480', '\u04AA'],
  'D': ['\u00d0'],
  'E': ['\u00ca', '\u0400', '\u018e', '\u0401', '\u0404', '\u0415', '\u04bc', '\u04d6'],
  'F': ['\u0191', '\u0492'],
  'G': ['\u011c', '\u011e', '\u0193', '\u01e4', '\u050c'],
  'H': ['\u0124', '\u01f6', '\u04A2', '\u043d', '\u04A4', '\u04c7', '\u040a'],
  'I': ['\u00cc', '\u00ce', '\u0407'],
  'J': ['\u0134', '\u0408'],
  'K': ['\u0136', '\u040C', '\u041A', '\u049A', '\u049C'],
  'L': ['\u053c', '\u013b', '\u013d', '\u0141'],
  'M': ['\u04cd'],
  'N': ['\u00d1', '\u040D', '\u0419', '\u048A'],
  'O': ['\u00d4', '\u00d8', '\u0472', '\u0488', '\u0555'],
  'P': ['\u01f7', '\u048E'],
  'Q': ['\u01ea', '\u01ec'],
  'R': ['\u0154', '\u0158', '\u0212', '\u042F'],
  'S': ['\u00a7',  '\u015A', '\u0218', '\u0405'],
  'T': ['\u0162', '\u0164', '\u021a', '\u04Ac'],
  'U': ['\u00dc', '\u01b2', '\u040F', '\u0531', '\u0544'],
  'V': ['\u0194'],
  'W': ['\u0174', '\u0460'],
  'X': ['\u04b2', '\u2715'],
  'Y': ['\u00dd', '\u0176', '\u01b3', '\u040E', '\u04af', '\u05e5'],
  'Z': ['\u0179', '\u017d', '\u01b5', '\u0224']
};

/**
 * Returns a random integer in the range 0 <= R < N.
 * @param {number} n Max number.
 * @return {number} Random number.
 */
pseudo.rand = function(n) {
  return Math.floor(Math.random() * n);
};

/**
 * Returns a random pseudo-localized character for 'c', or if 'c' isn't in the
 * lookup table, returns 'c'.
 * @param {string} c Character to look up.
 * @retur {string} Pseudo localized character or 'c'.
 */
pseudo.getChar = function(c) {
  var o = pseudo.CHAR_TABLE[c];
  return o ? o[pseudo.rand(o.length)] : c;  
};

/**
 * Returns a randomly generated psuedo-localized version of the provided word.
 * @param {string} word The word to psuedo localize.
 * @return {string} The pseudo localized word.
 */
pseudo.getWord = function(word) {
  var sb = [];
  for (var i = 0; i < word.length; i++) {
    sb.push(pseudo.getChar(word.charAt(i)));
  }
  return sb.join('');
};

/**
 * Returns the HTML entity for the given character 
 * @param {string} c The character to get the HTML entity for
 * @return {string} The HTML entity.
 */
pseudo.toHtmlEntity = function(c){
  var e = c.charCodeAt(0).toString(16);
  return '&#x' + e + ';';
};

/**
 * Returns the HTML entities for a word.
 * @param {string} word The word to HTML-ize
 * @return {string} HTML.
 */
pseudo.toHtml = function(word) {
  var sb = [];
  for (var i = 0; i < word.length; i++) {
    sb.push(pseudo.toHtmlEntity(word.charAt(i)));
  }
  return sb.join('');
};
