var MovieSearch = Class.create({
	initialize: function(){
		var self = this;
		// 検索オプションエリアを開閉イベントの設定
		if($('search_option_open')){
			$('search_option_open').observe('click', self.optionOpen.bind(self));
		}

		if($('search_option_close')){
			$('search_option_close').observe('click', self.optionClose.bind(self));
		}

		if($('btn_search_submit')){
			$('btn_search_submit').observe('click', self.chkSearchConditions.bind(self));
		}
		
		if($('btn_reset_search')){
			$('btn_reset_search').observe('click', self.resetSearchConditions.bind(self));
		}
		
		$('search_keyword').observe('blur', self.chkActivateSubmitBtn.bind(self));
		
		// 撮影地選択のイベントの設定
//		this.setLocationEvent();
		
		// カメラワーク群選択のイベントを設定
		$w('camerawork camerapoint cameraangle cameramove camerashutter').each(function(cw_name){
			$$('#option_' + cw_name + ' li').each(function(cw){
				cw.observe('click', self.selectCameraWorks.bindAsEventListener(self, cw_name));
			});
		});
		
		// 季節選択のイベントを設定
		$$('#RecSeason li').each(function(s){
			s.observe('click', self.selectSeason.bind(self));
		});
		
		// フォーマット選択のイベントの設定
		$$('#roll_format li').each(function(f){
			f.observe('click', self.selectFormat.bind(self));
		});
		
		// 最初から検索条件が設定されてたら、検索ボタンをアクティブにしとく
		this.chkActivateSubmitBtn();
	},
	/**
	 * 撮影地選択のイベントを設定する
	 */
//	setLocationEvent: function(){
//		var self = this;
//		$$('#location_cat_1 li').each(function(lc_1){
//			lc_1.observe('click', self.chgLocation2.bind(self));
//		});
//		if($('location_cat_2')){
//			$('location_cat_2').observe('change', self.chgLocation3.bind(self));
//		}
//
//		if($('location_cat_3')){
//			$('location_cat_3').observe('change', self.setLocation3.bind(self));
//		}
//	},
	/**
	 * 検索オプションエリアを開く
	 */
	optionOpen: function(event){
		event.element().visualEffect('BlindUp', {
			afterFinish: function(){
				$('search_option_area').visualEffect('BlindDown');
			}
		});
		
	},
	/**
	 * 検索オプションエリアを閉じる
	 */
	optionClose: function(event){
		$('search_option_area').visualEffect('BlindUp', {
			afterFinish: function(){
				$('search_option_open').visualEffect('BlindDown');
			}
		});
	},
	/**
	 * 検索条件をチェックする
	 */
	chkSearchConditions: function(event){
		var default_cond = $('search_keyword', 'h_category', 'h_location').all(function(c){
			return (c.getValue() == '');
		});
		if(default_cond){
			alert('検索条件を設定してください');
		}else{
			// 検索条件を整えて、検索画面へ遷移させる
			// オプションを取得する
			var cameraworks = [];
			$w('camerawork camerapoint cameraangle cameramove camerashutter').each(function(cw_name){
				var value = $('h_' + cw_name).getValue();
				if(value.length != 0){
					value.split(',').each(function(keywordId){
						cameraworks.push(keywordId);
					});
				}
			});
			var params = $('form_search').serialize();
			var cameraworks_params = '';
			if(cameraworks.size() > 0){
				cameraworks_params = cameraworks.join('&keywordId[]=');
			}
			if(cameraworks_params.length > 0){
				params += '&keywordId[]=' + cameraworks_params;
			}
			location.href = $('form_search').action + '?' + params;
		}
	},
	/**
	 * 撮影地（中カテゴリ）を更新する
	 */
//	chgLocation2: function(event){
//		var selected_lc_1 = event.element();
//		var location_id = selected_lc_1.id.split('_')[1];
//		$('h_location').setValue(location_id);
//		$$('#location_cat_1 li').each(function(lc_1){
//			if(lc_1.id == selected_lc_1.id){
//				lc_1.addClassName('active');
//			}else{
//				lc_1.removeClassName('active');
//			}
//		});
//		
//		new Ajax.Request('/keyword/getLocation.html', {
//			method: 'post',
//			parameters: 'locationId='+ location_id,
//			onSuccess: function(http){
//				$('location_cat_2').update(http.responseText);
//				$('location_cat_3').update('');
//			},
//			onFailure: function(http){
//				alert(http.responseText);
//			}
//		});
//	},
	/**
	 * 撮影地（小カテゴリ）を更新する
	 */
//	chgLocation3: function(event){
//		var location_id = event.element().getValue();
//		$('h_location').setValue(location_id);
//		new Ajax.Request('/keyword/getLocation.html', {
//			method: 'post',
//			parameters: 'locationId='+ location_id,
//			onSuccess: function(http){
//				$('location_cat_3').update(http.responseText);
//			},
//			onFailure: function(http){
//				alert(http.responseText);
//			}
//		});
//	},
	/**
	 * 撮影地（小カテゴリ）の値をh_locationに設定する
	 */
//	setLocation3: function(event){
//		var location_id = event.element().getValue();
//		$('h_location').setValue(location_id);
//	},
	/**
	 * カメラワーク群を設定する
	 */
	selectCameraWorks: function(event){
		var cameraworks = arguments[1];
		var element = event.element();
		if(element.id ==  cameraworks + '_CLEAR'){
			// 設定なしボタン
			$$('#option_' + cameraworks + ' li').each(function(cw){
				cw.removeClassName('active');
			});
			element.addClassName('active');
			$('h_' + cameraworks).setValue('');
		}else{
			// それ以外
			$(cameraworks + '_CLEAR').removeClassName('active');
			element.toggleClassName('active');
			var cw_active = $$('#option_' + cameraworks + ' li').findAll(function(cw){ return cw.hasClassName('active'); });
			if(cw_active.size() > 0){
				var keyword_ids = [];
				cw_active.each(function(cw){
					keyword_ids.push(cw.id.split('_')[1]);
				});
				$('h_' + cameraworks).setValue(keyword_ids.join(','));
			}else{
				// activeが1つもない
				$(cameraworks + '_CLEAR').addClassName('active');
				$('h_' + cameraworks).setValue('');
			}
		}
	},
	/**
	 * 季節を設定する
	 */
	selectSeason: function(event){
		var element = event.element();
		var selectId = element.id;
		var selectValue = selectId.split('_');
		$$('#RecSeason li').each(function(elm){
			if(selectId == elm.id){
				elm.addClassName('active');
			}else{
				elm.removeClassName('active');
			}
		});
		if(selectValue[1] == 'CLEAR'){
			$('h_season').setValue('');
		}else{
			$('h_season').setValue(selectValue[1]);
		}
	},
	/**
	 * 季節の設定をクリアする
	 */
	clearSeason: function(){
		$('h_season').setValue('');
		$$('#RecSeason li').each(function(elm){
			elm.removeClassName('active');
		});
		$('RecSeason_CLEAR').addClassName('active');
	},
	/**
	 * フォーマットを設定する
	 */
	selectFormat: function(event){
		var element = event.element();
		var selectId = element.id;
		var selectValue = selectId.split('_');
		$$('#roll_format li').each(function(elm){
			if(selectId == elm.id){
				if(elm.hasClassName('active')){
					elm.removeClassName('active');
					$('h_format').value = '';
				}else{
					elm.addClassName('active');
					$('h_format').value = selectValue[2];
				}
			}else{
				elm.removeClassName('active');
			}
		});
	},
	/**
	 * フォーマットの設定をクリアする
	 */
	clearFormat: function(){
		$('h_format').value = '';
		$$('#roll_format li').each(function(elm){
			elm.removeClassName('active');
		});
	},
	/**
	 * 検索ボタンをアクティブにするか確認する
	 */
	chkActivateSubmitBtn: function(){
		var default_cond = $('search_keyword', 'h_category', 'h_location').all(function(c){
			return (c.getValue() == '');
		});
		if(default_cond){
			$('btn_search_submit').removeClassName('active');
		}else{
			$('btn_search_submit').addClassName('active');
		}
	},
	/**
	 * カテゴリ、撮影地が検索条件に含まれている場合、メッセージを表示する
	 */
	chkResultText: function(){
		var cat_loc_cond = $('h_category', 'h_location').all(function(c){
			return (c.getValue() == '');
		});
		if(cat_loc_cond){
			$('selected_result').removeClassName('result_text');
		}else{
			$('selected_result').addClassName('result_text');
		}
	},
	/**
	 * 検索条件を全てリセットする
	 */
	resetSearchConditions: function(event){
		this.clearNormalConditions();
		this.clearCameraOption();
		this.clearOtherOption();
	},
	/**
	 * ノーマルの検索条件をクリアする
	 */
	clearNormalConditions: function(){
		// ノーマル
		$('search_keyword', 'h_category', 'h_location').each(function(c){
			c.setValue('');
		});
		$('selected_category', 'selected_location').each(function(c){
			c.update('');
			c.hide();
		});
		this.chkResultText();
		this.chkActivateSubmitBtn();
	},
	/**
	 * オプションの検索条件のカメラ情報をクリアする
	 */
	clearCameraOption: function(){
		// カメラ情報
		$w('camerawork camerapoint cameraangle cameramove camerashutter').each(function(cw_name){
			$('h_' + cw_name).setValue('');
			$$('#option_' + cw_name + ' li').each(function(cw){
				cw.removeClassName('active');
			});
			$(cw_name + '_CLEAR').addClassName('active');
		});
	},
	/**
	 * オプションの検索条件のカメラ情報以外をクリアする
	 */
	clearOtherOption: function(){
		// その他
		$('RecTimePeriod', 'RecWeather', 'TimePivot').each(function(other){
			other.setValue('');
		});
		this.clearSeason();
		this.clearFormat();
	}
});

/**
 * 地図から選択用コールバック関数
 * @param selected_locations
 * @return
 */
function setSearchLocation(selected_locations){
	var location_names = [];
//	var location_ids = [];
	$R(1,3).each(function(idx){
		if(selected_locations[idx -1]['id']){
//			location_ids.push(selected_locations[idx - 1]['id']);
			// 最も小さい撮影地のキーワードIDを設定
			$('h_location').setValue(selected_locations[idx - 1]['id']);
			location_names.push(selected_locations[idx - 1]['keyword']);
		}
	});
	
//	// 世界・日本の選択状態を変更
//	var location_1_id = location_ids[0];
//	$$('#location_cat_1 li').each(function(location_1){
//		if(location_1.id == 'location_'+location_1_id){
//			location_1.addClassName('active');
//		}else{
//			location_1.removeClassName('active');
//		}
//		// プルダウンがなくなるので、イベント解除
//		location_1.stopObserving();
//	});
//	
//	// location_2を置換
//	if(location_html[1]){
//		$('location_cat_2').replace('<p id="location_cat_2">' + location_html[1] + '</p>');
//	}else{
//		$('location_cat_2').replace('<p id="location_cat_2"></p>');
//	}
//	// location_3を置換
//	if(location_html[2]){
//		$('location_cat_3').replace('<p id="location_cat_3">' + location_html[2] + '</p>');
//	}else{
//		$('location_cat_3').replace('<p id="location_cat_3"></p>');
//	}
	
	$('selected_location').update(location_names.join(' &gt; '));
	$('selected_location').show();
	// 検索ボタンの状態を変更する
	movieSearch.chkActivateSubmitBtn();
	movieSearch.chkResultText();
}
/**
 * カテゴリを選択用コールバック関数
 * @param selected_categories
 * @return
 */
function setSearchCategory(selected_categories){
	var category_names = [];
	$R(1,3).each(function(idx){
		if(selected_categories[idx - 1]){
			$('h_category').setValue(selected_categories[idx - 1]['id']);
			category_names.push(selected_categories[idx - 1]['keyword']);
		}
	});
	$('selected_category').update(category_names.join(' &gt; '));
	$('selected_category').show();
	// 検索ボタンの状態を変更する
	movieSearch.chkActivateSubmitBtn();
	movieSearch.chkResultText();
}


var movieSearch;
document.observe('dom:loaded', function(event){
	if($('form_search')){
		movieSearch = new MovieSearch();
	}
});


// 要望を送る(送信)ボタンが押された時
function easySearchSubmit()
{
	if ($('easy_search_keyword').value == "") {
		alert('キーワードを入力して下さい。');
		return false;
	}
	
	return true;
}
