﻿function romanizeText() {
  var str = $("tag").value;
  var mecab = 1;
  if (str) {
    if (mecab) {
      str = mecapiJSON(str);
    } else {
      var r = new Romanize();
      options(r);
      output(r.conv(str));
    }
  } else {
    return false;
  }
}

function mecapiJSON(str) {
  $("romanized").innerHTML = "変換中...";
  this.url = "http://mimitako.net/api/mecapi.cgi";
  this.q = "?sentence=" + encodeURIComponent(str) + "&response=surface,pronounciation&format=json&callback=mecapiJSON_Result";
  this.req = this.url + this.q;
  var oJsr = new JSONscriptRequest(this.req);
  oJsr.buildScriptTag();
  oJsr.addScriptTag();
  setTimeout(function() {
    oJsr.removeScriptTag();
    clearTimeout();
  }, 2000);
}

mecapiJSON_Result = function(json) {

  var div = $("romanized");
  //if (!json) return div.innerHTML = "解析に失敗しました。";
  var result = [];
  //var surface = [];
  for (var i = 0; i < json.length; i++) {
    var bm = json[i];
    //surface.push(bm["surface"]);
    if (bm["pronounciation"]) {
      result.push(bm["pronounciation"]);
    } else {
      result.push(bm["surface"]);
    }
  }
  var str = result.join(" ");
  var r = new Romanize();
  options(r);
  output(r.conv(str));
  //output(surface.join(" "), "surface");
}

function options(obj) {
  if (!1) obj.option("simple");
//  if ($("OH").checked) obj.option("OH");
//  if ($("nonSpace").checked) obj.option("nonSpace");
}

function output(str, elm) {
  var elm = elm || "romanized";
  $(elm).innerHTML = str.replace(/\n/g, "<br \/>").replace(/。/g, ".");
  var tag=str.replace(/\n/g, "").replace(/。/g, ".");
  searchdmv(tag,1);
}
