/*! Responsive Navigation - v0.1.0 - 2012-06-28
* Copyright (c) 2012 Jeremy Worboys; Licensed ☺ */
;(function($, undefined) {
$.fn.responsiveNavigation = function(options) {
// Default parameter values
if (options === undefined) { options = {}; }
// Default options
var opts = $.extend({
search: "nav",
nested: true,
wrap: true,
wrapper: "nav",
wrapperClass: "responsive-nav-wrapper",
wrapperId: "",
selectClass: "responsive-nav",
selectId: "",
defaultText: "Navigation...",
group: true,
groupLabel: "label",
groupOrder: "order"
}, options),
// Sorting algorithm for ordering option groups
optgroupSort = function($a, $b) {
var a = parseInt($a.data("order"), 10),
b = parseInt($b.data("order"), 10);
if ((isNaN(a) && isNaN(b)) || a === b) {
return 0;
}
else if (isNaN(b) || a < b) {
return -1;
}
return 1;
};
// Process each $header
return this.each(function() {
var $header = $(this),
$navs = $header.find(opts.search),
optgroups = [],
$select;
// Make sure we can find a $nav element
if ($navs.length < 1) { return; }
// If there is only one $nav default grouping to off
if ($navs.length === 1) {
opts.group = options.group || false;
}
// If nesting is on, remove nested navs
if (opts.nested) {
$navs.each(function() {
// Remove from object but put back into the DOM
$(this).find(opts.search).detach().appendTo($header);
});
}
// Process all of the $navs
$navs.each(function() {
var $nav = $(this),
options = [];
// Find all links in this nav
$nav
.find("a")
.each(function() {
var $link = $(this);
// Pull them out as options
options.push(
$("