/* Author:

*/
/**
 * Site global JS file
 *
 */

	var LibMan = function(){

		/*
		 * String : The path to js root
		 */
		var path = null;

		/**
		 * set path to this script
		 *
		 * @return void
		 */
		var _calculatePath = function() {

			// condition : is object var already set?
			if (path == null) {
				path = "_includes/js/";
			}
		}();


		/*
		 * Expose methods and values
		 */
		return {
			path: path
		};
	}();

	/**
	 * on Dom ready functionality
	 */
	$(function(){
		// insert template image overlay for layout testing?
		var gridSettings = {
			imgExt: "png",
			gridPos: "center top",
			combineIdAndClass: false
		};
		$.gridOverlay(LibMan.path + "../images/templates/", gridSettings);


		// fix any browser percentage rounding errors
		resizeNav.trigger();

		// and again on resize
		$(window).resize(function() {
			resizeNav.trigger();

			console.info('Window resized');
//			console.log($('ul.nav-primary').outerWidth());
//			console.log($('ul.nav-primary').width());
//			console.log($('#nav-extras a').width());
		});
	});


	var resizeNav = (function(){

		// primary nav
		var $nav = $('ul.nav-primary');

		// width of nav ul
		var ulWidth = null;

		// width of window
		var windowWidth = null;

		// difference between the two widths
		var difference = null;

		// compare the widths and adjust nav size if necessary
		function trigger (){
			// get widths
			ulWidth = $nav.outerWidth();
			windowWidth = $(window).width();

			// readjust so they always match - up to the max-width of the #container
			if (ulWidth != windowWidth) {
				// first we want to make sure the nav is as wide as the parent
				difference = windowWidth - ulWidth;
				var newWidth = parseInt($nav.width() + difference);

				// don't allow new width to be larger than the max container width
				var maxWidth = parseInt($('body').css('max-width')) - 64; // todo: fix magic number from border widths
				var minWidth = parseInt($('body').css('min-width'));

				// work out how wide the nav needs to be
				if (newWidth > maxWidth) {
					newWidth = maxWidth;
				} else if (newWidth < minWidth){
					newWidth = minWidth;
				}

				$nav.width(newWidth);



// TODO: make this more sensible.  combine with above code.  do we even need the stuff above here?

				// now bulldoze li widths to avoid browser pixel rounding errors
				var $navEls = $('ul.nav-primary li'); // stop gazing
				var ulWidth = $('ul.nav-primary').width();
				var borderWidth = 0;
				var suggestedLiWidth = Math.floor((ulWidth - borderWidth) / $navEls.length);
				var remainder = ulWidth % $navEls.length;

//				console.log('ul width is ' + ulWidth);
//				console.log('suggested li width is ' + suggestedLiWidth );
//				console.log('remainder is ' + remainder );

				$navEls.each(function() {
					$(this).width(suggestedLiWidth);
				});

				$navEls.last().width(suggestedLiWidth + remainder);
			}
		};

		/*
		 * Expose methods and values
		 */
		return {
			trigger: trigger
		};
	}());









