//
//  店長ブログ記事の見出しを表示する
//  店長ブログページ用
//  要prototype.js
//  要Xml2Obj.js
//
//  showMonthlyDiaryHeadline(url, element)
//  url：ターゲットスクリプトのurl
//  ymelement：年月データの表示先id属性
//  element：受信データの表示先id属性
//  shopCode：DB情報セレクタ
//
//  2007/12/05 (c) Ohsako, Junichi
//


function showMonthlyDiaryHeadline(url, ymelement, element, shopCode) {
  // 一覧へ復帰するための自分自身のコード
  var backScript = '\'showMonthlyDiaryHeadline(\\\'' + url + '\\\', \\\'' + ymelement + '\\\', \\\'' + element + '\\\', \\\'' + shopCode + '\\\')\'';
  // URL引き数を取得
  var arg = getArg();
  // 現在時から年月データを生成
  var nowDate = new Date();
  var y = nowDate.getFullYear();
  var m = nowDate.getMonth() + 1;
  var ym = y.toString() + m.toString();
  // 引数があればそれを年月データとして利用
  if(arg) {
    ym = arg;
    y = arg.substring(0, 4);
    m = arg.substring(4);
  }
  document.getElementById(ymelement).innerHTML = y + '年' + m + '月';
  // prototype.js でAjax通信
  new Ajax.Request(url, {
    asynchronous: true,
    method: "post",
    parameters: 'ym=' + ym,
    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++) {
                      html += '<a class="headlineBox" href="javascript:showDiary(\'../common/php/get_edit_record.php?shopcode=' + shopCode + '\',' + records[i].diary_id + ',\'diary\', ' + backScript + ');">';
                      if(records[i].imagepath1) {
                        html += '<div class="dHeaddingImage">';
                        html += '<img src="' + records[i].imagepath1.match(/img\/.+/) + '" width="88" alt="';
                        if(records[i].alternate1) {
                          html += records[i].alternate1 + '" />';
                        } else {
                          html += '" />';
                        }
                        html += '</div>';
                      } else
                      if(records[i].imagepath2) {
                        html += '<div class="dHeaddingImage">';
                        html += '<img src="' + records[i].imagepath2.match(/img\/.+/) + '" width="88" alt="';
                        if(records[i].alternate2) {
                          html += records[i].alternate2 + '" />';
                        } else {
                          html += '" />';
                        }
                        html += '</div>';
                      }
                      var dDate = new Date(records[i].ddate * 1000); 
                      html += '<h3><div>' + dDate.getFullYear() + '年' + (dDate.getMonth() + 1) + '月' + dDate.getDate() + '日</div>';
                      if(records[i].title) {
                        html += records[i].title;
                      }
                      html += '</h3>';
                      if(records[i].article) {
                        var body = records[i].article;
                        // 改行の削除
                        body = body.replace(/\r\n/g, "\n");
                        body = body.replace(/\r/g, "\n");
                        body = body.replace(/\n/g, '');
                        // <a>エレメント表示データを削除
                        body = body.replace(/\{\{\/a\}\}/g, '');
                        body = body.replace(/\{\{a[^\}]*?\}\}/g, '');
                        // 本文の最初から、およそ80文字を切り出し
                        body = body.substring(0, 60);
                        html += '<p class="headlineBody">' + body + '&nbsp;……</p>';
                      }
                      html += '<p class="clearing">&nbsp;</p></a><p class="separator">&nbsp;</p>';
                    }
                  } else {
                    html += '<img id="yet" src="../common/parts/yet.gif" width="500" height="130" alt="登録された記事は、まだありません。" />'
                  }
                  // DOM出力
                  $(element).style.width = '540px';
                  $(element).style.border = '0';
                  $(element).innerHTML = html;
                }
    }  
  );
}
