//
//  ニュース項目の見出しを表示する
//  トップページ用
//  要prototype.js
//  要Xml2Obj.js
//
//  showNewsHeadline(url, element, shopCode)
//  url：ターゲットスクリプトのurl
//  element：受信データの表示先id属性
//  shopCode：店舗別DB情報セレクタ
//
//  2007/12/04 (c) Ohsako, Junichi
//  2010/01/24 update
//


function showNewsHeadline(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=news_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="newsHeader"><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:showNews(\'../common/php/get_edit_record.php?shopcode=' + shopCode + '\', ' + records[i].news_id + ', \'news\', \'' + shopCode + '\', ' + limit + ');">';
                      if(records[i].imagepath) {
                        html += '<div class="nHeaddingImage">';
                        html += '<img src="' + records[i].imagepath.match(/img\/.+/) + '" width="88" alt="';
                        if(records[i].alternate) {
                          html += records[i].alternate + '" />';
                        } else {
                          html += '" />';
                        }
                        html += '</div>';
                      }
                      var nDate = new Date(records[i].schedule * 1000); 
                      html += '<h3><div>' + nDate.getFullYear() + '年' + (nDate.getMonth() + 1) + '月' + nDate.getDate() + '日（' + week[nDate.getDay()] + '）</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:showNewsHeadline(\'../common/php/get_sorted_all_record.php?shopcode=' + shopCode + '\', \'news\', \'' + shopCode + '\')">全てのニュースを見る</a></p>';
                    }
                    // DOM出力
                    $(element).style.width = '540px';
                    $(element).style.border = '0';
                    $(element).innerHTML = html;
                  } else {
                    $(element).innerHTML = '';
                  }
                }
    }  
  );
}
