/*
 * Transposh v0.6.2
 * http://transposh.org/
 *
 * Copyright 2010, Team Transposh
 * Licensed under the GPL Version 2 or higher.
 * http://transposh.org/license
 *
 * Date: Mon, 09 Aug 2010 14:58:29 +0300
 */
(function (a) {
var t_jp = {};
    function u(c, d) {
        if (a.trim(d).length !== 0) {
            var b = function () {
                var e = a(this).attr("id").substr(a(this).attr("id").lastIndexOf("_") + 1),
                    i = a("#" + f + "img_" + e);
                a("#" + f + e).attr("data-source", 1);
                i.removeClass("tr-icon-yellow").removeCla
                ss("tr-icon-green").addClass("tr-icon-yellow")
            };
            a("*[data-token='" + c + "'][data-hidden!='y']").html(d).each(b);
            a("*[data-token='" + c + "'][data-hidden='y']").attr("data-trans", d).each(b)
        }
    }
    function v(c, d) {
        clearTimeout(o);
        h.push(c);
        n.push(d);
        u(c, d);
        o = setTimeout(function () {
            var b = {
                ln0: t_jp.lang,
                sr0: p,
                translation_posted: "2",
                items: h.length
            },
                e;
            for (e = 0; e < h.length; e += 1) {
                b["tk" + e] = h[e];
                b["tr" + e] = n[e];
                q += a("*[data-token='" + h[e] + "']").size()
            }
            a.ajax({
                type: "POST",
                url: t_jp.post_url,
                data: b,
                success: function () {
                    var i = q / j * 100;
                    t_jp.progress && a("#" + k).progressbar("value", i)
                }
            });
            n = [];
            h = []
        }, 200)
    }
    function l(c, d) {
        v(c, a("<div>" + d + "</div>").text());
        var b = (j - a("." + f + '[data-source=""]').size()) / j * 100;
        t_jp.progress && a("#" + m).progressbar("value", b)
    }
    function w(c, d) {
        var b = "";
        a(c).each(function (e) {
            b += "&q=" + encodeURIComponent(c[e])
        });
        a.ajax({
            url: "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0" + b + "&langpair=%7C" + t_jp.lang,
            dataType: "jsonp",
            success: d
        })
    }
    function x(c, d) {
        w(d, function (b) {
            if (b.responseStatus >= 200 && b.responseStatus < 300) b.responseData.translatedText !== undefined ? l(c[0], b.responseData.translatedText) : a(b.responseData).each(function (e) {
                this.responseStatus === 200 && l(c[e], this.responseData.translatedText)
            })
        })
    }
    function y(c, d) {
        var b = "[";
        a(c).each(function(e) {
            b += '"' + encodeURIComponent(c[e]) + '",'
        });
        b = b.slice(0, -1) + "]";
        a.ajax({
            url: "http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray?appId=" + t_jp.MSN_APPID + "&to=" + t_jp.binglang + "&texts=" + b,
            dataType: "jsonp",
            jsonp: "oncomplete",
            success: d
        })
    }
    function z(c, d) {
        p = 2;
        y(d, function(b) {
            a(b).each(function (e) {
                l(c[e], this.TranslatedText)
            })
        })
    }
    function A(c, d) {
        a.getJSON(t_jp.post_url + "?tgp=" + d + "&tgl=" + t_jp.lang, function (b) {
            b.sentences !== undefined && b.sentences[0].trans && l(c, b.sentences[0].trans)
        })
    }
    function r(c, d) {
        if (t_jp.msn && t_jp.preferred === "2") z(c, d);
        else if (t_jp.tgp) d[0] && A(c[0], d[0]);
        else x(c, d)
    }
    function s() {
        var c = [],
            d = 0,
            b = [],
            e = [];
        if (t_jp.tgp) t = 0;
        a("." + f + '[data-source=""]').each(function () {
            var i = a(this).attr("data-token"),
                g = a(this).attr("data-orig");
            if (g === undefined) g = a(this).html();
            if (c[g] !== 1) {
                c[g] = 1;
                if (d + g.length > t) {
                    r(e, b);
                    d = 0;
                    b = [];
                    e = []
                }
                d += g.length;
                e.push(i);
                b.push(g)
            }
        });
        r(e, b)
    }
    var t = 128,
        j, f = t_jp.prefix,
        m = f + "pbar",
        k = m + "_s",
        p = 1,
        q = 0,
        o, h = [],
        n = [];
    t_jp.MSN_APPID = "FACA8E2DF8DCCECE0DC311C6E57DA98EFEFA9BC6";
    a(document).ready(function () {
        if (t_jp.msn) {
            t_jp.binglang =
            t_jp.lang;
            if (t_jp.binglang === "zh") t_jp.binglang = "zh-chs";
            else if (t_jp.binglang === "zh-tw") t_jp.binglang = "zh-cht"
        }
        a("#" + f + "setdeflang").click(function () {
            a.get(t_jp.post_url + "?tr_cookie=" + Math.random());
            a(this).hide("slow");
            return false
        });
        j = a("." + f + '[data-source=""]').size();
        a.ajaxSetup({
            cache: true
        });
        if (j && !t_jp.noauto && (t_jp.google || t_jp.msn || t_jp.tgp)) if (t_jp.progress) {
            var c = function () {
                a.xLazyLoader({
                    js: "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js",
                    css: "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/ui-lightness/jquery-ui.css",
                    success: function () {
                        a("#" + f + "credit").css({
                            overflow: "auto"
                        }).append('<div style="float: left;width: 90%;height: 10px" id="' + m + '"/><div style="margin-bottom:10px;float:left;width: 90%;height: 10px" id="' + k + '"/>');
                        a("#" + m).progressbar({
                            value: 0
                        });
                        a("#" + k).progressbar({
                            value: 0
                        });
                        a("#" + k + " > div").css({
                            background: "#28F828",
                            border: "#08A908 1px solid"
                        });
                        s()
                    }
                })
            };
            typeof a.xLazyLoader === "function" ? c() : a.getScript(t_jp.plugin_url + "/js/lazy.js", c)
        } else s();
        t_jp.edit && a.getScript(t_jp.plugin_url + "/js/transposhedit.js")
    })
})(jQuery);

