/* 
 * Polaris Menu
 * - Header allcontent function.
 * - Footermenu layout.
 */
(function($) {
	
	$(document).ready(function(){
		/** HEADER **/
		// Initial height of #header
		var headerHeight = $('#header').height();

		// Ref. to all content container
		var allContentRef = $('#header .allContent');

		// Ref. to "Alt innhold"-button
		var showAllRef = $('.polarisMenu:eq(0) .showAll').parents('li:first');

		// Number of columns in all content
		var numOfCols = $('.level1 > li', allContentRef).length;

		// Set width of .level1 > li elements based on number of columns, available width and border-width.
		var borderWidth = [Number($('.level1 > li:eq(0)', allContentRef).css('borderLeftWidth').replace('px', '')), Number($('.level1 > li:eq(0)', allContentRef).css('borderRightWidth').replace('px', ''))];
		var columnWidth = Math.floor((allContentRef.width() - ((borderWidth[0] + borderWidth[1]) * numOfCols)) / numOfCols);
		$('.level1 > li', allContentRef).css('width', columnWidth);

		// Set height of .level1 > li elements equal to the tallest (makes all borders have equal height)
		var maximumHeight = 0;
		$('.level1 > li', allContentRef).each(function() {
			var thisHeight = $(this).outerHeight();
			if (thisHeight > maximumHeight)
			{
				maximumHeight = thisHeight;
			}
		});

		$('.level1 > li', allContentRef).css('height', maximumHeight);

		// Remove border from first and last element
		$('.level1 > li:first', allContentRef).css('borderLeft', 0);
		$('.level1 > li:last', allContentRef).css('borderRight', 0);

		// Height of content in .allContent. Use this height to animate height of .allContent
		var contentHeight = $('.allContent ul:first').outerHeight(true);
		showAllRef.toggle(
			function() {
				$('#header').stop().animate({height: (contentHeight + headerHeight)}, 350, 'swing');
			},
			function(){
				$('#header').stop().animate({height: headerHeight}, 250, 'swing');
			}
		);

		/** FOOTER **/
		// Ref. to all content container
		var allContentFooterRef = $('.allContentFooter');

		// Number of columns in all content
		var numOfColsFooter = $('.level1 > li', allContentFooterRef).length;

		var borderWidthFooter = [Number($('.level1 > li:eq(0)', allContentFooterRef).css('borderLeftWidth').replace('px', '')), Number($('.level1 > li:eq(0)', allContentFooterRef).css('borderRightWidth').replace('px', ''))];
		var columnWidthFooter = Math.floor((allContentFooterRef.width() - ((borderWidthFooter[0] + borderWidthFooter[1]) * numOfColsFooter)) / numOfColsFooter)
		$('.level1 > li', allContentFooterRef).css('width', columnWidthFooter);

		// Remove border from first and last element
		$('.level1 > li:first', allContentFooterRef).css('borderLeft', 0);
		$('.level1 > li:last', allContentFooterRef).css('borderRight', 0);
	});
	
})(jQuery);
