// @author kimura
// 必要なライブラリ
// prototype-1.6.0.3-alt4.js
var ResourceListAction = Class.create();
ResourceListAction.prototype = {
	
	// コンストラクター
	initialize: function(page, loadingMessage, errorMessage) {
		// ユーザー設定項目
		// リソースのヒット数の挿入先id
		this.resourceHitCountInListId = 'resourceHitCount';
		// リソースのページャーテーブルのid
		this.resourcePagerTableId = 'pager';
		// リソースのリストの挿入先id
		this.resourceListId = 'resourceListByAjax';
		// リソースの下部ページャーテーブルのid
		this.resourceBottomPagerTableId = 'bottomPager';
		// ソートオプション全体のid
		this.sortByDropdownId = 'sortByDropdown';
		// ソートオプションが可能な最大検索結果数
		this.sortEnableMaxHitCount = 10000;
		
		// 読み込み時のnow loading...画像ファイルのパス
		this.loadingImgBar = 'images/ajaxLoadingBar_dedede.gif';
		this.loadingImgCircle = 'images/ajaxLoadingCircle.gif';
		
		// リクエストを送信する方法
		this.requestMethod = 'post';
		// AJAX用のリクエストを送信するURI
		this.requestURI = 'ajaxResourceListAction.do';
		// ローディング中のメッセージ
		this.loadingMessage = loadingMessage;
		// エラーメッセージ
		this.errorMessage = errorMessage;
		
		this._processInitialize(page);
	},
	
	// インスタンス化時の処理を行います。
	_processInitialize: function(page) {
		// システム設定項目
		// ページ数
		this._page = 1;
		// ページャーのPageCountLinkの挿入先オブジェクト
		this._pageCountLink;
		// ページャーのHitItemCountの挿入先オブジェクト
		this._hitItemCount;
		// ページャーのShowAllItemsの挿入先オブジェクト
		this._showAllItems;
		// 下部ページャーの挿入先オブジェクト
		this._bottomPager;
		// ソートオプションフォームオブジェクト
		this._sortByDropdown;
		// ソートオプションオブジェクト
		this._sortByDropdownSelect;
		
		if (!this.resourceHitCountInListId) {throw 'Error: Not found the resourceHitCountInListId.';}
		if (!this.resourcePagerTableId) {throw 'Error: Not found the resourcePagerTableId.';}
		if (!this.resourceListId) {throw 'Error: Not found the resourceListId.';}
		if (!this.resourceBottomPagerTableId) {throw 'Error: Not found the resourceBottomPagerTableId.';}
		
		if (page && !isNaN(parseInt(page))) {
			this._page = parseInt(page);
		}
		
		this._pageCountLink = $(this.resourcePagerTableId).select('td.pageCountLink');
		this._pageCountLink = this._pageCountLink[0];
		this._hitItemCount = $(this.resourcePagerTableId).select('td.hitItemCount');
		this._hitItemCount = this._hitItemCount[0];
		this._showAllItems = $(this.resourcePagerTableId).select('td.showAllItems');
		this._showAllItems = this._showAllItems[0];
		this._bottomPager = $(this.resourceBottomPagerTableId).select('td.pageLink');
		this._bottomPager = this._bottomPager[0];
		
		if (!this._pageCountLink) {throw 'Error: Not found the _pageCountLink.'};
		if (!this._hitItemCount) {throw 'Error: Not found the _hitItemCount.'};
		if (!this._showAllItems) {throw 'Error: Not found the _showAllItems.'};
		if (!this._bottomPager) {throw 'Error: Not found the _bottomPager.'};
		
		this._sortByDropdown = $(this.sortByDropdownId).select('form');
		this._sortByDropdown = this._sortByDropdown[0];
		
		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() {
		this._createHitCountLoading(this.resourceHitCountInListId);
		this._createResourceListLoading(this.resourceListId);
	},
	
	// ヒット数の読み込み中...を作成します。
	_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': '15px',
				'marginRight': '0px',
				'marginTop': '0px',
				'overflow': 'hidden',
				'paddingBotton': '0px',
				'paddingLeft': '30px',
				'paddingRight': '0px',
				'paddingTop': '0px',
				'width': '705px'
		});
	},
	
	
	// リクエストが失敗した時の処理を行います。
	_processFailure: function(response) {
		this._displayErrorMessage(this.resourceListId);
	},	

	// http通信の途中でエラーが発生した時の処理を行います。
	_processException: function(response, ex) {
		this._displayErrorMessage(this.resourceListId);
	},
	
	// エラーメッセージを表示します。
	_displayErrorMessage: function(resourceListId) {
		$(this.resourceHitCountInListId).writeAttribute('style', '');
		$(this.resourceHitCountInListId).update(' ');
		
		$(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 += "&page=" + encodeURIComponent(this._page);
		
		return param;
	},
	
	// リクエストが返ってきた時の処理を行います。
	_processSuccess: function(xmlHttpRequest) {
		var responseXML = xmlHttpRequest.responseText;
		responseXML = responseXML.replace(/[\r\n|\n]/g, '');
		
		var resource = responseXML.match(/<resource>(.*)<\/resource>/);
		var hitCount = resource[1].match(/<hitCount>(.*)<\/hitCount>/);
		var sortOption = resource[1].match(/<sortOption>(.*)<\/sortOption>/);
		var pageCountLink = resource[1].match(/<pageCountLink>(.*)<\/pageCountLink>/);
		var hitItemCount = resource[1].match(/<hitItemCount>(.*)<\/hitItemCount>/);
		var showAllItems = resource[1].match(/<showAllItems>(.*)<\/showAllItems>/);
		var list = resource[1].match(/<resourceList>(.*)<\/resourceList>/);
		var bottomPager = resource[1].match(/<bottomPager>(.*)<\/bottomPager>/);
		
		this._insertResourceList(hitCount[1], pageCountLink[1], hitItemCount[1], showAllItems[1], list[1], bottomPager[1]);
		this._enbaleSortByDropdown(sortOption[1], hitCount[1]);
	},
	
	// リソースリストを挿入します。
	_insertResourceList: function(hitCount, pageCountLink, hitItemCount, showAllItems, list, bottomPager) {
		$(this.resourceHitCountInListId).writeAttribute('style', '');
		$(this.resourceHitCountInListId).update(hitCount + ' ');
		
		$(this._pageCountLink).update(pageCountLink);
		$(this._hitItemCount).update(hitItemCount);
		$(this._showAllItems).update(showAllItems);
		
		Element.replace(this.resourceListId, list);
		
		$(this._bottomPager).update(bottomPager);
	},
	
	// ソートオプションを表示します。
	_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();
	}
};