function select_region() {
    return false;
}
function show_menu(w) {
    /* Show the UL inside the given instance. */
    $(w).find('ul').show();
    return false;
}
function hide_menu(w) {
    /* Hides the UL inside the given instance. */
    $(w).find('ul').hide();
    return false;
}
function clear_field(w) {
    /* When hit, if the search field has the default initial value, set it to a
     * blank string. */
    if(w.value == 'Pesquisar')
        w.value = '';
    return false;
}
function reset_field(w) {
    /* After editted, if the search field is blank, set it back to the the
     * default initial value. */
    if(w.value == '')
        w.value = 'Pesquisar';
    return false;
}

function validate_form(w, p) {
    /* Before send the form, get the form action from the choosen category (on
     * the select widget). If no category was choosed, return false (do not
     * send the form). */
    var s = $(w).find('select').val();
    if(s == '')
        return false;
    $(w).attr('action', p + s + '/');
}
function popup(content, can_close) {
    /* Show a lightbox popup with determined content. This is a general
     * purpouse function. */
    var code_before = '<div class="popup"><table class="light-wrapper"><tr><td><div class="light">';
    var code_after = '</div></td></tr></table><div class="dark" onClick="return hide_popup()"></div></div>';
    if(typeof(can_close)=='undefined')
        can_close = false;
    if(can_close)
        code_before += '<a href="#" onclick="return hide_popup()" class="close">&nbsp;</a>';
    var code = code_before + content + code_after;
    $('body').prepend(code);
    return false;
}
function hide_popup() {
    /* Hides the opened popup. */
    $('.popup').hide();
    $('.popup').remove();
    return false;
}
function choose_location() {
    /* Opens a popup with a form for choose a location name and a list with all
     * locations grouped by province and region. */
    $.ajax({
        url: '/escolher-localidade/?next='+window.location.pathname+'&ajax=1',
        dataType: 'html',
        type: 'GET',
        success: function(data) {
            data = '<div class="locations-wrapper">'+data+'</div>';
            popup(data, 1);
            $('.locations ul span').click(toggle_location_item);
        }
    });
    return false;
}
function toggle_location_item() {
    /* Expands or collapses the list of cities on a region or regions on a
     * province. */
    var ul = $(this).parent().children('ul');
    if (ul.is(':visible')) {
        ul.hide();
    } else {
        ul.show();
    }
    $(this).toggleClass('opened');
}

var option_index = 1;

function show_options(w, e) {

    var qs = w.value.toLowerCase();
    var source = $('.locations .locations-list ul li');
    var output = $('.locations .location-search');
    var items = [];
    
    /* clear the form when the user hits ESC. */
    if(e.keyCode==27) {
        w.value = '';
        qs = '';
    }

    /* search for the query string on each li. Only starts the search when
     * there is at least 2 characters on the qs. */
    if(qs.length >= 2) {
        source.each(function() {
            var anchor = $(this).find('a:first');
            var name = anchor.text().toLowerCase();
            if(name.search(qs) == 0)
                items.push([name, anchor.attr('href')]);
        });
    }
    
    /* when the user hits the up or down arrow, run through the filtered items. */
    if(e.keyCode==40 || e.keyCode==38) {
        if (e.keyCode==40 && option_index<items.length)
            option_index = option_index + 1;
        if (e.keyCode==38 && option_index>1)
            option_index = option_index - 1;
    }
    
    /* make the control index stay between 1 and the last item index. */
    if(option_index>items.length)
        option_index = items.length;
    if(option_index==0)
        option_index = 1;

    /* when the user hits ENTER or right arrow, open the selected item. */
    if(e.keyCode==13 || e.keyCode==39)
        if(option_index>0 && option_index<=items.length)
            window.location = items[option_index-1][1];

    /* showing the filtered list. */
    if(items.length>0) {
        var s = "";
        for(i=0; i<items.length; i++) {
            s += '<li><a href="'+ items[i][1] + '">';
            if(option_index==(i+1)) s += '<b>';
            s += items[i][0];
            if(option_index==(i+1)) s += '</b>';
            s += '</a></li>';
        }
        output.html(s);
        output.show();
    } else {
        output.html('');
        output.hide();
    }

    return false;
}

