$(document).ready(function(){
resize_filter();

function resize_filter(){
	if($(window).width()>1240){
		$('#t-21bl').css('width',36+'%');
		$('#t-31bl').css('width',55+'%');
		$('#t-11bl').css('width',7+'%');
	}else{
		$('#t-21bl').css('width',45+'%');
		$('#t-31bl').css('width',44+'%');
		$('#t-11bl').css('width',9+'%');
	}
}

$(window).resize(function(){
	resize_filter();
});

	
	$('#model_counter_v').click(function() {
		SendFilter('second','main_filter');
	});

	//клик по ch
	$('.sub_nav4 li').click(function() {
		
		if($(this).is('.active')===false){
			
			ClickCh(this);
			
			var parent_ch = $(this).parent();
			if(parent_ch.is('.ncc')===false){	//игнорируем запрещенные
				if(($(this).is('.dch')===false)){ //игнорируем неактивные
					if(DefStatusFilterType=='hard'){
						LoadAddonFilter(last_act_ch[0]+last_act_ch[2],FormMas('main_filter'));
					}	
					else{
						$('#AddonFilter').html('');
					}
					ShowCntPbb();
				}
			}
		}
	});
	
	var LastValueNum = false;
	$('.tabs_content_main input:text').keydown(function() {
		if($(this).is('.num')){
			LastValueNum = this.value;
		}	  		
	});
	
	$('.tabs_content_main input:text').keyup(function() {
		if($(this).is('.num')){
			if(LastValueNum!=this.value){
				OnlyNumV2(this);
				ShowCntPbb();
				LastValueNum = false;
			}
		}
	});
	
	$('.tabs_content_main input:checkbox').click(function(){
		ShowCntPbb();
	});

	$('.tabs_content_main select').change(function(){
		ShowCntPbb();
	});
	
	$('select#category_earth').change(function(){
			ShowCntPbb();
	});
	
	
	function LoadAddonFilter(SubId,ReqUrl){
		//Строка Url Значения для фильтра
		//var ReqUrl =  document.location.href;
		$('#AddonFilter').html('<img src="http://afy.ru/loader.gif" title="загрузка" />');
		$.ajax({
				type: 'POST',
				url: '/addonfilter-ajax.php',
				data: 'switch=addon_filter&sub_id='+SubId+'&a='+encodeURIComponent(ReqUrl),
				cache: false,
				success: function(response){
					var response=unescape(response);
					response=response.split("|");
					var responseType=response[0];
					var responseMsg=response[1];
					if(responseType=="success"){
						$('#AddonFilter').html(responseMsg);
						if(responseMsg!=''){
							$('#AddonFilter').html(responseMsg);
							
							DefStatusFilterType = 'hard'; //меняем статус
     						$('#ChangeFilterType').text("Простой поиск");
							//$('#is_isset_img').show();
     						$('#AddonFilter').show();
							
							$('#AddonFilter input.txt11').keydown(function() {
								if($(this).is('.num')){
									LastValueNum = this.value;
								}	  		
							});
							
							$('#AddonFilter input.txt11').keyup(function() {
								if($(this).is('.num')){
									if(LastValueNum!=this.value){
										OnlyNumV2(this);
										ShowCntPbb();
										LastValueNum = false;
									}
								}
							});
							
							$('#AddonFilter select').change(function(){
								ShowCntPbb();
							});
							$('#AddonFilter input:checkbox').click(function(){
								ShowCntPbb();
							});
						}else{
							DefStatusFilterType = 'easy';
				     		$('#ChangeFilterType').text("Расширенный поиск");
							//$('#is_isset_img').hide();
				     		$('#AddonFilter').hide();
						}
					}
					else{
						ShowListItem(ListId,'<p style="color:red">Неизвестная ошибка, попробуйте позже<\/p>'+response);
					}
			    }
		});
	}
	

   
	function ChangeFilterType()
    {
    	if (DefStatusFilterType=='easy')
     	{
     		if(($('#AddonFilter')).html()==''){
     			//подгрузка фильтра тут
				LoadAddonFilter(last_act_ch[0]+last_act_ch[2],document.location.href);
     		}
     		else{
     			DefStatusFilterType = 'hard'; //меняем статус
     			$('#ChangeFilterType').text("Простой поиск");
				$('#AddonFilter').show();
				//$('#is_isset_img').show();
     		}
     	}
     	else
     	{
     		DefStatusFilterType = 'easy';
     		$('#ChangeFilterType').text("Расширенный поиск");
     		$('#AddonFilter').hide();
			//$('#is_isset_img').hide();
     	}
    } 
   
  	$('#ChangeFilterType').click(function() {	
  	 	ChangeFilterType();
	});
	
	ChangeFilterType();
});

	
function ShowCh(step,idx){
	$('#act_ch_'+step+'_'+idx).show();
}

function HideCh(step,idx){
	$('#act_ch_'+step+'_'+idx).hide();
}	

function HideAllCh(step){
	$('#act_ch_'+step).find('li').hide();
}
	
function DisabledCh(step,idx){
	if(last_act_ch[step]==idx){  //если актив выкл актив то сбрасываем
		ChDeAct(step,last_act_ch[step]);
		last_act_ch[step]=1;
	}
						
	$('#act_ch_'+step+'_'+idx).addClass('dch');
}

function EnebledCh(step,idx){
	$('#act_ch_'+step+'_'+idx).removeClass('dch');
}

function EnebledAllCh(step){
	$('#act_ch_'+step).find('li').removeClass('dch');
}

function ChDeAct(step,idx){
	$('#act_ch_'+step+'_'+idx).removeClass('active');
	$('#act_ch_'+step+'_'+idx+' div').hide();
}


function ClickCh(isthis){
	if(($(isthis).attr('id')!=undefined)){
		if($(isthis).is('.dch')===false){ //игнорируем неактивные
			act_ch_arr=$(isthis).attr('id').split('_'); //определеям группу выбора и индекс		
			if(act_ch_arr.length=='4'&&act_ch_arr[0]=='act'){	
				
				var step_ch=act_ch_arr[2];
				var idx_ch=act_ch_arr[3];
				//сброс
				ChDeAct(step_ch,last_act_ch[step_ch]);			
				//выбор
				$(isthis).addClass('active');
				$(isthis).children('a').children('div').show();
				last_act_ch[step_ch] = idx_ch;
				
				//изменения по отображению возможного выбора
				if(step_ch==0){ //Я хочу
					EnebledAllCh(2); //включаем все
					//$('#price_name').text('Стоимость');
					
					if(idx_ch==1){ //Купить
						
					}else
					if(idx_ch==2){ //Снят
						//$('#price_name').text('Стоимость за месяц');
						DisabledCh(2,3); 
						DisabledCh(2,6); 
						DisabledCh(2,7); 
						DisabledCh(2,13); 
						DisabledCh(2,14); 

					}
					else 
					if(idx_ch==3){ //Обменять
						DisabledCh(2,3); 
						DisabledCh(2,4); 
						DisabledCh(2,5); 
						DisabledCh(2,6); 
						DisabledCh(2,7); 
						DisabledCh(2,8); 
						DisabledCh(2,9); 
						DisabledCh(2,10); 
						DisabledCh(2,11); 
						DisabledCh(2,13); 
						DisabledCh(2,14); 
					}
					
					if(last_act_ch[2]>0){
						ClickCh('#act_ch_2_'+last_act_ch[2]); //устанавлеваем сл шаг
						
					}
					
				}else
				if(step_ch==2){ //Вид недвижимости
					$('#sq').show();
					$('#sq_type').html(' м&sup2;');
					$('#sq_name').text('Общая площадь,');
					$('#sq_select_type').hide();
					$('#category_earth_select').hide();
					$('#count_room_select').hide();
					$('#period_rent').hide();
					
					if((idx_ch==1||idx_ch==4)&&last_act_ch[0]==2)
						$('#period_rent').show();
						
					if(idx_ch==1||idx_ch==3)
						$('#count_room_select').show();
										
					if(idx_ch==14){
						$('#sq_type').text('');
						$('#sq_name').text('Площадь участка');
						$('#sq_select_type').show();
						$('#category_earth_select').show();
					}else if(idx_ch==13){
						$('#sq').hide();
					}
				}	
				
			}
		}
	}
}

function ShowCntPbb(){
	$('#model_counter').hide();
	SendFilter('second','main_filter',true);
}
	
function load_ajax_is_done(action,answer){
	if(action=='get_prev_result'){
		var counter_for = '#act_ch_2_'+last_act_ch[2];
		if(answer==0){
			$('#SendFilterButton').attr('value','объектов нет');
			$('#SendFilterButton').not('disabled').attr('disabled','disabled');
		}
		else{
			$('#SendFilterButton').attr('value','Показать '+answer);
			$('#SendFilterButton:disabled').attr('disabled','');
		}

		if($(counter_for).attr('id')!=undefined){
			$(counter_for).offset(function(i,val){
					if(answer==0){
						$('#model_counter_v').text('объектов нет');
					}
					$('#model_counter').css( 'top', val.top.toFixed()-16);
					$('#model_counter').css( 'margin-left', val.left+15);
					$('#model_counter').show();
			});
		}
	}
}
	
function OnlyNumV2(isthis) {  
	isthis.value = isthis.value.replace(/[^\d]/g, '');
};

