(function ($, w, d, h, b) {
  var ListingDetail = {
    slideshow: {
      default: function(){
        /** Main Slideshow */
        $('.listings-slideshow').slick({
          infinite: true,
          slidesToShow: 1,
          slidesToScroll: 1,
          autoplay: true,
          autoplaySpeed: 4000,
          dots: false,
          prevArrow: '.listings-slideshow-prev',
          nextArrow: '.listings-slideshow-next',
          fade: true,
          draggable: false,
        });
        
        centerMode = false;
        if($('.listings-slideshow-img').length > 6){
          centerMode = true;
        }
        
        /** Thumbnail Slideshow */
        $('.listings-slideshow-thumbnail').slick({
          infinite: true,
          slidesToShow: 7,
          slidesToScroll: 1,
          autoplay: true,
          autoplaySpeed: 4000,
          dots: false,
          arrows: false,
          focusOnSelect: true,
          centerMode: centerMode,
          centerPadding: 0,
          responsive: [
            {
              breakpoint: 992,
              settings: {
                slidesToShow: 5,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 768,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 1,
              }
            },
          ]
        });
        
        $('.listings-slideshow-thumbnail .slick-slide').click(function(){
          $('.listings-slideshow').slick('slickGoTo', $(this).data('slick-index'));
        });
        
        $('.listings-slideshow').on('beforeChange', function(event, slick, currentSlide, nextSlide){
          $('.listings-slideshow-thumbnail').slick('slickGoTo', nextSlide);
        });
        
      },
      
      frame1: function (){
        /** Main */
        $main = $('.listings-slideshow-layout-a');
        
        $main.slick({
          slidesToShow: 1,
          slidesToScroll: 1,
          dots: false,
          prevArrow: '.listings-slideshow-layout-a-prev',
          nextArrow: '.listings-slideshow-layout-a-next',
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-a-thumbnail',
        });
        
        /** Thumbnail */
        $thumbnail = $('.listings-slideshow-layout-a-thumbnail');
        
        
        $thumbnail.slick({
          slidesToShow: 6,
          slidesToScroll: 1,
          dots: false,
          arrows: false,
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-a',
          responsive: [
            {
              breakpoint: 992,
              settings: {
                slidesToShow: 4,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 768,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 481,
              settings: {
                slidesToShow: 2,
                slidesToScroll: 1,
              }
            },
          ]
        });
        
      },
      
      frame2: function () {
        /** Main */
        $main = $('.listings-slideshow-layout-b');
        
        $main.slick({
          slidesToShow: 1,
          slidesToScroll: 1,
          dots: false,
          prevArrow: '.listings-slideshow-layout-b-prev',
          nextArrow: '.listings-slideshow-layout-b-next',
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-b-thumbnail',
        });
        
        /** Thumbnail */
        $thumbnail = $('.listings-slideshow-layout-b-thumbnail');
        
        $thumbnail.slick({
          slidesToShow: 6,
          slidesToScroll: 1,
          dots: false,
          arrows: false,
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-b',
          responsive: [
            {
              breakpoint: 992,
              settings: {
                slidesToShow: 4,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 768,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 481,
              settings: {
                slidesToShow: 2,
                slidesToScroll: 1,
              }
            },
          ]
        });
        
      },
      
      frame3: function () {
        /** Main */
        $main = $('.listings-slideshow-layout-c');
        
        $main.slick({
          slidesToShow: 1,
          slidesToScroll: 1,
          dots: false,
          prevArrow: '.listings-slideshow-layout-c-prev',
          nextArrow: '.listings-slideshow-layout-c-next',
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-c-thumbnail',
        });
        
        /** Thumbnail */
        $thumbnail = $('.listings-slideshow-layout-c-thumbnail');
        
        $thumbnail.slick({
          slidesToShow: 6,
          slidesToScroll: 1,
          dots: false,
          arrows: false,
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-c',
          responsive: [
            {
              breakpoint: 992,
              settings: {
                slidesToShow: 4,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 768,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 481,
              settings: {
                slidesToShow: 2,
                slidesToScroll: 1,
              }
            },
          ]
        });
        
      },
      
      frame4: function () {
        /** Main */
        $main = $('.listings-slideshow-layout-d');
        
        $main.slick({
          slidesToShow: 1,
          slidesToScroll: 1,
          dots: false,
          prevArrow: '.listings-slideshow-layout-d-prev',
          nextArrow: '.listings-slideshow-layout-d-next',
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-d-thumbnail',
        });
        
        /** Thumbnail */
        $thumbnail = $('.listings-slideshow-layout-d-thumbnail');
        
        $thumbnail.slick({
          slidesToShow: 6,
          slidesToScroll: 1,
          dots: false,
          arrows: false,
          focusOnSelect: true,
          asNavFor: '.listings-slideshow-layout-d',
          responsive: [
            {
              breakpoint: 992,
              settings: {
                slidesToShow: 4,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 768,
              settings: {
                slidesToShow: 3,
                slidesToScroll: 1,
              }
            },
            {
              breakpoint: 481,
              settings: {
                slidesToShow: 2,
                slidesToScroll: 1,
              }
            },
          ]
        });
        
      },
      
      init: function(){
        this.default();
        this.frame1();
        this.frame2();
        this.frame3();
        this.frame4();
        
        // Loads the very first or current image on the slide
        setTimeout(function(){
          ListingDetail.lazyLoad();
        }, 500);
      }
      
    },
    
    lazyLoad: function(){
      $('.listings-hero .slick-slider').find('[data-slick-lazy]').each(function(){
        $(this).css({'background-image': 'url(' + $(this).data('slick-lazy') + ')'});
      });
    },
    
    shareListings: function () {
      $('.listings-smi a').on('click', function (e) {
        var $this = $(this),
          href = $this.attr('href'),
          title = $this.data('title');
        
        if (!$this.hasClass('mailto')) {
          e.preventDefault();
          
          newWindow = w.open(href, title, 'width=700,height=500');
          newWindow.focus();
        }
      });
    },
    accordion: function () {
      $('.listings-accordion-title').on('click', function () {
        var $this = $(this);
        var $icon = $this.find('i');
        var $content = $this.next();
        var $wrapper = $this.parent();
        
        if ($this.hasClass('active')) {
          $content.prop('aria-hidden', 'true');
          $icon.text($icon.data('icon-open'));
        } else {
          $content.prop('aria-hidden', 'false');
          $icon.text($icon.data('icon-close'));
        }
        
        $content.stop(false, true).slideToggle(300);
        $this.toggleClass('active');
      });
    },
    
    leafletMap: function (){
      if (!$('#listings-map').length) {
        return false;
      }
      
      fullAddress = $('#listing-pre-data').data('full-address');
      lat = $('#listing-pre-data').data('latitude');
      long = $('#listing-pre-data').data('longitude');
      zoom = $('#listing-pre-data').data('zoom');
      
      
      const mapIcon = L.icon({
        iconUrl: 'https://resources.agentimage.com/libraries/images/map-marker/52.png',
        iconSize: [52, 52],
      });
      
      
      // Initialize map to specified coordinates
      var map = L.map('listings-map', {
        center: [lat, long],
        zoom: zoom
      });
      
      marker = L.marker(
        [lat, long], {
          icon: mapIcon,
          alt: fullAddress
        }
      ).addTo(map);
      
      marker.bindPopup(fullAddress, {
        closeButton: false
      }).openPopup();
      
      
      // Add tiles (streets, etc)
      if(mapSettings.maptitler.custom_style != ''){
        mapType = mapSettings.maptitler.custom_style;
      }else{
        mapType = 'https://api.maptiler.com/maps/' + mapSettings.maptitler.map_type + '/style.json?key=' + mapSettings.maptitler.key;
      }
      
      L.mapboxGL({
        attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>',
        accessToken: 'not-needed',
        style: mapType
      }).addTo(map);
      
      map.closePopup();
    },
    
    googleMap: function(){
      if (!$('#listings-map').length) {
        return false;
      }
      
      fullAddress = $('#listing-pre-data').data('full-address');
      lat = $('#listing-pre-data').data('latitude');
      long = $('#listing-pre-data').data('longitude');
      zoom = $('#listing-pre-data').data('zoom');
      
      const map = new google.maps.Map(document.getElementById("listings-map"), {
        zoom: zoom,
        mapTypeId: mapSettings.google.map_type
      });
      
      
      //Pinning marker
      if(lat == '' || long == ''){
        const geocoder = new google.maps.Geocoder();
        
        geocoder.geocode({ address: fullAddress }, (results, status) => {
          if (status === "OK"){
            map.setCenter(results[0].geometry.location);
            
            new google.maps.Marker({
              map: map,
              position: results[0].geometry.location,
            });
          } else {
            console.log("Geocode was not successful for the following reason: " + status);
          }
        });
      }else{
        
        myLatLng = new google.maps.LatLng(lat,long);
        
        map.setCenter(myLatLng);
        
        new google.maps.Marker({
          position: myLatLng,
          map,
          title: fullAddress
        });
      }
      //end of Pinning marker
      
    },
    
    populate: function(){
      listingData = $('#listing-pre-data');
      $('#listings-request form .message textarea').text('RE: ' + listingData.data('full-address'));
      $('#listings-schedule form .message textarea').text('RE: ' + listingData.data('full-address'));
      $('#bot-listings-contact-form form textarea#listings-message').text('RE: ' + listingData.data('full-address'));
      $('.aios-mortgage-calculator-standalone-loan-amount').val(listingData.data('list-price'));
    },
    
    init: function () {
      this.slideshow.init();
      this.shareListings();
      this.accordion();
      this.populate();
        AOS.init();
        
        
        if (mapSettings.map == 'google'){
          this.googleMap();
        }else{
          this.leafletMap();
        }
      
      
    }
  }
  
  $(d).ready(function () {
    /* Initialize all app functions */
    ListingDetail.init();
  });
  
})(jQuery, window, document, 'html', 'body');
;
( function($) {
	$( document ).ready( function() {

		var $document 		= $( document ),
			$window 		= $( window ),
			$viewport 		= $( 'html, body' ),
			$html 			= $( 'html' ),
			$body 			= $( 'body' );

		function __construct() {
			
		}

		/** Instantiate **/
		__construct();

	} );
} )( jQuery );;
