/*
    http://unicode.org/charts/collation/chart_Latin.html
*/

$(function() {
    var $body = $("body"),
        latin = [];
    $.each("ABCDEFGHIJKLMNOPQRSTUVWXYZ", function(i, v) {
        latin.push(v);
    });
    $.each(["AE", "DZ", "NJ", "OE", "SZ"], function(i, v) {
        latin.push(v);
    });
    var unicode = {};
    $.each(latin, function(i, v) {
        unicode[v] = [];
    });
    $.each(latin, function(i, v) {
        unicode[v.toLowerCase()] = [];
    });
    $.get("chart_Latin.html", function(data) {
        data = "<table>" + data.split("<table>")[1].split("</table>")[0] + "</table>";
        $("body").append(data);
        $("td").each(function() {
            var $this = $(this);
            var title = $this.attr('title').split(":")[0].replace("SHARP S", "SZ");
            var words = title.split(" ");
            if (title.substr(-2, 1) != " "
                    && words[0] == "LATIN"
                    && $.inArray(words[1], ["CAPITAL", "SMALL"]) > -1
                    && $.inArray(words[2], ["LETTER", "LIGATURE"]) > -1
                    && $.inArray(words[3], latin) > -1) {
                var character = words[1] == "CAPITAL" ? words[3] : words[3].toLowerCase();
                unicode[character].push({
                    character: "\\u" + $(this).children().eq(1).html(),
                    comment: title.replace("SZ", "SHARP S")
                });
            }
        });
        $body.empty();
        $.each(unicode, function(k, v) {
            unicode[k].sort(function(a, b) {
                a = a.character.substr(2);
                b = b.character.substr(2);
                return (a < b) * -1 + (a > b);
            });
        });
        $div = $("<div/>")
            .css({
                fontFamily: "Consolas",
                fontSize: "12px"
            })
            .html(
                $.map(Ox.keys(unicode), function(v) {
                    return v + ": [<br/>" + Ox.repeat("&nbsp;", 4) + $.map(unicode[v], function(v_, i) {
                        return '"' + v_.character + '"' + (i < unicode[v].length - 1 ? "," : "&nbsp;") + " // " + v_.comment;
                    }).join("<br/>" + Ox.repeat("&nbsp;", 4));
                }).join("<br/>],<br/>") + "<br/>]"
            )
            .appendTo($body);
        /*
        $.each(unicode, function(k, v) {
            $div.append(k + ": [<br/>");
            var length = v.length;
            $.each(v, function(i, v_) {
                $div.append("&nbsp; &nbsp; \"" + v_.character + "\"" + (i < v.length - 1 ? "," : "&nbsp;") + " // " + v_.comment + "<br/>")
            });
            $div.append("]" + (k != 'sz' ? "," : "") + "<br/>");
        });
        */
        $div = $("<div/>").css({
            fontFamily: "Lucida Grande",
            fontSize: "24px"
        }).appendTo($body);
        $.each(unicode, function(k, v) {
            $div.append(k + ": ");
            $.each(v, function(i, v_) {
                console.log(v_)
                $div.append("<span title=\"" + v_.comment + "\">&#x" + v_.character.substr(2) + ";</span>");
            });
            $div.append("<br/>");
        });
    });
});
