$(document).ready(function()
{
    $('label[for=place], select#place, select#place + br:first').hide();
    $('label[for=quarter], select#quarter, select#quarter + br:first').hide();

    $('select#province').change(function()
    {
        // Only fire this if province has a value
        if($('select#province').val().length > 0)
        {
            $('label[for=place], select#place, select#place + br:first').show();
            $('label[for=quarter], select#quarter, select#quarter + br:first').hide();

            $('select#place').html('<option>Ładowanie...</option>');

            $.get('/ajax_endpoint.php', {'action': 'place', 'province': $(this).attr('value'), 'type': $('#search_type').attr('value'), 'min_count': 1}, function(response)
            {
                $('select#place').empty();
                $('select#place').append('<option value="">Dowolna</option>');
                selected_place = false;
                if ($.cookie('search_form_vars_'+$('#search_type').attr('value')+'_place') && $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_place').length>0)
                {
                    selected_place = $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_place');
                }
                for(var area in response.areas)
                {
                    if (selected_place == area)
                    {
                        $('select#place').append('<option value="'+area+'" selected="selected">'+area+' ('+response.areas[area]+')</option>');
                    }
                    else
                    {
                        $('select#place').append('<option value="'+area+'">'+area+' ('+response.areas[area]+')</option>');
                    }
                }
                if (selected_place) 
                    $('select#place').change();
            }
            , 'json');
        }
        else
        {
            $('label[for=place], select#place, select#place + br:first').hide();
            $('label[for=quarter], select#quarter, select#quarter + br:first').hide();
            // 'Any' has been chosen => clear the cookies
            $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_province', '');
            $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_place', '');
            $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter', '');
        }
    });

    $('select#place').change(function()
    {
        // Only fire this if place has a value
        if($('select#place').val().length > 0)
        {
        $('label[for=quarter], select#quarter, select#quarter + br:first').show();

        $('select#quarter').html('<option>Ładowanie...</option>');

        $.get('/ajax_endpoint.php', {'action': 'quarter', 'place': $(this).attr('value'), 'type': $('#search_type').attr('value'), 'min_count': 1}, function(response)
        {
            selected_quarters = false;
            $('select#quarter').empty();
            if(response.count > 0)
            {
                $('select#quarter').append('<option value="">Dowolna</option>');
                if ($.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter') && $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter').length>0)
                {
                    if ($.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter').indexOf(',')>0) // multiple values selected
                    {
                        selected_quarters = $.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter').split(',');
                    }
                    else
                    {
                        selected_quarters = [$.cookie('search_form_vars_'+$('#search_type').attr('value')+'_quarter')];
                    }
                }
                for(var area in response.areas)
                {
                    have_selected_quarter = false;
                    if (selected_quarters)
                    {
                    for (quarter in selected_quarters)
                    {
                        if (selected_quarters[quarter] == area)
                        {
                            $('select#quarter').append(sprintf('<option value="%1$s" selected="selected">%1$s (%2$d)</option>', area, response.areas[area]));
                        have_selected_quarter = true;
                        }
                    }
                    if (!have_selected_quarter)
                        $('select#quarter').append(sprintf('<option value="%1$s">%1$s (%2$d)</option>', area, response.areas[area]));

                    }
                    else
                        $('select#quarter').append(sprintf('<option value="%1$s">%1$s (%2$d)</option>', area, response.areas[area]));
                }
            }
            else
                $('select#quarter').append('<option value="">Dowlona</option>');

        }
            , 'json');
        }
        else
        {
            $('label[for=quarter], select#quarter, select#quarter + br:first').hide();	
            $('select#quarter').empty();
        }
    });

    $('select#mnb').change(function()
    {
        if(parseInt($(this).val()) > parseInt($('select#mxb').val()))
            $('select#mxb').val($(this).val());
    });

    $('select#mxb').change(function()
    {
        if(parseInt($(this).val()) < parseInt($('select#mnb').val()))
            $('select#mnb').val($(this).val());
    }); 

    $('select#mnp').change(function()
    {
        if(parseInt($(this).val()) > parseInt($('select#mxp').val()))
            $('select#mxp').val($(this).val());
    });

    $('select#mxp').change(function()
    {
        if(parseInt($(this).val()) < parseInt($('select#mnp').val()))
            $('select#mnp').val($(this).val());
    });

    // Only fire this event if the province has a selected value
    if($('select#province').val().length > 0)
    {
        $('select#province').change();
    }


    $('select#tt').change(function()
    {
        if ( $(this).val() )
        {
            $.get('/ajax_endpoint.php', {'action': 'tt', 'commercial_type': $(this).val()}, function(response)
            {
                $('select#mnp, select#mxp').empty();
                $('select#mnp, select#mxp').append('<option value="">dowolna</option>');
                $('select#mnp, select#mxp').append(response);
            }
        , 'json');
        }
    });

});
