
(function ($) {
  Drupal.behaviors.dropsolid_base = {
    attach: function (context, settings) {

      // Set current breakpoint
      var currentBreakpoint = $.getBreakpoint();

      //Once needed to avoid multiple clicks when extra content is added to the page. (even vimeo vid)
      // $('.menu h2', context).once('h2').click(function () {
      //   $(this).parents('.menu').toggleClass('open');
      // });
      // $('.menu a.active', context).once('a.active').click(function (event) {
      //   event.preventDefault();
      //   $(this).parents('.menu').toggleClass('open');
      // });

      // Click away Drupal messages
      $('.messages .close').click(function (event) {
        event.preventDefault();
        $(this).parents('.messages').addClass('closing');
      });
      $('.messages').bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function () {
        if ($(this).hasClass('closing')) {
          $(this).addClass('closed');
        }
      });

      // Scrollto
      $('a[href*=#]:not([href=#])').click(function (event) {
        var path = this.href;
        var Current = window.location.pathname;
        var id = '';

        // if link is on current page, prevent reload.
        if (path.indexOf(Current) >= 0 && Current != '/') {
          // get ID/hash from url
          id = event.target.href.substring(event.target.href.indexOf("#")+1);
        }
        else if(Current == '/') {
          var Hash = event.target.href.substring(event.target.href.indexOf("#")+1);
          if($('#'+Hash).length) {
            id = Hash;
          }
        }
        if(id != '') {
          event.preventDefault();
          $.scrollto($('#' + id), 56);
        }
      });

      // Add responsive functionality to tables added via WYSIWYG
      $('table').each(function() {
        if(!$(this).hasClass('responsive')) {
          if($(this).find('thead').length) {
            var headings = [];
            $(this).find('th').each(function(){
              headings.push($(this).text());
            });
            var count = 0;
            $(this).find('tr').each(function(){
              $(this).find('td').attr('data-title', headings[count-1]);
              ++count;
            });
          } else {
            $(this).find('tr').each(function(){
              var heading = $(this).find('th').text();
              $(this).find('td').each(function(){
                $(this).attr('data-title', heading);
              });
            });
          }
          $(this).addClass('responsive');
        }
      });

    }
  };
})(jQuery);
