// @author kimura
// 必要なライブラリ
// prototype-1.6.1.js
var SearchResultListAction = Class.create();
SearchResultListAction.prototype = {
	
	// コンストラクター
	initialize: function(resourceType, page, loadingMessage, errorMessage) {
		// ユーザー設定項目
		// 系統のヒット数の挿入先id
		this.strainHitCountInListId = 'strainHitCountInList';
		// 系統のページャーテーブルのid
		this.strainPagerTableId = 'strainList';
		// 系統のリストの挿入先id
		this.strainListId = 'strainListByAjax';
		// DNAのヒット数の挿入先id
		this.dnaHitCountInListId = 'dnaHitCountInList';
		// DNAのページャーテーブルのid
		this.dnaPagerTableId = 'dnaList';
		// DNAのリスト挿入先id
		this.dnaListId = 'dnaListByAjax';
		// ソートオプションが可能な最大検索結果数
		this.sortEnableMaxHitCount = 10000;
		
		// 読み込み時のnow loading...画像ファイルのパス
		this.loadingImgBar = 'images/ajaxLoadingBar_dedede.gif';
		this.loadingImgCircle = 'images/ajaxLoadingCircle.gif';
		
		// リクエストを送信する方法
		this.requestMethod = 'post';
		// AJAX用のリクエストを送信するURI
		this.requestURI = 'ajaxSearchResultListAction.do';
		// ローディング中のメッセージ
		this.loadingMessage = loadingMessage;
		// エラーメッセージ
		this.errorMessage = errorMessage;
		
		this._processInitialize(resourceType, page);
	},
	
	// インスタンス化時の処理を行います。
	_processInitialize: function(resourceType, page) {
		// システム設定項目
		this._RESOURCE_TYPE_STRAIN = 'strain';
		this._RESOURCE_TYPE_DNA = 'dna';
		
		// リソースの種類
		this._resourceType = resourceType;
		// ページ数
		this._page = 1;
		// ページャーのPageCountLinkの挿入先オブジェクト
		this._pageCountLink;
		// ページャーのHitItemCountの挿入先オブジェクト
		this._hitItemCount;
		// ソートオプションフォームオブジェクト
		this._sortByDropdown;
		// ソートオプションオブジェクト
		this._sortByDropdownSelect;
		
		if (!this.strainHitCountInListId) {throw 'Error: Not found the strainHitCountInListId.';}
		if (!this.strainPagerTableId) {throw 'Error: Not found the strainPagerTableId.';}
		if (!this.strainListId) {throw 'Error: Not found the strainListId.';}
		if (!this.dnaHitCountInListId) {throw 'Error: Not found the dnaHitCountInListId.';}
		if (!this.dnaPagerTableId) {throw 'Error: Not found the dnaPagerTableId.';}
		if (!this.dnaListId) {throw 'Error: Not found the dnaListId.';}
		
		if (!this._resourceType) {
			throw 'Error: resourceType is null.';
		} else {
			if (this._resourceType.toLowerCase() == this._RESOURCE_TYPE_STRAIN) {
				this._resourceType = this._RESOURCE_TYPE_STRAIN;
			} else if (this._resourceType.toLowerCase() == this._RESOURCE_TYPE_DNA) {
				this._resourceType = this._RESOURCE_TYPE_DNA;
			} else {
				throw 'Error: resourceType is invalid.';
			}
		}
		
		if (page && !isNaN(parseInt(page))) {
			this._page = parseInt(page);
		}
		
		if (this._resourceType == this._RESOURCE_TYPE_STRAIN) {
			this._pageCountLink = $(this.strainPagerTableId).select('td.pageCountLink');
			this._pageCountLink = this._pageCountLink[0];
			this._hitItemCount = $(this.strainPagerTableId).select('td.hitItemCount');
			this._hitItemCount = this._hitItemCount[0];
			this._sortByDropdown = $(this.strainPagerTableId).select('td.sortByDropdown form');
			this._sortByDropdown = this._sortByDropdown[0];
			
		} else if (this._resourceType == this._RESOURCE_TYPE_DNA) {
			this._pageCountLink = $(this.dnaPagerTableId).select('td.pageCountLink');
			this._pageCountLink = this._pageCountLink[0];
			this._hitItemCount = $(this.dnaPagerTableId).select('td.hitItemCount');
			this._hitItemCount = this._hitItemCount[0];
			this._sortByDropdown = $(this.dnaPagerTableId).select('td.sortByDropdown form');
			this._sortByDropdown = this._sortByDropdown[0];
		}
		
		if (!this._pageCountLink) {throw 'Error: Not found the _pageCountLink.';}
		if (!this._hitItemCount) {throw 'Error: Not found the _hitItemCount.';}
		if (!this._sortByDropdown) {throw 'Error: Not found the _sortByDropdown'};
		
		this._sortByDropdownSelect = $(this._sortByDropdown).select('select');
		this._sortByDropdownSelect = this._sortByDropdownSelect[0];
		
		if (!this._sortByDropdownSelect) {throw 'Error: Not found the _sortByDropdownSelect'};
		Event.observe(this._sortByDropdownSelect, 'change', this._processSortByDropdownOnChange.bind(this));
		
		this._getResourceList();
	},
	
	// リソースリストを取得します。
	_getResourceList: function() {
		var param = this._createParameter();
		new Ajax.Request(this.requestURI, {method: this.requestMethod, parameters: param, 
			onCreate: this._processLoading.bind(this), onFailure: this._processFailure.bind(this), onSuccess: this._processSuccess.bind(this), onException: this._processException.bind(this)});
	},
	
	// 読み込み中の処理を行います。
	_processLoading: function() {
		if (this._resourceType == this._RESOURCE_TYPE_STRAIN) {
			this._processStrainLoading();
		} else if (this._resourceType == this._RESOURCE_TYPE_DNA) {
			this._processDnaLoading();
		}
	},
	
	// 系統の読み込み中の処理を行います。
	_processStrainLoading: function() {
		this._createHitCountLoading(this.strainHitCountInListId);
		this._createResourceListLoading(this.strainListId);
	},
	
	// DNAの読み込み中の処理を行います。
	_processDnaLoading: function() {
		this._createHitCountLoading(this.dnaHitCountInListId);
		this._createResourceListLoading(this.dnaListId);
	},
	
	// ヒット数の読み込み中...を作成します。
	_createHitCountLoading: function(hitCountId) {
		$(hitCountId).update('&nbsp;');
		Element.setStyle($(hitCountId), {
			'backgroundColor': '#dedede',
			'backgroundImage': 'url(' + this.loadingImgBar + ')',
			'backgroundPosition': 'bottom left',
			'backgroundRepeat': 'no-repeat',
			'height': '18px',
			'lineHeight': '18px',
			'paddingBotton': '0px',
			'paddingLeft': '17px',
			'paddingRight': '0px',
			'paddingTop': '0px',
			'width': '1px'
		});
	},
	
	// リソースリストの「読み込み中...」を作成します。
	_createResourceListLoading: function(resourceListId) {
		$(resourceListId).update(this.loadingMessage);
		
		Element.setStyle($(resourceListId), {
				'backgroundColor': '#ffffff',
				'backgroundImage': 'url(' + this.loadingImgCircle + ')',
				'backgroundPosition': 'center left',
				'backgroundRepeat': 'no-repeat',
				'borderTop': '1px solid #cccccc',
				'fontSize': '1.1em',
				'height': '30px',
				'lineHeight': '30px',
				'marginBotton': '0px',
				'marginLeft': '10px',
				'marginRight': '0px',
				'marginTop': '0px',
				'overflow': 'hidden',
				'paddingBotton': '0px',
				'paddingLeft': '30px',
				'paddingRight': '0px',
				'paddingTop': '0px',
				'width': '705px'
		});
	},
	
	// リクエストが失敗した時の処理を行います。
	_processFailure: function(response) {
		this._displayErrorMessage();
	},	

	// http通信の途中でエラーが発生した時の処理を行います。
	_processException: function(response, ex) {
		this._displayErrorMessage();
	},
	
	// エラーメッセージを表示します。
	_displayErrorMessage: function() {
		if (this._resourceType == this._RESOURCE_TYPE_STRAIN) {
			$(this.strainHitCountInListId).writeAttribute('style', '');
			$(this.strainHitCountInListId).update(' ');
			this._createResourceListError(this.strainListId);
			
		} else if (this._resourceType == this._RESOURCE_TYPE_DNA) {
			$(this.dnaHitCountInListId).writeAttribute('style', '');
			$(this.dnaHitCountInListId).update(' ');
			this._createResourceListError(this.dnaListId);
		}
	},
	
	// リソースリストの「データを取得できませんでした」を作成します。
	_createResourceListError: function(resourceListId) {
		$(resourceListId).update(this.errorMessage);
		
		Element.setStyle($(resourceListId), {
				'backgroundColor': '#ffffff',
				'backgroundImage': '',
				'borderTop': '1px solid #cccccc',
				'color': '#ff0000',
				'fontSize': '1.1em',
				'height': '30px',
				'lineHeight': '30px',
				'marginBotton': '0px',
				'marginLeft': '20px',
				'marginRight': '0px',
				'marginTop': '0px',
				'overflow': 'hidden',
				'paddingBotton': '0px',
				'paddingLeft':  '0px',
				'paddingRight': '0px',
				'paddingTop': '0px',
				'width': '660px'
		});
	},
	
	// リスエクトパラメーターを作成します。
	_createParameter: function() {
		var param = new String;
		param += "&resourceType=" + encodeURIComponent(this._resourceType);
		param += "&page=" + encodeURIComponent(this._page);
		
		return param;
	},
	
	// リクエストが返ってきた時の処理を行います。
	_processSuccess: function(xmlHttpRequest) {
		var responseXML = xmlHttpRequest.responseText;
		responseXML = responseXML.replace(/[\r\n|\n]/g, '');
		
		if (this._resourceType == this._RESOURCE_TYPE_STRAIN) {
			var strain = responseXML.match(/<strain>(.*)<\/strain>/);
			var strainHitCount = strain[1].match(/<hitCount>(.*)<\/hitCount>/);
			var strainSortOption = strain[1].match(/<sortOption>(.*)<\/sortOption>/);
			var strainPageCountLink = strain[1].match(/<pageCountLink>(.*)<\/pageCountLink>/);
			var strainHitItemCount = strain[1].match(/<hitItemCount>(.*)<\/hitItemCount>/);
			var strainList = strain[1].match(/<resourceList>(.*)<\/resourceList>/);
			
			this._insertStrainList(strainHitCount[1], strainPageCountLink[1], strainHitItemCount[1], strainList[1]);
			this._enbaleSortByDropdown(strainSortOption[1], strainHitCount[1]);
			
		} else if (this._resourceType == this._RESOURCE_TYPE_DNA) {
			var dna = responseXML.match(/<dna>(.*)<\/dna>/);
			var dnaHitCount = dna[1].match(/<hitCount>(.*)<\/hitCount>/);
			var dnaSortOption = dna[1].match(/<sortOption>(.*)<\/sortOption>/);
			var dnaPageCountLink = dna[1].match(/<pageCountLink>(.*)<\/pageCountLink>/);
			var dnaHitItemCount = dna[1].match(/<hitItemCount>(.*)<\/hitItemCount>/);
			var dnaList = dna[1].match(/<resourceList>(.*)<\/resourceList>/);
			
			this._insertDnaList(dnaHitCount[1], dnaPageCountLink[1], dnaHitItemCount[1], dnaList[1]);
			this._enbaleSortByDropdown(dnaSortOption[1], dnaHitCount[1]);
		}
	},
	
	// 系統のリソースリストを挿入します。
	_insertStrainList: function(strainHitCount, strainPageCountLink, strainHitItemCount, strainList) {
		$(this.strainHitCountInListId).writeAttribute('style', '');
		$(this.strainHitCountInListId).update(strainHitCount + ' ');
		
		$(this._pageCountLink).update(strainPageCountLink);
		$(this._hitItemCount).update(strainHitItemCount);
		
		Element.replace(this.strainListId, strainList);
	},
	
	// DNAのリソースリストを挿入します。
	_insertDnaList: function(dnaHitCount, dnaPageCountLink, dnaHitItemCount, dnaList) {
		$(this.dnaHitCountInListId).writeAttribute('style', '');
		$(this.dnaHitCountInListId).update(dnaHitCount + ' ');
		
		$(this._pageCountLink).update(dnaPageCountLink);
		$(this._hitItemCount).update(dnaHitItemCount);
		
		Element.replace(this.dnaListId, dnaList);
	},
	
	// ソートオプションを表示します。
	_enbaleSortByDropdown: function (sortOption, hitCount) {
		if (0 < hitCount && hitCount <= this.sortEnableMaxHitCount) {
			$(this._sortByDropdown).setStyle('display:block');
		}
		
		var options = $(this._sortByDropdownSelect).select('option');
		options.each(function(option) {
			if (option.readAttribute('value') == sortOption) {
				option.writeAttribute('selected', 'true');
			}
		}, this);
	},
	
	// ソートオブションの値の変更時の処理を行います。
	_processSortByDropdownOnChange: function () {
		var sortByDropdownValue = $F(this._sortByDropdownSelect);
		$(this._sortByDropdown).submit();
	}
};
