function less_then_validator(left,right, validator_element){
    /* Sprawdz czy wartosc left jest mniejsz od wartosci right. 
     * Jeżeli nie pokaż komunikat validator */
    var cb = function(){
        var l = $(left).val();
        var r = $(right).val();
        try{
            l = parseInt(l);
        }catch(e){ }
        try{
            r = parseInt(r);
        }catch(e){ }
        if((l!='')&&(r!='')&&(l>r)){
            $(validator_element).show();
        }else{
            $(validator_element).hide();
        }
    }
    $(left).change(cb);
    $(right).change(cb);
}

function disable_less(src_elem, dest_elem){
    var cb = function(){
        var l = $(this).val();
        try{ l = parseInt(l);}catch(e){};
        $('option',dest_elem).each(function(){
                r = $(this).val();
                var disable = false;
                if(r){
                    if(parseInt(r)<l){
                        disable = true;
                    }
                }
                this.disabled=disable;
        });
    }
    $(src_elem).change(cb);
}

$(document).ready(function(){
    less_then_validator('#id_l_f','#id_l_t','#l_valid')
    less_then_validator('#id_r_f','#id_r_t','#r_valid')
    less_then_validator('#id_a_f','#id_a_t','#a_valid')
    less_then_validator('#id_aa_f','#id_aa_t','#aa_valid')
    less_then_validator('#id_p_f','#id_p_t','#p_valid')
    $("#search_form input[name='what']").change(function(){
        $('#id_w').change();
        })
    $(".searchForm input[name='what']").change(function(){
        this.form.submit();
        })
    $("#mini_search_t input[name='what']").change(function(){
        $('#mini-id_w').change();
        })
    $('#id_order').change(function(){
        this.form.submit()
    });
	$('#id_w').change(function(){
		jQuery('#id_p').load('/t/','q='+this.value+'&what='+$('input[name=\"what\"]:checked').val()).change()
		})
	$('#id_p').change(function(){
		jQuery('#id_g').load('/t/','q='+this.value+'&what='+$('input[name=\"what\"]:checked').val()).change()
		})

	$('#mini-id_w').change(function(){
		jQuery('#mini-id_p').load('/t/','q='+this.value+'&what='+$('input[name=\"what\"]:checked').val()).change()
		})
	$('#mini-id_p').change(function(){
		jQuery('#mini-id_g').load('/t/','q='+this.value+'&what='+$('input[name=\"what\"]:checked').val()).change()
		})
	$('#mini-id_q').defaultvalue('deweloper, inwestycja, miejscowość, ulica.');
});

$(document).ready(function() {
    var little_carousels = {};
    $('.activateLittleCarousel').each(function(idx) {
        var $container = $(this);
        var $control_box = $('.control-box', this);
        var url = $('.little-carousel-url', $control_box).text();
        $container.hover(function() {
            if (!little_carousels[idx]) {
                little_carousels[idx]= {'url': url, 'idx': 0, 'container': $container};
            }
            little_carousels[idx].active = true;
        }, function() {
            delete little_carousels[idx].active;
        });
    });
    var process_carousels = function() {
        for (var key in little_carousels) {
            var value = little_carousels[key];
            if (!value.active) continue;
            if ((value.response == undefined) && (!value.fetching)) {
                value.fetching = true;
                $.getJSON(value.url, function(data) {
                    value.response = data;
                });
            }
            if (value.response) {
                value.idx += 1;
                if (value.idx > value.response.length) value.idx = 0;
                $('img', value.container).attr('src', value.response[value.idx]);
            }
        }
    };
    window.setInterval(process_carousels, 1000)
});
$(document).ready(function(){
		$('#id_q').defaultvalue('deweloper, inwestycja, miejscowość, ulica.');
		$('#id_q').autocomplete(url_search_complete,{
			width:'208px',
			minChars: 2,
			cacheLength: 10,
			formatResult: function(row, i, max){
				return row.name;
			},
			formatItem: function(row, i, max) {
				return row.name+'<span class="address">'+row['_terc_cache']['name']+'&nbsp;&nbsp;'+row['address']+'</div>';
			},
			parse: function(data) {
				return $.map(eval(data), function(row) {
					return {
						data: row,
						value: row.name,
						result: row.name
					}
				});
			}
		})
		$('#id_q').bind('result',function(event, item) {
			location.href = 'http://'+window.location.host+'/i/pokaz/'+item['_developer_cache']['slug']+'/'+item['slug']+'/';
		});
});
