(function($) {
  "use strict";
  // START HOMEPAGE SPECIFIC
  if (document.location.pathname == "/" || document.location.pathname == '/home-mobile') {
    $(document).ready(function() {
      $('#off-canvas').offcanvas({
          modifiers: 'left,overlay',
          closeButtonClass: "js-offcanvas-close",
      });

      $(".nano").nanoScroller();

    });
  }

  // Scroll to top button appear
  $(document).scroll(function() {
    var scrollDistance = $(this).scrollTop();
    if (scrollDistance > 100) {
      $('.scroll-to-top').fadeIn();
    } else {
      $('.scroll-to-top').fadeOut();
    }
  });

  if (document.location.pathname == "/") {
    if($(window).width() < 1024) {
      window.location = '/home-mobile';
    }
  }

  $(document).ready(function(){

    $('#modalvideo').on('shown.bs.modal', function() {

      try {
        player.playVideo();
      } catch(error) {
      }

      try {
        player.play();
      } catch(error) {
      }

    });

    $('#modalvideo').on('hide.bs.modal', function() {

      try {
        player.stopVideo();
      } catch(error) {
      }

      try {
        player.pause();
      } catch(error) {
      }

    });
    
    $('body').on('contextmenu', 'img', function(e) {
      return false;
    }); 

    if ($(".owl-carousel").length) {
      if ($('.yacht-profile-inner').length) {
        $(".owl-carousel").owlCarousel({
          nav : true,
          navText: [ '<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>' ],
          loop: true,
          autoplay: true,
          autoplaySpeed: 2400,
          paginationSpeed: 2400,
          items: 1,
          responsiveClass:true,
          singleItem: true,
          pagination: false,
          animateOut: 'fadeOut',
          autoplayHoverPause: false,
          rewindSpeed: 500
        });
      }
    }
    $('#seoHeadDescription').on('shown.bs.collapse', function () {
      $('.seo-head-description').text('Read Less');
    })
    $('#seoHeadDescription').on('hidden.bs.collapse', function () {
      $('.seo-head-description').text('Read More');
    })
  });

  $(".leftGate").hover(function () {
    $(".gateHeaderData,.gateFooterData").addClass("goLeft");
  }, function() {
    $(".gateHeaderData,.gateFooterData").removeClass("goLeft");
  });

  $(".rightGate").hover(function () {
    $(".gateHeaderData,.gateFooterData").addClass("goRight");
  }, function() {
    $(".gateHeaderData,.gateFooterData").removeClass("goRight");
  });

  // END HOMEPAGE SPECIFIC

  // START INNER YACHT PROFILE
  if ($('.yacht-profile-inner').length) {
    setTimeout(function(){
      $('.js-offcanvas-trigger').click();
    }, 750);
    $(document).scroll(function() {
      console.log(window.location.hash);
      $('.yacht-panel').removeClass('active-panel');
      // Section One (#top) - Panel One (.panel-one)
      // Activates
      if (window.location.hash == '#top') $('.panel-one').addClass('active-panel');
      // Applies Delayed Animation
      if (window.location.hash == '#top') $('.panel-one').stop().delay(325).queue(function(){
        $(this).addClass('animated fadeInUp');
      });
      // Section Two (#middle)- Panel Two (.panel-two)
      // Activates
      if (window.location.hash == '#middle') $('.panel-two').addClass('active-panel');
      // Applies Delayed Animation
      if (window.location.hash == '#middle') $('.panel-two').stop().delay(325).queue(function(){
        $(this).addClass('animated fadeInUp');
      });
      // Panel Three (#bottom) - Panel Three (.panel-three)
      // Activates
      if (window.location.hash == '#bottom') $('.panel-three').toggleClass('active-panel');
      // Applies Delayed Animation
      if (window.location.hash == '#bottom') $('.panel-three').stop().delay(325).queue(function(){
        $(this).addClass('animated fadeInUp');
      });
      // Removes classes for previous panel depending on current page section
      if (window.location.hash == '#top') $('.panel-two').removeClass('animated fadeInUp active-panel');
      if (window.location.hash == '#middle') $('.panel-one').removeClass('animated fadeInUp active-panel');
      if (window.location.hash == '#middle') $('.panel-three').removeClass('animated fadeInUp active-panel');
      if (window.location.hash == '#bottom') $('.panel-two').removeClass('animated fadeInUp active-panel');
    });
    $('#off-canvas').offcanvas({
        modifiers: 'right, overlay'
    });
    $(window).scroll(function() {
      if ($(document).scrollTop() > 100) {
        $('body').addClass('shrink');
      } else {
        $('body').removeClass('shrink');
      }
    });
    $.scrollify({
      section : ".profile-section",
      sectionName : "section-name",
      interstitialSection : "",
      easing: "easeOutExpo",
      scrollSpeed: 1800,
      offset : 0,
      scrollbars: false,
      standardScrollElements: "",
      setHeights: true,
      overflowScroll: true,
      updateHash: true,
      touchScroll:true,
      before:function() {},
      after:function() {},
      afterResize:function() {},
      afterRender:function() {}
    });
  }

  // END INNER YACHT PROFILE

  // Menu Dropdowns as Select boxes
  $(".dropdown-form .dropdown-item").click(function(){
    var selText = $(this).text();
    $(this).parents('.dropdown-form').find('.dropdown-toggle').html(selText+'');
  });

  // Smooth scrolling
  $('a.smooth[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: (target.offset().top - 54)
        }, 1000, "easeInOutExpo");
        return false;
      }
    }
  });

  // Collapse Navbar
  var navbarCollapse = function() {
    if ($("#mainNavigation, #navigation").offset().top > 100) {
      $("#mainNavigation, #navigation").addClass("navbar-shrink");
    } else {
      $("#mainNavigation, #navigation").removeClass("navbar-shrink");
    }
  };

  if ($("#mainNavigation, #navigation").length) {
    // Collapse now if page is not at top
    navbarCollapse();
    // Collapse the navbar when page is scrolled
    $(window).scroll(navbarCollapse);
  }

	$("#mobileNavigation .dropdown-menu").click(function(e){
	   e.stopPropagation();
	})

  // Yacht Charter Search page range sliders
  if (document.location.pathname == "/yacht-charter-search.html") {
    $(document).ready(function(){
      //Price range slider
      $("#price-range").slider({
        range: true,
        min: 0,
        max: 500000,
        values: [150000, 500000],
        slide: function(event, ui) {
          $("#amount").val(ui.values[ 0 ] + " - " + ui.values[ 1 ]);
        }
      });
      $("#amount").val( $("#price-range").slider("values", 0) +
        " - " + $("#price-range").slider("values", 1));

      //Size range slider
      $("#size-range").slider({
        range: true,
        min: 0,
        max: 150,
        values: [50, 150],
        slide: function(event, ui) {
          $("#size").val(ui.values[ 0 ] + "M - " + ui.values[ 1 ] + "M");
        }
      });
      $("#size").val($("#size-range").slider("values", 0) +
        "M - " + $("#size-range").slider("values", 1) + "M");
    });
  }

  $(document).ready(function() {
    /* activate the carousel */
    if ($('.destinations-inner-node').length || ($('.inner-yacht-newv2').length && $('.additionalImages').length)) {
      /* change modal title when slide changes */
      $("#modal-carousel").on("slid.bs.carousel", function () {
          $(".modal-title")
          .html($(this)
          .find(".active img")
          .attr("title"));
      });

      /* when clicking a thumbnail */
      $(".row .thumb").click(function(){
        var content = $("#modal-carousel-inner");
        content.empty();

        var position = $(this).data('position');
        var repo = $("#img-repo .carousel-image");
        var repoCopy = repo.clone();
        var active = repoCopy.get(position);
        console.log('active', position, active);
        $(active).addClass("active");
        content.append(repoCopy);
      });

      $("#modal-carousel a.left").click(function(){
          $("#modal-carousel").carousel("prev");
      });
      $("#modal-carousel a.right").click(function(){
          $("#modal-carousel").carousel("next");
      });
    }

    /*Crew job questions radio button lable classes*/
    if ($('.job-board-inner .application-questions').length) {
      /* when clicking on radio */
      $(".app-quest-btns .form-check-label").click(function(){
        var parentDiv = $(this).parents().eq(1);
        var inputRadio = $(this).find('input:radio');
        if(inputRadio.val() == 'yes' && $(this).hasClass('toggle-inactive')) {
          $(this).removeClass('toggle-inactive').addClass('toggle-yes');
          $(parentDiv).find('label.toggle-no').removeClass('toggle-no').addClass('toggle-inactive');
        }
        if(inputRadio.val() == 'no' && $(this).hasClass('toggle-inactive')) {
          $(this).removeClass('toggle-inactive').addClass('toggle-no');
          $(parentDiv).find('label.toggle-yes').removeClass('toggle-yes').addClass('toggle-inactive');
        }
      });
    }
  });


  //Home page youtube video
  if (document.location.pathname == "/") {
    $(document).ready(function() {
      if ($('.bluewater-fullvid').length) {
        $('body')
          .prepend('<div class="bluewater-vidcover"></div>')
          .hover (
            function() {
              $('.bluewater-vidcover').css({'opacity':'0.4'})
            },
            function() {
              $('.bluewater-vidcover').css({'opacity':'0'})
            }
          );

        $(window).resize( function(){
          var theWidth = $(window).width();
          var theHeight = $(window).height();
          var newWidth = (theHeight*1.77777778);
          var newHeight = (theWidth/1.77777778);

          if ( (theWidth > 1280) && (newHeight > theHeight )) {
            $('.bluewater-fullvid').css({'width':theWidth, 'height':newHeight});
          }

          if ( (theHeight > 720) && (newWidth > theWidth )) {
            $('.bluewater-fullvid').css({'height':theHeight, 'width':newWidth});
          }

          $('.bluewater-vidcover').css({'height':theHeight, 'width':theWidth});
        }).resize();
      }
    });
  }


  //Start inner-course-data carousel
  if ($('.inner-course-data').length) {
    $(".owl-carousel").owlCarousel({
      nav : true,
      loop: true,
      autoplay: false,
      margin: 10,
      items: 3,
      autoplayHoverPause: true,
      navText: ['<i class="fa fa-angle-left"></i>','<i class="fa fa-angle-right"></i>'],
      responsive:{
        0:{
          items:3
        },
        600:{
          items:3
        },
        1000:{
          items:3
        }
      },
      video: true
    });

    /* when clicking a thumbnail */
    $(".thumb").click(function(){
      var content = $("#modal-carousel-inner");
      content.empty();

      var position = $(this).find('img').data('position');
      var repo = $("#img-repo .carousel-image");
      var repoCopy = repo.clone();
      var active = repoCopy.get(position);
      $(active).addClass("active");
      content.append(repoCopy);
    });

  } //End inner-course-data carousel

  //Start news-innerv2 carousel
  if ($('.news-innerv2').length) {
    $(".owl-carousel").owlCarousel({
      nav : true,
      loop: true,
      autoplay: true,
      margin: 10,
      items: 3,
      autoplayHoverPause: true,
      navText: ['<i class="fa fa-angle-left"></i>','<i class="fa fa-angle-right"></i>'],
      responsive:{
        0:{
            items:3
        },
        600:{
            items:3
        },
        1000:{
            items:3
        }
    }
    });

    /* when clicking a thumbnail */
    $(".thumb").click(function(){
      var content = $("#modal-carousel-inner");
      content.empty();

      var position = $(this).find('img').data('position');
      var repo = $("#img-repo .carousel-image");
      var repoCopy = repo.clone();
      var active = repoCopy.get(position);
      $(active).addClass("active");
      content.append(repoCopy);
    });

  } //End news-innerv2 carousel

  // START Charter Event Inner
  if ($('.charter-event-inner').length) {
    $('#thumbcarousel .carousel-item').each(function(){
      var next = $(this).next();
      if (!next.length) {
        next = $(this).siblings(':first');
      }
      next.children(':first-child').clone().appendTo($(this));

      for (var i=0;i<1;i++) {
        next=next.next();
        if (!next.length) {
          next = $(this).siblings(':first');
        }

        next.children(':first-child').clone().appendTo($(this));
      }
    });

    /* when clicking a thumbnail */
    $("#carousel .carousel-item .thumb").click(function(){
      var content = $("#modal-carousel-inner");
      content.empty();

      var position = $(this).find('img').data('position');
      var repo = $("#img-repo .carousel-image");
      var repoCopy = repo.clone();
      var active = repoCopy.get(position);
      $(active).addClass("active");
      content.append(repoCopy);
    });
  }
  // START Luxury Partners Inner
  if ($('.luxury-partners-inner').length) {
    $('#thumbcarousel .carousel-item').each(function(){
      var next = $(this).next();
      if (!next.length) {
        next = $(this).siblings(':first');
      }
      next.children(':first-child').clone().appendTo($(this));

      for (var i=0;i<1;i++) {
        next=next.next();
        if (!next.length) {
          next = $(this).siblings(':first');
        }

        next.children(':first-child').clone().appendTo($(this));
      }
    });

    $('.carousel-sync').carousel('cycle');
    $('.carousel-sync').on('click', '.carousel-control[data-slide]', function (ev) {
      ev.preventDefault();
      $('.carousel-sync').carousel($(this).data('slide'));
    });
    $('.carousel-sync').on('mouseover', function(ev) {
      ev.preventDefault();
      $('.carousel-sync').carousel('pause');
    });
    $('.carousel-sync').on('mouseleave', function(ev) {
      ev.preventDefault();
      $('.carousel-sync').carousel('cycle');
    });

    /* when clicking a thumbnail */
    $("#carousel .carousel-item .thumb").click(function(){
      var content = $("#modal-carousel-inner");
      content.empty();

      var position = $(this).find('img').data('position');
      var repo = $("#img-repo .carousel-image");
      var repoCopy = repo.clone();
      var active = repoCopy.get(position);
      $(active).addClass("active");
      content.append(repoCopy);
    });
  }

	$('.nav-item .dropdown').on('mouseover', function(){
    $('#mainNavigation, #navigation').addClass('dropdown-is-hov');
	}).on('mouseout', function(){
    $('#mainNavigation, #navigation').removeClass('dropdown-is-hov');
	})

	$('.bwmm-megamenu').on('mouseover', function(){
    $('#mainNavigation, #navigation').addClass('dropdown-is-hov');
	}).on('mouseout', function(){
    $('#mainNavigation, #navigation').removeClass('dropdown-is-hov');
	})

  $('.social-toggle').on('click', function() {
  $(this).next().toggleClass('open-menu');
  });



  $('#modal_location_popup').modal('show');


  $(".dashboard-redux #menu-toggle").click(function (e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
  });

  $(".dashboard-redux #menu-toggle-close").click(function (e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
  });


// Yacht Search - Disabling YearTo field when clicking on "Before2000" yachts.

$('select[name="yacht_year_min"]').on('change', null, function(e) {
  var year_min_value = e.target.value;

  if (year_min_value == -2000 || year_min_value == "new_build") {
    $('select[name="yacht_year_max"]').prop('disabled', true);
    $('select[name="yacht_year_max"] option').prop('selected', false);
  }
  else {
    $('select[name="yacht_year_max"]').prop('disabled', false);
  }

});

$('.reset-yacht-search').on('click', null, function(e) {
  e.preventDefault();
  $('select option').prop('selected', false);
  $('select').val('');
  $('#yachts_search_count').html('');
  $('form[name="brokerage_search_form"], form[name="charter_search_form"]').submit();
});




})(jQuery); // End of use strict
