is_dong_click=0;
is_danzi_click=0;
is_subway_click=0;
$(document).ready(function(){
	var main_click = 1;
	auto=0;
    
	$(function(){
		$('#form_search').ajaxForm({
			beforeSerialize: function(){
							},
			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 {
											$('#address_id').val('');
						$('#subway_id').val('');
						$('#subway_station').val('');
										$('#form_search input[name="lat"]').val('');
					$('#form_search input[name="lng"]').val('');
				}
			}, success: function(search_position) {
				search_position = search_position.replace(/\n/g, "");//행바꿈제거
				search_position = search_position.replace(/\r/g, "");//엔터제거
				if(search_position=='main'){
											location.href='/search';
					
				} else if(search_position=='map'){
					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=='sell') {
					if($('#delete').val()==1){
                        location.href='/admin_item/sell/1?delete=1';
						return;
					}

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

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

					location.href='/admin_item/sell/1';
				} else if(search_position=='admin_danzi'){
                    if($('#delete').val()==1){
                        location.href='/admin_danzi/view/1?delete=1&dl='+$("#dl").val();
                        return;
                    }

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

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

                    location.href='/admin_danzi/view/1?dl='+$("#dl").val();

                    } 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';
				} else if(search_position == "address_search") {
					location.href='/grid';
				}else {
					location.href='/search';
				}
			}
		});


		$('#mobileAddressForm').ajaxForm({
			beforeSerialize: function(){
			},
			beforeSubmit: function(){
				setTimeout(function(){
					$('#map_list_panel').scrollTop(0);
				},100)

								$('#address_id').val('');
				$('#subway_id').val('');
				$('#subway_station').val('');
								$('#lat').val('');
				$('#lng').val('');
			}, success: function(search_position){
				location.href='/grid';
			}
		});
	});


	//정렬
		//매물종류
		$('#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(){
					var theme = $("#theme").val();
			var themes = "";
			if(theme) {
				themes = theme.join(",");
			}
			$('.trigger_theme').removeClass('active');
			$(theme).each(function(k, v) {
				$('#theme1_' + v).addClass('active');
				$('#theme2_' + v).addClass('active');
			});
			searchConsoleTrigerThemeString = "0";
			if(themes != "")	searchConsoleTrigerThemeString += ","+themes;
				$('#form_search').submit();
			})
	
	//지역바로가기
		$('#location_id').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>";
			},
		});
	})
	$('#theme2').each(function(){
		$('#theme2').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]);
                    $( "#price_"+$(this).data('type')+"_start2").val(ui.values[0]);
            $( "#price_"+$(this).data('type')+"_end2").val(ui.values[1]);
        				$( "#"+$(this).data('type')+"_start").val(ui.values[0]);
				$( "#"+$(this).data('type')+"_end").val(ui.values[1]);
	      	          $( "#"+$(this).data('type')+"_start2, #"+$(this).data('type')+"_start3").val(ui.values[0]);
	          $( "#"+$(this).data('type')+"_end2, #"+$(this).data('type')+"_end3").val(ui.values[1]);

	          $( "#price_"+$(this).data('type')+"_start3").val(ui.values[0]);
	          $( "#price_"+$(this).data('type')+"_end3").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(){
				if($(this).hasClass("range_mobile")) {
					return;
				}
				                    get_position();
        			}
		});
	})

	//매물종류 트리거

	//$(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')){
                $('#theme1_'+$(this).data('id')).removeClass('active');
                $('#theme2_'+$(this).data('id')).removeClass('active');
            } else {
                $('#theme1_'+$(this).data('id')).addClass('active');
                $('#theme2_'+$(this).data('id')).addClass('active');
            }
            			trigger_theme=[];
			$('.trigger_theme.active').each(function(){
				trigger_theme.push($(this).data('id'));
			})
            if($(this).data('type') == 'img_filter'){
                $('#img_filter').val(trigger_theme);
                searchConsoleImgFilter = "";
                if(trigger_theme != "")	searchConsoleImgFilter += ","+trigger_theme;
            }else{
                $('#theme').val(trigger_theme);
                searchConsoleTrigerThemeString = "0";
                if(trigger_theme != "")	searchConsoleTrigerThemeString += ","+trigger_theme;
            }
							$('#theme').selectpicker("val", trigger_theme);
				$('#theme2').selectpicker("val", trigger_theme);
						$('#form_search').submit();
		});
	});
})

//검색 초기화 함수 -SJ
function search_all_reset(em_id, type){
    if(em_id){
        $('#'+em_id).val('');
        $('.'+em_id).val('');

        if(em_id === 'd_price_search_box'){
            $('.start_type').html('0');
            $('.end_type').html('무제한');
            var range = $('.range_type').get();
            for (var i=0;i<range.length;i++) {
                $('.range_'+range[i].getAttribute('data-type')).slider({range: true,values: [ 0, range[i].getAttribute('data-val') ]});
            }
        }
        if(em_id === 'd_area_search_box'){
            $('.start_area').html('0');
            $('.end_area').html('무제한');
            var range = $('.range_area').get();
            for (var i=0;i<range.length;i++) {
                $('.range_'+range[i].getAttribute('data-type')).slider({range: true,values: [ 0, range[i].getAttribute('data-val') ]});
            }
        }
    }else{
        var frm = document.getElementById('form_search');
        var em = frm.elements;
        frm.reset();
        for (var i = 0; i < em.length; i++) {
            $('#'+em[i].id).val('');
            $('#'+em[i].id).selectpicker('refresh');
        }
        $('#map_item').hide();
        $('.price_search').hide();
        $('.btn').removeClass('active');
        $('.val_zone_start').html('0');
        $('.type_sub_tit').hide();
        $('.type_sub_view').show();
        $('.type_sub_view_All').show();
        $('.start_area_law, .start_area_real').html('0py');
        $('.val_zone_end').html('무제한');
        var range = $('.range').get();
        for (var i=0;i<range.length;i++) {
            $('.range_'+range[i].getAttribute('data-type')).slider({range: true,values: [ 0, range[i].getAttribute('data-val') ]});
        }
        is_dong_click=0;
        is_subway_click=0;
        is_danzi_click=0;
								$(".select_dropdown > dt > a > span").text("최신순");
				$(".search_btn").removeClass("active");
				        msgbox("검색필터가 초기화 되었습니다.");
    }

    			if(!type) {
				get_position();
			}
    }

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');
    $('#img_filter').val('');
    $('#img_filter').selectpicker('refresh');
	$('.val_zone_start').html('0');
	$('.start_area_law, .start_area_real').html('0py');

	$('.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');
		$('input[name="align"]').val("new");
	$(".btn_align").removeClass("active");
	$('[data-type="new"]').addClass("active");
	$('#form_search').submit();
}

function mobile_search(type){
	if(type==1){
				
		$('#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);
		})
	})

	$('#dong_id_main').html('<option value="">도/동</option>');
	$('#sido_id_main').on('change',function(){
		$.post('/address/get_gugun_villa',{sido_id:$('#sido_id_main').val()},function(data){
			$('#gugun_id_main').html(data);
		})
		if(!$(this).val()){
			$('#dong_id_main').html('<option value="">도/동</option>');
		}
	})

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

									
    })
