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


function showNews(url, nid, element, shopCode, limit) {
  // 曜日判定用配列
  var week = ['日', '月', '火', '水', '木', '金', '土'];
  // prototype.js でAjax通信
  new Ajax.Request(url, {
    asynchronous: true,
    method: 'post',
    parameters: 'table=news_tbl&key=news_id&key_value=' + nid,
    onComplete: function(request) {
                  // 受信データオブジェクトをXMLとして取得し、レコード配列オブジェクトを生成
                  var dbResult = new Xml2Obj(request.responseXML);
                  dbResult.buildRecords();
                  var records = dbResult.getRecords();
                  // ここから変数htmlにHTML化した文字列を蓄積
                  var html = '';
                  // 有効なデータが存在するかを判定
                  if(records.length) { 
                    // レコード配列から値を取り出してDOM出力
                    for(var i = 0; i < records.length; i++) {
                      var nDate = new Date(records[i].schedule * 1000); 
                      html += '<h2 id="newsItem"><div>' + nDate.getFullYear() + '年' + (nDate.getMonth() + 1) + '月' + nDate.getDate() + '日' + '（' + week[nDate.getDay()] + '）</div>';
                      html += records[i].title + '</h2>';
                      if(records[i].imagepath) {
                        html += '<p class="click">▼クリックで写真を拡大</p>';
                        html += '<div class="imgbox">';
                        html += '<a href="javascript:openWinFix(\'photo\',\'photo.html?img/news/' + records[i].imagepath.slice(-15, -4) + '_l.jpg\',\'680\',\'710\');" style="border-bottom:none;"><img src="' + records[i].imagepath.match(/img\/.+/) + '" title="';
                        if(records[i].alternate) {
                          html += records[i].alternate + '" alt="' + records[i].alternate + '" /></a>';
                        } else {
                          html += '" alt="" /></a>';
                        }
                        html += '</div><!-- end imgbox -->';
                      }
                      html += '<dl>';
                      if(records[i].place) {
                        html += '<dt>会場：</dt><dd>' + records[i].place + '</dd>';
                      }
                      if(records[i].organizer) {
                        html += '<dt>主催：</dt><dd>' + records[i].organizer + '</dd>';
                      }
                      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, '</p><p>');
                        body = body.replace(/\{\{/g, '<');
                        body = body.replace(/\}\}/g, '>');
                        html += '<dt>概要：</dt><dd><p>' + body + '</p></dd></dl>';
                      }
                      html += '<p class="preView">&laquo;&nbsp;<a href="javascript:showNewsHeadline(\'../common/php/get_sorted_all_record.php?shopcode=' + shopCode + '\', \'news\', \'' + shopCode + '\', ' + limit + ');">もとの表示へ</a></p><p class="clearing">&nbsp;</p>';
                    }
                  }
                  $(element).style.width = '538px';
                  $(element).style.border = 'solid 1px #ddd';
                  $(element).innerHTML = html;
                }
    }
  );
}
