/*
 * Glig Unicode Text Generator
 *
 * Copyright (c) 2010, glig.com All Rights Reserved
 *
 * This widget does several unicode substitutions for interesting effects.
 * The substitution maps are bi-directional. Some effects require additional
 * operations such as reversal.
 *
 * If you're interested in learning more about unicode chars, go to this page:
 *
 * http://ascii-table.com/unicode-chars.php?p=0
 * http://www.starr.net/is/type/htmlcodes.html
 *
 * We can use either \u notation or direct input; we always try to use
 * more common characters as the chance of support is higher.
 */

var effects =
{
    // also fun: ♩ ♫ ♬ ♭ ♮ ♯ ♠ ♡ ♢ ♣ ♤ ♥ ♦ ♧ ⌘

    "rotated":
    {
        title: "rotated :: flipped both ways ⇄ ⇅",
        is_reversed: true,
        table:
        {
            'A': '\u2200',  'B': '\u1041',  'C': '\u2183',  'D': '\u25d6',  'E': '\u018e',
            'F': '\u2132',  'G': '\u2141',  'J': '\u017f',  'K': '\u22ca',  'L': '\U2142',
            'M': '\u019c',  'N': '\u1d0e',  'P': '\u0500',  'Q': '\u038c',  'R': '\u1d1a',
            'T': '\u22a5',  'u': '\u2229',  'V': '\u1d27',  'W': 'M',       'Y': '\u2144',

            'a': '\u0250',  'b': 'q',       'c': '\u0254',  'd': 'p',       'e': '\u01dd',
            'f': '\u025f',  'g': '\u0183',  'h': '\u0265',  'i': '\u0131',  'j': '\u027e',
            'k': '\u029e',  'l': '\u05df',  'm': '\u026f',  'n': 'u',       'r': '\u0279',
            't': '\u0287',  'v': '\u028c',  'w': '\u028d',  'y': '\u028e',

            '.': '\u02d9',  '[': ']',       '(': ')',       '{': '}',       '?': '\u00bf',
            '!': '\u00a1',  '&': '\u214b',  '\'': ',',      '\"': ',,',     '<': '>',
            '_': '\u203e',  '\\': '\\',     ';': '\u061b',  '`': ',',       ',': '\'',

            '1': '\u21c2',  '2': '\u1105',  '3': '\u1110',  '4': '\u3123',  '5': '\u078e',
            '6': '9',       '7': '\u3125',  '9': '6'
        }
    },

    "greek":
    {
        // To Greek people reading this: please accept my sincere apologies
        // for abusing your alphabet. We just really like the characters!

        title: "greek :: it looks greek (but isn't)",
        table:
        {
            'A': 'Δ',   'B': 'Β',   'C': 'С',   'D': 'ᐅ',   'E': 'Ξ',   'F': 'Φ',   'G': 'Γ',
            'H': 'Η',   'I': '|',   'J': 'Ј',   'K': 'Ƙ',   'L': 'Λ',   'M': 'Μ',   'N': 'Ŋ',
            'O': 'Ѳ',   'P': 'Π',   'Q': 'Θ',   'R': 'Ɍ',   'S': 'Ʃ',   'T': 'Γ',   'U': 'Ʊ',
            'V': 'Ɣ',   'W': 'ᴡ',   'X': 'χ',   'Y': 'Ψ',   'Z': 'Ȥ',

            'a': 'α',   'b': 'β',   'c': 'ϲ',   'd': 'δ',   'e': 'ε',   'f': 'φ',   'g': 'ϑ',
            'h': 'հ',   'i': 'ι',   'j': 'յ',   'k': 'Κ',   'l': 'λ',   'm': 'ʍ',   'n': 'ƞ',
            'o': 'ɸ',   'p': 'π',   'q': 'θ',   'r': 'ʀ',   's': 'σ',   't': 'τ',   'u': 'υ',
            'v': 'Ɣ',   'w': 'ѡ',   'x': 'ϰ',   'y': 'ψ',   'z': 'ȥ',

            '?': 'ʔ'
        }
    },

    "cryptic":
    {
        title: "cryptic text :: make it hard to read",
        table:
        {
            'A': 'Δ',   'B': 'β',   'C': 'Ɔ',   'D': 'Ɖ',   'E': 'Є',   'F': 'Ғ',
            'G': 'Ǥ',   'H': 'Ħ',   'I': 'Ɩ',   'J': 'Ĵ',   'K': 'Ƙ',   'L': '˩',
            'M': 'Ѧ',   'N': 'И',   'O': 'Ѳ',   'P': 'Ƥ',   'Q': 'Ǫ',   'R': 'Я',
            'S': 'Ƨ',   'T': 'Ƭ',   'U': 'Ʋ',   'V': 'Ѵ',   'W': 'Ɯ',   'X': 'χ',
            'Y': 'Ƴ',   'Z': 'Ƶ',

            'a': 'α',   'b': 'в',   'c': 'c',   'd': '∂',   'e': 'ɛ',   'f': 'ғ',
            'g': 'ɢ',   'h': 'н',   'i': 'ι',   'j': 'ʝ',   'k': 'κ',   'l': 'ℓ',
            'm': 'м',   'n': 'и',   'o': 'σ',   'p': 'ρ',   'q': 'զ',   'r': 'я',
            's': 'ƨ',   't': 'т',   'u': 'ʋ',   'v': 'и',   'w': 'ω',   'x': 'ϰ',
            'y': 'ʏ',   'z': 'ʓ'
        }
    },

    "accent":
    {
        title: "accents :: exclamations everywhere!",
        table:
        {
            'a': 'á',   'c': 'ć',   'd': 'ď',   'e': 'é',   'g': 'ġ',   'h': 'ĥ',
            'i': 'í',   'n': 'ń',   'o': 'ő',   'r': 'ŕ',   's': 'ś',   't': 'ť',
            'u': 'ú',   'w': 'ŵ',   'y': 'ý',   'z': 'ź',
            'A': 'Á',   'C': 'Č',   'D': 'Ď',   'E': 'É',   'G': 'Ġ',   'H': 'Ĥ',
            'I': 'í',   'L': 'Ĺ',   'N': 'Ń',   'O': 'Ó',   'R': 'Ŕ',   'S': 'Ś',
            'T': 'Ť',   'U': 'Ú',   'W': 'Ŵ',   'Y': 'Ŷ',   'Z': 'Ź'
        }
    },

    /*
    "striped":
    {
        // Also options: Ɇ

        title: "striped :: better with stripes!",
        table:
        {
            'A': 'Ⱥ',   'B': 'Ƀ',   'C': '₡',   'D': 'Ɖ',   'E': 'Ɇ',   'F': '₮',   'G': '₲',
            'H': 'Ħ',   'I': 'ⱡ',   'J': '#',   'K': '₭',   'L': '£',   'M': '#',   'N': '₦',
            'O': 'Ѳ',   'P': '₱',   'Q': '#',   'R': '®',   'S': '₴',   'T': 'Ⱦ',   'U': 'ʉ',
            'V': '℣',   'W': '₩',   'X': '#',   'Y': '¥',   'Z': 'Ƶ',

            'a': 'ⱥ',   'b': 'Ѣ',   'c': '¢',   'd': '#',   'e': 'ɇ',   'f': 'ғ',   'g': '#',
            'h': 'ħ',   'i': '#',   'j': '#',   'k': '#',   'l': '#',   'm': '₥',   'n': '₦',
            'o': '#',   'p': '#',   'q': '#',   'r': '#',   's': '$',   't': 'ⱦ',   'u': 'ʉ',
            'v': 'ⱴ',   'w': '#',   'x': '#',   'y': 'ɏ',   'z': '#'
        }
    },
    */

    "circles":
    {
        // Pretty funny characters in the U+2000 plane...

        title: "circles :: can you read this?",
        table:
        {
            'A': '\u24b6',  'B': '\u24b7',  'C': '\u24b8',  'D': '\u24b9',  'E': '\u24ba',  'F': '\u24bb',
            'G': '\u24bc',  'H': '\u24bd',  'I': '\u24be',  'J': '\u24bf',  'K': '\u24c0',  'L': '\u24c1',
            'M': '\u24c2',  'N': '\u24c3',  'O': '\u24c4',  'P': '\u24c5',  'Q': '\u24c6',  'R': '\u24c7',
            'S': '\u24c8',  'T': '\u24c9',  'U': '\u24ca',  'V': '\u24cb',  'W': '\u24cc',  'X': '\u24cd',
            'Y': '\u24ce',  'Z': '\u24bf',

            'a': '\u24d0',  'b': '\u24d1',  'c': '\u24d2',  'd': '\u24d3',  'e': '\u24d4',  'f': '\u24d5',
            'g': '\u24d6',  'h': '\u24d7',  'i': '\u24d8',  'j': '\u24d9',  'k': '\u24da',  'l': '\u24db',
            'm': '\u24dc',  'n': '\u24dd',  'o': '\u24de',  'p': '\u24df',  'q': '\u24e0',  'r': '\u24e1',
            's': '\u24e2',  't': '\u24e3',  'u': '\u24e4',  'v': '\u24e5',  'w': '\u24e6',  'x': '\u24e7',
            'y': '\u24e8',  'z': '\u24e9',

            // Note: in this plane, you can find versions up to (20), but that's not for us here.
            // Also check out 277* for more numbers.

            '0': '\u24ea',  '1': '\u2460',  '2': '\u2461',  '3': '\u2462',  '4': '\u2463',  '5': '\u2464',
            '6': '\u2465',  '7': '\u2466',  '8': '\u2467',  '9': '\u2468'
        }
    },

    "133t":
    {
        // Reference: http://nl.wikipedia.org/wiki/Leet
        title: "l33t :: talk like a hax0r",
        table:
        {
            'A': '@',   'B': '8',   'C': 'C',   'D': 'D',   'E': '€',   'F': '/=',  'G': 'G',
            'H': '/-/', 'I': '][',  'J': '_|',  'K': '|<',  'L': '£',   'M': '/V\\', 'N': 'И',
            'O': '0',   'P': '[]D', 'Q': '¥',   'R': 'Я',   'S': '§',   'T': '†',   'U': '|_|',
            'W': '\/\/', 'X': 'Ж',  'Y': 'Ч',   'Z': '7_',

            'a': '@',   'b': 'ß',   'c': '¢',   'D': 'd',   'e': '3',   'f': 'ƒ',   'g': '9',
            'h': 'h',   'i': '|',   'j': ';',   'k': '|<',  'l': '1',   'm': '^^',  'n': 'ท',
            'o': 'Ø',   'p': 'p',   'q': 'q',   'r': '®',   's': '5',   't': '7',   'u': 'µ',
            'w': 'Ш',   'x': '×',   'y': 'Ч',   'z': '%',

            'a': '4',   'e': '3',   'i': '1',   'o': '0',   't': '7',
'O': '0',   'T': '7'
        }
    },
};

$(document).ready(function()
{
    initialize_the_generator();
    handle_key_press();

    $("#enter_area textarea").keyup(function()
    {
        handle_key_press();
    });

    $("#enter_area textarea").click(function()
    {
        if($(this).val() === "Type your text here")
        {
            $(this).empty();
            handle_key_press();
        }
    });

    $(".code").click(function()
    {
        this.focus();
        this.select();
    });

//    glig.ui.activate_help_panels();
});

var in_handler = false;

function handle_key_press()
{
    if(in_handler)
        return;

    in_handler = true;

    glig.defer("process_key_update()", 200);
}

function process_key_update()
{
    var text = $("#enter_area > textarea").val();

    var count = 0;
    for(var label in effects)
    {
        handle_effect("#preview" + count + " textarea", label, text);
        count++;
    }

    in_handler = false;
}

function handle_effect(target, label, text)
{
    var map = effects[label];

    var table = map.table;

    var str = "";
    if(map.is_reversed)
    {
        for(var i = text.length - 1; i >= 0; i--)
        {
            var ch = text.charAt(i);
            str += table[ch] || ch;
        }
    }
    else
    {
        for(var i = 0; i < text.length; i++)
        {
            var ch = text.charAt(i);
            str += table[ch] || ch;
        }
    }

    $(target).html(str);
}

function initialize_the_generator()
{
    var count = 0;

    var str = "";

    for(var label in effects)
    {
        // Make the tables bi-directional
        //
        var table = effects[label].table;
        for(var key in table)
        {
            var val = table[key];
            if(!table[val])
                table[table[key]] = key;
        }

        // Let's add preview boxes
        //
        str += "<div class=entry id=preview" + count + ">";
        str += "<div class='title ui-corner-top'>";
        str += effects[label].title || label;
        str += "</div>";
        str += "<textarea class='code ui-corner-bottom'></textarea>"
        str += "</div>";

        count++;
    }

    str += "<div class=clear></div>";

    $("#preview_area")
        .html(str)
        .find(".entry:nth-child(even)")
            .css("margin-left", "12px");

    // @@@@ hack - do this centrally
    $(".glig_side_flow")
        .css
        ({
            "margin-top": "-25px"
        });

    // Now just the toolbar remains

    $(".toolbar .button[name=bold]").click(function()
    {
        var sel = $(this).is(".selected");
        $("#preview_area textarea").css("font-weight", (sel ? "normal" : "bold"));
        if(sel) $(this).removeClass("selected");
        else $(this).addClass("selected");
    });

    $(".toolbar .button[name=italic]").click(function()
    {
        var sel = $(this).is(".selected");
        $("#preview_area textarea").css("font-style", (sel ? "normal" : "italic"));
        if(sel) $(this).removeClass("selected");
        else $(this).addClass("selected");
    });

    $(".toolbar .button[name=underline]").click(function()
    {
        var sel = $(this).is(".selected");
        $("#preview_area textarea")
            .css("text-decoration", (sel ? "none" : "underline"))
            .filter(":first")
                .css("text-decoration", (sel ? "none" : "overline"));

        if(sel) $(this).removeClass("selected");
        else $(this).addClass("selected");
    });
}

