/*************************************************
 *  JavaScript that gets injected
 *************************************************/


/*************************************************
 *  Event: Card is clicked
 *************************************************/
$('.list-card-details').click( function() {
    setTimeout(function() {
        // Hide items
        hideCheckedItems();

        // Add toggle button
        addCheckedToggle();
    }, 1000);
});



/*************************************************
 *  Event: Card is refreshed
 *************************************************/
$(document).ready(function() {
    if( $('.js-checklist-list').length ) {
        setTimeout(function() {
            // Hide items
            hideCheckedItems();

            // Add toggle button
            addCheckedToggle();
        }, 1000);
    }
});



/*************************************************
 *  Event: Button is clicked
 *************************************************/
// $(document).ready(function() {
    $(document).on('click', '.js-tt-toggle-checked', function() {
        console.log('toggle checked items');
    });
// });



/*************************************************
 *  Helper Functions
 *************************************************/
// Hide checked items
function hideCheckedItems() {
    $('.js-hide-checked-items').each( function() {
        this.click();
    });
};

// Add a button for toggling checked items
function addCheckedToggle() {
    $('.window-module.other-actions .u-clearfix').append('<a class="button-link js-tt-toggle-checked" title="Toggle the finished checklist items."><span class="icon-sm icon-subscribe"></span> Hide Checked</a>');
}

@@ -132,16 +132,17 @@
 ****/%0A%0A%0A
+%0A
 /*******
@@ -303,24 +303,51 @@
 unction() %7B%0A
+    // If autohide checked%0A
     setTimeo
@@ -365,33 +365,37 @@
 () %7B%0A        // 
-H
+Autoh
 ide items%0A      
@@ -388,35 +388,37 @@
 e items%0A        
-hid
+toggl
 eCheckedItems();
@@ -419,40 +419,16 @@
 ems(
-);%0A%0A        // Add toggle button
+'hide');
 %0A   
@@ -445,24 +445,30 @@
 eckedToggle(
+'show'
 );%0A    %7D, 10
@@ -638,24 +638,51 @@
 unction() %7B%0A
+    // If autohide checked%0A
     if( $('.
@@ -759,17 +759,21 @@
      // 
-H
+Autoh
 ide item
@@ -786,19 +786,21 @@
         
-hid
+toggl
 eChecked
@@ -809,44 +809,16 @@
 ems(
-);%0A%0A            // Add toggle button
+'hide');
 %0A   
@@ -843,16 +843,22 @@
 dToggle(
+'show'
 );%0A     
@@ -1019,46 +1019,8 @@
 **/%0A
-// $(document).ready(function() %7B%0A    
 $(do
@@ -1085,60 +1085,77 @@
     
-    console.log('
+var action = $(this).data('action');%0A    
 toggle
- c
+C
 hecked
- i
+I
 tems
-');%0A    %7D);%0A// 
+(action);%0A
 %7D);%0A
@@ -1284,19 +1284,21 @@
 ***/%0A// 
-Hid
+Toggl
 e checke
@@ -1314,19 +1314,21 @@
 unction 
-hid
+toggl
 eChecked
@@ -1333,16 +1333,22 @@
 edItems(
+action
 ) %7B%0A    
@@ -1354,20 +1354,26 @@
  $('.js-
-hide
+'+action+'
 -checked
@@ -1429,16 +1429,379 @@
 %0A    %7D);
+%0A%0A    if( $('.js-tt-toggle-checked').length ) %7B%0A        var text = $('.js-tt-toggle-checked').html();%0A        if( action == 'show' ) %7B%0A            $('.js-tt-toggle-checked').data('action', 'hide').html(text.replace('Show', 'Hide'));%0A        %7D else %7B%0A            $('.js-tt-toggle-checked').data('action', 'show').html(text.replace('Hide', 'Show'));%0A        %7D%0A    %7D
 %0A%7D;%0A%0A// 
@@ -1866,16 +1866,22 @@
 dToggle(
+action
 ) %7B%0A    
@@ -2022,16 +2022,41 @@
  items.%22
+ data-action=%22'+action+'%22
 %3E%3Cspan c
@@ -2097,25 +2097,225 @@
 an%3E 
-Hide Checked%3C/a%3E'
+'+toTitleCase(action)+' Checked%3C/a%3E');%0A%7D%0A%0A// Convert a string to title case%0Afunction toTitleCase(str) %7B%0A    return str.replace(/%5Cw%5CS*/g, function(txt)%7Breturn txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();%7D
 );%0A%7D

Add the ability to toggle the checked items