1/*! http://tinynav.viljamis.com v1.1 by @viljamis */
2(function ($, window, i) {
3  $.fn.tinyNav = function (options) {
4
5    // Default settings
6    var settings = $.extend({
7      'active' : 'selected', // String: Set the "active" class
8      'header' : '', // String: Specify text for "header" and show header instead of the active item
9      'label'  : '' // String: sets the <label> text for the <select> (if not set, no label will be added)
10    }, options);
11
12    return this.each(function () {
13
14      // Used for namespacing
15      i++;
16
17      var $nav = $(this),
18        // Namespacing
19        namespace = 'tinynav',
20        namespace_i = namespace + i,
21        l_namespace_i = '.l_' + namespace_i,
22        $select = $('<select/>').attr("id", namespace_i).addClass(namespace + ' ' + namespace_i);
23
24      if ($nav.is('ul,ol')) {
25
26        if (settings.header !== '') {
27          $select.append(
28            $('<option/>').text(settings.header)
29          );
30        }
31
32        // Build options
33        var options = '';
34
35        $nav
36          .addClass('l_' + namespace_i)
37          .find('a')
38          .each(function () {
39            options += '<option value="' + $(this).attr('href') + '">';
40            var j;
41            for (j = 0; j < $(this).parents('ul, ol').length - 1; j++) {
42              options += '- ';
43            }
44            options += $(this).text() + '</option>';
45          });
46
47        // Append options into a select
48        $select.append(options);
49
50        // Select the active item
51        if (!settings.header) {
52          $select
53            .find(':eq(' + $(l_namespace_i + ' li')
54            .index($(l_namespace_i + ' li.' + settings.active)) + ')')
55            .attr('selected', true);
56        }
57
58        // Change window location
59        $select.change(function () {
60          window.location.href = $(this).val();
61        });
62
63        // Inject select
64        $(l_namespace_i).after($select);
65
66        // Inject label
67        if (settings.label) {
68          $select.before(
69            $("<label/>")
70              .attr("for", namespace_i)
71              .addClass(namespace + '_label ' + namespace_i + '_label')
72              .append(settings.label)
73          );
74        }
75
76      }
77
78    });
79
80  };
81})(jQuery, this, 0);