1jQuery(function(){
2
3    /**
4     * Open a tab
5     *
6     * Adds and removes classes, handles cookie setting, closes other tabs
7     *
8     * @param {jQuery} $box  The box we're handling
9     * @param {string} tabid The ID of the tab to open
10     */
11    function tabboxopen($box, tabid) {
12        // hide all tabs
13        $box.find('.tabboxtab').hide();
14        $box.find('.tabs li').removeClass('active');
15
16        // try the given ID
17        var $open = $box.find('#tab_'+tabid);
18        if(!$open.length) {
19            // fall back to first tab
20            $open = $box.find('.tabboxtab').first();
21        }
22
23        var id = $open.attr('id').substr(4);
24        $box.find('#lnk_'+id).addClass('active');
25        $open.show();
26        DokuCookie.setValue('tabbox', id);
27    }
28
29    /**
30     * Build the Tab Boxes
31     */
32    jQuery('.plugin_tabbox').each(function(){
33        var $box = jQuery(this);
34        $box.addClass('js');
35
36        // create the tabs
37        var $ul = jQuery(document.createElement('ul'));
38        $ul.addClass('tabs');
39        $box.find('.tabboxtab .hl').each(function(){
40            var $hl = jQuery(this);
41            $hl.hide();
42
43            var $a = jQuery(document.createElement('a'));
44            $a.attr('href', '#'+$hl.attr('id'));
45            $a.text($hl.text());
46            $a.click(function(){
47                tabboxopen($box, $hl.attr('id'));
48            });
49
50            var $li = jQuery(document.createElement('li'));
51            $li.attr('id', 'lnk_'+$hl.attr('id'));
52            $li.append($a);
53            $ul.append($li);
54        });
55        $box.prepend($ul);
56
57        // open a tab (falls back to first tab)
58        if(DokuCookie.getValue('tabbox')) {
59            tabboxopen($box, DokuCookie.getValue('tabbox'));
60        }
61
62        tabboxopen($box, window.location.hash.substring(1));
63
64    });
65
66});
67