(function( $ ) {
  'use strict';

  var loadingListingCount = false;

  $(document).ready(function(){
    $('select').addClass('selectrp');
    $('.selectrp').select2({});

    if($('#agent_nav').length) {
      $('.section-title .columns').append($('#agent_nav'));
      if($('#testimonials').length==0) {
        $('#agent_nav li a[href="#testimonials"]').hide();
      }
      var $el = $('.section-title .thumb');
      $el.css('cursor','pointer');
      $el.on('click',function(e) {
        e.preventDefault();
        $('html, body').animate({
          scrollTop: 0
        }, 800);
      });
    }

    $('#agent_nav li a').on('click',function(e) {
      e.preventDefault();
      var _id = $(this).attr('href');
      $('html, body').animate({
        scrollTop: $(_id).offset().top-270
      }, 800);
    });

    if($('.realplus').length>0) {

      $('.realplus .back_btn').on('click',function(e) {
        window.history.back();
      });

      $('.realplus .update_btn').on('click',function(e) {
        var lid = $(this).data('lid');
        var uid = $(this).data('uid');
        $.ajax({
          type: "post", url: ajaxurl, data: {uid: uid, lid: lid, action: 'addUpdate', _ajax_nonce: nonce},
          success: function (msg) {
            if(msg==333) {
              $('.realplus .update_btn').addClass('active');
              $('.realplus .update_btn').html('<i class="fa fa-envelope aria-hidden="true"></i> Stop Updates');
            } else if(msg==222) {
              $('.realplus .update_btn').removeClass('active');
              $('.realplus .update_btn').html('<i class="fa fa-envelope aria-hidden="true"></i> Get Updates');
            }
          }
        });
      });

      $('.realplus .save_btn').on('click',function(e) {
        var lid = $(this).data('lid');
        var set = $(this).data('set');
        $.ajax({
          type: "post", url: ajaxurl, data: {set: set, lid: lid, action: 'saveListing', _ajax_nonce: nonce},
          success: function (msg) {
            if(msg==111) {
              $('.realplus .save_btn').addClass('active');
              $('.realplus .save_btn').data('set',0);
              $('.realplus .save_btn').html('<i class="fa fa-heart aria-hidden="true"></i> Listing Saved');
            } else if(msg==222) {
              $('.realplus .save_btn').removeClass('active');
              $('.realplus .save_btn').data('set',1);
              $('.realplus .save_btn').html('<i class="fa fa-heart aria-hidden="true"></i> Save Listing');
            }
          }
        });
      });

      $('#rp_autocomplete').devbridgeAutocomplete({
        serviceUrl: ajaxurl,
        minChars: 4,
        dataType: 'json',
        type: 'POST',
        params: {action: 'getSearchOptions', _ajax_nonce: nonce},
        onSearchComplete: function (query, suggestions) {console.log(query);}
      });

      $('.realplus .card img.ajaximg').each(function() {
        var card = $(this).parent().parent().parent();
        var lid = card.attr('id');
        var imgtag = $(this);//card.find('img');
        if(baselink) {
          imgtag.attr('src', baselink + '/wp-content/plugins/realplus-listings-idx/public/images/loading.gif');
          $.ajax({
            type: "post", url: ajaxurl, data: {lid: lid, action: 'listingmedia', _ajax_nonce: nonce},
            success: function (imgsrc) {
              if (imgsrc != 404 && imgsrc != '404' && imgsrc != '') {
                var img;
                if (imgsrc.indexOf('.tall') > 0) {
                  img = imgsrc.replace('.tall', '');
                  imgtag.addClass('rp-img-tall');
                } else {
                  img = imgsrc;
                }
                imgtag.attr('src', img);
              } else {
                imgtag.attr('src', baselink + '/wp-content/plugins/realplus-listings-idx/public/images/noPhoto-icon.png');
              }
            }
          });
        }
      });
    }

    // Trigger Form
    $('.property-view-rp-form select').change(function(e){
      var _data = $(this).val();
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'setorder', _ajax_nonce: nonce, data: _data},
        success: function (html) {
          window.location.href = window.location.pathname;
        }
      });
    });

    $('.listing-icons a').on('click',function(e) {
      e.preventDefault();
      var _data = $(this).attr('href').replace('#','');
      $('#rp-wrap').removeClass().addClass('rp-'+_data);
      $('.listing-icons a').removeClass('active');
      $('.listing-icons a[href="#'+_data+'"]').addClass('active');
      if(_data=='map') {
        $('#rp-wrap').hide();
        $('#map-rp').show();
        google.maps.event.trigger(rpmap, 'resize');
        rpmap.fitBounds(latlngbounds);
      } else {
        $('#rp-wrap').show();
        $('#map-rp').hide();
        $('.card').matchHeight(true);
      }
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'setlisttype', _ajax_nonce: nonce, data: _data},
        success: function (html) {
        }
      });
    });

    $('.property-search-form select').on('change',function(){
      if($(this).val()!='Custom') {
        getListingCount();
      }
    });

    $('.property-search-form input').on('keyup',function(){
      getListingCount();
    });

    /*$('#rp-form-submit').on('click',function(e) {
      e.preventDefault();
      var _data = $('#rp-main-form').serialize();
      if($('.realplus input[name="rp_status"]').val()=='188') {
        var _url = listinglink;
      } else {
        var _url = rentallink;
      }
      console.log(_data);
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'setformsessions', _ajax_nonce: nonce, data: _data},
        cache: false,
        headers: {
          "cache-control": "no-cache"
        },
        xhrFields: {
          withCredentials: true
        },
        success: function (html) {
          console.log(html);
          window.location.href=_url;
        }
      });
    });

    $('#rp-short-form').on('submit',function(e) {
      e.preventDefault();
      var _data = $('#rp-short-form').serialize();
      console.log(_data);
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'setformsessions', _ajax_nonce: nonce, data: _data},
        cache: false,
        headers: {
          "cache-control": "no-cache"
        },
        xhrFields: {
          withCredentials: true
        },
        success: function (html) {
          window.location.href='/all-listings';
        }
      });
    });*/

    $('#rp-form-reset').on('click',function(e) {
      e.preventDefault();
      $('.property-search-form')[0].reset();
      $('.property-search-form select[name="rp_search"]').val('');
      $('.property-search-form select[name="min_bed"]').val('');
      $('.property-search-form select[name="min_bath"]').val('');
      $('.property-search-form select[name="l_price"]').val('200000');
      $('.property-search-form select[name="u_price"]').val('4000000');
      $('.property-search-form select[name="neighborhoods"]').val('');
      if($('.realplus input[name="rp_status"]').val()=='188') {
        var _url = listinglink;
      } else {
        var _url = rentallink;
      }
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'resetformsessions', _ajax_nonce: nonce},
        success: function (html) {
          window.location.href=_url;
        }
      });
    });

    function getListingCount() {
      if(!loadingListingCount) {
        loadingListingCount = true;
        setTimeout(loadListingCounts,2000)
      }
    }

    function loadListingCounts() {
      var _data = $('.property-search-form').serialize();
      $.ajax({
        type: "post", url: ajaxurl, data: {action: 'listingcount', _ajax_nonce: nonce, data: _data},
        success: function (html) {
          $('.listingcount span').html(html);
          $('.listingcount').fadeIn(500);
          loadingListingCount = true;
        }
      });
    }

    // Map
    var rpmap, latlngbounds;
    var rpmapdiv = $('#map-rp');
    if (rpmapdiv.length > 0) {
      var markers = rpmapdiv.children();
      var latlng = new google.maps.LatLng(40.765906, -73.976655);
      var defaults = {
        center: latlng,
        zoomControl: true,
        scaleControl: false,
        streetViewControl: true,
        panControl: false,
        mapTypeControl: true,
        overviewMapControl: false,
        scrollwheel: false,
        zoom: 14,
        maxZoom: 18,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.LARGE,
          position: google.maps.ControlPosition.RIGHT_TOP
        }
      };
      var options = $.extend({}, defaults, rpmapdiv.data());
      var map = new google.maps.Map(document.getElementById('map-rp'), options);
      rpmap = map;
      google.maps.event.addListener(map, 'idle', function () {
        $('.gm-style').removeClass('gm-style');
      });

      // Marker
      var mks = [];
      latlngbounds = new google.maps.LatLngBounds();
      for (var i = 0; i < markers.length; i++) {
        var marker_data = $(markers[i]).data();
        var latlng = new google.maps.LatLng(marker_data.latitude, marker_data.longitude);
        var marker_content = '<div class="rp-marker center-marker" data-lat="' + marker_data.latitude + '" data-lng="' + marker_data.longitude + '"><div class="dot"></div></div>';
        if (marker_data.content) marker_content = '<div class="rp-marker" data-ib="ib-' + i + '" data-lat="' + marker_data.latitude + '" data-lng="' + marker_data.longitude + '"><div class="dot"></div></div><div class="info-box ' + marker_data.active + '" id="ib-' + i + '">' + marker_data.content + '</div>';
        var richMarker = new RichMarker({
          position: latlng,
          flat: true,
          anchor: RichMarkerPosition.MIDDLE,
          map: map,
          content: marker_content
        });
        latlngbounds.extend(latlng);
        mks.push(richMarker);
      }
      var mcOptions = {maxZoom: 15, imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'};
      var markerCluster = new MarkerClusterer(map, mks, mcOptions);
      map.fitBounds(latlngbounds);

      google.maps.event.addListenerOnce(map, "idle", function () {
        if (options['zoom'] >= 0) map.setZoom(options['zoom']);
      });

      var styles = [
        {
          stylers: [
            {saturation: -100}
          ]
        }];
      map.setOptions({styles: styles});
    }

    $(document).on('click touchstart', '#map-rp .rp-marker', function () {
      if ($(this).hasClass('center-marker')) return;
      if (!$(this).siblings('.info-box').hasClass('rp-active')) {
        $('.rp-map-wrap .info-box.rp-active').removeClass('rp-active');
        var map = rpmap;
        var center = new google.maps.LatLng($(this).data('lat'), $(this).data('lng'));
        map.panTo(center);
        map_recenter(center, 0, -120, map);
      }

      var card = $(this).siblings('.info-box').find('.card');
      var lid = card.data('lid');
      card.find('.img-wrap img').attr('src', $('#' + lid).find('.img-wrap img').attr('src'));

      $(this).siblings('.info-box').toggleClass('rp-active')
    });

    if(!$('#rp-wrap').hasClass('rp-map')) {
      rpmapdiv.hide();
    }


    $('#tab-details a').hide();

    //var modal_login_script = {"ajax":"http:\/\/www.searchnynow.com\/wp-admin\/admin-ajax.php","redirecturl":"\/property-search\/","loadingmessage":"Checking Credentials..."};

    $('.realplus .card a.login').on('click',function() {
      var url = $(this).data('url');
      var modal_login_script = {"ajax":ajaxurl,"redirecturl":url,"loadingmessage":"Checking Credentials..."};
    });
    if($('#mortgage-price').length>0) {
      $('#lidd_mc_total_amount').val($('#mortgage-price').html());
    }

    /*
     $('.realplus .sale-tab').on('click',function(e){
     e.preventDefault();
     $('.realplus .sale-tab').parent().addClass('active');
     $('.realplus .rent-tab').parent().removeClass('active');
     $('.realplus .rent-prices [name="l_price"]').attr('name','l_price_r');
     $('.realplus .rent-prices [name="u_price"]').attr('name','u_price_r');
     $('.realplus .sale-prices [name="l_price_s"]').attr('name','l_price');
     $('.realplus .sale-prices [name="u_price_s"]').attr('name','u_price');
     $('.realplus .sale-prices').removeClass('hidden').addClass('shown');
     $('.realplus .rent-prices').removeClass('shown').addClass('hidden');
     $('.listingcount strong').html('listings for sale');
     //getListingCount();
     $('.realplus input[name="rp_status"]').val(188);
     $('#rp-form-submit').click();
     });

     $('.realplus .rent-tab').on('click',function(e){
     e.preventDefault();
     $('.realplus .sale-tab').parent().removeClass('active');
     $('.realplus .rent-tab').parent().addClass('active');
     $('.realplus .sale-prices [name="l_price"]').attr('name','l_price_s');
     $('.realplus .sale-prices [name="u_price"]').attr('name','u_price_s');
     $('.realplus .rent-prices [name="l_price_r"]').attr('name','l_price');
     $('.realplus .rent-prices [name="u_price_r"]').attr('name','u_price');
     $('.realplus .sale-prices').removeClass('shown').addClass('hidden');
     $('.realplus .rent-prices').removeClass('hidden').addClass('shown');
     $('.listingcount strong').html('listings for rent');
     //getListingCount();
     $('.realplus input[name="rp_status"]').val(187);
     $('#rp-form-submit').click();
     });
     */

    $('.realplus .contact-btn').on('click',function(e) {
      $('html, body').animate({
        scrollTop: $(".realplus .tab-contact").offset().top-130
      }, 500);
      setTimeout(function() {
        $('.realplus .floating-button, .realplus .floating-button2').css('right','-115px');
        $('.realplus .floating-button, .realplus .floating-button2').find('.rotate90').fadeIn(500);
      },3000);
    });

    $('.realplus .oh-btn').on('click',function(e) {
      $('html, body').animate({
        scrollTop: $(".realplus .property-open-house-tabs").offset().top-130
      }, 500);
      setTimeout(function() {
        $('.realplus .floating-button, .realplus .floating-button2').css('right','-115px');
        $('.realplus .floating-button, .realplus .floating-button2').find('.rotate90').fadeIn(500);
      },3000);

    });

    $('.realplus .floating-button, .realplus .floating-button2').delay(2000).css('right','-115px').find('.rotate90').fadeIn(500);
    $('.realplus .floating-button, .realplus .floating-button2').hover(function() {
      $(this).css('right','0');
      $(this).find('.rotate90').hide();
    },function() {
      $(this).css('right','-115px');
      $(this).find('.rotate90').fadeIn(500);
    });

    if($('.realplus .floating-button2').length>0)
      $('.realplus .floating-button2').css('top',$('.realplus .floating-button').position().top+$('.realplus .floating-button').height());


    if($('.realplus #l-price1').val()=='Custom') {
      $('.realplus .sale-prices .l-price-custom').show();
    }
    if($('.realplus #u-price1').val()=='Custom') {
      $('.realplus .sale-prices .u-price-custom').show();
    }
    if($('.realplus #l-price2').val()=='Custom') {
      $('.realplus .rent-prices .l-price-custom').show();
    }
    if($('.realplus #u-price2').val()=='Custom') {
      $('.realplus .rent-prices .u-price-custom').show();
    }

    $('.realplus .sale-prices #l-price1').on('change',function(){
      if($(this).val()=='Custom') {
        $('.realplus .sale-prices .l-price-custom').val('').show().focus();
      } else {
        $('.realplus .sale-prices .l-price-custom').hide();
      }
    });
    $('.realplus .sale-prices #u-price1').on('change',function(){
      if($(this).val()=='Custom') {
        $('.realplus .sale-prices .u-price-custom').val('').show().focus();
      } else {
        $('.realplus .sale-prices .u-price-custom').hide();
      }
    });
    $('.realplus .rent-prices #l-price2').on('change',function(){
      if($(this).val()=='Custom') {
        $('.realplus .rent-prices .l-price-custom').val('').show().focus();
      } else {
        $('.realplus .rent-prices .l-price-custom').hide();
      }
    });
    $('.realplus .rent-prices #u-price2').on('change',function(){
      if($(this).val()=='Custom') {
        $('.realplus .rent-prices .u-price-custom').val('').show().focus();
      } else {
        $('.realplus .rent-prices .u-price-custom').hide();
      }
    });

    //$('.realplus .l-price-custom, .realplus .u-price-custom').on('keyup',function() {
    //this.value = this.value.replace(/[^0-9]/g, '');
    //});
  });

})( jQuery );


jQuery(document).ready(function($) {
  'use strict';



  // Search
  $('.header-top .search-button').click(function(e){
    e.preventDefault();
    $('.search-box').toggleClass('active');
    if($('.search-box').hasClass('active')) {
      $('.search-box input').focus();
    }
  });
  $('.search-box .close-button').click(function(e){
    e.preventDefault();
    $('.search-box').removeClass('active');
  });

  // Message Mask
  $('.message-mask i').click(function(e){
    e.preventDefault();
    $(this).parents('.message-mask').addClass('inactive');
  });

  // Match Height
  $('.card, .mega-menu > .sub-menu > li').matchHeight(true);

  // Wrap Input Submit
  $('input[type="submit"].lt-button').wrap('<span class="lt-button-wrap"></span>');

  // Numberic
  $('.numeric').numeric();

  function formatProperty (state) {
    if (!state.id) { return state.text; }
    var $state = $(
        '<span><img src="'+$(state.element).data('img')+'" /> ' + state.text + '</span>'
    );
    return $state;
  };
  $('.property-select2').select2({
    templateResult: formatProperty
  });

  // Form
  $('.validate-form').each(function(){
    if( $(this).is('form') ) {
      var curForm = $(this);
    } else {
      var curForm = $('form', $(this));
    }
    $(curForm).validate({
      errorPlacement: function(error, element) {
        if( !$('.form-response label', curForm).is(':visible') ){
          $('.form-response', curForm).html(error).fadeIn();
        }
      }
    });
  });

  // Modal
  $('.modal-link').click(function(e){
    e.preventDefault();
    $('.modal-mask').addClass('active');
    $('.modal-mask').find('.'+$(this).data('modal')).addClass('active');
    $('.modal-mask').find('.'+$(this).data('modal')).find('.tab-list li:first-child a').trigger('click');
  });
  $('.modal-mask .close-bt').click(function(e){
    e.preventDefault();
    $(this).parents('.modal-mask').removeClass('active');
    $(this).parents('.modal').removeClass('active');
  });

  // Carousel
  var defaults = {
    navText: ['<i class="flaticon-arrow395"></i>','<i class="flaticon-move13"></i>'],
    responsiveRefreshRate: 50
  };
  imagesLoaded( $('.lt-carousel'), function( instance ) {
    $('.lt-carousel').each(function(){
      var options = $.extend({}, defaults, $(this).data());
      options.loop = true;
      options.responsive = {
        0: {items:$(this).data('s-items')},
        640: {items:$(this).data('m-items')},
        1025: {items:$(this).data('items')}
      }

      if(options.loop && $(this).children().length == 1) {
        options.loop = false;
      }

      $(this).owlCarousel(options);
      if($(this).data('bg')) {
        $(this).on('changed.owl.carousel', function(event){
          var index = event.item.index;

          if(options.loop) {
            index = (index - 2);
            if(index < 0) index = event.item.count-1;
            if(index == event.item.count) index = 0;
          }
          // console.log(event.item.index+' : '+index);

          $($(this).data('bg')).find('.item.active').removeClass('active');
          $($(this).data('bg')).find('.item').eq(index).addClass('active');

        });
      }
    });

    $('.lt-carousel').on('changed.owl.carousel', function(event){
      var options = $.extend({}, defaults, $(this).data());
      options.loop = true;
      var index = event.item.index;

      if(options.loop) index = (index - 2) % event.item.count;
      if($($(this).data('nav-thumb')).length){
        $('li', $(this).data('nav-thumb')).removeClass('active');
        $($(this).data('nav-thumb')).find('li').eq(index).addClass('active');
      }
    });

    $('.thumb-nav li:first-child').addClass('active');
    $('.thumb-nav li').click(function(){
      $($(this).parents('.thumb-nav').data('nav-thumb')).trigger('to.owl.carousel', [$(this).index(), null, true]);
    });


  });

  imagesLoaded( $('body'), function( instance ) {
    // Swipe Box
    $('.swipebox').swipebox();
  });

  // Accordion
  $('.wpb_accordion').each(function(){
    if($(this).data('active-tab')) {
      $('.wpb_accordion_section:eq('+($(this).data('active-tab')-1)+')', $(this)).addClass('active');
    }
  });
  $('.wpb_accordion_header').click(function(){
    if($(this).parents('.wpb_accordion_section').hasClass('active')) {
      if($(this).parents('.wpb_accordion').data('collapsible') == 'no' && $(this).parents('.wpb_accordion').find('.wpb_accordion_section.active').length <= 1)
        return;

      $(this).parents('.wpb_accordion_section').removeClass('active');
      $(this).parents('.wpb_accordion_section').find('.wpb_accordion_content').slideUp(250);
    } else {
      $(this).parents('.wpb_accordion').find('.wpb_accordion_section.active').removeClass('active');

      $(this).parents('.wpb_accordion_section').addClass('active');
      $(this).parents('.wpb_accordion_section').find('.wpb_accordion_content').slideDown(250);
    }
  });
  // Tab
  $('.wpb_tabs').each(function(){
    $('.wpb_tab', $(this)).first().addClass('active');
    $('.wpb_tabs_nav li:first-child').addClass('active');
  });
  $('.wpb_tabs_nav li').click(function(e){
    e.preventDefault();
    if(!$(this).hasClass('active')) {
      var index = $(this).index();
      $(this).addClass('active');
      $(this).siblings('li.active').removeClass('active');
      $('.wpb_tab', $(this).parents('.wpb_tabs')).removeClass('active');
      $('.wpb_tab:eq('+index+')', $(this).parents('.wpb_tabs')).addClass('active');
    }
  });
  $('.tab-list a').click(function(e){
    e.preventDefault();
    $(this).parents('.tab-list').find('li').removeClass('active');
    $(this).parents('li').addClass('active');
    $(this).parents('.tab-list').siblings('.pane').removeClass('active');
    $(this).parents('.tab-list').siblings('.pane.'+$(this).data('pane')).addClass('active');
  });

  // Transition Animate
  $('.wpb_animate_when_almost_visible').one('inview', function(){
    var delay = 150+(600*$(this).prevAll('.wpb_animate_when_almost_visible').length);
    $(this).delay(delay).queue(function(){
      $(this).addClass('animate-active').dequeue();
    });
  });

  // Animate Number
  $('.animate-number').html('0');
  var comma_separator_number_step = $.animateNumber.numberStepFactories.separator(',');
  $('.animate-number').one('inview', function() {
    var start = $(this).data('to')-100;
    if(start<0) start = 0;
    $(this).prop('number', start).animateNumber({
      number: $(this).data('to'),
      numberStep: comma_separator_number_step,
      easing: 'easeOutQuad'
    }, 1000);
  });

  // Toggle
  $('.wpb_toggle').click(function(e){
    e.preventDefault();
    $(this).toggleClass('active');
    $(this).next('.wpb_toggle_content').toggleClass('active');
  });

  // Trigger Form
  $('.property-view-form select').change(function(e){
    window.location.href = $(this).val();
  });

  // Map
  var maps = [];
  $('.rp2-map-wrap').each(function() {
    $(this).attr('id', 'map-'+Math.floor(Math.random() * 100));
    var markers = $(this).children();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var defaults = {
      center: latlng,
      zoomControl : true,
      scaleControl: false,
      streetViewControl: false,
      panControl : false,
      mapTypeControl: false,
      overviewMapControl: false,
      scrollwheel: false,
      zoom: 5,
      maxZoom: 18,
      zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.RIGHT_TOP
      }
    };
    var options = $.extend({}, defaults, $(this).data());
    var map = new google.maps.Map($(this).get(0), options);
    maps[$(this).attr('id')] = map;
    google.maps.event.addListener( map, 'idle', function(){
      $('.gm-style').removeClass('gm-style');
    });

    // Marker
    var mks = [];
    var latlngbounds = new google.maps.LatLngBounds();
    for (var i = 0; i < markers.length; i++) {
      var marker_data = $(markers[i]).data();
      var latlng = new google.maps.LatLng(marker_data.latitude, marker_data.longitude);
      var marker_content = '<div class="marker center-marker" data-lat="'+marker_data.latitude+'" data-lng="'+marker_data.longitude+'"><div class="dot"></div></div>';
      if(marker_data.content) marker_content = '<div class="marker" data-lat="'+marker_data.latitude+'" data-lng="'+marker_data.longitude+'"><div class="dot"></div></div><div class="info-box '+marker_data.active+'">'+marker_data.content+'</div>';
      var richMarker = new RichMarker({
        position: latlng,
        flat: true,
        anchor: RichMarkerPosition.MIDDLE,
        map: map,
        content: marker_content
      });
      latlngbounds.extend(latlng);
      mks.push(richMarker);
    }
    var mcOptions = {maxZoom: 15, imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'};
    var markerCluster = new MarkerClusterer(map, mks, mcOptions);
    map.fitBounds(latlngbounds);
    // map.setZoom(options['zoom']);

    google.maps.event.addListenerOnce(map, "idle", function() {
      //console.log(options['zoom']);
      if (options['zoom'] >= 0) map.setZoom(options['zoom']);
    });

    var styles = [
      {
        stylers: [
          { saturation: -100 }
        ]
      }];
    map.setOptions({styles: styles});
  });

  $(document).on('click touchstart', '.marker', function(){
    if($(this).hasClass('center-marker')) return;
    if(!$(this).siblings('.info-box').hasClass('active')) {
      $('.map-wrap .info-box.active').removeClass('active');

      var map = maps[$(this).parents('.map-wrap').attr('id')];
      var center = new google.maps.LatLng($(this).data('lat'), $(this).data('lng'));
      map.panTo(center);
      map_recenter(center, 0, -120, map);
    }
    $(this).siblings('.info-box').toggleClass('active');
  });



  // Sharrre
  $('.share-box .facebook').sharrre({
    share: {
      facebook: true
    },
    template: '<a class="box facebook" href="#"><i class="nt-icon-facebook"></i><span class="count" href="#">{total}</span></a>',
    enableHover: false,
    enableTracking: true,
    click: function(api, options){
      api.simulateClick();
      api.openPopup('facebook');
    }
  });
  $('.share-box .twitter').sharrre({
    share: {
      twitter: true
    },
    template: '<a class="box twitter" href="#"><i class="nt-icon-twitter"></i><span class="count" href="#">{total}</span></a>',
    enableHover: false,
    enableTracking: true,
    click: function(api, options){
      api.simulateClick();
      api.openPopup('twitter');
    }
  });
  var urlCurl = $('.share-box .googleplus').data('urlCurl');
  $('.share-box .googleplus').sharrre({
    urlCurl: urlCurl,
    share: {
      googlePlus: true
    },
    template: '<a class="box googleplus" href="#"><i class="nt-icon-gplus"></i><span class="count" href="#">{total}</span></a>',
    enableHover: false,
    enableTracking: true,
    click: function(api, options){
      api.simulateClick();
      api.openPopup('googlePlus');
    }
  });
  $('.share-box .pinterest').sharrre({
    urlCurl: urlCurl,
    share: {
      pinterest: true
    },
    template: '<a class="box pinterest" href="#"><i class="nt-icon-pinterest-circled"></i><span class="count" href="#">{total}</span></a>',
    enableHover: false,
    enableTracking: true,
    click: function(api, options){
      api.simulateClick();
      api.openPopup('pinterest');
    }
  });

});

function map_recenter(latlng,offsetx,offsety, map) {
  var point1 = map.getProjection().fromLatLngToPoint(
      (latlng instanceof google.maps.LatLng) ? latlng : map.getCenter()
  );
  var point2 = new google.maps.Point(
      ( (typeof(offsetx) == 'number' ? offsetx : 0) / Math.pow(2, map.getZoom()) ) || 0,
      ( (typeof(offsety) == 'number' ? offsety : 0) / Math.pow(2, map.getZoom()) ) || 0
  );
  map.panTo(map.getProjection().fromPointToLatLng(new google.maps.Point(
      point1.x - point2.x,
      point1.y + point2.y
  )));
}

