var swto = null;

$(document).ready(function(){
	$("#visible_price_min").html($("#price_range").slider("values", 0));
	$("#visible_price_max").html($("#price_range").slider("values", 1));
	$("#price_min").val($("#price_range").slider("values", 0));
	$("#price_max").val($("#price_range").slider("values", 1));
	$("#count").html($("#smarty_count").val()?$("#smarty_count").val():0);
	$('.search_options ul li:last-child').addClass('last');
	$('#select_sub_spec, #select_specialty, #select_type').change(function(){
		$('#count').html('...');
	});
	$('#search_word').keyup(function(){
		$('#count').html('...');
		if(swto != null)
			window.clearTimeout(swto);
		swto = window.setTimeout('set_count()',500);
	});
	$('.search_options ul li.toggle').click(function(e){
		e.preventDefault();
		if(!$(this).hasClass('clicked')){
			$(this).addClass('clicked').children('a').html('ukryj wszystkie');
			$(this).parent().children('li.hidden').slideDown(1000,function(){});
		}else{
			$(this).removeClass('clicked').children('a').html('pokaż wszystkie');
			$(this).parent().children('li.hidden').slideUp(1000,function(){});
		}
	});
	$('.search_result li').not('.simple').children('a.img, span.min_price').each(function(){
		var oh = 0;
		oh += $(this).parent().children('h4').outerHeight(true);
		oh += $(this).parent().children('p.object_name').outerHeight(true);
		$(this).css({
			marginTop: (oh-$(this).outerHeight())/2+'px'
			});
	});
});

function set_specialty(){
	if(($("#select_sub_spec").selectedValues()).length == 1){
		$('#select_sub_spec').removeOption(/./);
//	$('#select_sub_spec').removeOption(/./);
//	if(($("#select_sub_spec").selectedValues()).length == 0){
		$.post(
			'/actions/product_search/search_sub_spec.html'
			,{
				specialty: $('#select_specialty').val()
				}
			,function(data){
				$('#select_sub_spec').removeOption(/./).addOption(data, false);
				set_count();
			}
			,"json"
			);
	}
}
function set_count(){
	$.post(
		'/actions/product_search/search_count.html'
		,{
			specialty: $('#select_specialty').val(),
			sub_spec: $('#select_sub_spec').val(),
			type: $('#select_type').val(),
			search_word: $('#search_word').val(),
			pmin: $('#visible_price_min').html(),
			pmax: $('#visible_price_max').html()
			}
		,function(data){
			$('#count').html(data.count);
		}
		,"json"
		);
}

