(function($){
	var srch_itm = $.trim($(".alert_search .srch_itm .val").html());
	if (!srch_itm){
		$(".alert_search .srch_itm .val").html("Все");
	}else{
		$(".alert_search .srch_itm .val").html(srch_itm);
	};
	var srch_loc = $.trim($(".alert_search .srch_loc .val").html());
	if (!srch_loc){
		$(".alert_search .srch_loc .val").html("Везде");
	}else{
		$(".alert_search .srch_loc .val").html(srch_loc);
	};
})(jQuery);

(function($){

    $(".ab_search .b_ftr .b_ctrls .lnk").toggle(function(){
		var obj = $(this);
		obj.parent().find(".minformtext").css('display', 'none');
        obj.parent().parent().prev().find(".dspl_search_form").slideDown(function(){
			obj.html('<a class="ab_dotted_spaned" href="javascript:void(0)" id="ctrl_search_change"><span>отмена</span></a>');
		});
    }, function(){
		var obj = $(this);
        obj.parent().parent().prev().find(".dspl_search_form").slideUp(function(){
			obj.html('<a id="ctrl_search_change" href="javascript:void(0)" class="ac_button btn_search">Изменить параметры поиска</a>');
			obj.parent().find(".minformtext").css('display', 'block');
		});
    });

    $(".ab_search .f_ctrls .ab_dropdown").toggle(function(){
        $(this).parent().parent().prev().slideDown();
        $(this).addClass("ab_dropdown_opened");
    }, function(){
        $(this).parent().parent().prev().slideUp();
        $(this).removeClass("ab_dropdown_opened");
    });

	if (location.hash=="#extend"){
		$(".ab_search .f_ctrls .ab_dropdown").click();
	}
	if ((location.search).indexOf("mini_to_extended=")>-1){
		$("#ctrl_search_change").click();
		$(".ab_search .f_ctrls .ab_dropdown").click();
	}

    $(".ab_search .moremarka").click(function(){
		if ($("#search_form .marki .line:nth-child(2)").css("display")=="none"){
			$("#search_form .marki .line:nth-child(2)").css("display", "block");
			//$(this).find(":hidden").val('1');
		}else if ($("#search_form .marki .line:nth-child(3)").css("display")=="none"){
			$("#search_form .marki .line:nth-child(3)").css("display", "block");
			//$(this).find(":hidden").val('2');
			$(this).css("display", "none");
		}
    });

    $(".ab_search .moreregion").click(function(){
		if ($("#search_form .regions .line:nth-child(2)").css("display")=="none"){
			$("#search_form .regions .line:nth-child(2)").css("display", "block");
		}else if ($("#search_form .regions .line:nth-child(3)").css("display")=="none"){
			$("#search_form .regions .line:nth-child(3)").css("display", "block");
			$(this).css("display", "none");
		}
    });


	if ($("#search_form .regions .line:nth-child(2)").css("display")=="block" && $("#search_form .regions .line:nth-child(3)").css("display")=="block"){
		$(".ab_search .moreregion").css("display", "none");
	}
	if ($("#search_form .marki .line:nth-child(2)").css("display")=="block" && $("#search_form .marki .line:nth-child(3)").css("display")=="block"){
		$(".ab_search .moremarka").css("display", "none");
	}

})(jQuery);


jQuery(function($j){
	$j(".ab_search").keyup(function(event){
		if (event.keyCode==13)
			$j("#search_form").submit();
	});
});


$j("select[name^='make']").map(function(index){
	if ($j(this).parents('.minisearch').is('.minisearch')){
		var spinnered_text = $j('<span></span>').appendTo($j("select[name='model"+(index+1)+"']").parent().prev('h5'));
		$j(this).ajaxselect({ajaxpath:"'/classifier/get/children/'+this.value+'/related/'+related_class+'?count-styles'", target_select:$j("select[name='model"+(index+1)+"']"), spinner_class:'spinner10', spinnered_element: spinnered_text});
	}else{
		$j(this).ajaxselect({ajaxpath:"'/classifier/get/children/'+this.value+'/related/'+related_class+'?count-styles'", target_select:$j("select[name='model"+(index+1)+"']"), spinner_class:'spinner15'});
	}
	if ($j(this).get(0).selectedIndex == 0){
		$j("select[name='model"+(index+1)+"']").attr('disabled', 'disabled');
	}else if ($j("select[name='model"+(index+1)+"'] option").length<2){
		$j(this).change();
	};
});

$j("select[name^='country']").map(function(index){
	$j(this).ajaxselect({ajaxpath:"'/classifier/get/children/'+this.value+'/related/'", target_select:$j("select[name='region"+(index+1)+"']"), spinner_class:'spinner15'});
	if ($j(this).get(0).selectedIndex == 0){
		$j("select[name='region"+(index+1)+"']").attr('disabled', 'disabled');
	}else if ($j("select[name='region"+(index+1)+"'] option").length<2){
		$j(this).change();
	};
});

// input hidden is for goback
/*
$j("select[name='model1'], select[name='model2'], select[name='model3']").change(function(){
	$j(this).next(":hidden").val($j(this).get(0).selectedIndex);
});
$j("select[name='region1'], select[name='region2'], select[name='region3']").change(function(){
	$j(this).next(":hidden").val($j(this).get(0).selectedIndex);
});
*/
/*
$j("select[name='country2'], select[name='country3']").map(function(index){
	if ($j(this).get(0).selectedIndex == 0){
		$j("select[name='region"+(index+2)+"']").attr('disabled', 'disabled');
	}else //if (!$j("select[name='region"+(index+2)+"'] option").length)
	{
		$j(this).change();
	};
});
$j("select[name='country1']").map(function(){
	if ($j(this).val() != "1911"){
		$j(this).change();
	};
});
*/

jQuery(".ab_search .b_hdr .ab_dropdown").toggle(function(){
    jQuery(this).parent().find(".params_search_popup").css("display", "block");
    jQuery(this).addClass("ab_dropdown_opened");
    return false;
}, function(){
    jQuery(this).parent().find(".params_search_popup").css("display", "none");
    jQuery(this).removeClass("ab_dropdown_opened");
    return false;
});

//jQuery(".ab_search .b_hdr .params_search_popup a").click(function(){
//    var dyntab = jQuery(this).parent().parent().parent().parent().find(".b_tabs .dynamic_tab");
//    dyntab.find("a").html(jQuery(this).attr("title")).attr("rel", jQuery(this).attr("rel"));
//    dyntab.addClass("active").siblings().removeClass("active");
//    jQuery(this).css("display", "none").siblings().css("display","block");
//    jQuery(this).parent().parent().parent().parent().find(".ab_dropdown").click();
//    return false;
//});

