
/**
 * self invoking function:
 *
 *    +function() {}();
 *    or
 *    (function() {})();
 *
 *
 * document ready:
 *
 * $() is shorthand for $( document ).ready()
 *
 * Code included inside $( window ).on( "load", function() { ... }) will run
 * once the entire page (images or iframes), not just the DOM, is ready.
 *
 * @see http://thanpol.as/javascript/you-dont-need-dom-ready
 *
 */

/**
 * jQuery section
 */
+function ($) {

	/**
	 * Set max. font-size
	 */
	var fontSizeService = {
		$h1: null,
		$h2: null,
		$h3: null,
		$h4: null,
		$lead: null,

		init: function () {
			this.$h1 = $('#page-content h1, #page-content .h1');
			this.$h2 = $('#page-content h2, #page-content .h2');
			this.$h3 = $('#page-content h3, #page-content .h3');
			this.$h4 = $('#page-content h4:not(.accordion-title), #page-content .h4, #page-content h5, #page-content .h5, #page-content h6, #page-content .h6');
			this.$lead = $('#page-content p.lead');
		},

		update: function () {
			fontSizeService.fontSizeResize(this.$h1, 0.035, 30, 42);
			fontSizeService.fontSizeResize(this.$h2, 0.03, 26, 38);
			fontSizeService.fontSizeResize(this.$h3, 0.025, 23, 34);
			fontSizeService.fontSizeResize(this.$h4, 0.02, 20, 30);
			fontSizeService.fontSizeResize(this.$lead, 0.02, 20, 30, true);
		},

		fontSizeResize: function (header, fontBase, fontBaseMin, fontBaseMax, setMargin=false) {
			header.each(function () {
				let fontSize = $(window).width() * fontBase;
				fontSize = (fontSize > fontBaseMax) ? fontBaseMax : fontSize;
				fontSize = (fontSize < fontBaseMin) ? fontBaseMin : fontSize;
				$(this).css('font-size', fontSize + 'px');
				if (setMargin) $(this).css('margin-bottom', fontSize + 'px');
			});
		},
	};

	$(function () {
		// dom ready
		$(function () {
			fontSizeService.init();
			fontSizeService.update();
			$(window).resize(function () {
				fontSizeService.update();
			});
		});
	});

	/**
	 * Scrolling
	 */
	var scrollingService = {
		update: function () {
			$(window).scroll(function () {
				var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
				if (winScroll > 200) $('body').addClass('scroll');
				else $('body').removeClass('scroll');

				var scrollStop = $('#page-content').height()-$('.backendlayout-subnavigation_left .subnav-wrap .frame-type-subnavigation').height()-90;
				if (winScroll >= scrollStop) $('.backendlayout-subnavigation_left .subnav-wrap .frame-type-subnavigation').addClass('scrollstop');
				else $('.backendlayout-subnavigation_left .subnav-wrap .frame-type-subnavigation').removeClass('scrollstop');
			});
		},
	};

	$(function () {
		// dom ready
		$(function () {
			$(window).scroll(function () {
				scrollingService.update();
			});
		});
	});

	/**
	 * Random Container
	 */
	$.fn.random = function() {
		return this.eq(Math.floor(Math.random() * this.length));
	}

	$('.ce-container').each(function(){
		if ($(this).data('randomcontainer') > 0) {
			$('> div', this).hide()
			$('> div', this).random().show()
		}
	});

}(jQuery);