1function togglewrap(checkbox, duration) {
2    if (checkbox.value.substr(0,4) == 'not_') {
3        value = checkbox.value.substr(4);
4        reverse = true;
5    } else {
6        value = checkbox.value;
7        reverse = false;
8    }
9    if ((checkbox.checked && !reverse) || (!checkbox.checked && reverse) ) {
10        jQuery('.wrap_'+value).show(duration);
11        jQuery('.wrap_not_'+value).hide(duration);
12        jQuery('input.togglewrap').each(function() {
13            if (this.value == value) {
14                jQuery(this).prop('checked',true);
15            } else if (this.value == 'not_'+value) {
16                jQuery(this).prop('checked',false);
17            }
18        } );
19    } else {
20        jQuery('.wrap_'+value).hide(duration);
21        jQuery('.wrap_not_'+value).show(duration);
22        jQuery('input.togglewrap').each(function() {
23            if (this.value == value) {
24                jQuery(this).prop('checked',false);
25            } else if (this.value == 'not_'+value) {
26                jQuery(this).prop('checked',true);
27            }
28        } );
29    }
30}
31
32function togglewrapUpdateTOC(duration) {
33    var titleHidden = new Array();
34    jQuery('input.togglewrap').each(function() {
35        if ( (this.value.substr(0,4) == 'not_') && (!this.checked)) {
36            value = this.value.substr(4);
37            reverse = true;
38        } else {
39            value = this.value;
40            reverse = false;
41        }
42        $wrap = jQuery('.wrap_'+value);
43        $wrap_not = jQuery('.wrap_not_'+value);
44        if ((this.checked && !reverse) || (!this.checked && reverse) ) {
45            $wrap_not.find("h1, h2, h3, h4, h5, h6").each(function() { titleHidden.push(this.id); } );
46        } else {
47            $wrap.find("h1, h2, h3, h4, h5, h6").each(function() { titleHidden.push(this.id); } );
48        }
49    } );
50    jQuery("#dw__toc li a").each(function() {
51        $this = jQuery(this);
52        if (jQuery.inArray($this.attr('href').substr(1), titleHidden) != -1) {
53            $this.parent().parent().hide(duration);
54        } else {
55            $this.parent().parent().show(duration);
56        }
57    } );
58}
59
60jQuery(function() {
61    jQuery("input.togglewrap").each(function() { togglewrap (this, 0); } );
62    togglewrapUpdateTOC(0);
63    jQuery("input.togglewrap").change(function() { togglewrap (this, 400); togglewrapUpdateTOC(400)});
64
65});
66