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

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



//  requestDiaryHeadlinePage(url, ym)
//  日記（ブログ）記事頭出し表示のHTMLを出力する拡張メソッド（ブログページ用） ※カスケード不可
//  arguments => url：記事頭出し表示のデータを取得するサーバサイドスクリプト（文字列）
//               ym：4桁西暦年号 + 月（整数）
//  return => 整形済みHTML文字列
//  delegation => makeDiaryHeadlinePage()：記事のHTML整形メソッド
//  dependent => $.ajax：Ajax通信オブジェクト（jQuery）
//
tetraunit.requestDiaryHeadlinePage =
function () {
  var _getHtml =                                                          // Ajax通信メソッド
  function (url, ym) {
    var html;
    $.ajax({url: url,
        data: 'table=diary_tbl&key=ddate&ym=' + ym,
        type: 'post',
        dataType: 'xml',
        async: false,
        complete: function (request) {                                    // HTMLを整形出力するメソッド呼出しのコールバック関数 
          html = tetraunit.makeDiaryHeadlinePage(request);
        }
      }
    );
    return html;
  };
  
  return function (url, ym) {
    return _getHtml(url, ym);
  }
}();



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

  var _buildHtml =
  function () {
    var html = [], dDate;
    _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.diaryFadeIn(' + _that[i].diary_id + ')">');
        if (_that[i].imagepath1) {
          html.push('<img class="dHeaddingImage" 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="dHeaddingImage" src="' + _that[i].imagepath2.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate2) {
            html.push(_that[i].alternate2 + '" />');
          } else {
            html.push('" />');
          }
          html.push('</div>');
        }
        dDate = new Date(_that[i].ddate * 1000); 
        html.push('<h3><span>' + dDate.getFullYear() + '年' + (dDate.getMonth() + 1) + '月' + dDate.getDate() + '日(' + tetraunit.week[dDate.getDay()] + ')</span>');
        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();
  }
}();



//  makeDiaryHeadlinePage(response)
//  日記（ブログ）記事頭出し表示のデータをHTMLに整形する拡張メソッド（ブログページ用） ※カスケード不可
//  arguments => response：サーバから取得した記事頭出し表示データMXLオブジェクト（XMLオブジェクト）
//  return => 整形済みHTML文字列
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//                week[]：曜日文字列の共有オブジェクト
//
tetraunit.makeDiaryHeadlinePage =
function () {
  var _that;
  var _buildHtml =
  function () {
    var html = [], dDate;
    _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.diaryFadeIn(' + _that[i].diary_id + ')">');
        if (_that[i].imagepath1) {
          html.push('<img class="dHeaddingImage" 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="dHeaddingImage" src="' + _that[i].imagepath2.match(/img\/.+/) + '" width="56" alt="');
          if (_that[i].alternate2) {
            html.push(_that[i].alternate2 + '" />');
          } else {
            html.push('" />');
          }
          html.push('</div>');
        }
        dDate = new Date(_that[i].ddate * 1000); 
        html.push('<h3><span>' + dDate.getFullYear() + '年' + (dDate.getMonth() + 1) + '月' + dDate.getDate() + '日(' + tetraunit.week[dDate.getDay()] + ')</span>');
        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();
  }
}();




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



// makeDiary(response, url, id)
//  日記（ブログ）記事のデータをHTMLに整形する拡張メソッド ※カスケード不可
//  arguments => response：サーバから取得した記事データMXLオブジェクト（XMLオブジェクト）
//               url：記事執筆者のデータを取得するサーバサイドスクリプト（文字列）
//               id：記事のid（整数）
//  return => 整形済みHTML文字列
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//                requestAuthor()：記事執筆者表示のHTML出力メソッド
//  dependent => nwopen()：ウィンドウオープンメソッド
//
tetraunit.makeDiary =
function () {
  var _that;
  
  var _buildHtml =
  function (url, id) {
    var html = [], dDate;
    _that = tetraunit.xml2Obj(_that.responseXML);                                // XMLをレコード単位の配列オブジェクトに変換
    if (_that.length) {                                                           // 有効なデータが存在するかを判定
      for (var i = 0; i < _that.length; i++) {                                    // レコード数分の繰り返し（想定数は"１"）
        if (_that[i].staff_id) {                                                  // 執筆者の登録があればHTMLを取得
          html.push(tetraunit.requestAuthor(url, _that[i].staff_id));
        }
        dDate = new Date(_that[i].ddate * 1000);                                 // 日付を整形
        html.push('<h3 id="diaryItemHeader"><span>' + dDate.getFullYear());
        html.push('年' + (dDate.getMonth() + 1));
        html.push('月' + dDate.getDate());
        html.push('日（' + tetraunit.week[dDate.getDay()] + '）</span>');
        html.push(_that[i].title + '</h3><div id="diaryBody">');
        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 < 11; j++) {
            if (_that[i]['imagepath' + j]) {
              html.push('<a href="javascript: tetraunit.nwopen(\'photo\',\'photo.html?img/diary/' + _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>');
        }
        html.push('<p id="permalink">この記事のリンク　http://www.ducati-yokohama.com/yokohama/blog.html?id=' + id + '</p></div>');
      }
    }
    return html.join('');
  };

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

    return _buildHtml(url, id);
  }
}();



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



//  makeAuthor(response)
//  日記（ブログ）記事執筆者のデータをHTMLに整形する拡張メソッド ※カスケード不可
//  arguments => response：サーバから取得した記事執筆者データMXLオブジェクト（XMLオブジェクト）
//  delegation => xml2Obj()：XMLをレコードリストオブジェクトに変換するメソッド
//  return => 整形済みHTML文字列
//
tetraunit.makeAuthor =
function () {
  var _that, _path;
  
  var _buildHtml =
  function () {
    var html, i;
    _that = tetraunit.xml2Obj(_that.responseXML);                                // XMLをレコード単位の配列オブジェクトに変換
    if (_that.length) {                                                           // 有効なデータが存在するかを判定
      for (i = 0; i < _that.length; i++) {                                        // 配列オブジェクトのプロパティから値を取り出してHTMLを構成
        html = '<p><img src="' + _that[i]['imagepath'].match(/img\/.+/);
        html += '" height="50" />&nbsp;<span>' + _that[i].sname.replace(/\{\{.*\}\}/g, '') + '</span></p>';
      }
    }
    return html;
  };

  return function (response) {
    _that = response;

    return _buildHtml();
  }
}();



// diaryFadeIn(keyValue)
// フェードイン処理のコールバック関数
//  arguments => keyValue：記事執筆者のデータを特定するidフィールド値（整数）
//  return => なし
//  delegation => dragger()：ドラッグエレメント生成メソッド
//                requestDiary()：記事執筆者・記事本文のHTML出力メソッド
//                tetraunit.exStyle：エグゾーストノート出力用Flashコンテンツのエレメントオブジェクトのスタイルプロパティ
//                tetraunit.hbStyle：ブラックアウトハーフトーンエレメントオブジェクトのスタイルプロパティ
//                tetraunit.cfStyle：Ajaxコンテンツ表示エレメントオブジェクトのスタイルプロパティ
tetraunit.diaryFadeIn =
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.requestDiary(url, keyValue, urlAuthor);
  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';
  };
};

