(function($) {
	
	$.fn.truncate_nav = function(user_settings) {
		
		var options = {
			selector: 'ul.level-0 > li',
			child_selector: 'ul.level-1 > li',
			moreUrl: window.location + '?display=list',
			currentPageUrl: window.location
		}
		
		var settings = $.extend(true, options, user_settings);
		
		// Get the absolute selector for the children, so we don't pick up anything else
		var absolute_parent_selector;
		if($(this).attr('id')) {
			absolute_parent_selector = '#' + $(this).attr('id');
		} else {
			absolute_parent_selector = '.' + $(this).attr('class');
		}
		var children_absolute = absolute_parent_selector  + ' > ' + settings.selector;
		var children = $(children_absolute);
		var sub_pages;
		var total_length = 0;
		var has_sub_pages = false;

		if(!children.length) {
			return;
		}
		
		// Find out if the page has sub pages
		if($(children_absolute + ' > ' + settings.child_selector).length > 0) {
			has_sub_pages = true;
			sub_pages = $(children_absolute + ' > ' + settings.child_selector);
		}
				
		// Get the current menu position plus its height
		var menu_position_height = children.offset().top + (28 * children.length);
		// If we have child pages, factor those into the nav height
		if(has_sub_pages) {
			menu_position_height = parseInt(menu_position_height) + parseInt((28 * sub_pages.length));
		}
		
		// Get the window height
		var window_height = $(window).height() / 1.2;
		// How much play do we have between the window and the nav?
		var play = window_height - (children.offset().top + children.height());
		// Get how many we actually have
		var fitted = Math.round(play) / children.height();
		// Round off the fitted amount, subtract 2 to make sure we're clear of the bottom of the screen, and to make room for the more link
		fitted = Math.round(fitted) - 2;
		
		// If we have sub pages, alter fitted to accomodate
		if(has_sub_pages) {
			fitted = Math.round(fitted / 2);
		}
		
		// Add the child length to the total length variable
		total_length = children.length;
		// If we've got sub pages
		if(has_sub_pages) {
			total_length = total_length + sub_pages.length;
		}
		
		// We need to work out if we need to extend the content
		var content_height = $('#unit-content').height();
		var menu_height = 0;
		
		for(var i = 0; i < children.length; i++) {
			menu_height = menu_height + (children.eq(i).height() + 6);
		}
	
		if(menu_height > content_height) {
			$('#unit-content').css({height: menu_height + 'px'});
		}
		
		// Is the menu position more than the window can handle?
		if(menu_position_height > window_height) {
			
			// We're not truncating anymore, because it's for people who like it.
			/*if(total_length > fitted) {
				var i = 0;
				var moreCount = 0;
				var subMoreCount = 0;
				for(i = fitted;i < children.length;i++) {
					// Truncate the normal pages
					if(children.eq(i).children('a').attr('href') != settings.currentPageUrl){
						children.eq(i).addClass('hidden');
						moreCount++;
					}
				}
				
				// If we have sub pages, truncate those
				if(has_sub_pages) {
					for(i = fitted; i < sub_pages.length;i++) {
						if(sub_pages.eq(i).children('a').attr('href') != settings.currentPageUrl) {
							sub_pages.eq(i).addClass('hidden');
							subMoreCount++;
						}
					}
				}
				
				if(moreCount > 0) {
					// Append the more link
					children.parent().append('<li class="end last"><a href="' + settings.moreUrl + '" class="show-more">And '+(moreCount)+' More...</a></li>');
				}
				if(subMoreCount > 0) {
					// Append the more link
					settings.moreUrl = window.location + '?display=list';
					sub_pages.parent().append('<li class="end last"><a href="' + settings.moreUrl + '" class="show-more">And '+(subMoreCount)+' More...</a></li>');
				}
			}*/
		} else {
			if($.browser.msie && parseInt($.browser.version.substr(0, 1)) < 7) {
				
			} else {
				$('#unit-subnav').slide_sidenav();
			}
		}
		
	}
	
})(jQuery);
