//get info for marker map
function getInfo(name, url, img, city, country) {
    return '<div class="map-infowindow"><div class="map-infowindow-image"><img src="' + img + '"/></div>' +
           '<div class="map-infowindow-text"><span class="map-company"><a href="' + url + '">' + name + '</a></span><br/>' +
           '<span class="map-city">' + city + '</span><br/>' +
           '<span class="map-country">' + country +'</span></div></div>';
}

function show_popup(text) {
    $.fancybox(text);
}

$(document).ready(function() {
    $('#show-map').toggle(function(e) {
        e.preventDefault();
        $('#country-list').show();
    }, function(e){
        e.preventDefault();
        $('#country-list').hide();
    });
});

// ***** Base page *****
$(function() {
    $.ui.autocomplete.prototype._renderItem = function (ul, item) {
        var text = item.label
        if (this.term) {
            text = text.replace(new RegExp("("+this.term+")", "gi"),'<b>$1</b>');
        }
        var $li = $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + text + "</a>")
        if ('class' in item)
            $li.attr('class', item['class'])
        return $li.appendTo(ul);
    }



/*    var category_autocomplete = $('#searchForm #id_category').autocomplete({
        source: category_list,
        minLength: 0
    });
*/

    $("#id_q").autocomplete({
            minLength: 1,
            source: function(request, response) {
                $.ajax({
                    url: search_list_url,
                    type: 'get',
                    dataType: 'json',
                    data: {'q': request.term},
                    success: function(data) {
                        if (data.success) {
                            response(data.items);
                        }
                   }
                });
            },
            select: function(event, ui) {
                if (ui.item.url != false)
                    location.href = ui.item.url;
            }

        });
});


// ***** Company form *****
var get_city_list_url;

function set_city_url(url) { get_city_list_url = url; }

$(function() {
    if ($('input[id$=city_autocomplete]').length) {
        var country_el = $('select[id$=country]');
        $('input[id$=city_autocomplete]').autocomplete({
            source: function(request, response){
                var country = $(country_el).val();
                $.ajax({
                    url: get_city_list_url,
                    type: 'post',
                    dataType: 'json',
                    data: {'q': request.term,
                           'country': country},
                    success: function(data) {
                        response(data.city_list);
                    }
                });
            }
        });
    }
});


// ***** Show and hide errors in forms *****

function show_errors(form, errors) {
    $.each(errors, function(name, list) {
        var el = $(form).find('input[name='+name+']');
        var message = ''
        $(list).each(function(i, text) {
            message += text;
        });
        el.addClass('error');
        el.attr('title', message);
    });
}

function clean_errors(form) {
    $(form).find('.error').removeClass('error').removeAttr('title');
}

