
/**
 * Javascript initializing
 *
 * @author HAAN <haan@fynskemdier.dk>
 */
(function($) {
	/**
	 * Execute when the DOM is rendered
	 */
	$(function() {
		/**
		 * Search block initlizing
		 */
		$('input#form-top-search-block-text, body.video-archive form.search-form div.form-search-value input[type=text]').searchComment();

		/**
		 * Videolist setup
		 */
		$('div.multiblock-tabs.videolist div.tab-item-content div.videolist').pager({
			content : '> div.videos',
			pager : 'inline'
		}).find('.pager').addClass('site-color-bg');

		/**
		 * News ticker - cycle
		 */
		$('.ticker-cycle').cycle({
			fx : 'scrollLeft',
			pause : true
		});

		/**
		 * Embed video
		 */
		$('#embed-video-source').removeClass('active');

		$('.show-embed-video').click(function(e) {
			$('#embed-video-source').toggleClass('active');
		});

		/**
		 * Article image cycle
		 */
		$('body.article .top-big-image .article-images').cycle({
			timeout : 0,
			prev : 'body.article .top-big-image .pager .prev',
			next : 'body.article .top-big-image .pager .next'
		});

		/**
		 * Article image cycle
		 */
		$('body.article .top-big-image .image-captions').cycle({
			timeout : 0,
			prev : 'body.article .top-big-image .pager .prev',
			next : 'body.article .top-big-image .pager .next'
		});
	});
	
	/**
	 * Execute when all content is loaded
	 */
	$(window).load(function() {
		/**
		 * Rerender Column heights
		 */
		$('div.table.splitblock:not(#ticker-flexblock div.table.splitblock)').each(function() { // Fetch all splitblocks
			var table = $(this); // Get table

			table.addClass('js-rendered'); // Tell the table that it has been rerendered by JS, useful with CSS

			var columns = table.find('> div.row > div.column'); // Get columns
			
			columns.each(function(i) { // Walk through all columns
				var last = $(this).find('> div.column-content > *:not(style, script):last'); // Find last useable item

				if(last.length == 1) {
					last.addClass('js-rendered'); // Tell last item that it has been modified by JS

					var last_margin_bottom = parseInt(last.css('margin-bottom')); // Calculate the bottom margin of last item
					var last_padding_top = parseInt(last.css('padding-top'));
					var last_padding_bottom = parseInt(last.css('padding-bottom'));
					var last_offset_top = (last.offset().top - table.offset().top); // Find last item offset relative to the table

					if(isNaN(last_margin_bottom)) {
						last_margin_bottom = 0;
					}
					if(isNaN(last_padding_top)) {
						last_padding_top = 0;
					}
					if(isNaN(last_padding_bottom)) {
						last_padding_bottom = 0;
					}

					var height = table.height() - last_offset_top - last_margin_bottom - last_padding_bottom - last_padding_top; // Define new height

					last.height(height) // Write new height
				}
			});
		});
	});
})(jQuery);


