$(function() {
    $('input.geocode').each(function() {
        $(this).after(
            '<span style="display:none" class="geocode-progress">&nbsp;</span>' + 
            '<span style="display:none" class="geocode-success">&nbsp;</span>' + 
            '<span style="display:none" class="geocode-geotags"></span>' + 
            '<div style="display:none" class="geocode-matches"></div>'
        );
    });

    $('input.geocode').change(function() {
        var input = $(this);

        input.siblings('.geocode-progress').css('display', '');
        input.siblings('.geocode-success').css('display', 'none');
        input.siblings('.geocode-geotags').css('display', 'none');
        input.siblings('.geocode-geotags-notice').css('display', 'none');

        $.getJSON('/geo/ajax/geocode?q=' + input.val(), function(data, textStatus) {
            if (textStatus == 'success') {
                if (!data.length) {
                    input.siblings('.geocode-matches').html('<p>Could not geocode the location you provided.</p>');
                    input.siblings('.geocode-matches').show(500)
                } else if (data.length > 1) {
                    var items = [];
                    $.each(data, function(i) {items.push('<li><a href="javascript:void(0)">' + this[0] + '</a></li>');});
                    input.siblings('.geocode-matches').html('<p>Found several matches, please select one below:</p><ul>' + items.join('') + '</ul>');
                    $('ul li', input.siblings('.geocode-matches')[0]).click(function() {
                        input.val($(this).text());
                        input.siblings('.geocode-success').css('display', '');
                        input.siblings('.geocode-matches').html('');
                        input.siblings('.geocode-matches').hide();
                    });
                    input.siblings('.geocode-matches').show(500);
                } else {
                    input.val(data[0][0]);
                    input.siblings('.geocode-success').css('display', '');
                    input.siblings('.geocode-matches').html('');
                    input.siblings('.geocode-matches').hide();

                    if (input.hasClass('geotags')) {
                        input.siblings('.geocode-geotags').html(
                            '<span class="geocode-progress">&nbsp;</span>' +
                            'Geocoding tags...').css('display', '');
                        $.getJSON(
                            '/geo/ajax/geotags?q=' + input.val() +
                            '&generalize=' + input.val() +
                            '&lps=2', function(data, textSuccess) {
                                if (textStatus == 'success') {
                                    input.siblings('.geocode-geotags').css('display', 'none');
                                    //input.siblings('.geocode-geotags-notice').show(500);
                                    //$('.geocode-geotags-notice-suggestions', input.siblings('.geocode-geotags-notice')[0]).html(input.val());
                                    if (input[0].on_geotags) {
                                        input[0].on_geotags(data);
                                    }
                            }
                        });
                    }
                }
                input.siblings('.geocode-progress').css('display', 'none');
            }
        });
    });
});
