//
//  イベント項目の見出しを表示する
//  コミュニティページ用
//  要prototype.js
//  要Xml2Obj.js
//
//  showEventHeadline(url, element, shopCode)
//  url：ターゲットスクリプトのurl
//  element：受信データの表示先id属性
//  shopCode：メール送信の個別情報設定のためのセレクタ
//
//  2007/12/06 (c) Ohsako, Junichi
//  2010/01/24 update
//


function showEventHeadline(url, element, shopCode, limit) {
  // 曜日判定用配列
  var week = ['日', '月', '火', '水', '木', '金', '土'];
  // 表示件数制限
  var argLimit = '';
  if (limit) {
    argLimit = '&limit=' + limit;
  }
  // prototype.js でAjax通信
  new Ajax.Request(url, {
    asynchronous: true,
    method: "post",
    parameters: 'table=event_tbl&&sort_key=schedule&sort_flag=DESC' + argLimit,
    onComplete: function(request) {
                  // 受信データオブジェクトをXMLとして取得し、レコード配列オブジェクトを生成
                  var dbResult = new Xml2Obj(request.responseXML);
                  dbResult.buildRecords();
                  var records = dbResult.getRecords();
                  // ここから変数htmlにHTML化した文字列を蓄積
                  var html = '<div id="eventHeader"><h2><img src="../common/parts/marker.gif" width="17" height="13" />&nbsp;最新イベント情報</h2></div>';
                  // 有効なデータが存在するかを判定
                  if(records.length) { 
                    // レコード配列から値を取り出してHTML生成
                    for(var i = 0; i < records.length; i++) {
                      html += '<a class="headlineBox" href="javascript:showEvent(\'../common/php/get_edit_record.php?shopcode=' + shopCode + '\', ' + records[i].event_id + ', \'event\', \'' + shopCode + '\', ' + limit + ');">'
                      if(records[i].imagepath) {
                        html += '<div class="eHeaddingImage">';
                        html += '<img src="' + records[i].imagepath.match(/img\/.+/) + '" width="88" alt="';
                        if(records[i].alternate) {
                          html += records[i].alternate + '" />';
                        } else {
                          html += '" />';
                        }
                        html += '</div>';
                      }
                      var eDate = new Date(records[i].schedule * 1000); 
                      html += '<h3><div>' + eDate.getFullYear() + '年' + (eDate.getMonth() + 1) + '月' + eDate.getDate() + '日（' + week[eDate.getDay()] + '）';
                      if(records[i].to_schedule) {
                        var e2Date = new Date(records[i].to_schedule * 1000); 
                        html += '〜&nbsp;' + e2Date.getFullYear() + '年' + (e2Date.getMonth() + 1) + '月' + e2Date.getDate() + '日（' + week[e2Date.getDay()] + '）';
                      }
                      html += '</div>';
                      if(records[i].title) {
                        html += records[i].title;
                      }
                      html += '</h3>';
                      if(records[i].outline) {
                        var body = records[i].outline;
                        // 改行の削除
                        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>';
                    }
                    if (argLimit) {
                      html += '<p class="showAll"><span>&raquo;&nbsp;</span><a href="javascript:showEventHeadline(\'../common/php/get_sorted_all_record.php?shopcode=' + shopCode + '\', \'event\', \'' + shopCode + '\')">全てのイベント情報を見る</a></p>';
                    }
                    // DOM出力
                    $(element).style.width = '540px';
                    $(element).style.border = '0';
                    $(element).innerHTML = html;
                  } else {
                    $(element).innerHTML = '';
                  }
                }
    }  
  );
}
