(function($) {
    function render_map($el) {
        var $markers = $el.find('.marker');
        var is_navionics = $el.hasClass('acf-map-navionics') && acnl_maps && acnl_maps.navionics;
        var map_center;
        var args = {
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        if (is_navionics) {
            args.center = new google.maps.LatLng($el.data('lat'), $el.data('lng'));
            args.mapTypeControlOptions = {
                mapTypeIds: [google.maps.MapTypeId.HYBRID, JNC.Views.gNavionicsOverlay.CHARTS.SONAR, google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN],
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
            };
        } else {
            args.center = new google.maps.LatLng(0, 0);
        }
        var map = new google.maps.Map($el[0], args);
        map.markers = [];
        $markers.each(function() {
            add_marker($(this), map);
        });
        if (is_navionics) {
            var navionicsSonarChartOverlay = new JNC.Views.gNavionicsOverlay({
                navKey: acnl_maps.navionics,
                chartType: JNC.Views.gNavionicsOverlay.CHARTS.SONAR,
                isTransparent: false
            });
            map.mapTypes.set(JNC.Views.gNavionicsOverlay.CHARTS.SONAR, navionicsSonarChartOverlay);
            map.setMapTypeId(google.maps.MapTypeId.HYBRID);
            // map.setMapTypeId(JNC.Views.gNavionicsOverlay.CHARTS.SONAR);
        } else {
            center_map(map);
        }
    }

    function add_marker($marker, map) {
        var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng'));
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
        map.markers.push(marker);
        if ($marker.html()) {
            var infowindow = new google.maps.InfoWindow({
                content: $marker.html()
            });
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map, marker);
            });
        }
    }

    function center_map(map) {
        var bounds = new google.maps.LatLngBounds();
        $.each(map.markers, function(i, marker) {
            var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng());
            bounds.extend(latlng);
        });
        if (map.markers.length == 1) {
            map.setCenter(bounds.getCenter());
            map.setZoom(12);
        } else {
            map.fitBounds(bounds);
        }
    }
    $(document).ready(function($) {
        $(window).on('load', function() {
            $('.acf-map').each(function() {
                render_map($(this));
            });
        });
        $('#logo_right_button').on('click', '#search_icon', function(e) {
            e.preventDefault();
            setTimeout(function() {
                $('#overlay_background_search #s').get(0).focus();
            }, 100);
        });
    });
})(jQuery);