//  共有配列の追加
tetraunit.week = ['日', '月', '火', '水', '木', '金', '土'];

//  requestNewsHeadline(url, limit)
//  ニュース記事頭出し表示のHTMLを出力する拡張メソッド ※カスケード不可
//  arguments => url：記事頭出し表示のデータを取得するサーバサイドスクリプト（文字列）
//               limit：頭出し表示列数の制限値（整数）
//  return => 整形済みHTML文字列
//  delegation => makeNewsHeadline()：記事のHTML整形メソッド
//  dependent => $.ajax：Ajax通信オブジェクト（jQuery）
//
tetraunit.requestNewsHeadline =
function () {
  var _that;
  
  var _getHtml =                                                          // Ajax通信メソッド
  function (url, limit) {
    var html;
    $.ajax({url: url,
        data: 'table=news_tbl&sort_key=ndate&sort_flag=DESC&limit=' + limit,
        type: 'post',
        dataType: 'xml',
        async: false,
        complete: function (request) {                                    // HTMLを整形出力するメソッド呼出しのコールバック関数 
          html = tetraunit.makeNewsHeadline(request);
        }
      }
    );
    return html;
  };
  
  return function (url, limit) {
    return _getHtml(url, limit);
  }
}();



//  requestNewsHeadlinePage(url)
//  ニュース記事頭出し表示のHTMLを出力する拡張メソッド（ニュースページ用） ※カスケード不可
//  arguments => url：記事頭出し表示のデータを取得するサーバサイドスクリプト（文字列）
//  return => 整形済みHTML文字列
//  delegation => makeNewsHeadlinePage()：記事のHTML整形メソッド
//  dependent => $.ajax：Ajax通信オブジェクト（jQuery）
//
tetraunit.requestNewsHeadlinePage =
function () {
  var _getHtml =                                                          // Ajax通信メソッド
  function (url) {
    var html;
    $.ajax({url: url,
        data: 'table=news_tbl&sort_key=ndate&sort_flag=DESC',
        type: 'post',
        dataType: 'xml',
        async: false,
        complete: function (request) {                                    // HTMLを整形出力するメソッド呼出しのコールバック関数 
          html = tetraunit.makeNewsHeadlinePage(request);
        }
      }
    );
    return html;
  };
  
  return function (url) {
    return _getHtml(url);
  }
}();



//  makeNewsHeadline(response)
//  ニュース記事頭出し表示のデータをHTMLに整形する拡張メソッド ※カスケード不可
//  arguments => response：サーバから取得した記事頭出し表示データMXLオブジェクト（XMLオブジェクト）
//  return => 整形済みHTML文字列
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//
tetraunit.makeNewsHeadline =
function () {
  var _that;

  var _buildHtml =
  function () {
    var html = [];
    _that = tetraunit.xml2Obj(_that.responseXML);                                // XMLをレコード単位の配列オブジェクトに変換
    if (_that.length) {                                                           // 有効なデータが存在するかを判定
      for (var i = 0; i < _that.length; i++) {                                    // レコード数分の繰り返し（想定数は"１"）
        html.push('<a class="headlineBox" href="javascript:tetraunit.newsFadeIn(' + _that[i].news_id + ')">');
        if (_that[i].imagepath1) {
          html.push('<img class="nHeaddingImage" src="' + _that[i].imagepath1.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate1) {
            html.push(_that[i].alternate1 + '" />');
          } else {
            html.push('" />');
          }
        } else
        if (_that[i].imagepath2) {
          html.push('<img class="nHeaddingImage" src="' + _that[i].imagepath2.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate2) {
            html.push(_that[i].alternate2 + '" />');
          } else {
            html.push('" />');
          }
          html.push('</div>');
        }
        html.push('<h3 class="newsTitle">');
        if (_that[i].title) {
          html.push(_that[i].title);
        }
        html.push('</h3><p class="clearing">&nbsp;</p></a><p class="separator">&nbsp;</p>');
      }
    }
    return html.join('');
  };

  return function (response) {
    _that = response;

    return _buildHtml();
  }
}();



//  makeNewsHeadlinePage(response)
//  ニュース記事頭出し表示のデータをHTMLに整形する拡張メソッド（ニュースページ用） ※カスケード不可
//  arguments => response：サーバから取得した記事頭出し表示データMXLオブジェクト（XMLオブジェクト）
//  return => 整形済みHTML文字列
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//                week[]：曜日文字列の共有オブジェクト
//
tetraunit.makeNewsHeadlinePage =
function () {
  var _that;
  var _buildHtml =
  function () {
    var html = [];
    _that = tetraunit.xml2Obj(_that.responseXML);                                // XMLをレコード単位の配列オブジェクトに変換
    if (_that.length) {                                                           // 有効なデータが存在するかを判定
      for (var i = 0; i < _that.length; i++) {                                    // レコード数分の繰り返し（想定数は"１"）
        html.push('<a class="headlineBox" href="javascript:tetraunit.newsFadeIn(' + _that[i].news_id + ')">');
        if (_that[i].imagepath1) {
          html.push('<img class="nHeaddingImage" src="' + _that[i].imagepath1.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate1) {
            html.push(_that[i].alternate1 + '" />');
          } else {
            html.push('" />');
          }
        } else
        if (_that[i].imagepath2) {
          html.push('<img class="nHeaddingImage" src="' + _that[i].imagepath2.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate2) {
            html.push(_that[i].alternate2 + '" />');
          } else {
            html.push('" />');
          }
          html.push('</div>');
        }
        html.push('<h3 class="newsTitle">');
        if (_that[i].title) {
          html.push(_that[i].title);
        }
        html.push('</h3>');
        if (_that[i].article) {
          var body = _that[i].article;
          body = body.replace(/\r\n/g, "\n");                             // 改行の削除
          body = body.replace(/\r/g, "\n");
          body = body.replace(/\n/g, '');
          body = body.replace(/\{\{\/a\}\}/g, '');                        // <a>エレメント表示データを削除
          body = body.replace(/\{\{a[^\}]*?\}\}/g, '');
          body = body.slice(0, 60);                                       // 本文の最初から、およそ60文字を切り出し
          html.push('<p class="headlineBody">' + body + '&nbsp;……</p>');
        }
        html.push('<p class="clearing">&nbsp;</p></a><p class="separator">&nbsp;</p>');
      }
    }
    return html.join('');
  };

  return function (response) {
    _that = response;

    return _buildHtml();
  }
}();




//  requestNews(url, keyValue)
//  ニュース記事のHTMLを出力する拡張メソッド ※カスケード不可
//  arguments => url：記事のデータを取得するサーバサイドスクリプト（文字列）
//               keyValue：記事データを特定するidフィールド値（整数）
//  return => 整形済みHTML文字列
//  delegation => makeNews()：記事のHTML整形メソッド
//  dependent => $.ajax：Ajax通信オブジェクト（jQuery）
//
tetraunit.requestNews =
function () {
  var _that;
  
  var _getHtml =                                                          // Ajax通信メソッド
  function (url, keyValue) {
    var html;
    $.ajax({url: url,
        data: 'table=news_tbl&key=news_id&key_value=' + keyValue,
        type: 'post',
        dataType: 'xml',
        async: false,
        complete: function (request) {                                    // HTMLを整形出力するメソッド呼出しのコールバック関数 
          html = tetraunit.makeNews(request);
        }
      }
    );
    return html;
  };
  
  return function (url, keyValue) {
    return _getHtml(url, keyValue);
  }
}();



// makeNews(response, url)
//  ニュース記事のデータをHTMLに整形する拡張メソッド ※カスケード不可
//  arguments => response：サーバから取得した記事データMXLオブジェクト（XMLオブジェクト）
//               url：記事執筆者のデータを取得するサーバサイドスクリプト（文字列）
//  return => 整形済みHTML文字列
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//  dependent => nwopen()：ウィンドウオープンメソッド
//
tetraunit.makeNews =
function () {
  var _that;
  
  var _buildHtml =
  function (url) {
    var html = [], nDate;
    _that = tetraunit.xml2Obj(_that.responseXML);                                // XMLをレコード単位の配列オブジェクトに変換
    if (_that.length) {                                                          // 有効なデータが存在するかを判定
      for (var i = 0; i < _that.length; i++) {                                   // レコード数分の繰り返し（想定数は"１"）
        nDate = new Date(_that[i].ndate * 1000);                                 // 日付を整形
        html.push('<h3 id="newsItemHeader">' + _that[i].title + '</h3>');
        html.push('<p id="publishedDay">' + nDate.getFullYear());
        html.push('年' + (nDate.getMonth() + 1));
        html.push('月' + nDate.getDate());
        html.push('日（' + tetraunit.week[nDate.getDay()] + '）&nbsp;掲載</p>');
        html.push('<div id="newsBody">');
        if(_that[i].imagepath1 || _that[i].imagepath2 || _that[i].imagepath3 || _that[i].imagepath4 || _that[i].imagepath5 || _that[i].imagepath6 || _that[i].imagepath7 || _that[i].imagepath8 || _that[i].imagepath9 || _that[i].imagepath10) {
          html.push('<div class="imgbox">');                                     // 写真の登録があればその分だけ整形
          for (var j = 1; j < 3; j++) {
            if (_that[i]['imagepath' + j]) {
              html.push('<a href="javascript: tetraunit.nwopen(\'photo\',\'photo.html?img/news/' + _that[i]['imagepath' + j].slice(-15, -4));
              html.push('_l.jpg\',\'680\',\'710\');"><img src="' + _that[i]['imagepath' + j].match(/img\/.+/) + '" title="');
              if (_that[i]['alternate' + j]) {
                html.push(_that[i]['alternate' + j] + '" alt="');
                html.push(_that[i]['alternate' + j] + '" /></a>');
              } else {
                html.push('" alt="" /></a>');
              }
            }
          }
          html.push('</div>');
        }
        if(_that[i].article) {
          var body = _that[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, '<br />');
          body = body.replace(/\{\{/g, '<');
          body = body.replace(/\}\}/g, '>');
          html.push('<p class="articleBody">' + body + '</p></div>');
        }
      }
    }
    return html.join('');
  };

  return function (response, url) {
    _that = response;

    return _buildHtml(url);
  }
}();



// newsFadeIn(keyValue)
// フェードイン処理のコールバック関数
//  arguments => keyValue：記事執筆者のデータを特定するidフィールド値（整数）
//  return => なし
//  delegation => dragger()：ドラッグエレメント生成メソッド
//                requestNews()：記事本文のHTML出力メソッド
//                tetraunit.exStyle：エグゾーストノート出力用Flashコンテンツのエレメントオブジェクトのスタイルプロパティ
//                tetraunit.hbStyle：ブラックアウトハーフトーンエレメントオブジェクトのスタイルプロパティ
//                tetraunit.cfStyle：Ajaxコンテンツ表示エレメントオブジェクトのスタイルプロパティ
tetraunit.newsFadeIn =
function (keyValue) {
  var url = '../common/php/get_edit_record.php?shopcode=' + tetraunit.shopCode,
  urlAuthor = '../common/php/get_edit_record.php?shopcode=' + tetraunit.shopCode;

  document.documentElement.style.overflow = 'hidden';
  if (tetraunit.exStyle) {
    tetraunit.exStyle.display = 'none';
  }
  tetraunit.hbStyle.display = 'block';
  if (navigator.userAgent.indexOf('AppleWebKit/') > -1) {
    tetraunit.cfStyle.top = document.body.scrollTop + 30 + 'px';
  } else {
    tetraunit.cfStyle.top = document.documentElement.scrollTop + 30 + 'px';
  }
  tetraunit.cfStyle.left = '50px';
  tetraunit.cfStyle.display = 'block';
  tu('#resultContent').innerHTML = tetraunit.requestNews(url, keyValue);
  tu('#winBar').dragger(tu('#contentFloat'));
  tu('#closeButton').onclick = function () {
    tetraunit.cfStyle.display = 'none';
    tetraunit.hbStyle.display = 'none';
    if (tetraunit.exStyle) {
      tetraunit.exStyle.display = 'block';
    }
    document.documentElement.style.overflow = 'auto';
  };
};

