$(document).ready(function() {
    $('#show').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed: 1000,
		timeout: 5000
	});
    
    $('.menu-root').mouseenter(function(){
    	$(this).addClass('current');
    	$(this).children('.pop-menu').css('display','block');
    });
    
    $('.menu-root').mouseleave(function(){
    	$(this).removeClass('current');
    	$(this).children('.pop-menu').css('display','none');
    });
    
    
    $("a.slimbox").slimbox();
    
    //$("#search-box").tabs();
    
    $(function() {
    	slider_step = $('#offer-type').val() == 'sell' ? 1000 : 50;
    	slider_max = $('#offer-type').val() == 'sell' ? sellMax : rentMax;
    	if (price_max == 0 | price_max < price_min) {price_max = slider_max;}
    	
		$(".slider-range").slider({
			range: true,
			min: 0,
			max: slider_max,
			values: [price_min, price_max],
			step: slider_step,
			slide: function(event, ui) {
				vmin = ui.values[0];
				vmax = ui.values[1];
				
//				$("#amount-min").val($(".slider-range").slider('values', 0));
//				$("#amount-max").val($(".slider-range").slider('values', 1));
				$("#amount-min").val(vmin);
				$("#amount-max").val(vmax);
//				vmin = $("#amount-min").val();
//				vmax = $("#amount-max").val();
				$(".amount").html('€' + stringFormat(vmin) + ' - €' + stringFormat(vmax));
				//$('#test').html($(".slider-range").slider('values', 1));
			}
		});
		
		$(".amount").html('€' + $(".slider-range").slider("values", 0) + ' - €' + $(".slider-range").slider("values", 1));
		$("#amount-min").val($(".slider-range").slider("values", 0));
		$("#amount-max").val($(".slider-range").slider("values", 1));
		
	});
    
//    $("li a[href|=#tabs]").click(function(){
//    	$(this).parent().parent().parent().children('div[id|=tabs]').hide();
//    	$($(this).attr('href')).show();
//    	return false;
//    });
    
    // Переключение типа сделки в блоке поиска в правой колонке
    $("li a[href*=sell]").click(function(){
    	$('#offer-type').val('sell');
    	$("li a[href|=#tabs]").parent().addClass('dimm');
    	$(this).parent().removeClass('dimm');
    	$('#amount-min').val(0);
    	$('#amount-min').val(sellMax);
    	$(".slider-range").slider('option','max',sellMax);
    	$(".slider-range").slider('option','values',[0,sellMax]);
    	$(".slider-range").slider('option','step',1000);
    	
    	$(".amount").html('€' + stringFormat(0) + ' - €' + stringFormat(sellMax));
		//$("#amount-min").val($(".slider-range").slider("values", 0));
		//$("#amount-max").val($(".slider-range").slider("values", 1));
    	return false;
    });
    $("li a[href*=rent]").click(function(){
    	$('#offer-type').val('rent');
    	$("li a[href|=#tabs]").parent().addClass('dimm');
    	$(this).parent().removeClass('dimm');
    	$('#amount-min').val(0);
    	$('#amount-min').val(rentMax);
    	$(".slider-range").slider('option','max',rentMax);
    	$(".slider-range").slider('option','values',[0,rentMax]);
    	$(".slider-range").slider('option','step',50);
    	
    	$(".amount").html('€' + stringFormat(0) + ' - €' + stringFormat(rentMax));
		//$("#amount-min").val($(".slider-range").slider("values", 0));
		//$("#amount-max").val($(".slider-range").slider("values", 1));
    	return false;
    });
    
    
});

/**
 * Форматирование числа по разрядам
 * @param str
 * @returns
 */
function stringFormat(str)
{
	str = String(str);
	ns = new String;
	k = 1;
	for (var i=str.length-1;i>=0;i--)
	{
		//ns = str[i] + ns;
		ns = str.charAt(i) + ns;
		if (k % 3 == 0) ns = " " + ns;
		k++
	}
	//alert(ns);
	return ns;
}
