(function($){ //plugin's default options var settings = { combine: true, //combine multiple menus into a single select groupPageText: 'Main', //optgroup's aren't selectable, make an option for it nested: true, //create optgroups by default prependTo: 'body', //insert at top of page by default switchWidth: 480, //width at which to switch to select, and back again topOptionText: 'Select a page' //default "unselected" state }, //used to store original matched menus $menus, //used as a unique index for each menu if no ID exists menuCount = 0, //used to store unique list items for combining lists uniqueLinks = []; //go to page function goTo(url){ document.location.href = url; } //does menu exist? function menuExists(){ return ($('.mnav').length) ? true : false; } //validate selector's matched list(s) function isList($this){ var pass = true; $this.each(function(){ if(!$(this).is('ul') && !$(this).is('ol')){ pass=false; } }); return pass; }//isList() //function to decide if mobile or not function isMobile(){ return ($(window).width() < settings.switchWidth); } //function to get text value of element, but not it's children function getText($item){ return $.trim($item.clone().children('ul, ol').remove().end().text()); } //function to check if URL is unique function isUrlUnique(url){ return ($.inArray(url, uniqueLinks) === -1) ? true : false; } //function to do duplicate checking for combined list function checkForDuplicates($menu){ $menu.find(' > li').each(function(){ var $li = $(this), link = $li.find('a').attr('href'), parentLink = function(){ if($li.parent().parent().is('li')){ return $li.parent().parent().find('a').attr('href'); } else { return null; } }; //check nested
  • s before checking current one if($li.find(' ul, ol').length){ checkForDuplicates($li.find('> ul, > ol')); } //remove empty UL's if any are left by LI removals if(!$li.find(' > ul li, > ol li').length){ $li.find('ul, ol').remove(); } //if parent
  • has a link, and it's not unique, append current
  • to the "unique parent" detected earlier if(!isUrlUnique(parentLink(), uniqueLinks) && isUrlUnique(link, uniqueLinks)){ $li.appendTo( $menu.closest('ul#mmnav').find('li:has(a[href='+parentLink()+']):first ul') ); } //otherwise, check if the current
  • is unique, if it is, add it to the unique list else if(isUrlUnique(link)){ uniqueLinks.push(link); } //if it isn't, remove it. Simples. else{ $li.remove(); } }); } //function to combine lists into one function combineLists(){ //create a new list var $menu = $('