//
//  日記の項目を表示する
//  要prototype.js
//  要Xml2Obj.js
//  要basic.js
//
//  showDiary(url, element)
//  url：ターゲットスクリプトのurl
//  did：表示レコードのid
//  element：受信データの表示先id属性
//  backScript：一覧表示にもどるためのコード
//
//  2007/12/05 (c) Ohsako, Junichi
//


function showDiary(url, did, element, backScript) {
  
  // 曜日判定用配列
  var week = ['日', '月', '火', '水', '木', '金', '土'];
  new Ajax.Request(url, {
    asynchronous: true,
    method: 'post',
    parameters: 'table=diary_tbl&key=diary_id&key_value=' + did,
    onComplete: function(request) {
                  // 受信データオブジェクトをXMLとして取得し、レコード配列オブジェクトを生成
                  var dbResult = new Xml2Obj(request.responseXML);
                  dbResult.buildRecords();
                  var records = dbResult.getRecords();
                  // ここから変数htmlにHTML化した文字列を蓄積
                  var html = '';
                  // 有効なデータが存在するかを判定
                  if(records.length) { 
                    // レコード配列から値を取り出してhtmlを生成
                    for(var i = 0; i < records.length; i++) {
                      var dDate = new Date(records[i].ddate * 1000); 
                      html += '<h2 id="diaryItem"><div>' + dDate.getFullYear() + '年' + (dDate.getMonth() + 1) + '月' + dDate.getDate() + '日（' + week[dDate.getDay()] + '）</div>';
                      html += records[i].title + '</h2>';
                      if(records[i].imagepath1) {
                        html += '<p class="click">▼クリックで写真を拡大</p>';
                        html += '<div class="imgbox">';
                        html += '<a href="javascript:openWinFix(\'photo\',\'photo.html?img/diary/' + records[i].imagepath1.slice(-15, -4) + '_l.jpg\',\'680\',\'710\');" style="border-bottom:none;"><img src="' + records[i].imagepath1.match(/img\/.+/) + '" title="';
                        if(records[i].alternate1) {
                          html += records[i].alternate1 + '" alt="' + records[i].alternate1 + '" /></a>';
                        } else {
                          html += '" alt="" /></a>';
                        }
                      }
                      if(records[i].imagepath2) {
                        if(!records[i].imagepath1) {
                          html += '<p class="click">▼クリックで写真を拡大</p>';
                          html += '<div class="imgbox">';
                        }
                        html += '<a href="javascript:openWinFix(\'photo\',\'photo.html?img/diary/' + records[i].imagepath2.slice(-15, -4) + '_l.jpg\',\'680\',\'710\');" style="border-bottom:none;"><img src="' + records[i].imagepath2.match(/img\/.+/) + '" title="';
                        if(records[i].alternate2) {
                          html += records[i].alternate2 + '" alt="' + records[i].alternate2 + '" /></a>';
                        } else {
                          html += '" alt="" /></a>';
                        }
                      }
                      html += '</div>';
                      if(records[i].article) {
                        var body = records[i].article.replace(/\r\n/g, "\n");
                        body = body.replace(/\r\n/g, "\n");
                        body = body.replace(/\r/g, "\n");
                        body = body.replace(/\n/g, '</p><p class="diaryBody">');
                        body = body.replace(/\{\{/g, '<');
                        body = body.replace(/\}\}/g, '>');
                        html += '<p class="diaryBody">' + body + '</p>';
                      }
                      html += '<p class="preView">&laquo;&nbsp;<a href="javascript:' + backScript + ';">もとの表示へ</a></p><p class="clearing">&nbsp;</p>';
                    }
                  }
                  $(element).style.width = '538px';
                  $(element).style.border = 'solid 1px #ddd';
                  $(element).innerHTML = html;
                }
    }
  );
}
