//
//  安全情報の表示をするモジュール
//  要prototype.js
//  要Xml2Obj.js
//
//  2008/03/29 (c) Ohsako, Junichi
//



/*---------- 名前空間の初期化 ----------*/
var showSafe;
if(!showSafe) {
  showSafe = Object();
}


/*---------- プロパティの設定 ----------*/
  // サーバサイドターゲット
  showSafe.url = '../safetyData.xml?' + (new Date).getTime();
  // 曜日選択用配列
  showSafe.wday = ['（日）', '（月）', '（火）', '（水）', '（木）', '（金）', '（土）']; 
  // 出港情報選択用配列
  showSafe.voyage = ['dummy', '情報未定', '禁止', '注意', '可能'];
  // フラッグ選択用配列
  showSafe.flags = ['dummy', 'flag_w.gif', 'flag_r.gif', 'flag_y.gif', 'flag_g.gif'];
  // フラッグ選択用配列
  showSafe.tides = ['dummy', '大潮', '中潮', '小潮', '長潮', '若潮'];


/*---------- 読み込み表示用関数 ----------*/
showSafe.showData =
function() {
  // オブジェクト生成
  showSafe.makeObj('showSafe', ['result']);

  // リモートのXMLファイルからデータを取得・表示
  new Ajax.Request(showSafe.url, {
    asynchronous: true,
    method: 'post',
    onComplete: function(request) {
                  // 受信データオブジェクトをXMLとして取得し変換オブジェクトを生成
                  var dbResult = new Xml2Obj(request.responseXML);
                  dbResult.buildRecords();
                  var records = dbResult.getRecords();
                  // ここからHTMLを生成
                  var html = '';
                  // 有効なデータが存在するかを判定
                  if(records.length) { 
                    for(var i = 0; i < records.length; i++) {
                      var renewDate = new Date(parseInt(records[i].register));
                      // プロパティから値を取り出してHTMLを生成
                      html += '<dl id="renew">';
                      html += '<dt>更新日時&raquo;</dt>';
                      html += '<dd>' + renewDate.getFullYear() + '年' + (renewDate.getMonth() + 1) + '月' + renewDate.getDate() + '日' + showSafe.wday[renewDate.getDay()] + '&nbsp;' + showSafe.arrengeNum(renewDate.getHours()) + ':' + showSafe.arrengeNum(renewDate.getMinutes()) + '</dd>';
                      html += '</dl>';
                      html += '<h2>出港情報</h2>';
                      html += '<dl id="voyage">';
                      if (records[i].vinfo) {
                        html += '<dt>海況&raquo;</dt>';
                        html += '<dd>出港' + showSafe.voyage[records[i].sea] + '&nbsp;<img src="../img/' + showSafe.flags[records[i].sea] + '" width="37" height="30" alt="">' + records[i].vinfo + '<br /></dd>';
                        html += '<dt>出港艇数&raquo;</dt>';
                        html += '<dd>' + records[i].vfig + '&nbsp;艇</dd>';
                      } else {
                        html += '<dt>海況&raquo;</dt>';
                        html += '<dd>出港' + showSafe.voyage[records[i].sea] + '&nbsp;<img src="../img/' + showSafe.flags[records[i].sea] + '" width="37" height="30" alt=""></dd>';
                        html += '<dt>出港艇数&raquo;</dt>';
                        html += '<dd>' + records[i].vfig + '&nbsp;艇</dd>';
                      }
                      html += '</dl>';
                      html += '<h2>潮汐情報</h2>';
                      if(records[i].date1) {
                        var date1 = new Date(parseInt(records[i].date1));
                        html += '<h3>' + date1.getFullYear() + '年' + (date1.getMonth() + 1) + '月' + date1.getDate() + '日</h3>';
                      }
                      html += '<dl class="tide">';
                      if (records[i].u80time1 && records[i].u80time2 && records[i].u80time3 && records[i].u80time4) {
                        var u80Time1 = new Date(parseInt(records[i].u80time1));
                        var u80Time2 = new Date(parseInt(records[i].u80time2));
                        html += '<dt>潮位80cm以下時間帯(その1)&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time1.getHours()) + ':' + showSafe.arrengeNum(u80Time1.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time2.getHours()) + ':' + showSafe.arrengeNum(u80Time2.getMinutes()) + '</dd>';
                      } else if (records[i].u80time1 && records[i].u80time2 && !records[i].u80time3 && !records[i].u80time4) {
                        var u80Time1 = new Date(parseInt(records[i].u80time1));
                        var u80Time2 = new Date(parseInt(records[i].u80time2));
                        html += '<dt>潮位80cm以下時間帯&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time1.getHours()) + ':' + showSafe.arrengeNum(u80Time1.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time2.getHours()) + ':' + showSafe.arrengeNum(u80Time2.getMinutes()) + '</dd>';
                      }
                      if (records[i].u80time3 && records[i].u80time4) {
                        var u80Time3 = new Date(parseInt(records[i].u80time3));
                        var u80Time4 = new Date(parseInt(records[i].u80time4));
                        html += '<dt>(その2)&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time3.getHours()) + ':' + showSafe.arrengeNum(u80Time3.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time4.getHours()) + ':' + showSafe.arrengeNum(u80Time4.getMinutes()) + '</dd>';
                      }
                      html += '</dl>';
                      html += '<dl class="left" id="cycle">';
                      html += '<dt>潮回り&raquo;</dt>';
                      if(records[i].cycle1) {
                        html += '<dd>' + showSafe.tides[records[i].cycle1] + '</dd>';
                      }
                      html += '</dl>';
                      html += '<dl class="left hilo">';
                      html += '<dt>満潮&raquo;</dt>';
                      if(records[i].htime1 && records[i].hheight1) {
                        var hTime1 = new Date(parseInt(records[i].htime1));
                        html += '<dd>' + showSafe.arrengeNum(hTime1.getHours()) + ':' + showSafe.arrengeNum(hTime1.getMinutes()) + '&nbsp;/&nbsp;' + records[i].hheight1 + 'cm';
                      }
                      if (records[i].htime2 && records[i].hheight2) {
                        var hTime2 = new Date(parseInt(records[i].htime2));
                        html += '<br />' + showSafe.arrengeNum(hTime2.getHours()) + ':' + showSafe.arrengeNum(hTime2.getMinutes()) + '&nbsp;/&nbsp;' + records[i].hheight2 + 'cm';
                      } else {
                        html += '<br />&nbsp;';
                      }
                      html += '</dd>';
                      html += '</dl>';
                      html += '<dl class="left hilo">';
                      html += '<dt>干潮&raquo;</dt>';
                      if(records[i].ltime1 && records[i].lheight1) {
                        var lTime1 = new Date(parseInt(records[i].ltime1));
                        html += '<dd>' + showSafe.arrengeNum(lTime1.getHours()) + ':' + showSafe.arrengeNum(lTime1.getMinutes()) + '&nbsp;/&nbsp;' + records[i].lheight1 + 'cm';
                      }
                      if (records[i].ltime2 && records[i].lheight2) {
                        var lTime2 = new Date(parseInt(records[i].ltime2));
                        html += '<br />' + showSafe.arrengeNum(lTime2.getHours()) + ':' + showSafe.arrengeNum(lTime2.getMinutes()) + '&nbsp;/&nbsp;' + records[i].lheight2 + 'cm';
                      } else {
                        html += '<br />&nbsp;';
                      }
                      html += '</dd>';
                      html += '</dl>';
                      if(records[i].date2) {
                        var date2 = new Date(parseInt(records[i].date2));
                        html += '<h3>' + date2.getFullYear() + '年' + (date2.getMonth() + 1) + '月' + date2.getDate() + '日</h3>';
                      }
                      html += '<dl class="tide">';
                      if (records[i].u80time5 && records[i].u80time6 && records[i].u80time7 && records[i].u80time8) {
                        var u80Time5 = new Date(parseInt(records[i].u80time5));
                        var u80Time6 = new Date(parseInt(records[i].u80time6));
                        html += '<dt>潮位80cm以下時間帯(その1)&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time5.getHours()) + ':' + showSafe.arrengeNum(u80Time5.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time6.getHours()) + ':' + showSafe.arrengeNum(u80Time6.getMinutes()) + '</dd>';
                      } else if (records[i].u80time5 && records[i].u80time6 && !records[i].u80time7 && !records[i].u80time8) {
                        var u80Time5 = new Date(parseInt(records[i].u80time5));
                        var u80Time6 = new Date(parseInt(records[i].u80time6));
                        html += '<dt>潮位80cm以下時間帯&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time5.getHours()) + ':' + showSafe.arrengeNum(u80Time5.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time6.getHours()) + ':' + showSafe.arrengeNum(u80Time6.getMinutes()) + '</dd>';
                      }
                      if (records[i].u80time7 && records[i].u80time8) {
                        var u80Time7 = new Date(parseInt(records[i].u80time7));
                        var u80Time8 = new Date(parseInt(records[i].u80time8));
                        html += '<dt>(その2)&raquo;</dt>';
                        html += '<dd>' + showSafe.arrengeNum(u80Time7.getHours()) + ':' + showSafe.arrengeNum(u80Time7.getMinutes()) + '〜' + showSafe.arrengeNum(u80Time8.getHours()) + ':' + showSafe.arrengeNum(u80Time8.getMinutes()) + '</dd>';
                      }
                      html += '</dl>';
                      html += '<dl class="left" id="cycle">';
                      html += '<dt>潮回り&raquo;</dt>';
                      if(records[i].cycle2) {
                        html += '<dd>' + showSafe.tides[records[i].cycle2] + '</dd>';
                      }
                      html += '</dl>';
                      html += '<dl class="left hilo">';
                      html += '<dt>満潮&raquo;</dt>';
                      if(records[i].htime3 && records[i].hheight3) {
                        var hTime3 = new Date(parseInt(records[i].htime3));
                        html += '<dd>' + showSafe.arrengeNum(hTime3.getHours()) + ':' + showSafe.arrengeNum(hTime3.getMinutes()) + '&nbsp;/&nbsp;' + records[i].hheight3 + 'cm';
                      }
                      if (records[i].htime4 && records[i].hheight4) {
                        var hTime4 = new Date(parseInt(records[i].htime4));
                        html += '<br />' + showSafe.arrengeNum(hTime4.getHours()) + ':' + showSafe.arrengeNum(hTime4.getMinutes()) + '&nbsp;/&nbsp;' + records[i].hheight4 + 'cm';
                      } else {
                        html += '<br />&nbsp;';
                      }
                      html += '</dd>';
                      html += '</dl>';
                      html += '<dl class="left hilo">';
                      html += '<dt>干潮&raquo;</dt>';
                      if(records[i].ltime3 && records[i].lheight3) {
                        var lTime3 = new Date(parseInt(records[i].ltime3));
                        html += '<dd>' + showSafe.arrengeNum(lTime3.getHours()) + ':' + showSafe.arrengeNum(lTime3.getMinutes()) + '&nbsp;/&nbsp;' + records[i].lheight3 + 'cm';
                      }
                      if (records[i].ltime4 && records[i].lheight4) {
                        var lTime4 = new Date(parseInt(records[i].ltime4));
                        html += '<br />' + showSafe.arrengeNum(lTime4.getHours()) + ':' + showSafe.arrengeNum(lTime4.getMinutes()) + '&nbsp;/&nbsp;' + records[i].lheight4 + 'cm';
                      } else {
                        html += '<br />&nbsp;';
                      }
                      html += '</dd>';
                      html += '</dl>';
                    }
                  }
                  showSafe.result.innerHTML = html;
                }
    }
  );
}




/**************** ここからヘルパー関数群 ****************/

/*---------- 要素オブジェクト生成関数 ----------*/
// IDで指定されるエレメントのオブジェクトを生成
// 引数：obj -> 名前空間オブジェクト
//      ids -> 対象エレメントのIDで構成された配列
// 返り値：なし
showSafe.makeObj =
function(obj, ids) {
  for(var i = 0; i < ids.length; i++) {
    // オブジェクト生成ステートメントテキストを構成
    var statement = obj + '[ids[i]] = document.getElementById(ids[i])';
    // ステートメントを評価
    eval(statement);
  }
}

/*---------- 桁揃え関数 ----------*/
// 10未満の整数の時'0'を補う関数
// 引数：intnum -> 調整対象数字
// 返り値：調整後数字
showSafe.arrengeNum =
function(intnum) {
  var arrenged = '0' + intnum;
  return arrenged.slice(-2);
}
