Array.prototype.sum = function() {
return (! this.length) ? 0 : this.slice(1).sum() +
((typeof this[0] == 'number') ? this[0] : 0);
};
function menu() {
var nav = $('ul.nav'),
nav_item_height = 0,
numMenuItems = $('> li', nav).size(),
totalMenuItemWidth = 0,
menuWidthRemainder = 0,
wrapperWidth = 969,
maxNavItemWidth = 200,
priNavItems = new Array();
/* First, determine the total width of each item in the navigation. */
$('> li', nav).each(function (i) {
totalMenuItemWidth += $(this).width();
priNavItems.push(($(this).width() > maxNavItemWidth) ? maxNavItemWidth : $(this).width());
});
/* Primary navigation items (combined) are less width than the
wrapper. */
if(totalMenuItemWidth < wrapperWidth) {
menuWidthRemainder = wrapperWidth - totalMenuItemWidth;
$('> li', nav).each(function() {
var tmp_width = $(this).width();
$(this).width(tmp_width + parseInt(menuWidthRemainder / numMenuItems));
});
tmp_width = $('> li:first', nav).width();
$('> li:first', nav).width(tmp_width + (menuWidthRemainder % numMenuItems));
}
/* Primary navigation items (combined) are greater than the width of
the wrapper. */
else if(totalMenuItemWidth > wrapperWidth) {
var priResizedNav = [];
for(var x = 0; x < numMenuItems; x++) {
/* Always take the lowest value as the sum total of the resized
navigation items must never exceed the wrapper width. */
priResizedNav.push(Math.floor((priNavItems[x] / priNavItems.sum()) * wrapperWidth));
}
$('> li', nav).each(function(i) {
$(this).width(priResizedNav[i]).css('wordWrap', 'break-word');
});
$('> li:first', nav).width(priResizedNav[0] + (wrapperWidth - priResizedNav.sum()));
}
$('> li', nav).each(function (i) {
nav_item_height = ($(this).height() > nav_item_height) ? $(this).height() : nav_item_height;
});
$('> li', nav).each(function (i) {
if($(this).height() < nav_item_height) {
// Default padding: 6/6 (top/bottom).
var paddingTop = parseInt((nav_item_height - $(this).height()) / 2);
var paddingBottom = parseInt((nav_item_height - $(this).height()) - paddingTop);
$('a', this).css({
paddingTop: paddingTop + 6 + 'px',
paddingBottom: paddingBottom + 6 + 'px'
});
}
});
} 
