custom-javascript.js 5.44 KB
// Add your custom JS here.



	//In order to do that we have to flex:1 each item.. see flexMatrix
	function correctDropdownMenuItemGaps() {
        var drop_index = 0;
        jQuery('#main-menu > li').each(function () {
          var dropdown = jQuery(this).find('.dropdown-menu');
          consecutive_normals = 0;
          if (dropdown.length > 0) {
            var items = jQuery(dropdown).find('li'),
              idx = 0,
              total_width = 0;
            items.each(function (item) {
              ++idx;
              if (flexMatrix(items, idx)) {
                items.eq(idx - 1).css('flex', 1);
                total_width += items.eq(idx - 1).width();
                // if(drop_idx == 2) {
                //     console.log(total_width)
                // }
              }
            });
            if (drop_index >= 3) {
              jQuery(this).find('.dropdown-shadow').css('width', total_width);
            } else {
              jQuery(this).find('.dropdown-shadow').css('width', total_width);
            }
          }
          ++drop_index;
        });
      }
      var consecutive_normals = 0;
  
      //Returns true if this menu item should flex:1, it's true if an image is the next item and this item is not a 3rd.. etc.
      function flexMatrix(items, current_idx) {
        var last_item = items.eq(current_idx - 2);
        var next_item = items.eq(current_idx);
        var item = items.eq(current_idx - 1);
  
        //if the last item was an image we started a new col so do not break here unless its the last one
        if (last_item) {
          if (jQuery(last_item).find('img').length > 0 && !next_item) {
            return false;
          }
        }
  
        //if the next item is an image item or this is the last one
        if (next_item) {
          if (jQuery(next_item).find('img').length > 0) {
            consecutive_normals = 0;
            return true;
          }
        }
        //last item flex it
        if (items.length === current_idx) {
          return true;
        }
        if (jQuery(item).hasClass('break-here')) {
          consecutive_normals = 0;
          return true;
        }
  
        //If this is an image item flex
        if (jQuery(item).find('img').length > 0) {
          consecutive_normals = 0;
          return true;
        }
        ++consecutive_normals;
        //If this is a 3rd consecutive normal item
        if (consecutive_normals == 3) {
          consecutive_normals = 0;
          return true;
        }
      }
  
      //This adjusts the menu dropdown location to never go past the inner edge of the Donate Now button 
      //and line up with it if so
      function lineUpThePositionsOfTheDropdowns() {
        var menu_item_widths = [];
        jQuery('#main-menu > li').each(function () {
          menu_item_widths.push(jQuery(this).outerWidth());
        });
  
        //Remove the donate button
        menu_item_widths.pop();
        var idx = 0;
        jQuery('#main-menu > li').each(function () {
          if (jQuery(this).find('.dropdown-menu').length > 0) {
            if (idx == 0) {
              jQuery(this).find('.dropdown-menu').css('left', -100);
            } else {
              jQuery(this).find('.dropdown-menu').css('right', -100);
            }
          }
          ++idx;
        });
      }
      var HeaderMenu = function () {
        return {
          alignTheSideCaptions: function () {
            jQuery('.side-caption').each(function () {
              var that = this;
              var _bottom = jQuery(this).find('.image-side-caption').innerHeight();
              if (!Responsive.isDesktop()) {
                if (jQuery(that).parent().is('.entry-header') && !jQuery(that).parent().is('.no-photo')) {
                  jQuery(that).css('margin-bottom', -_bottom);
                  var _bottom = jQuery(that).parent().find('.title-container').innerHeight();
                  jQuery(that).css('bottom', _bottom);
                  setTimeout(function () {
                    jQuery(that).addClass('ready');
                  }, 500);
                } else {
                  jQuery(that).css('bottom', -_bottom);
                  setTimeout(function () {
                    jQuery(that).addClass('ready');
                  }, 500);
                }
              } else {
                jQuery(this).css('bottom', -_bottom);
                setTimeout(function () {
                  jQuery(that).addClass('ready');
                }, 500);
              }
            });
          }
        };
      }();
      jQuery(function ($) {
        correctDropdownMenuItemGaps();
        lineUpThePositionsOfTheDropdowns();
        setInterval(function () {
          HeaderMenu.alignTheSideCaptions();
        }, 1000);
        $('.copy-link').on('click', function (e) {
          e.preventDefault();
        });
        var body = document.getElementsByTagName("BODY")[0];
        body.offsetWidth;
        if (window.addEventListener) {
          // all browsers except IE before version 9
          window.addEventListener("resize", onResizeEvent, true);
        } else {
          if (window.attachEvent) {
            // IE before version 9
            window.attachEvent("onresize", onResizeEvent);
          }
        }
        function onResizeEvent() {
          //   bodyElement = document.getElementsByTagName("BODY")[0];
          //   newWidth = bodyElement.offsetWidth;
          //   if(newWidth != width){
          //     width = newWidth;
          //   }
        }
      });