// w3voiceIM.js, version 0.02
// Copyright (c) 2007 Ryuichi NISIMURA (nisimura@sys.wakayama-u.ac.jp)
// http://w3voice.jp/
// This program is freely distributable under the terms of an MIT-style license.

var myselfURL = 'http://w3voice.jp/engine/w3voiceIM.js';
var pluginjsURL = 'http://w3voice.jp/engine/plugin.js';
var prototypejsURL = 'http://w3voice.jp/js/prototype.js';

var w3voiceIMversion = 0.02;
var appletCodebase = 'http://w3voice.jp/applet/';
var engineURL = 'http://engine.w3voice.jp/engine/upload.cgi';

var fPanel = 0;
var elementID = null;
var url = escape(window.location);
var uploadURL = engineURL + "?url=" + url;
var oPanel = [
 '<div align="center" style="width:250px;font-size:80%;background-color:#ffcc66;color:white;">Recording Panel by <a href="http://w3voice.jp/">w3voice.jp</a></div>',
 '<div id="panel" align="center" style="margin-top:10px;height:30px;">',
 '<applet name="w3voiceApplet" code="w3voice.class" codebase="'+appletCodebase+'" archive="w3voice.jar" width="200" height="20" scritable="true">',
 '<param name="UploadURL" value="'+uploadURL+'">',
 '<param name="SamplingRate" value="44100">',
 '</applet>',
 '</div>',
 '<div id="control" style="font-size:9pt;" align="right"><a href="javascript:closePanel()">[Close]</a></div>',
].join("\n");

window.onload = function (){
   var s = document.createElement("script");
   s.type = "text/javascript";
   s.src = prototypejsURL;
   document.body.appendChild(s);

   wait('Form.getElements', function() {
     for(i=0;i<window.document.forms.length;i++){
         var oText = Form.getElements(window.document.forms[i]);
         for(j=0;j<oText.length;j++){
            if(oText[j].type == "text" || oText[j].type == "textarea"){
               Event.observe(oText[j], "dblclick", startASR, false);
            }
         }
      }
   });
}

function startASR(event){
   elementID = Event.element(event);
   var mouseX = Event.pointerX(event);
   var mouseY = Event.pointerY(event);
   
   if(fPanel == 0){
   
     var oDiv = document.createElement('div');
     oDiv.setAttribute("id", "w3voiceInputPanel");

     document.body.appendChild(oDiv);
     Element.setStyle("w3voiceInputPanel", { 'position':'absolute', 'top':(mouseY-80)+'px', 'left':(mouseX-100)+'px', 'width':'250px', 'height':'0px', 'border':'2px dotted #ffcc66', 'background':'#ffeedd', 'color':'black', 'z-index':'99' });

     showPanel();
  }
}

function showPanel(){
   var bt = Element.getStyle("w3voiceInputPanel", 'height');
   var btInt = parseInt(bt.replace("px", ""));
   btInt = btInt+4;
   Element.setStyle("w3voiceInputPanel", { 'height':btInt+'px' });
   if(btInt <= 80){
     setTimeout('showPanel()', 1);
   }else{
     Element.update("w3voiceInputPanel", oPanel);

     fPanel = 1;
     runASR();
   }
}

function closePanel(){
   fPanel = 0;
   elementID = null;
   Element.remove("w3voiceInputPanel");	
}

function runASR(){
   var w3voiceRes = null;
   if(fPanel == 1) {
      w3voiceRes = document.w3voiceApplet.getResponse();
   }

   if(w3voiceRes != null){
      fPanel = 2;
      w3voiceRes = w3voiceRes + "";
      Element.update("panel", w3voiceRes);
      var result =  $('recogout').innerHTML;
      Element.update("control", '<a href="javascript:formInsert(\'' + result + '\')">[Add]</a> / <a href="javascript:closePanel()">[Close]</a>');

   }else{
      setTimeout('runASR()', 200);
   }
}

function formInsert(text){
   var tmpValue = elementID.value;
   elementID.value = tmpValue+text;
   closePanel();
}

// wait function from http://clouder.jp/yoshiki/mt/archives/000631.html   
function wait(a,func){
   var check = 0;
   try{
     eval("check = " + a);
   }catch(e){}
   if(check){
     func()
   }else{
     var f = function(){wait(a,func)};
     setTimeout(f,100);
   }
}

// End of javascript

