(function ($) { 
    "use strict";
    function mainMap() { 
        function locationData(locationURL, locationImg, locationTitle, locationAddress, locationCategory, locationStarRating, locationRevievsCounter, locationStatus) { 
            return ('<div class="map-popup-wrap"><div class="map-popup"><div class="infoBox-close"><i class="fal fa-times"></i></div><a href="' + locationURL + '" class="listing-img-content fl-wrap"><img src="' + locationImg + '" alt=""><span class="map-popup-reviews-count">( ' + locationRevievsCounter + ' reviews )</span></div></a> <div class="listing-content"><div class="listing-content-item fl-wrap"><div class="listing-title fl-wrap"><h4><a href=' + locationURL + '>' + locationTitle + '</a></h4><div class="map-popup-location-info"><i class="fas fa-map-marker-alt"></i>' + locationAddress + '</div></div><div class="map-popup-footer"><a href=' + locationURL + ' class="main-link">Подробнее <i class="fal fa-long-arrow-right"></i></a></div></div></div></div> ')
         }
	    //  Map Infoboxes ------------------
        var locations = [

            [locationData('hvalynskie-termy', '/assets/cache_image/data/images/30/20231228-041814_600x350_9e4.jpg', 'Хвалынские термы', "Парк отдыха «Хвалынь»", 'cafe-cat', "5", "12", "open"  ), 52.52437413613969, 48.066006450927205, 0 , '/assets/cache_image/data/images/30/20231228-041814_100x100_aa2.jpg'],
            [locationData('volernoe-hozajstvo', '/assets/cache_image/data/images/75/20231113-062640_600x350_9e4.jpg', 'Вольерное хозяйство', "Октябрьская ул., 2Б, Хвалынск", 'cafe-cat', "5", "12", "open"  ), 52.498642, 48.078607, 1 , '/assets/cache_image/data/images/75/20231113-062640_100x100_aa2.jpg'],
            [locationData('gornolyzhnyj-kompleks', '/assets/cache_image/data/images/218/20240305-114522_600x350_9e4.jpg', 'Горнолыжный комплекс парк-отель «Хвалынский»', "пос. Дом отдыха Черемшаны-2", 'cafe-cat', "5", "12", "open"  ), 52.50337562760759, 48.04973032325337, 2 , '/assets/cache_image/data/images/218/20240305-114522_100x100_aa2.jpg'],
            [locationData('cheremshanskie-termy', '/assets/cache_image/data/images/181/20231228-031858_600x350_9e4.jpg', 'Черемшанские термы', "поселок Черемшаны-2", 'cafe-cat', "5", "12", "open"  ), 52.503575284138215, 48.05018925006885, 3 , '/assets/cache_image/data/images/181/20231228-031858_100x100_aa2.jpg'],
            [locationData('tyubingovaya-trassa-park-otel-hvalynskij', '/assets/cache_image/data/images/219/20240312-141158_600x350_9e4.jpg', 'Тюбинговая трасса парк-отель «Хвалынский»', "пос. Дом отдыха Черемшаны-2", 'cafe-cat', "5", "12", "open"  ), 52.50328630798561, 48.05064899483247, 4 , '/assets/cache_image/data/images/219/20240312-141158_100x100_aa2.jpg'],
            [locationData('ledovyj-katok-park-otel-hvalynskij', '/assets/cache_image/data/images/220/20240312-143421_600x350_9e4.jpg', 'Ледовый каток парк-отель «Хвалынский»', "пос. Дом отдыха Черемшаны-2", 'cafe-cat', "5", "12", "open"  ), 52.50071744815218, 48.04952764087131, 5 , '/assets/cache_image/data/images/220/20240312-143421_100x100_aa2.jpg'],
            [locationData('prokat-1', '/assets/cache_image/data/images/246/20250115-194941_600x350_9e4.jpg', 'Прокат №1 - прокат горнолыжного снаряжения', " ул. Российской Республики, 1Е", 'cafe-cat', "5", "12", "open"  ), 52.51524, 48.090752, 6 , '/assets/cache_image/data/images/246/20250115-194941_100x100_aa2.jpg'],
            [locationData('kartinnaya-galereya-ks-petrova-vodkina', '/assets/cache_image/data/images/221/20240316-232635_600x350_9e4.jpg', 'Картинная галерея К.С. Петрова-Водкина', "ул. Ленина 89", 'cafe-cat', "5", "12", "open"  ), 52.492776, 48.108673, 7 , '/assets/cache_image/data/images/221/20240316-232635_100x100_aa2.jpg'],
            [locationData('hvalyinskij-kraevedcheskij-muzej', '/assets/cache_image/data/images/80/20231113-131743_600x350_447.jpeg', 'Хвалынский краеведческий музей', "ул. Революционная 120", 'cafe-cat', "5", "12", "open"  ), 52.49313277229232, 48.10470356312938, 8 , '/assets/cache_image/data/images/80/20231113-131743_100x100_ada.jpeg'],
            [locationData('bar-bajbak', '/assets/cache_image/data/images/151/20231204-214323_600x350_9e4.jpg', 'Бар «Байбак»', "Парк отдыха «Хвалынь»", 'cafe-cat', "5", "12", "open"  ), 52.52359435464301, 48.07133511585684, 9 , '/assets/cache_image/data/images/151/20231204-214323_100x100_aa2.jpg'],
            [locationData('razvlekatelnyj-centr-zharpticca', '/assets/cache_image/data/images/225/20240324-105200_600x350_9e4.jpg', 'Развлекательный центр «Жарптицца»', " ул. Достоевского, 12", 'cafe-cat', "5", "12", "open"  ), 52.491849, 48.106984, 10 , '/assets/cache_image/data/images/225/20240324-105200_100x100_aa2.jpg'],

        ];
	    //   Map Infoboxes end ------------------
        var map = new google.maps.Map(document.getElementById('map-main'), { 
            zoom: 14,
            scrollwheel: true,
            center: new google.maps.LatLng(52.49380586291523, 48.10999329634643),
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            zoomControl: false,
            mapTypeControl: false,
            scaleControl: false,
            panControl: false,
            fullscreenControl: true,
            navigationControl: false,
            streetViewControl: false,
            animation: google.maps.Animation.BOUNCE,
            gestureHandling: 'cooperative',
            styles: [{
                    "featureType": "administrative",
                    "elementType": "labels.text.fill",
                    "stylers": [{
                        "color": "#444444"
                    }]
                },
                {
                    "featureType": "poi",
                    "stylers": [{
                        "visibility": "off"
                    }]
                }
            ]
         });
        var boxText = document.createElement("div");
        boxText.className = 'map-box'
        var currentInfobox;
        var boxOptions = { 
            content: boxText,
            disableAutoPan: true,
            alignBottom: true,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-150, -55),
            zIndex: null,
            boxStyle: { 
                width: "300px"
             },
            closeBoxMargin: "0",
            closeBoxURL: "",
            infoBoxClearance: new google.maps.Size(1, 1),
            isHidden: false,
            pane: "floatPane",
            enableEventPropagation: false,
         };








      var markerCluster, overlay, i;
      var allMarkers = [];

      var clusterStyles = [
        { 
          textColor: 'white',
          url: '',
          height: 50,
          width: 50
         }
      ];

      var ib = new InfoBox();
             google.maps.event.addListener(ib, "domready", function () { 
                cardRaining();
 
             });
	  var markerImg;
	  var markerCount;
      for (i = 0; i < locations.length; i++) { 
             var labels = '123456789';
		markerImg = locations[i][4];
		markerCount = locations[i][3] + 1;
        var overlaypositions = new google.maps.LatLng(locations[i][1], locations[i][2]),

        overlay = new CustomMarker(
         overlaypositions, map,{  marker_id: i },  markerImg , markerCount
        );

        allMarkers.push(overlay);

        google.maps.event.addDomListener(overlay, 'click', (function(overlay, i) { 

        return function() { 
             ib.setOptions(boxOptions);
             boxText.innerHTML = locations[i][0];
             ib.close();
             ib.open(map, overlay);
             currentInfobox = locations[i][3];
 
                    var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
                    map.panTo(latLng);
                    map.panBy(0, -110);

            google.maps.event.addListener(ib,'domready',function(){ 
              $('.infoBox-close').click(function(e) { 
                  e.preventDefault();
                  ib.close();
                  $('.map-marker-container').removeClass('clicked infoBox-opened');
               });

             });

           }
         })(overlay, i));

       }
        var options2 = { 
            imagePath: '',
            styles: clusterStyles,
            minClusterSize: 3
         };
        markerCluster = new MarkerClusterer(map, allMarkers, options2);
        google.maps.event.addDomListener(window, "resize", function () { 
            var center = map.getCenter();
            google.maps.event.trigger(map, "resize");
            map.setCenter(center);
         });
 
        $('.map-item').on("click", function (e) { 
            e.preventDefault();
            map.setZoom(15);
            var index = currentInfobox;
            var marker_index = parseInt($(this).attr('href').split('#')[1], 10);
            google.maps.event.trigger(allMarkers[marker_index-1], "click");
            if ($(window).width() > 1064) { 
                if ($(".map-container").hasClass("fw-map")) { 
                    $('html, body').animate({ 
                        scrollTop: $(".map-container").offset().top + "-110px"
                     }, 1000)
                    return false;
                 }
             }
         });
        $('.nextmap-nav').on("click", function (e) { 
            e.preventDefault();
             map.setZoom(15);
            var index = currentInfobox;
            if (index + 1 < allMarkers.length) { 
                google.maps.event.trigger(allMarkers[index+ 1], 'click');
             } else { 
                google.maps.event.trigger(allMarkers[0], 'click');
             }
         });
        $('.prevmap-nav').on("click", function (e) { 
            e.preventDefault();
            map.setZoom(15);
            if (typeof (currentInfobox) == "undefined") { 
                google.maps.event.trigger(allMarkers[allMarkers.length - 1], 'click');
             } else { 
                var index = currentInfobox;
                if (index - 1 < 0) { 
                    google.maps.event.trigger(allMarkers[allMarkers.length - 1], 'click');
                 } else { 
                    google.maps.event.trigger(allMarkers[index - 1], 'click');
                 }
             }
         });
      // Scroll enabling button
      var scrollEnabling = $('.scrollContorl');

      $(scrollEnabling).click(function(e){ 
          e.preventDefault();
          $(this).toggleClass("enabledsroll");

          if ( $(this).is(".enabledsroll") ) { 
             map.setOptions({ 'scrollwheel': true });
           } else { 
             map.setOptions({ 'scrollwheel': false });
           }
       });		
        var zoomControlDiv = document.createElement('div');
        var zoomControl = new ZoomControl(zoomControlDiv, map);
        function ZoomControl(controlDiv, map) { 
            zoomControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(zoomControlDiv);
            controlDiv.style.padding = '5px';
            var controlWrapper = document.createElement('div');
            controlDiv.appendChild(controlWrapper);
            var zoomInButton = document.createElement('div');
            zoomInButton.className = "mapzoom-in";
            controlWrapper.appendChild(zoomInButton);
            var zoomOutButton = document.createElement('div');
            zoomOutButton.className = "mapzoom-out";
            controlWrapper.appendChild(zoomOutButton);
            google.maps.event.addDomListener(zoomInButton, 'click', function () { 
                map.setZoom(map.getZoom() + 1);
             });
            google.maps.event.addDomListener(zoomOutButton, 'click', function () { 
                map.setZoom(map.getZoom() - 1);
             });
         }
		
		
      // Geo Location Button
      $(".geoLocation, .input-with-icon.location a").on("click", function (e) { 
          e.preventDefault();
          geolocate();
       });

      function geolocate() { 

          if (navigator.geolocation) { 
              navigator.geolocation.getCurrentPosition(function (position) { 
                  var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                  map.setCenter(pos);
                  map.setZoom(12);
				  
		  var avrtimg = $(".avatar-img").attr("data-srcav");
         var markerIcon3 = { 
            url: avrtimg,
         }		
        var marker3 = new google.maps.Marker({ 
            position: pos,
             map:  map,
 
            icon: markerIcon3,
            title: 'Your location'
         });			  
	 var myoverlay = new google.maps.OverlayView();
       myoverlay.draw = function () { 
           // add an id to the layer that includes all the markers so you can use it in CSS
           this.getPanes().markerLayer.id='markerLayer';
        };
       myoverlay.setMap(map);			  
				  
               });
           }
       }		
     }
	
 	
	
	
	
 
	
	
	
    // Custom Map Marker
    // ----------------------------------------------- //

    function CustomMarker(latlng, map, args,   markerImg , markerCount) { 
      this.latlng = latlng;
      this.args = args;
 
	  this.markerImg = markerImg;
	  this.markerCount = markerCount;
      this.setMap(map);
     }

    CustomMarker.prototype = new google.maps.OverlayView();

    CustomMarker.prototype.draw = function() { 

      var self = this;

      var div = this.div;

      if (!div) { 

        div = this.div = document.createElement('div');
        div.className = 'map-marker-container';

        div.innerHTML = '<div class="marker-container">'+
							'<span class="marker-count">'+self.markerCount+'</span>'+
                            '<div class="marker-card">'+
                               '<div class="marker-holder"><img src="'+self.markerImg+'" alt=""></div>'+
                            '</div>'+
                         '</div>'
 

        // Clicked marker highlight
        google.maps.event.addDomListener(div, "click", function(event) { 
            $('.map-marker-container').removeClass('clicked infoBox-opened');
            google.maps.event.trigger(self, "click");
            $(this).addClass('clicked infoBox-opened');
         });


        if (typeof(self.args.marker_id) !== 'undefined') { 
          div.dataset.marker_id = self.args.marker_id;
         }

        var panes = this.getPanes();
        panes.overlayImage.appendChild(div);
       }

      var point = this.getProjection().fromLatLngToDivPixel(this.latlng);

      if (point) { 
        div.style.left = (point.x) + 'px';
        div.style.top = (point.y) + 'px';
       }
     };

    CustomMarker.prototype.remove = function() { 
      if (this.div) { 
        this.div.parentNode.removeChild(this.div);
        this.div = null; $(this).removeClass('clicked');
       }
     };

    CustomMarker.prototype.getPosition = function() {  return this.latlng;  };

    // -------------- Custom Map Marker / End -------------- //	
	
	
var head = document.getElementsByTagName( 'head' )[0];

// Save the original method
var insertBefore = head.insertBefore;

// Replace it!
head.insertBefore = function( newElement, referenceElement ) { 

    if ( newElement.href && newElement.href.indexOf( 'https://fonts.googleapis.com/css?family=Roboto' ) === 0 ) { 
        return;
     }

    insertBefore.call( head, newElement, referenceElement );
 };	
	
	    var map = document.getElementById('map-main');
    if (typeof (map) != 'undefined' && map != null) { 
        google.maps.event.addDomListener(window, 'load', mainMap);
     }
	
	
	
	
 })(this.jQuery);