var $dom = $(document), $window = $(window); (function() { var $hero = $('.hero'), $projects = $('.section-projects'), $quotes = $hero.find('.hero--quote'), $mainnav = $('#main-nav'), $projectsTitle = $('.projects-home--title'), $projectsDivider = $('.projects-home--divider'), $contentWrapper = $("#content-wrapper"), animationEvent = "animationend webkitAnimationEnd oanimationend MSAnimationEnd", transitionEvent = "webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend"; var biggerQuoteSize = null, HeroSizeCalc = function() { var self = this, $quote = $quotes.filter('.active'), windowHeight = $window.height(), projectsFullHeight = $projects.height(), projectsLatestHeight = $projectsDivider.outerHeight() + $projectsTitle.outerHeight(), heroOuterheight = $quote.outerHeight(), $biggestQuote, biggestHeight = 0, newHeight, newHeightNoProjects, newHeightIncludingProjects; $quotes.each(function(index) { var self = this, $self = $(self), selfHeight = $self.outerHeight(); if (biggestHeight < selfHeight) { $biggestQuote = $self; biggestHeight = selfHeight; } }); newHeightNoProjects = windowHeight - projectsLatestHeight - $mainnav.outerHeight(); newHeightIncludingProjects = windowHeight - projectsFullHeight - $mainnav.outerHeight(); if (newHeightIncludingProjects < 0) { newHeight = newHeightNoProjects; } else if (newHeightIncludingProjects < biggestHeight) { newHeight = newHeightNoProjects; } else { newHeight = newHeightIncludingProjects; } $hero.height(newHeight); $quote.css({ top: '50%', marginTop: -heroOuterheight / 2 }); }; var QuoteDisplayer = function() { var self = this, shownQuotesIndexes = [], quoteRepeatMargin = 2, randomize = function() { var finalIndex; do { finalIndex = Math.floor((Math.random() * $quotes.length) + 0); } while (shownQuotesIndexes.indexOf(finalIndex) > -1); shownQuotesIndexes.push(finalIndex); if (shownQuotesIndexes.length > quoteRepeatMargin) shownQuotesIndexes.shift(); return finalIndex; }, hide = function($quote) { var $author = $quote.find('.hero--author'), i = 0, time; $quote.addClass('animate-hideQuote'); $author.removeClass('animate-revealUp').addClass('animate-hideQuote'); $quote.one(animationEvent, function(e) { $quote.removeClass('animation-end active animate-revealQuote'); show(); }); }, show = function() { var $quote = $quotes.filter('.active'), $quoteToShow = $($quotes.get(randomize())), time; if ($quote.hasClass('animation-end')) { hide($quote); } else { $quote.removeClass('active'); $quoteToShow.removeClass('init animate-hideQuote').addClass('active'); $('.hero-wrapper').removeClass('init'); HeroSizeCalc(); if ($contentWrapper.hasClass('not-visible')) { $contentWrapper.removeClass('not-visible'); $('footer').addClass('visible'); } $quoteToShow.addClass('animate-revealQuote'); $quoteToShow.one(animationEvent, function(e) { $quoteToShow.find('.hero--author').removeClass('animate-hideQuote').addClass('animate-fast animate-revealUp'); $quoteToShow.addClass('animation-end'); if ($quotes.length > 1) setTimeout(show, 7000); }); } }; (function(self) { show(); })(this); return this; }; $dom.ready(function() { var quoteDisplayer, resizeTimer; $window.on('resize', function() { if (resizeTimer) setTimeout(resizeTimer); resizeTimer = setTimeout(HeroSizeCalc, 200); }); quoteDisplayer = new QuoteDisplayer(); }); })();