$(window).load(function () {
    $('#accept-google-maps-helpdesk').on('click', function(event) {
        event.preventDefault();
        $('#google-maps-helpdesk-notice').toggle();
        $('#map-helpdesk').toggle();

        $.getScript(
            'https://maps.googleapis.com/maps/api/js?sensor=false&key=AIzaSyBFfXCJLOwYDfb_CrEW8KexIQ5PmwfL41A',
            function () {

                var latlng = new google.maps.LatLng(49.8337471,8.4979723);
                var options = {
                    zoom: 13,
                    center: latlng
                }
                var map = new google.maps.Map(document.getElementById("map-helpdesk"), options);
                var geocoder = new google.maps.Geocoder();
                google.maps.event.addListener(map, 'click', function(event) {
                    geocoder.geocode({
                        'latLng': event.latLng
                    }, function(results, status) {
                        if(status == google.maps.GeocoderStatus.OK) {
                            if(results[0]) {
                                var street  = '';
                                var houseNr = '';
                                
                                $.each(results[0].address_components, function(i, address_component) {
                                    //alert(address_component.toSource());
                                    if (address_component.types[0] == "route") {
                                        street = address_component.long_name;
                                    }
                                    if (address_component.types[0] == "street_number") {
                                        houseNr = address_component.long_name;
                                    }
                                    if (address_component.types[0] == "locality") {
                                        $("#city").val(address_component.long_name);
                                    }
                                    if (address_component.types[0] == "postal_code") {
                                        $("#zip").val(address_component.long_name);
                                    }
                                });
                                $("#street").val(street + " " + houseNr);
                            }
                        }
                    });
                });
            }
        );
    });
        

    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    };
  
    var meanBarLogo = '<div class="logo">' +
                  '<img  srcset="/typo3conf/ext/riedstadt/Resources/Public/Images/header-slogan-2019-white-1x.png 1x, /typo3conf/ext/riedstadt/Resources/Public/Images/header-slogan-2019-white-2x.png 2x" '+
                  'src="/typo3conf/ext/riedstadt/Resources/Public/Images/header-slogan-2019-white-1x.png" alt="Riedstadt, die Büchnerstadt"></div>';
  
    var checkTableSizeForResponsiveFormatting = function() {
      var contentAreaWidth = $("#page > div > div > div > div:first-child").width();
      $("#page > div > div > div table").each(function() {
          if (contentAreaWidth < $(this).width()) {
              if (!$(this).parent("div.table-responsive").length) {
                  $(this).wrap("<div class=\"table-responsive\"></div>");
              }
          }
      });
    }
  
    $("#mobile-nav").meanmenu({meanScreenWidth: "767"});
    $(".mean-bar").append(meanBarLogo);
    checkTableSizeForResponsiveFormatting();
    
    $(window).resize(function () {
        $(".mean-bar").append(meanBarLogo);  
        checkTableSizeForResponsiveFormatting();
    })
  
    $("figure.image a[data-fancybox] img").each(function() {
        var title = $(this).attr("title");
        $(this).parent("a").data("caption", title);
    });

    $(document).ajaxError(function(e,xhr,opts,err) {
        var status = (xhr||{}).status;
        xhr = null;
        setTimeout(function() {
            throw(e.type+'|'+status+opts.url);
        });
    });
    
    if ($(".owl-gallery").length) {
        $(".owl-gallery").each(function() {
            $(this).owlCarousel({ 
                autoPlay: 3000,
                itemsScaleUp: true,
                items : 6,
                itemsDesktop : [1199,5],
                itemsDesktopSmall : [991,3],
                itemsTablet: [767,3],
                itemsMobile : [479,2]
            });
        });
    }
  
});
