jQuery(function(){
	jQuery.extend({
		/**
		 * インタフェースパーツ設定
		 *
		 * @param	void
		 * @return	void
		 */
		set_interface_parts: function(){
			/* ##### クリッカブルテーブル設定 ##### */
			jQuery('.clickable').hover(function(){
				jQuery(this).addClass('col');
			},
			function(){
				jQuery(this).removeClass('col');
			});
			jQuery('.clickable_closed').hover(function(){
				jQuery(this).addClass('col_closed');
			},
			function(){
				jQuery(this).removeClass('col_closed');
			});
			// クリック時遷移先
			/*
			// クリック時遷移先 クリック時は遷移しないことにしました。
			jQuery('.clickable').click(function(e){
				if($(e.target).hasClass('disabled')) return;
				var targetURI = jQuery(this).find('a:first').attr('href');
				location.href = targetURI;
				return false;
			});
			*/
			jQuery('.clickable_closed').click(function(e){
				if($(e.target).hasClass('disabled')) return;
				var targetURI = jQuery(this).find('a:first').attr('href');
				location.href = targetURI;
				return false;
			});
			/* ##### パーツ設定 ##### */
			// マイリストボタン
			jQuery('.add_my_list_trigger').click(function(e){
				e.stopPropagation();
				var tmp = jQuery(this).attr('rel').split('/');
				// リクエスト先URL
				var targetURI = '/json_datas/add_my_list';
				targetURI += '/' + tmp[0] + '/' + tmp[1];
				// HTTPリクエスト
				var response = jQuery.execute_command(targetURI);
				if(eval(response.responseText) == true){
					alert('マイリストに追加しました');
				}
			});
			// 問い合わせボタン
			jQuery('.add_contact_trigger').click(function(e){
				e.stopPropagation();
				var tmp = jQuery(this).attr('rel').split('/');
				// 遷移先URL
				var targetURI = '/contact_item/index';
				targetURI += '/' + tmp[0] + '/' + tmp[1];
				// 遷移
				location.href = targetURI;
			});
			// 友達へ送るボタン
			jQuery('.add_friends_trigger').click(function(e){
				e.stopPropagation();
				var tmp = jQuery(this).attr('rel').split('/');
				// 遷移先URL
				var targetURI = '/friends_item/index';
				targetURI += '/' + tmp[0] + '/' + tmp[1];
				// 遷移
				location.href = targetURI;
			});
			// 空き待ちへ送るボタン
			jQuery('.add_empty_trigger').click(function(e){
				e.stopPropagation();
				var tmp = jQuery(this).attr('rel').split('/');
				// 遷移先URL
				var targetURI = '/contact_empty/index';
				targetURI += '/' + tmp[0] + '/' + tmp[1];
				// 遷移
				location.href = targetURI;
			});

			// チェックボックス(イベント無効化)
			jQuery('.clickable').find('input').click(function(e){
				e.stopPropagation();
			});
			// チェックボックス動作
			jQuery('.checkbox_trigger').click(function(){
				if(jQuery(this).attr('checked') == true){
					jQuery(this).parents('.checkbox_group').find('.checkbox_target').attr('checked', true);
					jQuery(this).parents('.checkbox_group').find('.checkbox_trigger').attr('checked', true);
				}else{
					jQuery(this).parents('.checkbox_group').find('.checkbox_target').attr('checked', false);
					jQuery(this).parents('.checkbox_group').find('.checkbox_trigger').attr('checked', false);
				}
			});
			// お気に入り検索条件に追加
			jQuery('.add_my_condition_trigger').click(function(){
				var tmp = jQuery(this).attr('rel').split('/');
				// リクエスト先URL
				var targetURI = '/json_datas/add_my_condition';
				targetURI += '/' + tmp[0] + '/' + encodeURI(tmp[1]);

				// HTTPリクエスト
				var response = jQuery.execute_command(targetURI);
				if(eval(response.responseText) == true){
					alert('お気に入り検索条件に追加しました');
				}
			});
			// まとめてマイリストに追加
			jQuery('.add_my_list_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					var items = [];
					jQuery(target).each(function(){
						items.push(jQuery(this).val());
					});
					items = items.join(',');
					// リクエスト先URL
					var targetURI = '/json_datas/add_my_list';
					targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
					// HTTPリクエスト
					var response = jQuery.execute_command(targetURI);
					if(eval(response.responseText) == true){
						alert('マイリストに追加しました');
					}
				}
			});
			// まとめて問い合わせ
			jQuery('.add_contact_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					var items = [];
					jQuery(target).each(function(){
						items.push(jQuery(this).val());
					});
					items = items.join(',');
					// 遷移先URL
					var targetURI = '/contact_item/index';
					targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
					// 遷移
					location.href = targetURI;
				}
			});
			// まとめて友達に送る
			jQuery('.add_friends_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					var items = [];
					jQuery(target).each(function(){
						items.push(jQuery(this).val());
					});
					items = items.join(',');
					// 遷移先URL
					var targetURI = '/friends_item/index';
					targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
					// 遷移
					location.href = targetURI;
				}
			});
			// まとめてのまとめて
			// まとめてマイリストに追加
			jQuery('.add_my_list_all_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_all_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					var items = [];
					jQuery(target).each(function(){
						items.push(jQuery(this).val());
					});
					items = items.join(',');
					// リクエスト先URL
					var targetURI = '/json_datas/add_my_list';
					targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
					// HTTPリクエスト
					var response = jQuery.execute_command(targetURI);
					if(eval(response.responseText) == true){
						alert('マイリストに追加しました');
					}
				}
			});
			// まとめて問い合わせ
			jQuery('.add_contact_all_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_all_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					var items = [];
					jQuery(target).each(function(){
						items.push(jQuery(this).val());
					});
					items = items.join(',');
					// 遷移先URL
					var targetURI = '/contact_item/index';
					targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
					// 遷移
					location.href = targetURI;
				}
			});
			// ページネートリミット
			jQuery('.change_limit_trigger').change(function(){
				if(jQuery(this).val() != ''){
					location.href = jQuery(this).attr('rel') + '/limit:' + jQuery(this).val();
				}
			});

			jQuery('.show_toggle_btn').click(function(){
				var t = jQuery(this).attr('rel');
				jQuery('#'+ t).slideToggle(500);
				return false;
			});
		},
		/**
		 * 削除系インタフェースパーツ設定
		 *
		 * @param	void
		 * @return	void
		 */
		set_remove_parts: function(){
			// マイリスト削除
			jQuery('.remove_favorite_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					if(window.confirm('選択項目をマイリストから削除します。\nよろしいですか？')){
						var items = [];
						jQuery(target).each(function(){
							items.push(jQuery(this).val());
						});
						items = items.join(',');
						// リクエスト先URL
						var targetURI = '/json_datas/remove_my_list';
						targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
						// HTTPリクエスト
						var response = jQuery.execute_command(targetURI);
						if(eval(response.responseText) == true){
							alert('選択項目をマイリストから削除しました。');
							jQuery(target).attr('checked', false);
							jQuery(this).parents('.checkbox_group').find('.checkbox_trigger:checked').attr('checked', false);
							window.location.reload();
						}
					}
				}
			});
			// 閲覧履歴削除
			jQuery('.remove_hiostory_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					if(window.confirm('選択項目を物件閲覧履歴から削除します。\nよろしいですか？')){
						var items = [];
						jQuery(target).each(function(){
							items.push(jQuery(this).val());
						});
						items = items.join(',');
						// リクエスト先URL
						var targetURI = '/json_datas/remove_history';
						targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
						// HTTPリクエスト
						var response = jQuery.execute_command(targetURI);
						if(eval(response.responseText) == true){
							alert('選択項目を物件閲覧履歴から削除しました。');
							jQuery(target).attr('checked', false);
							jQuery(this).parents('.checkbox_group').find('.checkbox_trigger:checked').attr('checked', false);
							window.location.reload();
						}
					}
				}
			});
			// お気に入り検索条件削除
			jQuery('.remove_tag_group_trigger').click(function(){
				var target = jQuery(this).parents('.checkbox_group').find('.checkbox_target:checked');
				if(target.length < 1){
					alert('項目が選択されていません');
				}else{
					if(window.confirm('選択項目をお気に入り検索条件から削除します。\nよろしいですか？')){
						var items = [];
						jQuery(target).each(function(){
							items.push(jQuery(this).val());
						});
						items = items.join(',');
						// リクエスト先URL
						var targetURI = '/json_datas/remove_tag';
						targetURI += '/' + jQuery(this).attr('rel') + '/' + encodeURI(items);
						// HTTPリクエスト
						var response = jQuery.execute_command(targetURI);
						if(eval(response.responseText) == true){
							alert('選択項目をお気に入り検索条件から削除しました。');
							jQuery(target).attr('checked', false);
							jQuery(this).parents('.checkbox_group').find('.checkbox_trigger:checked').attr('checked', false);
							window.location.reload();
						}
					}
				}
			});
		},
		/**
		 * Ajaxリクエスト送信
		 *
		 * @param	String requestURI リクエスト先URI
		 * @return	Object レスポンステキスト
		 */
		execute_command: function(requestURI){
			return jQuery.ajax({
				'url':			requestURI,
				'cache':		false,
				'dataType':		'json',
				'async':		false,
				'error':		function(){return false;}
			});
		},
		/**
		 * チェックリスト生成(エリア)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_check_area_if_list: function(HTTP_RESPONSE){
			var append_child_node = '';
			jQuery(HTTP_RESPONSE).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);">' + this.City.name + '</a><input type="hidden" name="data[Member][request_area][' + this.City.id + ']" value="' + this.City.id + '" /></li>' + "\n";
			});
			window.parent.jQuery('#check_if_list_area').append(append_child_node);
			window.parent.tb_remove();
		},
		/**
		 * チェックリスト生成(路線)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_check_station_if_list: function(HTTP_RESPONSE){

			var append_child_node = '';
			var data = eval("("+HTTP_RESPONSE+")");
			jQuery(data["route"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Member][request_station][' + this.id + ']" value="' + this.name + '" id="route'+this.id+'"/></li>' + "\n";
			});
			jQuery(data["station"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Member][request_station][' + this.id + ']" value="' + this.name + '" id="station'+this.id+'" /></li>' + "\n";
			});
			window.parent.jQuery('#check_if_list_station li').remove();
			window.parent.jQuery('#check_if_list_station').append(append_child_node);
			window.parent.tb_remove();

		},
		/**
		 * チェックリスト生成(こだわり条件)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_check_condition_if_list : function(HTTP_RESPONSE){
		    	var append_child_node = '';
			var data = eval("("+HTTP_RESPONSE+")");
			jQuery(data["request_building_condition"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Member][request_building_condition][' + this.id + ']" value="' + this.name + '" id="MemberRequestBuildingCondition'+this.id+'" /></li>' + "\n";
			});
			jQuery(data["request_room_condition"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Member][request_room_condition][' + this.id + ']" value="' + this.name + '" id="MemberRequestRoomCondition'+this.id+'" /></li>' + "\n";
			});
			window.parent.jQuery('#check_request_condition li').remove();
			window.parent.jQuery('#check_request_condition').append(append_child_node);
			window.parent.tb_remove();
		},
		/**
		 * チェックリスト生成(エリアを選ぶ)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_select_area_city : function(HTTP_RESPONSE){
		    	var append_child_node = '';
			var data = eval("("+HTTP_RESPONSE+")");
			jQuery(data["area"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][area][' + this.id + ']" value="' + this.name + '" id="area'+this.id+'" /></li>' + "\n";
			});
			jQuery(data["city"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][city][' + this.id + ']" value="' + this.name + '" id="city'+this.id+'" /></li>' + "\n";
			});
			window.parent.jQuery('#check_area_city li').remove();
			window.parent.jQuery('#check_area_city').append(append_child_node);
			window.parent.tb_remove();
		},
		/**
		 * チェックリスト生成(沿線・駅を選ぶ)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_select_route_station: function(HTTP_RESPONSE){
		    	var append_child_node = '';
			var data = eval("("+HTTP_RESPONSE+")");
			jQuery(data["route"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][route][' + this.id + ']" value="' + this.name + '" id="route'+this.id+'"/></li>' + "\n";
			});
			jQuery(data["station"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][station][' + this.id + ']" value="' + this.name + '" id="station'+this.id+'" /></li>' + "\n";
			});
			window.parent.jQuery('#check_route_station li').remove();
			window.parent.jQuery('#check_route_station').append(append_child_node);
			window.parent.tb_remove();
		},
		/**
		 * チェックリスト生成(沿線・駅を選ぶ)
		 *
		 * @param	Object HTTP_RESPONSE JSONデータ
		 * @return	void
		 */
		set_select_school: function(HTTP_RESPONSE){
		    	var append_child_node = '';
			var data = eval("("+HTTP_RESPONSE+")");

			console.log(data);

			jQuery(data["name_junior_school"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][name_junior_school][' + this.id + ']" value="' + this.name + '" id="name_junior_school'+this.id+'" /></li>' + "\n";
			});
			jQuery(data["name_junior_high_school"]).each(function(){
				append_child_node += '<li><a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">' + this.name + '</a><input type="hidden" name="data[Item][name_junior_high_school][' + this.id + ']" value="' + this.name + '" id="name_junior_high_school'+this.id+'" /></li>' + "\n";
			});
			window.parent.jQuery('#check_school li').remove();
			window.parent.jQuery('#check_school').append(append_child_node);
			window.parent.tb_remove();
		},
		/**
		 * チェックリスト削除
		 *
		 * @param	Object TGT_ELEM 削除対象エレメント
		 * @return	void
		 */
		remove_check_if_list: function(TGT_ELEM){
			jQuery(TGT_ELEM).parent('li').remove();
			return false;
		}


		/**
		 * map_sub用
		 * tickboxのiframe内でのみ使用
		 * [使用箇所]
		 *   /check_boxes/map_sub	map_sub.ctp内バッファ書き込み
		 */
		//setup_map_sub_selecter : function(){
			//window.parent.$("#search_param_lists :hidden").each(function(index,elem){
				//var $t = $("#" +$(elem).attr("id") );
				//if($t.is(":checkbox")){
					//$t.attr("checked",true);
				//}else if($t.is(":text")){
					//$t.val($(elem).val());
				//}else if($t.is("select")){
					//$t.find("option[value="+$(elem).val()+"]").attr("selected","selected");
				//}
			//});
//
			//$(".add_select_sub_map_trigger").click(function(){
				//var $target = window.parent.$("#search_param_lists").empty();
				//var html = "";
				//var $request = $("#request_article_type :checked");
				//if($request.size()){
					//html = "";
					//$request.each(function(){
						//var $t = $(this);
						//var id = this.id;
						//html += getListElement(id,$t.attr("name"),$t.val(),$("label[for="+id+"]").text());
					//});
					//$target.append(getListContainerElement("物件種別",html))
				//};
				//
				//var $request = $("#request_budget :selected");
				//if($request.size()){
					//html = "";
					//html += getListElement(this.id,$(this).val());
					//$target.append(getListContainerElement("物件種別",html))
				//}
//
				//$(":checked,:selected").each(function(){
					//$target.append("<li>"+this.id + $(this).val()+"</li>");
				//});
				//$(":text").each(function(){
					//if($(this).val() != "") $target.append("<li>"+this.id + $(this).val()+"</li>");
				//});
				//window.parent.tb_remove();
				//return false;
			//})
			//
			//function getListContainerElement(name,content){
				//var html = '<dl>';
				//html += '<dt>'+name+'</dt>';
				//html += '<dd>';
				//html += '<ul class="sea_ul01 clearFix check_if_list">';
				//html += content;
				//html += '</ul>';
				//html += '</dd>';
				//html += '</dl>';
				//return html;
			//}
			//
			//function getListElement(id,name,value,label) {
				//var html = '';
				//html += '<li>'
				//html += '<a href="javascript:void(0);" onclick="javascript:jQuery.remove_check_if_list(this);" title="条件からはずす">'
				//html += label;
				//html += '</a>'
				//html += '<input type="hidden" name="'+name+'" value="' + value + '" id="'+id+'" />'
				//html += '</li>'
				//return html;
			//}
//
		//}

	});
	// ページロード完了時トリガ
	jQuery(document).ready(function(){
		// インタフェースパーツ設定
		jQuery.set_interface_parts();
		jQuery.set_remove_parts();

		add_search_param_select = function(trigger, uri, data_ids, callback){
		    jQuery(trigger).click(function(){
			    var $target = jQuery('.add_select_target:checked');
			    if($target.length < 1){
				    alert('項目が選択されていません');
			    }else{
				    var items;
				    var i =0, len=data_ids.length;
				    for(; i< len; i++){
					items = [];
					jQuery(data_ids[i] + ' .add_select_target:checked').each(function(){
						items.push(jQuery(this).val());
					});
					uri += items.join(",") + ","+"/";
				    }
				    uri = uri.substr(0,uri.length-1);

				    jQuery.ajax({
					    'url' :		    uri,
					    'cache' :		false,
					    'dataType ':     'json',
					    'async' :		true,
					    'success' :		function(res){callback.apply(null, [res])},
					    'error' :		function(res){alert('通信エラーが発生しました');}
				    });
			    }
			    return false;
		    });
		}

		// 希望物件お問い合わせ 駅名を選ぶ
		add_search_param_select(
		    '.add_select_station_trigger',
		    '/json_datas/check_select_route/',
		    ['#select_route_list','#select_station_list'],
		    jQuery.set_check_station_if_list);


		// エリアを選ぶ
		add_search_param_select(
		    '.add_select_area_city_trigger',
		    '/json_datas/check_select_area/',
		    ['#select_area_list','#select_city_list'],
		    jQuery.set_select_area_city);

		// 沿線・駅を選ぶ
		add_search_param_select(
		    '.add_select_route_station_trigger',
		    '/json_datas/check_select_route/',
		    ['#select_route_list','#select_station_list'],
		    jQuery.set_select_route_station);

		// 学区を選ぶ
		add_search_param_select(
		    '.add_select_school_trigger',
		    '/json_datas/check_select_school/',
		    ['#select_elementary_school_list','#select_junior_high_school_list'],
		    jQuery.set_select_school);

		// こだわり条件
		add_search_param_select(
		    '.add_request_condition_trigger',
		    '/json_datas/check_request_condition/',
		    ['#request_building_condition','#request_room_condition'],
		    jQuery.set_check_condition_if_list);

	});

});



