// @author kimura
// 必要なライブラリ
// prototype-1.6.0.1.js
var ResourceCountListAction = Class.create();
ResourceCountListAction.prototype = {
	
	// コンストラクター
	initialize: function(loadingMessage, errorMessage) {
		// ユーザー設定項目
		// 全ヒット数の挿入先id
		this.allHitCountId = 'allHitCount';
		// カテゴリー別リソース数テーブルに関する設定項目
		// 系統のヒット数の挿入先id
		this.strainHitCountInCategoryId = 'strainHitCountInCategory';
		// 系統のカテゴリー別リストの挿入先の親id
		this.strainHitCountListParentId = 'strainHitCountList';
		// 系統の詳細リストへのリンクを表示するid
		this.linkToStrainDetailListId = 'linkToStrainDetailList';
		// DNAのヒット数の挿入先id
		this.dnaHitCountInCategoryId = 'dnaHitCountInCategory';
		// DNAのカテゴリー別リストの挿入先の親id
		this.dnaHitCountListParentId = 'dnaHitCountList';
		// DNAの詳細リストへのリンクを表示するid
		this.linkToDnaDetailListId = 'linkToDnaDetailList';
		
		// 読み込み時のnow loading...画像ファイルのパス
		this.loadingImgBar = 'images/ajaxLoadingBar.gif';
		this.loadingImgCircle = 'images/ajaxLoadingCircle.gif';
		
		// リクエストを送信する方法
		this.requestMethod = 'post';
		// AJAX用のリクエストを送信するURI
		this.requestURI = 'ajaxResourceCountListAction.do';
		// ローディング中のメッセージ
		this.loadingMessage = loadingMessage;
		// エラーメッセージ
		this.errorMessage = errorMessage;
		
		this._processInitialize();
	},
	
	// インスタンス化時の処理を行います。
	_processInitialize: function() {
		if (!this.strainHitCountInCategoryId) {throw 'Error: Not found the strainHitCountInCategoryId.';}
		if (!this.strainHitCountListParentId) {throw 'Error: Not found the strainHitCountListParentId.';}
		if (!this.linkToStrainDetailListId) {throw 'Error: Not found the linkToStrainDetailListId.';}
		
		if (!this.dnaHitCountInCategoryId) {throw 'Error: Not found the dnaHitCountInCategoryId.';}
		if (!this.dnaHitCountListParentId) {throw 'Error: Not found the dnaHitCountListParentId.';}
		if (!this.linkToDnaDetailListId) {throw 'Error: Not found the linkToDnaDetailListId.';}
	
		// 系統のカテゴリー別リストの挿入先の1つ前のオブジェクト
		this._trPreviousStrainHitCountList = $(this.strainHitCountListParentId).select('tr:first-of-type');
		this._trPreviousStrainHitCountList = this._trPreviousStrainHitCountList[0];
		
		// DNAのカテゴリー別リストの挿入先の1つ前のオブジェクト
		this._trPreviousDnaHitCountList = $(this.dnaHitCountListParentId).select('tr:first-of-type');
		this._trPreviousDnaHitCountList = this._trPreviousDnaHitCountList[0]; 
		
		this._getResourceCountList();
	}, 
	
	// カテゴリー別リソース数を取得します。
	_getResourceCountList: function() {
		new Ajax.Request(this.requestURI, {method: this.requestMethod, 
			onCreate: this._processLoading.bind(this), onFailure: this._processFailure.bind(this), onSuccess: this._processSuccess.bind(this), onException: this._processException.bind(this)});
	},
	
	// 読み込み中の処理を行います。
	_processLoading: function() {
		this._createHitCountLoading(this.allHitCountId);
		this._createHitCountLoading(this.strainHitCountInCategoryId);
		this._createHitCountLoading(this.dnaHitCountInCategoryId);
		
		Element.insert(this._trPreviousStrainHitCountList, {after: this._createResourceCountListLoading()});
		Element.insert(this._trPreviousDnaHitCountList, {after: this._createResourceCountListLoading()});
	},
	
	// ヒット数の読み込み中...を作成します。
	_createHitCountLoading: function(hitCountId) {
		$(hitCountId).update('&nbsp;');
		Element.setStyle($(hitCountId), {
			'backgroundColor': '#ffffff',
			'backgroundImage': 'url(' + this.loadingImgBar + ')',
			'backgroundPosition': 'bottom left',
			'backgroundRepeat': 'no-repeat',
			'height': '18px',
			'lineHeight': '18px',
			'paddingBotton': '0px',
			'paddingLeft': '17px',
			'paddingRight': '0px',
			'paddingTop': '0px',
			'width': '1px'
		});
	},
	
	// カテゴリー別リストの読み込み中...を作成します。
	_createResourceCountListLoading: function() {
		var loading = new Element('div').update(this.loadingMessage);
		Element.setStyle(loading, {
				'backgroundColor': '#ffffff',
				'backgroundImage': 'url(' + this.loadingImgCircle + ')',
				'backgroundPosition': 'center left',
				'backgroundRepeat': 'no-repeat',
				'height': '24px',
				'lineHeight': '24px',
				'marginBotton': '0px',
				'marginLeft': '6px',
				'marginRight': '0px',
				'marginTop': '0px',
				'paddingBotton': '0px',
				'paddingLeft': '24px',
				'paddingRight': '0px',
				'paddingTop': '0px'
		});
		loading = new Element('td').writeAttribute('colspan', '4').update(loading);
		
		return new Element('tr').insert(loading);
	},

	// リクエストが失敗した時の処理を行います。
	_processFailure: function(response) {
		this._displayErrorMessage();
	},	

	// http通信の途中でエラーが発生した時の処理を行います。
	_processException: function(response, ex) {
		this._displayErrorMessage();
	},
	
	// エラーメッセージを表示します。
	_displayErrorMessage: function() {
		$(this.allHitCountId).writeAttribute('style', '');
		$(this.allHitCountId).update(' ');
		
		$(this.strainHitCountInCategoryId).writeAttribute('style', '');
		$(this.strainHitCountInCategoryId).update(' ');
		$(this.dnaHitCountInCategoryId).writeAttribute('style', '');
		$(this.dnaHitCountInCategoryId).update(' ');
		
		$(this._trPreviousStrainHitCountList).next().remove();
		Element.insert(this._trPreviousStrainHitCountList, {after: this._createResourceCountListError()});
		$(this._trPreviousDnaHitCountList).next().remove();
		Element.insert(this._trPreviousDnaHitCountList, {after: this._createResourceCountListError()});
	},
	
	// カテゴリー別リストの「データを取得できませんでした」を作成します。
	_createResourceCountListError: function() {
		var error = new Element('div').update(this.errorMessage);
		Element.setStyle(error, {
				'backgroundColor': '#ffffff',
				'color': '#ff0000',
				'height': '24px',
				'lineHeight': '24px',
				'marginBotton': '0px',
				'marginLeft': '6px',
				'marginRight': '0px',
				'marginTop': '0px',
				'paddingBotton': '0px',
				'paddingLeft': '0px',
				'paddingRight': '0px',
				'paddingTop': '0px'
		});
		
		error = new Element('td').writeAttribute('colspan', '4').update(error);
		
		return new Element('tr').insert(error);
	},
	
	// リクエストが返ってきた時の処理を行います。
	_processSuccess: function(response) {
		var responseXML = response.responseText;
		responseXML = responseXML.replace(/[\r\n|\n]/g, '');
		var allHitCount = responseXML.match(/<allHitCount>(.*)<\/allHitCount>/);
		var strain = responseXML.match(/<strain>(.*)<\/strain>/);
		var dna = responseXML.match(/<dna>(.*)<\/dna>/);
		
		var strainHitCount = strain[1].match(/<hitCount>(.*)<\/hitCount>/);
		var strainHitCountList = strain[1].match(/<categoryHitList>(.*)<\/categoryHitList>/);
		
		var dnaHitCount = dna[1].match(/<hitCount>(.*)<\/hitCount>/);
		var dnaHitCountList = dna[1].match(/<categoryHitList>(.*)<\/categoryHitList>/);
		
		this._insertAllHitCount(allHitCount[1]);
		this._insertStrainHitCount(strainHitCount[1], strainHitCountList[1]);
		this._insertDnaHitCount(dnaHitCount[1], dnaHitCountList[1]);
	}, 
	
	// 全ヒット数を挿入します。
	_insertAllHitCount: function(allHitCount) {
		$(this.allHitCountId).writeAttribute('style', '');
		$(this.allHitCountId).update(allHitCount + ' ');
	},
	
	// 系統のカテゴリー別リソース数を挿入します。
	_insertStrainHitCount: function(strainHitCount, strainHitCountList) {
		$(this.strainHitCountInCategoryId).writeAttribute('style', '');
		$(this.strainHitCountInCategoryId).update(strainHitCount + ' ');
		$(this._trPreviousStrainHitCountList).next().remove();
		Element.insert(this._trPreviousStrainHitCountList, {after: strainHitCountList});
		
		if (strainHitCount > 0) {
			$(this.linkToStrainDetailListId).show();
		}
	},
	
	// DNAのカテゴリー別リソース数を挿入します。
	_insertDnaHitCount: function(dnaHitCount, dnaHitCountList) {
		$(this.dnaHitCountInCategoryId).writeAttribute('style', '');
		$(this.dnaHitCountInCategoryId).update(dnaHitCount + ' ');
		$(this._trPreviousDnaHitCountList).next().remove();
		Element.insert(this._trPreviousDnaHitCountList, {after: dnaHitCountList});
		
		if (dnaHitCount > 0) {
			$(this.linkToDnaDetailListId).show();
		}
	}
};
