/**
 * prototype.js を使った Ajax.
 * MQカスタマイズ.
 *
 * 依存ファイル
 *   prototype.js
 *
 */
function Reseaver() {
    this.initialize();
}
Reseaver.prototype.initialize = function() {
    this._sSearchDirection = 'backward';
    this._iBaseTime  = 0;
	this._iSmallestTime = 0;
	this._iLargestTime  = 0;
	this._bFirstPage = true;
}
Reseaver.prototype.getSearchDirection = function() {
    return this._sSearchDirection;
}
Reseaver.prototype.getBaseTime = function() {
    return this._iBaseTime;
}
Reseaver.prototype.doFirstTime = function() {
    return this._bFirstPage;
}


/**
 * レスポンスの実行をする.
 *
 * @param  XMLHttpRequest oXmlHttp  XMLHttpRequestオブジェクト
 * @return None
 */
Reseaver.prototype.handleHttpResponse = function(oXmlHttp){
	var oXmlDoc = oXmlHttp.responseXML;
	if (oXmlDoc.documentElement == null || oXmlDoc.getElementsByTagName('entry').item(0) == null) {
		// 案内のdivを開く
		this.openNotHaveEntryMessage();
		return;
	}
	// 結果のdivを開く
	this.openHaveEntryMessage();
	// 各結果のセット
	for (i = 0 ; i < KIZASI_MQ_KWIC_LIST_MAX ; i++) {
		var iID = i + 1;
		var oEntry = oXmlDoc.getElementsByTagName('entry').item(i);
		if (oEntry) {
			divOpen('result' + iID);
			this.writesOutContents(oEntry, iID);
			// ついでに、tmの最小値、最大値をセット
			this.setIssuedUTC(oEntry, iID);
		} else {
			divClose('result' + iID);
		}
	}

	// ページングの設定
	this.pageLinkVisiblity(oXmlDoc.getElementsByTagName('next_or_prev').item(0).firstChild.data);

	// 初回アクセスフラグOFF
	this._bFirstPage = false;
}


Reseaver.prototype.openHaveEntryMessage = function() {
    divOpen('blogEntries');
    divClose('noBlogEntry');
}
Reseaver.prototype.openNotHaveEntryMessage = function() {
    divClose('blogEntries');
    divOpen('noBlogEntry');
}


Reseaver.prototype.writesOutContents = function(oEntry, iID) {
    rewrite('result' + iID, 'entry_title', getTagData(oEntry, 'title'));
    rewrite('result' + iID, 'entry_date', getTagData(oEntry, 'issued'));
    rewrite('result' + iID, 'entry_content', getTagData(oEntry, 'content'));
    rewrite('result' + iID, 'entry_look', 'このブログを見る');
    rewrite_href('result' + iID, 'entry_title', getTagAttribute(oEntry, 'link', "href"));
    rewrite_href('result' + iID, 'entry_look' , getTagAttribute(oEntry, 'link', "href"));
}


Reseaver.prototype.setIssuedUTC = function(oEntry, iID) {
    // tmの最小値と最大値をセット (prevPage(), nextPage()で使用する)
    if (iID == 1) { this._iSmallestTime = oEntry.getElementsByTagName('issued_utc').item(0).firstChild.data; }
    this._iLargestTime = oEntry.getElementsByTagName('issued_utc').item(0).firstChild.data;
}

Reseaver.prototype.setPrevPage = function() {
    this._iBaseTime = this._iSmallestTime;
    this._sSearchDirection = 'forward';
}
Reseaver.prototype.setNextPage = function() {
    this._iBaseTime = this._iLargestTime;
    this._sSearchDirection = 'backward';
}

Reseaver.prototype.pageLinkVisiblity = function(iNextOrPrev) {
    // 逆方向か、次も同じ方向にいける、かつ初回アクセスじゃないか
    if ((this._sSearchDirection != 'forward' || iNextOrPrev == 1) &&  this._bFirstPage != true) {
        rewrite('entryPaging_top',    'entry_page_prev', '<a href="javascript: prevPage()">前の'+KIZASI_MQ_KWIC_LIST_MAX+'件</a>');
        rewrite('entryPaging_bottom', 'entry_page_prev', '<a href="javascript: prevPage()">前の'+KIZASI_MQ_KWIC_LIST_MAX+'件</a>');
    } else {
        rewrite('entryPaging_top',    'entry_page_prev', '前の'+KIZASI_MQ_KWIC_LIST_MAX+'件');
        rewrite('entryPaging_bottom', 'entry_page_prev', '前の'+KIZASI_MQ_KWIC_LIST_MAX+'件');
    }
    // 逆方向か、次も同じ方向にいける
    if (this._sSearchDirection != 'backward' || iNextOrPrev == 1) {
        rewrite('entryPaging_top',    'entry_page_next', '<a href="javascript: nextPage()">次の'+KIZASI_MQ_KWIC_LIST_MAX+'件</a>');
        rewrite('entryPaging_bottom', 'entry_page_next', '<a href="javascript: nextPage()">次の'+KIZASI_MQ_KWIC_LIST_MAX+'件</a>');
    } else {
        rewrite('entryPaging_top',    'entry_page_next', '次の'+KIZASI_MQ_KWIC_LIST_MAX+'件');
        rewrite('entryPaging_bottom', 'entry_page_next', '次の'+KIZASI_MQ_KWIC_LIST_MAX+'件');
    }
}

