is_dong_click=0;
is_danzi_click=0;
is_subway_click=0;
$(document).ready(function(){
	auto=0;
		//키워드
	$(function() {
		$( "#keyword" ).autocomplete({
			autoFocus: true,
			delay:250,
			source: function( request, response ) {
				$.ajax( {
					url: "/address/get_address_json",
					dataType: "json",
					data: {
					term: request.term
				},
				success: function( data ) {
						response( data );
					}
				} );
			},focus: function(event,ui) {
				//event.preventDefault();
				return false;
			},
			select: function( event, ui ) {
				if($("#keyword").val()){
					$("#keyword").val( ui.item.title );
				}
				if(ui.item.id){
					if(ui.item.type=='address'){
						$('#address_id').val(ui.item.id);
					} else {
						$('#subway_id').val(ui.item.id);
					}


					$('#lat').val(ui.item.lat);
					$('#lng').val(ui.item.lng);
					auto=1;

					lat=ui.item.lat
					lng=ui.item.lng;
					setCookie('lat',lat,7);
					setCookie('lng',lng,7);
					setCookie('level',14,7);
											center='{"lat":'+lat+',"lng":'+lng+'}';
						map.panTo($.parseJSON(center));
						map.setZoom(14);

										$('#form_search').submit();
					return false;
				}
			}, close:function(){
				
			}, open:function(event, ui){
				auto=0;
			}, change: function (event, ui) {

			}
		}).autocomplete( "instance" )._renderItem = function( ul, item ) {
			return $( "<li>" )
			.append( item.title )
			.appendTo( ul );
		};
	});
	

	$(function(){
		$('#form_search').ajaxForm({
			beforeSerialize: function(){
								if(!auto){
					$('#address_id').val(0);
					$('#subway_id').val(0);
				}
							},
			beforeSubmit: function(){
				setTimeout(function(){
					$('#map_list_panel').scrollTop(0);
				},100)

				keyword=$('#keyword').val();
				if(keyword){
					if($.isNumeric(keyword) && keyword >= 10000){
												$.post('/item/item_chk',{id:keyword},function(data){
							if(data){
																	location.href='/item/view/'+keyword;
								
								return false;
							} else {
								alert('해당 번호의 매물이 없습니다.');
								return false;
							}
						})
						return false;
											}
				} else {
										$('#lat').val('');
					$('#lng').val('');
				}
			}, success: function(search_position){
				if(search_position=='main'){
											location.href='/search';
					
				} else if(search_position=='map'){
					console.log('bbb');
					get_position();
				} else if(search_position=='grid'){
					get_grid();
				} else if(search_position=='admin_item'){
					if($('#delete').val()==1){
													location.href='/admin_item/item/1?delete=1';
												return;
					}

					if($(".is_public_class").length > 0) {
													location.href='/admin_item/item/1?is_public='+$("#is_public").val();
												return;
					}

					if($(".is_finish_class").length > 0) {
													location.href='/admin_item/item/1?is_finish='+$("#is_finish").val();
												return;
					}



											location.href='/admin_item/item/1';
										
				} else if(search_position=='admin_item_naver'){
					if($('#delete').val()==1){
													location.href='/admin_item_naver/item/1?delete=1';
												return;
					}

					if($(".is_public_class").length > 0) {
													location.href='/admin_item_naver/item/1?is_public='+$("#is_public").val();
												return;
					}

					if($(".is_finish_class").length > 0) {
													location.href='/admin_item_naver/item/1?is_finish='+$("#is_finish").val();
												return;
					}



											location.href='/admin_item_naver/item/1';
					

				}
			}
		});
	});


	//정렬
		//매물종류
		$('#category').on('change',function(){
		
		valTxt = [];
		valArr = [];
		valSub = 0;
		valSub *= 1;
		if($(this).val()){
			valTxt = $(this).val();
			$.each(valTxt, function(index, category_val){
				valArr[valSub] = 'tcategory_main_'+category_val;
				valSub++;
				valArr[valSub] = 'tcategory_sub_'+category_val;
				valSub++;
			}) 				
		}
		 
		$('#category').selectpicker('val', $(this).val());
		$('#category').selectpicker('refresh')
		
		$('#trigger_type2').selectpicker('val', valArr);
		$('.selectpicker').selectpicker('refresh')
		
		$('#form_search').submit();
	}) 
	
	//테마
		$('#theme').on('change',function(){
		$('#form_search').submit();
	})
	
	//지역바로가기
		$('#location_id').on('change',function(){
					lat=$(this).find(':selected').data('lat');
			lng=$(this).find(':selected').data('lng');
			if(lat && lng){
				go_center='{"lat":'+lat+',"lng":'+lng+'}';
				map.setZoom($(this).find(':selected').data('level'));
				map.panTo($.parseJSON(go_center));
			}
				$('#form_search').submit();
	})
	
	//지하철호선
		$('#subway_line').on('change',function(){
		$('#form_search').submit();
	})
	
	//추가선택항목
		$('#optional_select').on('change',function(){

		$('#form_search').submit();
	})

	$('#optional_select2').on('change',function(){

		$('#form_search').submit();
	})

	$('#optional_select3').on('change',function(){

		$('#form_search').submit();
	})

	$('#optional_select4').on('change',function(){

		$('#form_search').submit();
	})

	$('#optional_select5').on('change',function(){

		$('#form_search').submit();
	})
	
	//방, 화장실
		$('#room').on('change',function(){

		$('#form_search').submit();
	})
	$('#bath').on('change',function(){

		$('#form_search').submit();
	})
	
	//검색항목 셋팅
	function price_search_set(type){
		$('.price_search').hide();
		if(type=='inst'){
			$('.price_search_'+'real').show();
		}
		if(type=='month_rent'){
			$('.price_search_'+'month_deposit').show();
		}
		$('.price_search_'+type).show();
	}
	price_search_set('');

	//거래유형
	$('.types').click(function(){
		if($(this).hasClass('active')){
			$(this).removeClass('active');
			types=0;
			$('.types').removeClass('active');
			$('#type').val('none');
		} else {
			$('.types').removeClass('active');
			types=$(this).data('type');
			$(this).addClass('active');
			$('#type').val(types);
		}

		if(types){
			price_search_set(types);
			$('#trigger_type').selectpicker('val','btn_type_'+types);
		} else {
			$('#trigger_type').selectpicker('val','');
			$('.price_search').hide();
		}
					$('#form_search').submit();
				$('html').scrollTop(0);
	})
	//거래유형
	$('#type').on('change',function(){
		price_search_set($(this).val());
					$('#form_search').submit();
			})

	//드랍다운 토글 유지
	$('.price_drop_box, .area_drop_box').on({
		"click":function(e){
			e.stopPropagation();
		}
	});

	//금액검색창
	$('.price_start').click(function(){
		$('#price_'+$(this).data('type')+'_start').val($(this).data('val'));
		$('.start_'+$(this).data('type')).html($(this).html());
		$('.price_start.li_'+$(this).data('type')).removeClass('active');
		$(this).addClass('active');
				$('#form_search').submit();
			})
	$('.price_end').click(function(){
		$('#price_'+$(this).data('type')+'_end').val($(this).data('val'));
		$('.end_'+$(this).data('type')).html($(this).html());
		$('.price_end.li_'+$(this).data('type')).removeClass('active');
		$(this).addClass('active');
				$('#form_search').submit();
			})
	$('.price_search_box').on('keyup',function(){
		$('.'+$(this).data('type')).html($(this).val()+$(this).data('unit'));
				$('#form_search').submit();
			})

	//면적검색창
	$('.area_start').click(function(){
		$('#'+$(this).data('type')+'_start').val($(this).data('val'));
		$('.start_'+$(this).data('type')).html($(this).html());
		$('.area_start.li_'+$(this).data('type')).removeClass('active');
		$(this).addClass('active');
				$('#form_search').submit();
			})
	$('.area_end').click(function(){
		$('#'+$(this).data('type')+'_end').val($(this).data('val'));
		$('.end_'+$(this).data('type')).html($(this).html());
		$('.area_end.li_'+$(this).data('type')).removeClass('active');
		$(this).addClass('active');
				$('#form_search').submit();
			})
	$('.area_search_box').on('keyup',function(){
		$('.'+$(this).data('type')).html($(this).val()+$(this).data('unit'));
				$('#form_search').submit();
			})

	//모바일검색
	$('.mobile_search_btn').click(function(){
		if($(this).hasClass('active')){

			$('#grid_list').show();
			$('html').css('position','relative');

			$('.search_console').hide();
			$(this).removeClass('active');



		} else {

			$('#grid_list').hide();
			$('html').css('position','fixed');

			$('.search_console').show();
			$(this).addClass('active');

			
		}
	})

	//멀티셀렉트
	$('#category').each(function(){
		$('#category').selectpicker({
		  noneSelectedText: '학원 분류',
			countSelectedText: function (numSelected, numTotal) {
				return "학원 분류 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#trigger_type2').each(function(){
		$('#trigger_type2').selectpicker({
		  noneSelectedText: '학원 분류',
			countSelectedText: function (numSelected, numTotal) {
				return "학원 분류 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#categories').each(function(){
		$('#categories').selectpicker({
		  noneSelectedText: '카테고리',
			countSelectedText: function (numSelected, numTotal) {
				return "카테고리 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#theme').each(function(){
		$('#theme').selectpicker({
		  noneSelectedText: '매물테마',
			countSelectedText: function (numSelected, numTotal) {
				return "매물테마 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#room').each(function(){
		$('#room').selectpicker({
		  noneSelectedText: '강의실',
			countSelectedText: function (numSelected, numTotal) {
				return "강의실 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#bath').each(function(){
		$('#bath').selectpicker({
		  noneSelectedText: '욕실',
			countSelectedText: function (numSelected, numTotal) {
				return "욕실 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#location_id').each(function(){
		$('#location_id').selectpicker({
		  noneSelectedText: '위치',
			countSelectedText: function (numSelected, numTotal) {
				return "위치 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#type').each(function(){
		$('#type').selectpicker({
		  noneSelectedText: '거래 유형',
			countSelectedText: function (numSelected, numTotal) {
				return "거래 유형 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#subway_line').each(function(){
		$('#subway_line').selectpicker({
		  noneSelectedText: '호선 검색',
			countSelectedText: function (numSelected, numTotal) {
				return "호선 검색 <div class='badge'>{0}</div>";
			},
		});
	})

	$('#optional_select').each(function(){
		$('#optional_select').selectpicker({
		  noneSelectedText: '추가입력항목',
			countSelectedText: function (numSelected, numTotal) {
				return "추가입력항목 <div class='badge'>{0}</div>";
			},
		});
	})
	$('#optional_select2').each(function(){
		$('#optional_select2').selectpicker({
		  noneSelectedText: '',
			countSelectedText: function (numSelected, numTotal) {
				return " <div class='badge'>{0}</div>";
			},
		});
	})
	$('#optional_select3').each(function(){
		$('#optional_select3').selectpicker({
		  noneSelectedText: '',
			countSelectedText: function (numSelected, numTotal) {
				return " <div class='badge'>{0}</div>";
			},
		});
	})
	$('#optional_select4').each(function(){
		$('#optional_select4').selectpicker({
		  noneSelectedText: '',
			countSelectedText: function (numSelected, numTotal) {
				return " <div class='badge'>{0}</div>";
			},
		});
	})
	$('#optional_select5').each(function(){
		$('#optional_select5').selectpicker({
		  noneSelectedText: '',
			countSelectedText: function (numSelected, numTotal) {
				return " <div class='badge'>{0}</div>";
			},
		});
	})


	//2차매물종류
	$('.category_main').click(function(){
		if($(this).parent().hasClass('selected')){
			$('#category option[data-parent_id="'+$(this).data('tokens')+'"]').prop('selected','');
			$('.category_sub_'+$(this).data('tokens')).parent().removeClass('selected');
		} else {
			$('#category option[data-parent_id="'+$(this).data('tokens')+'"]').prop('selected','selected');
			$('.category_sub_'+$(this).data('tokens')).parent().addClass('selected');
		}
	})
	$('.c_sub').click(function(){
		if($('.category_main_'+$(this).data('tokens').replace('s_','')).parent().hasClass('selected')){ //부모가 선택되어있으면
			$('#category option[data-tokens="'+$(this).data('tokens').replace('s_','')+'"]').prop('selected','');
			$('.category_main_'+$(this).data('tokens').replace('s_','')).parent().removeClass('selected');
		}
	})
	
	
	//2차매물종류
	$('.category_main2').click(function(){
		if($(this).parent().hasClass('selected')){
			$('#trigger_type2 option[data-parent_id="'+$(this).data('tokens')+'"]').prop('selected','');
			$('.category_subs_'+$(this).data('tokens')).parent().removeClass('selected');
		} else {
			$('#trigger_type2 option[data-parent_id="'+$(this).data('tokens')+'"]').prop('selected','selected');
			$('.category_subs_'+$(this).data('tokens')).parent().addClass('selected');
		}
	})
	$('.c_sub2').click(function(){
		if($('.category_mains_'+$(this).data('tokens').replace('s_','')).parent().hasClass('selected')){ //부모가 선택되어있으면
			$('#trigger_type2 option[data-tokens="'+$(this).data('tokens').replace('s_','')+'"]').prop('selected','');
			$('.category_mains_'+$(this).data('tokens').replace('s_','')).parent().removeClass('selected');
		}
	})



	//슬라이더
	$('.range').each(function(){
		if($(this).data('end')){
			end_val=$(this).data('end');
		} else {
			end_val=$(this).data('val');
		}
		$( '.range_'+$(this).data('type') ).slider({
			range: true,
			min: 0,
			max: $(this).data('val'),
			values: [ $(this).data('start'), end_val ],
			slide: function( event, ui ) {
				$( "#price_"+$(this).data('type')+"_start").val(ui.values[0]);
				$( "#price_"+$(this).data('type')+"_end").val(ui.values[1]);
				$( "#"+$(this).data('type')+"_start").val(ui.values[0]);
				$( "#"+$(this).data('type')+"_end").val(ui.values[1]);
				$( ".start_"+$(this).data('type')).html(ui.values[0]+$(this).data('unit'));
				$( ".end_"+$(this).data('type')).html(ui.values[1]+$(this).data('unit'));
			},
			stop:function(){
				$('#form_search').submit();
			}
		});
	})

	//매물종류 트리거
	
	//$(function(){
	//	trigger_category=[];
	//	$('.trigger_category.active').each(function(){
	//		trigger_category.push($(this).data('id'));
	//	})
	//	$('.trigger_category').click(function(){
	//		if($(this).hasClass('active')){
	//			$(this).removeClass('active');
	//			//trigger_category.pop($(this).data('id'));
	//		} else {
	//			$(this).addClass('active');
	//			//trigger_category.push($(this).data('id'));
	//		}
	//		trigger_category=[];
	//		$('.trigger_category.active').each(function(){
	//			trigger_category.push($(this).data('id'));
	//		})
	//		
	//		
	//		
	//		
	//		$('#category').val(trigger_category);
	//
	//		$('#form_search').submit();
	//	})
	//})
	
	
	//매물종류 트리거
	$(function(){
		trigger_category=[];
		$('.trigger_category.active').each(function(){
			trigger_category.push($(this).data('id'));
		})
		
		$('.trigger_category').click(function(){
			categorys = [];
			trigger_id = [];
			//if('#trigger_type2.'){
				
				//console.log($('#trigger_type2').val());
				
				if($(this).hasClass('active')){
					$('.trigger_category.tcategory_main_'+$(this).data('id')).removeClass('active');
					//$(this).removeClass('active');
					$('.trigger_category.active').each(function(){
						categorys.push($(this).data('id'));
						trigger_id.push('tcategory_main_' + $(this).data('id'));
					})
					
				} else {
					$(this).addClass('active');
					$('.trigger_category.active').each(function(){
						categorys.push($(this).data('id'));
						trigger_id.push('tcategory_main_' + $(this).data('id'));
					})
				}
			//}
			
			
			/*
			trigger_category=[];
			$('.trigger_category.active').each(function(){
				trigger_category.push($(this).data('id'));
			})
			*/		
		
			
			$('#category').selectpicker('val',categorys);
			$('#category').selectpicker('refresh');
			
			//$('#trigger_type2 option:selected').val('default');
			$('#trigger_type2').selectpicker('val', trigger_id);
			$('.selectpicker').selectpicker('refresh');
			
			//$('#category').val(trigger_category);
			
			$('#form_search').submit();
		
		})
	})
	 
	//테마 트리거
	$(function(){
		trigger_theme=[];
		$('.trigger_theme.active').each(function(){
			trigger_theme.push($(this).data('id'));
		})
		$('.trigger_theme').click(function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
				//trigger_theme.pop($(this).data('id'));
			} else {
				$(this).addClass('active');
				//trigger_theme.push($(this).data('id'));
			}
			trigger_theme=[];
			$('.trigger_theme.active').each(function(){
				trigger_theme.push($(this).data('id'));
			})
			$('#theme').val(trigger_theme);

			$('#form_search').submit();
		})
	})


})

function search_reset(){
	$('.types.active').click();
	$('#keyword').val('');
	$('#category').val('');
	$('#category').selectpicker('refresh');
	$('#trigger_type').val('');
	$('#trigger_type').selectpicker('refresh');
	$('#trigger_type2').val('');
	$('#trigger_type2').selectpicker('refresh');
	$('.trigger_category').removeClass('active');
	$('#theme').val('');
	$('#theme').selectpicker('refresh');
	$('#room').val('');
	$('#room').selectpicker('refresh');
	$('#bath').val('');
	$('#bath').selectpicker('refresh');
	$('#location_id').val('');
	$('#location_id').selectpicker('refresh');
	$('#type').val('');
	$('#type').selectpicker('refresh');
	$('.price_search_box').val('');
	$('.price_search').hide();
	$('.area_search_box').val('');
	$('#sido_id').val('');
	$('#subway_line').val('');
	$('#subway_line').selectpicker('refresh');
	$('#optional_select').val('');
	$('#optional_select').selectpicker('refresh');
	$('#optional_select2').val('');
	$('#optional_select2').selectpicker('refresh');
	$('#optional_select3').val('');
	$('#optional_select3').selectpicker('refresh');
	$('#optional_select4').val('');
	$('#optional_select4').selectpicker('refresh');
	$('#optional_select5').val('');
	$('#optional_select5').selectpicker('refresh');
	$('#gugun_id').val('');
	$('#dong_id').val('');
	$('#member_id').val('');
	$('#member_id').selectpicker('refresh');
	$('#owner_id').val('');
	$('#owner_id').selectpicker('refresh');
	$('#is_finish').val('');
	$('#is_finish').selectpicker('refresh');
	$('#is_public').val('');
	$('#is_public').selectpicker('refresh');
	$('.val_zone_start').html('0');
	$('.start_area_law, .start_area_real').html('0㎡');

	$('.val_zone_end').html('무제한');
	$('#map_item').hide();
	is_dong_click=0;
	is_subway_click=0;
	is_danzi_click=0;
	$('.trigger_category, .trigger_theme').removeClass('active');
	$('#form_search').submit();
}

function mobile_search(type){
	if(type==1){
					d_keywordClick('click');
		
		$('#form_search').submit();
		$('.search_console').hide();
				} else if(type==2){
		$('#form_search').submit();
		$('.search_console').hide();
			
	} else {
		$('.search_console').hide();
				}
	$('#grid_list').show();
	$('html').css('position','relative');
	$('.mobile_search_btn').removeClass('active');
}

		function naverMapControlFlag(mode){
			

		}

$(document).ready(function(){
	$('#dong_id').html('<option value="">도/동</option>');
	$('#sido_id').on('change',function(){
		$.post('/address/get_gugun',{sido_id:$('#sido_id').val()},function(data){
			$('#gugun_id').html(data);
		})
		if(!$(this).val()){
			$('#dong_id').html('<option value="">도/동</option>');
		}
	})

	$('#gugun_id').on('change',function(){
		$.post('/address/get_dong',{gugun_id:$('#gugun_id').val()},function(data){
			$('#dong_id').html(data);
		})
	})

									})
