(function() {
  var evalAsJson, get, html2text, wrap;

  evalAsJson = function(json) {
    var fn;
    fn = new Function("return " + json);
    return fn();
  };

  get = function(options, callback, func) {
    return http.request(options, function(res, err) {
      var result;
      if (err) {
        return callback(err);
      } else {
        try {
          result = func(res);
        } catch (ex) {
          result = ex.toString();
        }
        return callback(result);
      }
    });
  };

  html2text = function(html) {
    var $tmp;
    $tmp = $("<span>" + html + "</span>");
    return $.trim($tmp.text());
  };

  wrap = function(fn) {
    return function(src, callback, ex) {
      var fixedCallback, fixedSrc, q;
      q = /(.*?)([「『（][\s\S]*[」』）])\s*$/.exec(src);
      if (q) {
        fixedSrc = q[2].substr(1, q[2].length - 2);
        fixedCallback = function(res) {
          res = q[1] + q[2].charAt(0) + res + q[2].charAt(q[2].length - 1);
          return callback(res);
        };
        return fn(fixedSrc, fixedCallback, ex);
      } else {
        return fn(src, callback, ex);
      }
    };
  };

  registerTranslators({
    "ATLAS": function(src, callback) {
      return host().translateAtlas(src, callback);
    },
    "ATLAS with TAHelper replacements": function(src, callback) {
      return host().translateAtlas2(src, callback);
    },
    "Custom": function(src, callback, ex) {
      return host().translateCustom(ex.rawText, callback);
    },
    "Google": wrap(function(src, callback) {
      var url;
      src = encodeURIComponent(src);
      url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ja&tl=en&dt=t&q=" + src;
      return get(url, callback, function(res) {
        var s, ss;
        res = evalAsJson(res);
        ss = (function() {
          var _i, _len, _ref, _results;
          _ref = res[0];
          _results = [];
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            s = _ref[_i];
            _results.push($.trim(s[0]));
          }
          return _results;
        })();
        return ss.join(' ').replace(/\btsu\b/ig, '');
      });
    }),
    "Excite": wrap(function(src, callback) {
      var url;
      src = encodeURIComponent(src);
      url = "http://www.excite.co.jp/world/english/?wb_lp=JAEN&before=" + src;
      return get({
        url: url,
        method: 'post'
      }, callback, function(res) {
        res = /\<textarea id="after".*?\>([\s\S]*?)\<\/textarea\>/.exec(res);
        return html2text(res[1]);
      });
    }),//ばか
	//私は愚かだ
	"Babylon": wrap(function(src, callback) {
      src = encodeURIComponent(src);
      var url ="http://translation.babylon-software.com/translate/babylon.php?v=1.0&langpair=8%7C0&callback=babylonTranslator.callback&context=babylon.8.0._babylon_api_response&q="+src,
			rege = /"translatedText":"(.*)"/g;
	  return get({
		  url: url,
		  method: 'post'
	  }, callback, function(res){
		  res = rege.exec(res);
		  return res[1];
	  });
    }),
	"Yandex": wrap(function(src, callback) {
      var keyAPI = "trnsl.1.1.20170703T143335Z.f3b470bf9941c709.6f5e988e6cfefb7af03b1501a60bd1acabd618c4",
			url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key="+keyAPI+"&lang=ja-en&text="+src;
			//didn't know it shows error anyway so i got carried away with this...
			//btw, error 404 means that key reached limit of translations, 401 - invalid key, 402 - blocked key
			//1 000 000 characters is daily limit, 10 000 000 is monthly
			//rege = /(?:(?:code)":(\d{1,3}))(?:.*)(?:text"\:\[\"|message":")(.*)(?:".+)/g;
			rege = /(?:(?:code)":(200)).*(?:text":\[")(.*)(?:"\]})/g;
	  return get({
		  url: url,
		  method: 'post'
	  }, callback, function(res){
		  res = rege.exec(res);
		  return res[2].replace(/\\n/g,'');
	  });
    }),
    "SysTran": wrap(function(src, callback) {
      var url;
      url = "http://www.systranet.com/sai?lp=ja_en&service=translate";
      return get({
        url: url,
        method: 'post',
        query: src
      }, callback, function(res) {
        return $.trim(res.replace("body=", ""));
      });
    })
  });

}).call(this);

// removed dead translators, kept them for reference
/* i mean sdl is not dead but i don't know how to use header in this script
    "SDL": wrap(function(src, callback) {
      var url;
      src = encodeURIComponent(src);
      url = "http://tets9.freetranslation.com/?sequence=core&charset=UTF-8&language=Japanese%2FEnglish&srctext=" + src;
      return get({
        url: url,
        method: 'post'
      }, callback, function(res) {
        return res.replace(/《.*?》/g, '');
      });
    }),
    "Honyaku": wrap(function(src, callback) {
      var url;
      src = encodeURIComponent(src);
      url = "http://honyaku.yahoo.co.jp/transtext?both=TH&eid=CR-JE&text=" + src;
      return get({
        url: url,
        method: 'post'
      }, callback, function(res) {
        res = /id="trn_textText".*?\>([\s\S]*?)\<\/textarea\>/.exec(res);
        return res[1];
      });
    }),
*/