//**********************************************************
//    検索結果画面に関するJavaScript
//            - require jquery.js
//            - require jquery.scrollTo-min.js
//                                    muraki@triax.jp
//**********************************************************

//--------------------------------------------------
// 
// サブミット時、範囲指定系項目（プルダウン）の
// 上限・下限の異常をチェックする関数
// 
//--------------------------------------------------
function regist_raise_range_error_event(form){
	// 「月額賃料下限」を含むformを対象とする
	$("form:has(#rent_a_min)").submit(function(event){
		var range_fields = [
			{prefix:'footprint_t', label:'面積'},
			{prefix:'footprint_m', label:'面積'},
			{prefix:'rent_t', label:'賃料'},
			{prefix:'rent_a', label:'賃料'}
		];
		var error_occured = false;
		$.each(range_fields, function(i, val){
			var min = $('#' + this.prefix + '_min').val();
			var max = $('#' + this.prefix + '_max').val();
			this.range_error = parseInt(min) > parseInt(max);
			// console.debug(this.range_error, ':', this.prefix, this.label);
			if(this.range_error){
				alert(this.label + 'の設定を変更してください');
				error_occured = true; // submitキャンセルのためのフラグ設定
				return false;
			}
		});
		if(error_occured)
			return false;
		
		// submit後に「共益費を含む」をenableに戻す補正
		cac_enable_on_submit();
		
		return true;
	});
}

//--------------------------------------------------
// 
// 検索フォームの開閉を行う関数
// 
//--------------------------------------------------
function openSearch(){
	$('.top_search').attr('src', '/images/narrow_down_opened.gif');
	$('.search_refine').slideDown('slow');
	return true;
}
function closeSearch(){
	$('.top_search').attr('src', '/images/narrow_down_out.gif');
	$('.search_refine').slideUp('slow');
	return true;
}
function regist_search_open_close_event(){
	$('.top_search').toggle(openSearch, closeSearch);
	$('.bottom_search').click(function(){
		var scroll_params = {
			speed: 'slow',
			axis: 'y',
			queue: true,
			onAfter: function(){
				if($('.search_refine').is(":hidden"))
					$('.top_search').click();
			}
		};
		$.scrollTo($('#rMainArea'), scroll_params); // コンテンツのトップへ移動
	});
}

//--------------------------------------------------
//
// 以下の要素の背景色をつける関数
// - 選択されているラジオボタン・チェックボックスを囲むspan/td
// - 選択されているセレクトボックス自信
//
//--------------------------------------------------
function change_form_background(){
	if($(this).is(":radio")){
		var radio_button_name = $(this).attr("name");
		$("[name=" + radio_button_name + "]").each(function(){
			$(this).closest("span,td").toggleClass("checked", $(this).attr("checked"));
		});
	}else if($(this).is(":checkbox")){
		$(this).closest("span,td").toggleClass("checked", $(this).attr("checked"));
	}else if($(this).is("select")){
		$(this).toggleClass("checked", $(this).val() != '');
	}
	return true;
}

//--------------------------------------------------
//
// 検索結果の4件以上の募集区画の表示・非表示を
// トグルするイベントの登録
//
//--------------------------------------------------
function regist_property_list_toggle_event(property_display_count){
	// greater thanなので-1&ヘッダーカラムを含むので+1;
	var bound_num = property_display_count -1 + 1;
	
	// 建物のループ
	$(".property_info").each(function(){
		var links = $(this).find(".property_show_link,.property_hide_link");
		if(links.size() == 0)
			return true; // continue loop
		
		var table = $(this).find(".office_detail");
		var p = $(this).find(".property_link_wrap");
		
		// イベント登録
		links.bind("click", function(event){
			var tr = table.find("tr:gt(" + bound_num + ")")
			// 募集の表の表示・非表示をトグル
			if(tr.css("display") == "none")
				tr.show();
			else
				tr.hide();
			$(p).toggle(0); // リンクの表示・非表示をトグル
			return false;
		});
	});
	
	// クリックイベント発火：テンプレート側でやるので使わない
	// $(".property_hide_link").click();
}

//-------------------------------------------------
//
// １ページあたりの表示件数を変更する処理
//
//-------------------------------------------------
function regist_num_of_page_event(){
	$(".num_of_page:select").change(function(){
		$("form.search").submit();
	});
}

//-------------------------------------------------
//
// エリアコード用のhiddenに値を設定
//
//-------------------------------------------------
function set_area_code_hidden_field(){
	$(".area_code_hidden:hidden").each(function(){
		$(this).val($(this).attr("id"));
	});
}

//-------------------------------------------------
//
// 他のINPUTフィールドに連動して状態が変わる
// ラジオボタンの登録
//
//-------------------------------------------------
var target_elem_list = [
	{from_elem:"#footprint_t_min,#footprint_t_max", target:"#footprint_type_t"},
	{from_elem:"#footprint_m_min,#footprint_m_max", target:"#footprint_type_m"},
	{from_elem:"#rent_t_min,#rent_t_max,#cac_t_flg", target:"#rent_type_t"},
	{from_elem:"#rent_a_min,#rent_a_max,#cac_a_flg", target:"#rent_type_a"}
];

function regist_coupled_element_event(){
	$.each(target_elem_list, function(){
		var target = this.target;
		$(this.from_elem).click(function(){
			$(target).attr("checked", "checked");
		});
	});
}

//-------------------------------------------------
//
// ラジオボタンで選択されていない方の、
// 面積および賃料のINPUTフィールドをdisable状態にする
//
//-------------------------------------------------
function disable_unselect_element(){
	$.each(target_elem_list, function(){
		if($(this.target).attr("checked"))
			$(this.from_elem).removeAttr("disabled");
		else
			$(this.from_elem).attr("disabled", "disabled");
	});
}

//--------------------------------------------------
// 
// 賃料のdisable状態の方の「共益費を含む」チェックボックスが、
// submit後の画面でチェックが外れてしまわないよう、
// submit時にenable状態に戻す処理
// 
//--------------------------------------------------
function cac_enable_on_submit(){
	$("#cac_t_flg,#cac_a_flg").filter(":disabled").removeAttr("disabled");
}

// execute function on document ready
$(function(){
	// 選択されているフォーム要素に背景色をつける
	$(".office_search_refine :input").click(change_form_background);
	$(".office_search_refine :input").each(change_form_background);
	// 検索フォームの開閉
	regist_search_open_close_event();
	// 4件以上の募集区画の表示・非表示切り替え
	regist_property_list_toggle_event(3);
	// １ページあたりの表示件数を変更する処理
	regist_num_of_page_event();
	// エリアコード用のhiddenに値を設定
	set_area_code_hidden_field();
	// 範囲指定系項目（プルダウン）の上限・下限の異常チェック
	regist_raise_range_error_event();
	// 他のフィールドに連動して状態が変わるラジオボタンの登録
	regist_coupled_element_event();
	
	// 選択されていない方の面積・賃料のINPUTフィールドをdisable状態にする
	disable_unselect_element();
	$("#footprint_type_t,#footprint_type_m,#rent_type_t,#rent_type_a").click(disable_unselect_element);
});
