﻿// Content Box Equal Height Creation Function
// This function will loop through a selected list of content block
// detect the tallest one and adjust all the heights to match
(function($) {
    $.fn.createEqualHeight = function() {
        createEHFeatureBoxes();
        createEHContentBoxes();
        function createEHContentBoxes() {
            // Find all elements with the assigned "equal height" class with a nested "content" class name
            var headings = $('.headingEqualHeight');
            var maxHeight = 0;
            var promoCheck = 0; // Added to make the promo block line up with the banner block in an HTML Banner without QL case.
            headings.each(function() {
                var currentHeadingHeight = $(this).outerHeight();
                if (currentHeadingHeight > maxHeight) { maxHeight = currentHeadingHeight; }
            });
            setContentHeight(headings, maxHeight, promoCheck);
            var rcTopDiv = $('.rc_top', $(this));
            if ($(rcTopDiv).parents('#quicklinks').length == 0) {
                $(rcTopDiv).css('height', $(this).outerHeight());
            }
        }
        function setContentHeight(elems, mh, promoCheck) {
            elems.each(function() {
                var firstContentBlock = $(".content", $(this));
                if (!firstContentBlock.is(".rotatingPromoOverride")) {
                    if (firstContentBlock.size() > 0 && $(firstContentBlock[0]).outerHeight() > 0) {
                        var rcTopDiv = $('.rc_top', $(this));
                        if ($(rcTopDiv).parents('#quicklinks').length == 0) {
                            $(firstContentBlock[0]).css({ height: (mh - $(this).outerHeight() + $(firstContentBlock[0]).outerHeight() - 2 + promoCheck) + "px" });
                        } else {
                            $(firstContentBlock[0]).css({ height: (mh - $(this).outerHeight() + $(firstContentBlock[0]).outerHeight()) + "px" });
                        }
                        $('.rc_top', $(firstContentBlock[0])).css('height', firstContentBlock.outerHeight());
                    } else {
                        $(this).css({ height: mh + "px" });
                    }    
                }
                
                promoCheck += 4;
            });
        }
        function createEHFeatureBoxes() {
            // Find all elements with the assigned "equal height" class with a nested "content" class name
            var eh = $('.equalHeight .content');
            var maxHeight = 0;
            eh.each(function() {
                var height = this.offsetHeight;
                if (height > maxHeight) { maxHeight = this.offsetHeight; }
            });
            setHeight(eh, maxHeight);
            // If the homepage Carousel has a link, we need to extend the clickable region to the whole box.
            var carouselBoxes = $('.carouselLinkBox');
            setHeight(carouselBoxes, maxHeight);
            carouselBoxes.css({ cursor: 'pointer' });
        }
        function setHeight(elems, mh) {
            elems.each(function() { $(this).css({ height: mh + "px" }); });
            $('.btnFeatureNext a').css({ height: mh + 2 + 'px' });
            $('.btnFeaturePrev a').css({ height: mh + 2 + 'px' });
        }
    };
})(jQuery);
