$(document).ready(function(){
    /*
     * Location choice box
     */
    var d = new Dropdown(gId('near_by-dropdown'));
    d.select = function(item) {
        $(item).find('input[type=radio]').attr('checked', 'checked');
        $('#near_by-input').val($(item).find('.title').text()).click();
        $('#filter-form').submit();
    }
    $('#near_by-input').bind('keydown', {context: d}, d.keypress);
    $('#near-by-button-arrow').click(function(){ d.toggle(); return false; })

    /*
     * Time choice box
     */
    var e = new Dropdown(gId('time-dropdown'));
    e.select = function(item) {
        $(item).find('input[type=radio]').attr('checked', 'checked');
        $('#time-input').val($(item).find('.title').text()).click();
        $('#filter-form').submit();
    }
    $('#time-input').bind('keydown', {context: e}, e.keypress);
    $('#time-button-arrow').click(function(){ e.toggle(); return false; })

    /*
     * Filters' styling
     */
    var fd = new Dropdown($('#main-filters-box')[0]);
    fd.select = function(item) {
        item = $(item);
        item.find('input[type=radio]').attr('checked', 'checked');
        title = item.find('label.title').text();
        $('#main-filters-title').text(title);
        $('#filter-form').submit();
    }
    $('#main-filters-button').click(function() {
        fd.toggle();
        return false;
    });
    $('.filters input[type=radio]').change(function() {
        // Prefix for this filter group
        group_prefix = this.id.split('-', 2).join('-'); 
        // Unselect all others
        all_boxes = $('div[id^=' + group_prefix + '-]');
        all_boxes.removeClass('pressed');
        all_boxes.find('img').hide();
        // Select the new one
        box = $('#' + this.id + '-box');
        box.addClass('pressed');
        box.find('img').show();
    });
    
    // Advanced filter mode switch
    toggle_advanced_search($('input[name=adv]').val()=='on');

    // Clicking range fields activates the filter
    $('input[name=from_price], input[name=to_price]').focus(function() {
        $('#filter-p-int').attr('checked', 'checked');
        $('#filter-p-int').change();
    });
    $('input[name=from_date], input[name=to_date]').focus(function() {
        $('#filter-l-int').attr('checked', 'checked');
        $('#filter-l-int').change();
    });

    // Enable datepickers
    $('#filter-from-date, #filter-to-date').datepicker({
        dateFormat: 'yy-mm-dd'
    });

    /* I hate IE 8 */
    /* When label is clicked, IE focuses on the radio button and
     * waits until it is blured, before calling onchange. And also
     * IE disables hidden fields. I hate IE.
     */
    $('input[type=radio]').focus(function(e) {
        e.target.blur();
    });

    // On form submit
    $('#filter-form').submit(function() {
        if ($('#locator').length) {
            var locator = $('#locator')[0]._googlemap.map;
            $('<input type="hidden" name="loclat"/>').
                    appendTo('#filter-form').val(locator.center.lat());
            $('<input type="hidden" name="loclon"/>').
                    appendTo('#filter-form').val(locator.center.lng());
            $('<input type="hidden" name="loczoom"/>').
                    appendTo('#filter-form').val(locator.zoom);
        }
    });
});

/*
 * Switch advanced search mode on/off
 */
function toggle_advanced_search(advanced) {
    if (advanced == true) {
        $('.advanced-search').show();
    } else if (advanced == false) {
        $('.advanced-search').hide();
    } else {
        advanced = false;
        $('.advanced-search').toggle();
        if ($('.advanced-search').is(':visible')) {
            advanced = true;
        }
    }
    
    // Save into field
    if (advanced) {
        $('input[name=adv]').val('on');
    } else {
        $('input[name=adv]').val('');
    }

    $('#advanced-search-button').toggleClass('pressed', advanced);
    $('#header-separator').toggleClass('darker', advanced);
}

