// =========================================
//   vds関連
// =========================================
var vdsPlayBtn = "http://www.sayamahp.jp/vds/vds_read.gif";       // 読み上げボタンのファイル名
var vdsStopBtn = "http://www.sayamahp.jp/vds/vds_stop.gif";       // 停止ボタンのファイル名
var vdsErrBtn = "http://www.sayamahp.jp/vds/vds_mente.gif";       // 中止ボタンのファイル名
var vdsMenteBtn = "http://www.sayamahp.jp/vds/vds_mente.gif";    // メンテナンス中のファイル名
var vdsLoaderImg = "http://www.sayamahp.jp/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 == "vdsGairai"){
      //vdsobj.setCache(0);
      txt = "外来診療のご案内。";
      txt += "さやま総合クリニック　診療受付時間。";
      txt += "月曜日から金曜日。";
      txt += "午前　9時から11時半。";
      txt += "午後　13時半から16時半。";
      txt += "土曜日。";
      txt += "午前　9時から11時半。";
      txt += "午後　休診。";
      txt += "電話予約受付時間。";
      txt += "月曜日から金曜日。";
      txt += "8時半から19時（歯科のみ8時半から17時まで）。";
      txt += "土曜日。";
      txt += "8時半から12時半（歯科は12時まで）。";
      txt += "予約受付センター。";
      txt += "04-2953-9995。";
      txt += "※入院および救急の受け入れや特殊検査については、狭山病院で承っています。";
      txt += "病院TEL：04-2953-6611。";
      txt += "外来予約のご案内。";
      txt += "予約制の詳細をご覧になりたい方は、外来予約のご案内をご確認ください。";
      txt += "外来診療体制。";
      txt += "曜日ごとにどのような外来診療が行われているかをお知りになりたい方は、外来診療体制をご確認ください。";
    }else if(vdsId == "vdsYoyaku"){
      //vdsobj.setCache(0);
      txt = "予約の方法。";
      txt += "予約受付は下記のとおりの方法を準備しています。";
      txt += "1. 予約専用電話での予約。";
      txt += "お電話は次の要領でお願いします。";
      txt += "電話番号　04-2953-9995。";
      txt += "受付時間。";
      txt += "月曜日から金曜日。";
      txt += "8時半から19時（歯科のみ8時半から17時まで）。";
      txt += "土曜日。";
      txt += "8時半から12時半（歯科は12時まで）。";
      txt += "ご希望の診療科と日時を電話を承った受付担当者とお決めください。";
      txt += "その際、診察券の番号が必要となりますので、あらかじめご用意ください。";
      txt += "もし、お手元に診察券がない場合は、お名前と生年月日をお伝えください。";
      txt += "初めて受診される方（診察券のない方）は、診察券を作成するため、ご住所・お名前・生年月日・お電話番号・ご紹介状の有無等をお伺いします。";
      txt += "診察券は、受診当日にお渡しします。";
      txt += "2. 診療室での次回来院予約。";
      txt += "次回来院日時を担当医師とお決めください。";
      txt += "3. 地域の先生を通じての予約。";
      txt += "かかりつけの先生とよくご相談の上、お決めください。";
      txt += "4. 当日予約も対応できます。";
      txt += "当日予約も対応できます。";
      txt += "お電話でご確認ください。";
      txt += "5. ファックスでの予約。";
      txt += "聴力に障害のある方のために、ファックスでの予約を受付けています。";
      txt += "ファックス専用送付状をご利用ください。";
      txt += "FAXによる予約方法。";
      txt += "1.診察体制表をよくご覧の上、専用FAX用紙に必要事項をもれなくご記入ください。";
      txt += "2.専用FAX用紙をFAXしてください。";
      txt += "3.ご予約完了のFAXが届きます。";
      txt += "注意事項。";
      txt += "専用FAX用紙の「その他の連絡先」はできる限り記入してください。";
      txt += "専用FAX用紙がない場合は、お手持ちの紙でも受付します。";
      txt += "その場合は必要事項を必ずご記入ください。";
      txt += "必要事項。";
      txt += "a　登録番号（診察券の番号）。";
      txt += "b　お名前。";
      txt += "c　電話番号/FAX番号。";
      txt += "d　その他の連絡先（お名前、電話番号）。";
      txt += "e　予約事項（希望診療日、診察科、医師名）。";
      txt += "予約申込み専用FAX用紙。";
      txt += "MSワードの文書ファイル、印刷してご利用ください。";
    }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, "げつ　すい　きん");;
     txt = txt.replace(/火木土|火・木・土/g, "か　もく　ど");;
     txt = txt.replace(/火木|火・木/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);  // メンテナンス中
        }
      }
    }
  }
}

