$(document).ready(function() {

	// Fix for lack of vertical-align support in css 
	function reAlignTertiary() {
		$('#tertiarynav div a').each(function() {
			var compensation = ($(this).parent().height() - $(this).height())/2 + 3; //microcorrection
			if (compensation > 0) {
				$(this).css({'padding-top':compensation + 'px'});
			}
		});
		
	}

	jQuery.fn.correctCurrentAnchor = function() {
		//assuming that by then the anchor change has taken place
		setTimeout(function() {
			// If we have multiple pages that are current, 
			// see if we can find one that matches on the anchor and highlight that one only
				if ($('#tertiarynav .menuitem').length > 1) {
					$('#tertiarynav .menuitem').each(function() {
						var href = $(this).find('a').attr('href');
						if (href && window.location.href.indexOf(href) > 0) {
							//this is it.
							$('#tertiarynav .current').removeClass('current');
							$(this).addClass('current');
						}
					});
				}
		}, 100);
	};

	reAlignTertiary();
	$('#tertiarynav').correctCurrentAnchor(); //initial fix

	$('#tertiarynav a').click(function() {
		$(this).correctCurrentAnchor();
	});
	

	//for some reason it doesn't work for all of them immediately but does the second time
	setTimeout(function() {
		reAlignTertiary();
	}, 300);
});
