$(document).ready(function() { var lisInRow = 0; var hasLangNav = $("nav.mainMenu>ul.languageNav>li").length; var langNavChildren = $("nav.mainMenu>ul.languageNav>li"); var menuItemsNav = $('nav.mainMenu>ul.menuItemsNav'); var menuItemsNavChildren = $('nav.mainMenu>ul.menuItemsNav>li'); var menuItemsNavChildrenWidth = 0; var langNavWidth = 0; var moreButtonMeasureContainerWidth = 0; var navMenuWidth = parseInt($('nav.mainMenu').outerWidth(), 10); var navMenuWidthAfterCalc; var topRowWidth = 0; var lisInRowArr = []; var moreVerbiage='More'; //count total width of all menu items menuItemsNavChildren.each(function(index) { menuItemsNavChildrenWidth += parseInt($(this).outerWidth(true), 10) + 1; // + 1 pixel to counteract some browsers' subpixel calculation }); //console.log('menuItemsNavChildrenWidth ' + menuItemsNavChildrenWidth); //if language nav is present, calc its width if ( hasLangNav > 0 ) { langNavChildren.each(function(index) { langNavWidth += parseInt($(this).outerWidth(true), 10); }); //console.log("langNavWidth " + langNavWidth); } // if total width of all menu items plus language nav width exceeds nav menu width, then perform more calculations and subsequent DOM manipulation if ((menuItemsNavChildrenWidth + langNavWidth) > $('nav.mainMenu').outerWidth()) { //find all items on the top row of menu (including pipe divider items) before any DOM manipulation menuItemsNavChildren.each(function() { if($(this).prev().length > 0) { if($(this).position().top != $(this).prev().position().top) { return false; } lisInRow++; } else { lisInRow++; } }); //console.log('lisInRow ' + lisInRow); //measure the width of "more" button using a hidden moreButtonMeasure element in markup moreButtonMeasureContainerWidth = $('.moreButtonMeasure').outerWidth(true); //measure the width of each top row item, then push them into an array //console.log('lis in row '+lisInRow); menuItemsNavChildren.slice(0,lisInRow).each(function(i) { //console.count("toprow item"); //console.log("=======toprow width=======" +topRowWidth); topRowWidth += parseInt($(this).outerWidth(true), 10); lisInRowArr.push(topRowWidth); }); //console.log(lisInRowArr); //calculate the width of available menu space within which we will pack in as many top row menu items as possible navMenuWidthAfterCalc = navMenuWidth - langNavWidth - moreButtonMeasureContainerWidth; //console.log("navMenuWidth " + navMenuWidth); //console.log("navMenuWidthAfterCalc (navMenuWidth - langNavWidth - moreButtonMeasureContainerWidth) " + navMenuWidthAfterCalc); //console.log('top row width '+ topRowWidth); //console.log('more button measure '+ $('.moreButtonMeasure').outerWidth(true)); //console.log('top row width + more button measure '+(topRowWidth + $('.moreButtonMeasure').outerWidth(true))); //console.log('nav mainmenu '+$('nav.mainMenu').outerWidth()); //console.log('lisInRowArr.length ' + lisInRowArr.length); // lisInRow operation - figure out how many top menu items can be pack in the available menu space for (var i = lisInRowArr.length - 1 ; i > 0 ; i--) { //console.log(lisInRowArr); //console.log("lisInRowArr[" + i + "]'s length " + lisInRowArr[i]); //console.log("=======lisInRow operation======= " +lisInRow); if ( lisInRowArr[i] < navMenuWidthAfterCalc ) { break; } if ( lisInRowArr[i] > navMenuWidthAfterCalc ) { lisInRow--; } } //console.log("=======lisInRow operation======= FINAL OUTPUT" +lisInRow); // if last item on menuItems nav is not a pipe, pop that item so that the pipe will appear properly next to the appended More button and language nav items // menuItemsNavLastChild = menuItemsNavChildren.eq(lisInRow-1); // if ( !( menuItemsNavLastChild.hasClass("noLink") ) ) { // lisInRow--; // //console.log("=======pipe popped======"); // } //perform DOM manipulation if needed menuItemsNav .append( $('
  • ') .append('
    '+moreVerbiage+'
    ') .append($('