// =========================================
//   vds関連
// =========================================
var vdsPlayBtn = "vds/vds_read.gif";       // 読み上げボタンのファイル名
var vdsStopBtn = "vds/vds_stop.gif";       // 停止ボタンのファイル名
var vdsErrBtn = "vds/vds_mente.gif";       // 中止ボタンのファイル名
var vdsMenteBtn = "vds/vds_mente.gif";    // メンテナンス中のファイル名
var vdsLoaderImg = "vds/vds_loading.gif";  // 作成中のファイル名
var vdsplay;  // vds Playerオブジェクト
var vdsobj;   // vds オブジェクト
var vdsNowReadID = "";    // 現在、読み上げ中のID

function $(id) {
  return document.getElementById(id);
}

// 読み上げボタンの設置
function vdsSetButton(vdsId,flg){
  var setTag = "";
  var vdsArea = $(vdsId);
  if(vdsArea == null){
    return false;
  }
  switch (flg){
    case 1:
      // 再生可能なボタンの設置

      setTag = '<input class="vdsBTN" type="image" id="' + vdsId + '_stopBtn" ';
      setTag += 'src="' + vdsStopBtn + '" alt="停止" ';
      setTag += 'onClick="vdsStop(\'' + vdsId + '\');"/>';
      setTag += '<input class="vdsBTN" type="image" id="' + vdsId + '_playBtn" ';
      setTag += 'src="' + vdsPlayBtn + '" alt="読み上げ" ';
      setTag += 'onClick="vdsRead(\'' + vdsId + '\');" />';	  
	  
//      setTag = '<input class="vdsBTN" type="image" id="' + vdsId + '_playBtn" ';//読上げ、停止ボタンの位置入れ替えのため
//      setTag += 'src="' + vdsPlayBtn + '" alt="読み上げ" ';
//      setTag += 'onClick="vdsRead(\'' + vdsId + '\');" />';
//      setTag += '<input class="vdsBTN" type="image" id="' + vdsId + '_stopBtn" ';
//      setTag += 'src="' + vdsStopBtn + '" alt="停止" ';
//      setTag += 'onClick="vdsStop(\'' + vdsId + '\');"/>';
      break;
    case 2:
      // 停止ボタンの設置
      setTag = '<input class="vdsBTN" type="image" id="' + vdsId + '_stopBtn" ';
      setTag += 'src="' + vdsStopBtn + '" alt="停止" ';
      setTag += 'onClick="vdsStop(\'' + vdsId + '\');"/>';
      break;
    case 3:
      // flashplayer がないことによるエラー
      setTag = '<input class="vdsBTN" type="image" src="' + vdsErrBtn + '" ';
      setTag += 'alt="中止" onClick="alert(\'お使いのAdobe Flash Playerをバージョンアップしてください。\');" />';
      break;
    default:
      // メンテナンス中の設置
      setTag = '<img class="vdsBTN" src="' + vdsMenteBtn + '" alt="メンテナンス中" />';
      break;
  }
  vdsArea.innerHTML = setTag;
  return true;
}

/*
   読み上げ処理
       読み上げ中の文章があれば停止する。
       読み上げボタンを「作成中」に変更し、音声合成を行う。
*/
function vdsRead(vdsId){
  var ttlbtn,txt,vdsret;
  if(typeof(vdsplay) != "undefined"){
    vdsStop(vdsNowReadID);  // 音声作成または再生中の場合のための停止処理
    if(vdsId == vdsNowReadID){
      return;  // 同じ音声を作成中のときには無視する
    }
    if(vdsId == "vdsJikan"){
      //vdsobj.setCache(0);
      txt = "診療時間";
      txt += "午前診療　9時から12時30分。";
      txt += "午後診療　14時30分から16時30分。";
      txt += "夕方診療　17時30分から19時30分。";
      txt += "休診日 日曜・祝祭日、GW期間、年末年始。";
      txt += "お電話での外来診療予約。";
      txt += "044-556-2722。";
      txt += "診察券をお持ちの方は、診察券番号をお伺いいたしますので、診察券をご用意下さい。";
    }else if(vdsId == "vdsTantou"){
      //vdsobj.setCache(0);
      txt = "担当医・診療時間";
      txt += "月曜日、午前、邱、笹島。";
      txt += "午後、邱、笹島。";
      txt += "夕方診療、邱、新井。";
      txt += "火曜日、午前、新井、非常勤。";
      txt += "午後、新井。";
      txt += "夕方診療、新井。";
      txt += "水曜日、午前、新井、高木。";
      txt += "午後、邱、新井。";
      txt += "夕方診療、新井。";
      txt += "木曜日、午前、新井、非常勤。";
      txt += "午後、新井、九島。";
      txt += "夕方診療、なし。";
      txt += "金曜日、午前、杉山、新井、邱。";
      txt += "午後、新井、邱。";
      txt += "夕方診療、みずむら。";
      txt += "土曜日、午前、邱、非常勤。";
      txt += "午後、邱。";
      txt += "夕方診療、なし。";
    }else if(vdsId == "vdsGairai"){
      //vdsobj.setCache(0);
      txt = "外来診療表";
      txt += "内科、月曜日、午前、邱、笹島、山崎。";
      txt += "午後、邱、笹島。";
      txt += "夕方診療、邱、新井。";
      txt += "火曜日、午前、新井、山崎。";
      txt += "午後、新井。";
      txt += "夕方診療、新井。";
      txt += "水曜日、午前、新井、高木。";
      txt += "午後、邱、新井。";
      txt += "夕方診療、新井。";
      txt += "木曜日、午前、新井、櫻木。";
      txt += "午後、新井、九島。";
      txt += "夕方診療、なし。";
      txt += "金曜日、午前、杉山、新井、邱。";
      txt += "午後、新井、邱。";
      txt += "夕方診療、みずむら。";
      txt += "土曜日、午前、邱、非常勤。";
      txt += "午後、邱。";
      txt += "夕方診療、なし。";
      txt += "女性外来、月曜日、午前、百瀬、10時30分から。";
      txt += "午後、百瀬。";
      txt += "夕方診療、百瀬。";
      txt += "火曜日、午前、百瀬。";
      txt += "午後、百瀬。";
      txt += "夕方診療、なし。";
      txt += "水曜日、午前、百瀬、10時30分から。";
      txt += "午後、百瀬。";
      txt += "夕方診療、百瀬。";
      txt += "木曜日、なし。";
      txt += "金曜日、午前、百瀬。";
      txt += "午後、百瀬。";
      txt += "夕方診療、なし。";
      txt += "土曜日、午前、百瀬。";
      txt += "午後、なし。";
      txt += "夕方診療、なし。";
      txt += "女性外来（心理相談）、水曜日、夕方診療、成田。";
      txt += "循環器科、月曜日、午前、笹島。";
      txt += "午後、笹島。";
      txt += "火曜日、午前、山下。";
      txt += "午後、なし。";
      txt += "水曜日、木曜日、なし。";
      txt += "金曜日、午前、なし。";
      txt += "午後、山下。";
      txt += "土曜日、なし。";
      txt += "腎臓内科、月曜日〜土曜日、朝倉／木暮（どちらか一診・基本的に金曜午後）。";
      txt += "大動脈センター、土曜日、山本/他。";
      txt += "脳神経外科（頭痛外来）、月曜日、夕方診療、竹内、水曜日、夕方診療、遠藤。";
      txt += "栄養相談、管理栄養士（完全電話予約制）。";
    }else{
      txt = "";
      ttl = $(vdsId + "_title");
      if(ttl){
        txt = ttl.innerHTML + "。";
      }
      txt += $(vdsId + "_text").innerHTML;
      txt = txt.replace(/&nbsp;/g," ");

	// 日付置換え処理

     txt = txt.replace(/(\d{4})\.0([1-9]+)\.0([1-9]+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\.(\d+)\.0([1-9]+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\.0([1-9]+)\.(\d+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\.(\d+)\.(\d+)/g, "$1年$2月$3日");;
	 
	 txt = txt.replace(/(\d{4})\/0([1-9]+)\/0([1-9]+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\/(\d+)\/0([1-9]+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\/0([1-9]+)\/(\d+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d{4})\/(\d+)\/(\d+)/g, "$1年$2月$3日");;
     txt = txt.replace(/(\d+)\/(\d+)/g, "$1月$2日");;
	 
	// 時間置換え処理	 
     txt = txt.replace(/(\d+)\:(\d+)|(\d+)\：(\d+)/g, "$1時$2分");;
	 

	// 曜日置換え処理
     txt = txt.replace(/\(月\)|（月）/g, "(月曜日)");;
     txt = txt.replace(/\(火\)|（火）/g, "(火曜日)");;
     txt = txt.replace(/\(水\)|（水）/g, "(水曜日)");;
     txt = txt.replace(/\(木\)|（木）/g, "(木曜日)");;
     txt = txt.replace(/\(金\)|（金）/g, "(金曜日)");;
     txt = txt.replace(/\(土\)|（土）/g, "(土曜日)");;
     txt = txt.replace(/\(日\)|（日）/g, "(日曜日)");;
	 

	// その他置換え処理
     txt = txt.replace(/夕診/g, "ゆうしん");;
     txt = txt.replace(/水村/g, "みずむら");;
     txt = txt.replace(/u|m2/g, "ヘイホウメートル");;
     txt = txt.replace(/木・土除く/g, "もく　ど　のぞく");;
    }
    vdsret = vdsobj.setRate(2);  // ちょっと早め
	vdsret = vdsobj.setCast("KeikoJPf"); //音声の種類：ケイコ
    if(vdsret == 0){
      vdsret = vdsobj.setString(txt);
    }
    if(vdsret == 0){
      $(vdsId + "_playBtn").src = vdsLoaderImg;
      $(vdsId + "_playBtn").alt = "作成中";
      vdsNowReadID = vdsId;
      vdsobj.setFinishCallback("vdsServerCallbak(\"" + vdsId + "\")");
      vdsobj.speak();
    }else{
      vdsSetButton(vdsId,0);
    }
  }
}

/*
   音声合成作成後の処理
       正常終了：読み上げボタンが「作成中」のときには「停止」に変更
       エラー  ：読み上げボタンを「中止」に変更
*/
function vdsServerCallbak(vdsId){
  var ret = vdsobj.getServerError();
  var vdsBtn = $(vdsId + "_playBtn");
  if(ret == 0){
    vdsBtn.src = vdsPlayBtn;
    vdsBtn.alt = "読み上げ";
  }else{
    vdsSetButton(vdsId,0);    // 音声作成エラー
  }
  vdsNowReadID = "";
}

/*
   読み上げ停止処理
*/
function vdsStop(vdsId){
  if(typeof(vdsplay) == "undefined" ){ return;}
  vdsplay.speakStop();
  if(vdsNowReadID != ""){
    var vdsBtn = $(vdsId + "_playBtn");
    vdsBtn.src = vdsPlayBtn;
    vdsBtn.alt = "読み上げ";
  }
  vdsNowReadID = "";
}


/*
   vdsの準備処理(onloadイベントで行う）
       vdsの準備処理を行い、読み上げボタンを設置する
*/
function vdsSetup(){
  var flg = 0;
  var errcd;
  try{
    flg = 1;
    vdsplay = new VoiceDeliveryPlayer("vdsSetFlash");
    flg = 2;
    vdsobj = new VoiceDelivery(vdsplay,"vdsobj");
    flg = 3;
//    vdsobj.setCache(0);   // テスト用に常に音声合成
  }catch (errcd) {
    // エラー処理があれば入れる
    //alert("vdsObjectError=" + errcd);
  }
  if(navigator.appName == "Microsoft Internet Explorer"){
    initBtn4IE(flg,errcd);
  }else{
    initBtn4FF(flg,errcd);
  }
}

// ＩＥ用読み上げボタンの初期セット処理
//   各読み上げボタンのセットとハイライトのための読み上げ文章の分割処理
function initBtn4IE(setFlg,err){
  var ret;
  var ii;
  var vdsBtnList = document.getElementsByTagName("div");
  cnt = vdsBtnList.length - 1;
  // ボタンの設置とvdsobjの生成
  for(ii=0;ii<=cnt;ii++){
    ret = false;
    if(vdsBtnList[ii].name == "vdsReadBtn"){
      vdsID = vdsBtnList[ii].id;
      if(vdsID != ""){
        if(err == "-701"){
          ret = vdsSetButton(vdsID,3);    // メンテナンス中（Flash対応）
        }else{
          if(setFlg == 3){
            ret = vdsSetButton(vdsID,1);  // 読み上げボタン
          }else{
            ret = vdsSetButton(vdsID,0);  // メンテナンス中
          }
        }
      }
    }
  }
}


// ＦＦなど（ＩＥ以外）用読み上げボタンの初期セット処理
//   各読み上げボタンのセット処理
function initBtn4FF(setFlg,err){
  var ret;
  var ii;
  var vdsBtnList = document.getElementsByName("vdsReadBtn");
  cnt = vdsBtnList.length - 1;
  // ボタンの設置とvdsobjの生成
  for(ii=0;ii<=cnt;ii++){
    ret = false;
    vdsID = vdsBtnList[ii].id;
    if(vdsID != ""){
      if(err == "-701"){
        ret = vdsSetButton(vdsID,3);    // メンテナンス中（Flash対応）
      }else{
        if(setFlg == 3){
          ret = vdsSetButton(vdsID,1);  // 読み上げボタン
        }else{
          ret = vdsSetButton(vdsID,0);  // メンテナンス中
        }
      }
    }
  }
}


