<!-- admin panel js for weatherForecast plugin settings -->

<script>
var weather_default_location_id = '{$config.weatherForecast_wb_location_id}';
var weather_request_url = '{$smarty.const.RL_PLUGINS_URL}weatherForecast/request.php';
{literal}

$(document).ready(function(){
    var input = $('input[name="post_config[weatherForecast_wb_location][value]"][type=text]');
    var pac_input = input.get(0);
    var weather_prev_locatin = input.val();

    if (typeof pac_input == 'undefined')
        return;

    var allow_submit = true;

    $(pac_input).focus(function(){
        allow_submit = false;
    }).blur(function(){
        allow_submit = true;
    });

    // add hidden inputs
    $(pac_input).after('<input type="hidden" name="weather_default_location_id" value="'+weather_default_location_id+'" />');

    // empty selection state simulation
    (function pacSelectFirst(input){
        // store the original event binding function
        var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;

        // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected,
        // and then trigger the original listener.
        function addEventListenerWrapper(type, listener) {
            if (type == 'keydown') {
                var orig_listener = listener;

                listener = function (event) {
                    var suggestion_selected = $('.pac-item-selected').length > 0;
                    if (event.which == 13 && !suggestion_selected) {
                        var simulated_downarrow = $.Event('keydown', {keyCode: 40, which: 40})
                        orig_listener.apply(input, [simulated_downarrow]);
                    }

                    orig_listener.apply(input, [event]);
                };
            }

            // add the modified listener
            _addEventListener.apply(input, [type, listener]);
        }

        if (input.addEventListener) {
            input.addEventListener = addEventListenerWrapper;
        } else if (input.attachEvent) {
            input.attachEvent = addEventListenerWrapper;
        }

    })(pac_input);

    var autocomplete = new google.maps.places.Autocomplete(pac_input);
    google.maps.event.addListener(autocomplete, 'place_changed', function(res) {
        var place = autocomplete.getPlace();

        if (place.formatted_address) {
            input.closest('table.form').find('input[type=submit]').val(lang['loading']).attr('disabled', true);

            $.post(weather_request_url, {item: 'getByLocation', location: place.formatted_address}, function(response){
                input.closest('table.form').find('input[type=submit]').val(lang['save']).attr('disabled', false);

                if (response.status == 'OK') {
                    $('input[name=weather_default_location_id]').val(response.results.city_id);
                } else {
                    input.val(weather_prev_locatin);
                    printMessage('error', response.message);
                }
            }, 'json');
        } else {
            input.val(weather_prev_locatin);
            printMessage('error', 'No data found for your location, autocomplete.getPlace() method failed, please make sure your Google Maps API Key is active or contact Flynax Support.');

            console.log('Weather Forecast Error: geolocation response failed, no place geometry');
        }
    });

    $(pac_input).closest('form').submit(function(e){
        if (!allow_submit) {
            e.stopPropagation();
            return false;
        }
    });

    // widget positioning handler
    var field_position = $('select[name="post_config[weatherForecast_position][value]"]');
    var field_type = $('input[name="post_config[weatherForecast_group_possition][value]"]');
    var field_group = $('select[name="post_config[weatherForecast_group][value]"]');

    var weatherForecast_check = function(){
        var val = field_position.val();

        if (val == 'in_group') {
            field_type.closest('tr').show();
            field_group.closest('tr').show();
        } else {
            field_type.closest('tr').hide();
            field_group.closest('tr').hide();
        }
    }

    weatherForecast_check();
    field_position.change(function(){
        weatherForecast_check();
    });
});

{/literal}
</script>

<!-- admin panel js for weatherForecast plugin settings end -->